1

clang -S compiles the following source:

int main {
  return 0;
} // literally nothing but this main function

to this:

pushq   %rbp
movq    %rsp, %rbp
xorl    %eax, %eax
movl    $0, -4(%rbp)  # what is this?
popq    %rbp
retq
# extracted out directives...

I cannot understand why movl $0, -4(%rbp) exists. There is no local variables at all.

My clang version: Apple LLVM version 9.0.0 (clang-900.0.39.2)

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Jinux
  • 177
  • 1
  • 1
  • 12
  • 3
    maybe it's the return value copy, were there some verbose directives marking which part of source the machine code reflects? Anyway, you are looking at unoptimized debug machine code, so you will see meaningless instructions in such code quite often, as the compiler is trying to produce working code ASAP, and in a way to be debugging friendly. There's little point to reason about it on assembly level (as it is not even close to the production code done with `-O2` or `-O3`). – Ped7g Apr 14 '18 at 11:33
  • It seems `xorl %eax, %eax` is for the return value. And your are right! The instruction disappeared when compiled with `-O3`. You mean that it's meaningless to analyze the code produced without optimization? – Jinux Apr 14 '18 at 13:36
  • 1
    "little point" .. it's meaningful if you don't understand some of your bugs, which does manifest both in production and debug code. Then studying the unoptimized assembly, if you know assembly, may give you extra hint, what is wrong with original source. Or if you are really unlucky, you may even hit compiler bug. But it makes sense only in connection to the original source, because the debug assembly is intentionally following the source in "per line" way, to make debugging of original source reasonable. In production one usually does use at least some optimization level, that differs a lot. – Ped7g Apr 14 '18 at 15:01

0 Answers0