I have this code in C:
int main(void){ int a = 1 + 2; return 0;}
When I objdump -x86-asm-syntax=intel -d a.out
which is compiled with -O0
flag with GCC 9.3.0_1, I get:
0000000100000f9e _main:100000f9e: 55 push rbp100000f9f: 48 89 e5 mov rbp, rsp100000fa2: c7 45 fc 03 00 00 00 mov dword ptr [rbp - 4], 3100000fa9: b8 00 00 00 00 mov eax, 0100000fae: 5d pop rbp100000faf: c3 ret
and with -O1
flag:
0000000100000fc2 _main:100000fc2: b8 00 00 00 00 mov eax, 0100000fc7: c3 ret
which removes the unused variable a
and stack managing altogether.
However, when I use Apple clang version 11.0.3 with -O0
and -O1
, I get
0000000100000fa0 _main:100000fa0: 55 push rbp100000fa1: 48 89 e5 mov rbp, rsp100000fa4: 31 c0 xor eax, eax100000fa6: c7 45 fc 00 00 00 00 mov dword ptr [rbp - 4], 0100000fad: c7 45 f8 03 00 00 00 mov dword ptr [rbp - 8], 3100000fb4: 5d pop rbp100000fb5: c3 ret
and
0000000100000fb0 _main:100000fb0: 55 push rbp100000fb1: 48 89 e5 mov rbp, rsp100000fb4: 31 c0 xor eax, eax100000fb6: 5d pop rbp100000fb7: c3 ret
respectively.I never get the stack managing part stripped off as in GCC.Why does (Apple) Clang keep unnecessary push
and pop
?
This may or may not be a separate question, but with the following code:
int main(void){ // return 0;}
GCC creates a same ASM with or without the return 0;
.However, Clang -O0
leaves this extra
100000fa6: c7 45 fc 00 00 00 00 mov dword ptr [rbp - 4], 0
when there is return 0;
.
Why does Clang keep these (probably) redundant ASM codes?