#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 toupper(unsigned char);
int strncasecmp(const char *s1, const char *s2, size_t n) {
  const unsigned char *c1 = (const unsigned char *)s1;
  const unsigned char *c2 = (const unsigned char *)s2;
  unsigned char ch;
  int d = 0;
  while (n--) {
    d = toupper(ch = *c1++) - toupper(*c2++);
    if (d || !ch)
      break;
  }
  return d;
}


x86 assembly (clang, -O0):
<strncasecmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     11b2 <strncasecmp+0xa2>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%al
mov    %al,-0x29(%rbp)
movzbl %al,%edi
call   1030 <toupper@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movzbl (%rax),%edi
call   1030 <toupper@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    11a8 <strncasecmp+0x98>
cmpb   $0x0,-0x29(%rbp)
jne    11ad <strncasecmp+0x9d>
jmp    11b2 <strncasecmp+0xa2>
jmp    113b <strncasecmp+0x2b>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncasecmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
cmp    %rbx,%r15
je     115f <strncasecmp+0x4f>
movzbl (%r12,%rbx,1),%r13d
mov    %r13d,%edi
call   1030 <toupper@plt>
mov    %eax,%ebp
movzbl (%r14,%rbx,1),%edi
call   1030 <toupper@plt>
sub    %eax,%ebp
test   %r13d,%r13d
je     1161 <strncasecmp+0x51>
add    $0x1,%rbx
test   %ebp,%ebp
je     1130 <strncasecmp+0x20>
jmp    1161 <strncasecmp+0x51>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @strncasecmp(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %4
store i8* %11, i8** %7
%12 = load i8*, i8** %5
store i8* %12, i8** %8
store i32 0, i32* %10
br label %13
13: ; preds = %33, %3
%14 = load i64, i64* %6
%15 = add i64 %14, -1
store i64 %15, i64* %6
%16 = icmp ne i64 %14, 0
br i1 %16, label %17, label %34
17: ; preds = %13
%18 = load i8*, i8** %7
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %7
%20 = load i8, i8* %18
store i8 %20, i8* %9
%21 = call i32 @toupper(i8 zeroext %20)
%22 = load i8*, i8** %8
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %8
%24 = load i8, i8* %22
%25 = call i32 @toupper(i8 zeroext %24)
%26 = sub nsw i32 %21, %25
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %17
%30 = load i8, i8* %9
%31 = icmp ne i8 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %29, %17
br label %34
33: ; preds = %29
br label %13
34: ; preds = %32, %13
%35 = load i32, i32* %10
ret i32 %35
}

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