My compiler (gcc 4.7.2) produces nearly identical code for the two statements:
_main:
LFB1018:
pushq %rbx
LCFI0:
movq __ZSt4cout@GOTPCREL(%rip), %rbx
; std::cout << foo(123) << std::endl;
movl $123, %esi
movq %rbx, %rdi
call __ZNSolsEi
movq %rax, %rdi
call __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
; std::cout << 123 << std::endl;
movq %rbx, %rdi
movl $123, %esi
call __ZNSolsEi
movq %rax, %rdi
call __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
xorl %eax, %eax
popq %rbx
LCFI1:
ret
The only difference is the order of the first two instructions. I've experimented with it, and this difference doesn't appear to have anything to do with foo()
: if I repeat the two lines twice, only the last of the four statements has the instruction order reversed. This makes me think that this artifact probably has something to do with the pipeline optimizer or something of that nature.