2

For

int callee(void);
int caller(void) { return callee()+1; }

gcc and clang generate (https://gcc.godbolt.org/z/R6ZKZA):

clang:

caller:                                 # @caller
        push    rax
        call    callee
        inc     eax
        pop     rcx
        ret

gcc:

caller:                                 # @caller
        push    rax
        call    callee
        inc     eax
        pop     rcx
        ret

Why are the push/pop instructions around the call needed?

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • 2
    Is [this](https://stackoverflow.com/questions/37773787/why-does-this-function-push-rax-to-the-stack-as-the-first-operation) related? – Tony Tannous May 20 '20 at 10:53
  • 2
    @TonyTannous Yes. It answers the question. Thanks! I'll close this this as a duplicate rather than delete it in case my phrasing helps others find the answer quicker. – Petr Skocik May 20 '20 at 11:06
  • 1
    Fun fact: clang does this at `-O3` as well as the `-Os` you used, because it's actually faster as well as more compact on some modern CPUs. GCC still favours `sub`/`add` outside of `-Os`. – Peter Cordes May 20 '20 at 18:37

0 Answers0