https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64753
Bug ID: 64753 Summary: Redundant cmp instruction on x86_64 Product: gcc Version: 5.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: rv at rasmusvillemoes dot dk Host: x86_64 Target: x86_64 The linux kernel's library strncmp is this: int strncmp(const char *cs, const char *ct, size_t count) { unsigned char c1, c2; while (count) { c1 = *cs++; c2 = *ct++; if (c1 != c2) return c1 < c2 ? -1 : 1; if (!c1) break; count--; } return 0; } Compiling with gcc -O2 -S I get this: strncmp: .LFB0: .cfi_startproc testq %rdx, %rdx je .L10 movzbl (%rdi), %ecx movzbl (%rsi), %r8d # cmpb %r8b, %cl # jne .L3 testb %cl, %cl je .L10 subq $1, %rdx xorl %eax, %eax jmp .L4 .p2align 4,,10 .p2align 3 .L6: movzbl 1(%rdi,%rax), %ecx movzbl 1(%rsi,%rax), %r8d # cmpb %r8b, %cl # jne .L3 addq $1, %rax testb %cl, %cl je .L10 .L4: cmpq %rdx, %rax jne .L6 .L10: xorl %eax, %eax ret .p2align 4,,10 .p2align 3 .L3: cmpb %r8b, %cl sbbl %eax, %eax orl $1, %eax ret .cfi_endproc At the two places marked # we do a cmp and a conditional jump to .L3, where for good measure the same cmp is done again... there's no other path to .L3, so it would seem that simply omitting that extra cmp should be ok. This is with gcc-5.0 (GCC) 5.0.0 20150112 (experimental), but I see the same with gcc (Debian 4.7.2-5) 4.7.2.