#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 */
unsigned char tolower(unsigned char const);
int strncasecmp(const char *str1, const char *str2, size_t length) {
  const unsigned char *s1 = (const unsigned char *)str1;
  const unsigned char *s2 = (const unsigned char *)str2;
  for (size_t i = 0; i < length; i++) {
    unsigned char c1 = tolower(s1[i]);
    unsigned char c2 = tolower(s2[i]);
    if (c1 < c2) {
      return -1;
    } else if (c1 > c2) {
      return 1;
    } else if (c1 == '\0') {
      return 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<strncasecmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11e3 <strncasecmp+0xd3>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <tolower@plt>
mov    %al,-0x39(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <tolower@plt>
mov    %al,-0x3a(%rbp)
movzbl -0x39(%rbp),%eax
movzbl -0x3a(%rbp),%ecx
cmp    %ecx,%eax
jge    118e <strncasecmp+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <strncasecmp+0xda>
movzbl -0x39(%rbp),%eax
movzbl -0x3a(%rbp),%ecx
cmp    %ecx,%eax
jle    11aa <strncasecmp+0x9a>
movl   $0x1,-0x4(%rbp)
jmp    11ea <strncasecmp+0xda>
movzbl -0x39(%rbp),%eax
cmp    $0x0,%eax
jne    11c3 <strncasecmp+0xb3>
movl   $0x0,-0x4(%rbp)
jmp    11ea <strncasecmp+0xda>
jmp    11c8 <strncasecmp+0xb8>
jmp    11cd <strncasecmp+0xbd>
jmp    11d2 <strncasecmp+0xc2>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    113c <strncasecmp+0x2c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncasecmp>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdx,%rdx
je     1160 <strncasecmp+0x50>
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
add    $0xffffffffffffffff,%r12
xor    %r13d,%r13d
xchg   %ax,%ax
movzbl (%r15,%r13,1),%edi
call   1030 <tolower@plt>
mov    %eax,%ebx
movzbl (%r14,%r13,1),%edi
call   1030 <tolower@plt>
cmp    %al,%bl
jb     1164 <strncasecmp+0x54>
ja     116b <strncasecmp+0x5b>
xor    %eax,%eax
test   %bl,%bl
je     1170 <strncasecmp+0x60>
lea    0x1(%r13),%rcx
cmp    %r13,%r12
mov    %rcx,%r13
jne    1130 <strncasecmp+0x20>
jmp    1170 <strncasecmp+0x60>
xor    %eax,%eax
jmp    1170 <strncasecmp+0x60>
mov    $0xffffffff,%eax
jmp    1170 <strncasecmp+0x60>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @strncasecmp(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8
%12 = alloca i8
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%13 = load i8*, i8** %5
store i8* %13, i8** %8
%14 = load i8*, i8** %6
store i8* %14, i8** %9
store i64 0, i64* %10
br label %15
15: ; preds = %51, %3
%16 = load i64, i64* %10
%17 = load i64, i64* %7
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %54
19: ; preds = %15
%20 = load i8*, i8** %8
%21 = load i64, i64* %10
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8, i8* %22
%24 = call zeroext i8 @tolower(i8 zeroext %23)
store i8 %24, i8* %11
%25 = load i8*, i8** %9
%26 = load i64, i64* %10
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8, i8* %27
%29 = call zeroext i8 @tolower(i8 zeroext %28)
store i8 %29, i8* %12
%30 = load i8, i8* %11
%31 = zext i8 %30 to i32
%32 = load i8, i8* %12
%33 = zext i8 %32 to i32
%34 = icmp slt i32 %31, %33
br i1 %34, label %35, label %36
35: ; preds = %19
store i32 -1, i32* %4
br label %55
36: ; preds = %19
%37 = load i8, i8* %11
%38 = zext i8 %37 to i32
%39 = load i8, i8* %12
%40 = zext i8 %39 to i32
%41 = icmp sgt i32 %38, %40
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 1, i32* %4
br label %55
43: ; preds = %36
%44 = load i8, i8* %11
%45 = zext i8 %44 to i32
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %43
store i32 0, i32* %4
br label %55
48: ; preds = %43
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50
%52 = load i64, i64* %10
%53 = add i64 %52, 1
store i64 %53, i64* %10
br label %15
54: ; preds = %15
store i32 0, i32* %4
br label %55
55: ; preds = %54, %47, %42, %35
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
define i32 @strncasecmp(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %16
%6 = phi i64 [ %18, %16 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = tail call zeroext i8 @tolower(i8 zeroext %8)
%10 = getelementptr inbounds i8, i8* %1, i64 %6
%11 = load i8, i8* %10
%12 = tail call zeroext i8 @tolower(i8 zeroext %11)
%13 = icmp ult i8 %9, %12
br i1 %13, label %21, label %14
14: ; preds = %5
%15 = icmp ugt i8 %9, %12
br i1 %15, label %21, label %16
16: ; preds = %14
%17 = icmp eq i8 %9, 0
%18 = add nuw i64 %6, 1
%19 = icmp eq i64 %18, %2
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %21, label %5
21: ; preds = %16, %5, %14, %3
%22 = phi i32 [ 0, %3 ], [ 1, %14 ], [ -1, %5 ], [ 0, %16 ]
ret i32 %22
}