Search code examples
c++assemblygcc

How is this function's assembly implementing the conditional?


The following code,

int foo(int);
int bar(int);

int foobar(int i) {
    int a = foo(i);
    int b = bar(i);
    return a == b ? a : b;
};

with GCC trunk is compiling to this assembly:

foobar(int):
        push    rbx
        mov     ebx, edi
        call    foo(int)
        mov     edi, ebx
        pop     rbx
        jmp     bar(int)

Here is it live.

This TU has no clue what i it will be given, and it has no clue what foo and bar will return, so it has no clue whether a == b will be true or false. So it must inspect the outputs of the two calls and somehow compare them to decide which one it shuld return.

But I can't see that in the assembly. What am I missing?


Solution

  • return a == b ? a : b; is same as return a == b ? b : b; is same as return b;.