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


x86 assembly (clang, -O0):
<strncasecmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a1 <strncasecmp+0x91>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1030 <toupper@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    118b <strncasecmp+0x7b>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1190 <strncasecmp+0x80>
jmp    11a1 <strncasecmp+0x91>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    112b <strncasecmp+0x1b>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncasecmp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1161 <strncasecmp+0x51>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
add    $0xffffffffffffffff,%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%r12,%rbx,1),%edi
call   1030 <toupper@plt>
mov    %eax,%ebp
movsbl (%r14,%rbx,1),%edi
call   1030 <toupper@plt>
sub    %eax,%ebp
jne    1163 <strncasecmp+0x53>
xor    %ebp,%ebp
cmpb   $0x0,(%r12,%rbx,1)
je     1163 <strncasecmp+0x53>
lea    0x1(%rbx),%rax
cmp    %rbx,%r15
mov    %rax,%rbx
jne    1130 <strncasecmp+0x20>
jmp    1163 <strncasecmp+0x53>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
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 i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %7
br label %8
8: ; preds = %29, %3
%9 = load i64, i64* %6
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %32
11: ; preds = %8
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = call i32 @toupper(i8 signext %13)
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %5
%17 = load i8, i8* %15
%18 = call i32 @toupper(i8 signext %17)
%19 = sub nsw i32 %14, %18
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %11
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %4
%25 = load i8, i8* %23
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22, %11
br label %32
29: ; preds = %22
%30 = load i64, i64* %6
%31 = add i64 %30, -1
store i64 %31, i64* %6
br label %8
32: ; preds = %28, %8
%33 = load i32, i32* %7
ret i32 %33
}

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