#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int memcmp(char const *, char const *, size_t);
size_t strnlen(char const *, size_t);
int strncmp(const char *a, const char *b, size_t length) {
  size_t n = strnlen(a, length);
  size_t m = strnlen(b, length);
  int r = memcmp(a, b, (n < m ? n : m));
  return r ? r : n == m ? 0 : n < m ? -1 : +1;
}


x86 assembly (clang, -O0):
<strncmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1181 <strncmp+0x61>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1189 <strncmp+0x69>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
call   1040 <memcmp@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11b2 <strncmp+0x92>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    11eb <strncmp+0xcb>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11ca <strncmp+0xaa>
xor    %eax,%eax
mov    %eax,-0x50(%rbp)
jmp    11e5 <strncmp+0xc5>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rsi
mov    $0x1,%eax
mov    $0xffffffff,%ecx
cmp    %rsi,%rdx
cmovb  %ecx,%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncmp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rsi
call   1030 <strnlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <strnlen@plt>
xor    %ecx,%ecx
xor    %edx,%edx
cmp    %rax,%rbx
cmovae %rax,%rbx
setae  %dl
lea    -0x1(%rdx,%rdx,1),%ebp
cmove  %ecx,%ebp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcmp@plt>
test   %eax,%eax
cmove  %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @strncmp(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %4
%11 = load i64, i64* %6
%12 = call i64 @strnlen(i8* %10, i64 %11)
store i64 %12, i64* %7
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i64 @strnlen(i8* %13, i64 %14)
store i64 %15, i64* %8
%16 = load i8*, i8** %4
%17 = load i8*, i8** %5
%18 = load i64, i64* %7
%19 = load i64, i64* %8
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i64, i64* %7
br label %25
23: ; preds = %3
%24 = load i64, i64* %8
br label %25
25: ; preds = %23, %21
%26 = phi i64 [ %22, %21 ], [ %24, %23 ]
%27 = call i32 @memcmp(i8* %16, i8* %17, i64 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* %9
br label %45
32: ; preds = %25
%33 = load i64, i64* %7
%34 = load i64, i64* %8
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
br label %43
37: ; preds = %32
%38 = load i64, i64* %7
%39 = load i64, i64* %8
%40 = icmp ult i64 %38, %39
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 -1, i32 1
br label %43
43: ; preds = %37, %36
%44 = phi i32 [ 0, %36 ], [ %42, %37 ]
br label %45
45: ; preds = %43, %30
%46 = phi i32 [ %31, %30 ], [ %44, %43 ]
ret i32 %46
}

LLVM IR (-O2):
define i32 @strncmp(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @strnlen(i8* %0, i64 %2)
%5 = tail call i64 @strnlen(i8* %1, i64 %2)
%6 = icmp ult i64 %4, %5
%7 = select i1 %6, i64 %4, i64 %5
%8 = tail call i32 @memcmp(i8* %0, i8* %1, i64 %7)
%9 = icmp eq i32 %8, 0
%10 = icmp eq i64 %4, %5
%11 = select i1 %6, i32 -1, i32 1
%12 = select i1 %10, i32 0, i32 %11
%13 = select i1 %9, i32 %12, i32 %8
ret i32 %13
}