I know that the topic "short circuit operators" has been discussed a lot here on StackExchange. But w.r.t specifically the 'C' language, there is an aspect that bothers me for quite a while.
Consider some C code like
if (b && f(&s)) {...}
where b is of boolean type, s is some variable that has the type of some more or less elaborate struct and f is a getter to the struct. A more concrete example may look like:
if (active && isEmpty(&list)) {...}
Usually, I would expect that any decent compiler will inline the isEmpty
function and optimize the whole expression to a single AND
assembler instruction with the first operand already held in a data register and the second operand in memory, accessed via 'address register with offset' addressing mode (or something similar).
However, since the compiler is enforced (by the language rules) to generate the short circuit form, this kind of optimization must not be done and would ultimately produce drastically inefficient code. It is hard for me to believe that this is actually happening!
What version of assembler code will an optimizing C compiler usually generate?