Execution order of assignment operator in C

Hi all,

I came across a (for me) strange behavior of clang for C.

For the following code

typedef struct { int x; } a;

a *f1();
a *f2();
void f3() { *f1() = *f2(); }

the function f1 is called before f2.

If I change the type from a struct to a scalar

int *f1();
int *f2();
void f3() { *f1() = *f2(); }

the function f2 is called before f1.

I know that for the latest C standard the order of evaluation of rhs and lhs of assignment is unspecified … so both is correct. For C++, clang always calls f2 before f1 since a sequence point requirement was introduced in C++17.

It seems that in Clang front-end some effort has been made to evaluate lhs before rhs for aggregated types (see AggExprEmitter::VisitBinAssign in CGExprAgg.cpp).

My questions is why lhs is evaluated sometimes before rhs? Is there a performance benefit? Why not simple always evaluate rhs before lhs to make the evaluation order more deterministic?

Btw. gcc always evaluates f2 before f1.