I'm getting
-1
-3
-1
output for optimized (-O4
) build with GCC 4.1.2 on Linux. Here's the code that the compiler generates for main
main:
.LFB25:
subq $8, %rsp
.LCFI0:
movl $-1, %esi
xorl %eax, %eax
movl $.LC0, %edi
call printf
movzbl .LC1(%rip), %edx
movzbl .LC2(%rip), %eax
movl %edx, %esi
subl %eax, %esi
jne .L2
movzbl .LC1+1(%rip), %esi
movzbl .LC2+1(%rip), %eax
subl %eax, %esi
.L2:
movl $.LC0, %edi
xorl %eax, %eax
call printf
movl $-1, %esi
movl $.LC0, %edi
xorl %eax, %eax
call printf
xorl %eax, %eax
addq $8, %rsp
ret
which means that the first and the last comparisons were actually optimized out, while the middle comparison was actually implemented intrinsically through subtraction (which is why it produced -3
). I don't see any logic in this selective behavior, so it is probably just a quirk of the optimizer.
BTW, without optimization the same GCC 4.1.2 produces
-1
-1
-1
output because it calls strcmp
. strcmp
in this standard library is implemented as
mov (%rdi),%al
cmp (%rsi),%al
jne
inc %rdi
inc %rsi
test %al,%al
jne
xor %eax,%eax
retq
mov $0x1,%eax
mov $0xffffffff,%ecx
cmovb %ecx,%eax
retq
which means that it is intentionally implemented to return -1
, 0
or +1
, even if it might be seen as suboptimal.