Do we know that the rule in 4/4 is correct? Could it be, instead, that variables are sorted in descending order of bind count? IIRC the latter has some form of precedent; I think that's how numeric variables are sorted for sm1.
Also, from patch 4/4:
``` + list_remove(&var->extern_entry); + list_add_after(insertion_point, &var->extern_entry); + insertion_point = &var->extern_entry; ```
This works, but I think it might be slightly clearer to build a local list, append to its tail, and then list_move_head() to ctx->extern_vars.