Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created September 12, 2012 16:41
Show Gist options
  • Save davidbalbert/3707988 to your computer and use it in GitHub Desktop.
Save davidbalbert/3707988 to your computer and use it in GitHub Desktop.
Clang code generation

When I compile hello.c with: clang -g -O0 hello.c -o hello, and disassemble it in GDB, I get the contents of hello.s.

The line that confuses me is <main+34>. It seems that when optimizations are off, clang saves unused return values on the stack. <main+34> is saving the return value of printf even though hello.c doesn't do anything with it at all. I assume there's a reason for this. Why does it happen?

int main()
{
printf("Hello, world!");
return 0;
}
0x0000000100000f10 <main+0>: push %rbp
0x0000000100000f11 <main+1>: mov %rsp,%rbp
0x0000000100000f14 <main+4>: sub $0x10,%rsp
0x0000000100000f18 <main+8>: lea 0x3f(%rip),%rdi # 0x100000f5e
0x0000000100000f1f <main+15>: movl $0x0,-0x4(%rbp)
0x0000000100000f26 <main+22>: mov $0x0,%al
0x0000000100000f28 <main+24>: callq 0x100000f3e <dyld_stub_printf>
0x0000000100000f2d <main+29>: mov $0x0,%ecx
0x0000000100000f32 <main+34>: mov %eax,-0x8(%rbp)
0x0000000100000f35 <main+37>: mov %ecx,%eax
0x0000000100000f37 <main+39>: add $0x10,%rsp
0x0000000100000f3b <main+43>: pop %rbp
0x0000000100000f3c <main+44>: retq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment