#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 tolower(char const);
int strncasecmp(const char *s1, const char *s2, size_t n) {
  int res;
  while (n--) {
    if ((res = tolower(*s1) - tolower(*s2)) != 0 || *s1 == '\0')
      break;
    s1++, s2++;
  }
  return res;
}


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)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     119d <strncasecmp+0x8d>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    117b <strncasecmp+0x6b>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1180 <strncasecmp+0x70>
jmp    119d <strncasecmp+0x8d>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1124 <strncasecmp+0x14>
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 <tolower@plt>
mov    %eax,%ebp
movsbl (%r14,%rbx,1),%edi
call   1030 <tolower@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
br label %8
8: ; preds = %27, %3
%9 = load i64, i64* %6
%10 = add i64 %9, -1
store i64 %10, i64* %6
%11 = icmp ne i64 %9, 0
br i1 %11, label %12, label %32
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = call i32 @tolower(i8 signext %14)
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = call i32 @tolower(i8 signext %17)
%19 = sub nsw i32 %15, %18
store i32 %19, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %12
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21, %12
br label %32
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %4
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
br label %8
32: ; preds = %26, %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, %16
%6 = phi i64 [ %9, %16 ], [ %2, %3 ]
%7 = phi i8* [ %20, %16 ], [ %1, %3 ]
%8 = phi i8* [ %19, %16 ], [ %0, %3 ]
%9 = add i64 %6, -1
%10 = load i8, i8* %8
%11 = tail call i32 @tolower(i8 signext %10)
%12 = load i8, i8* %7
%13 = tail call i32 @tolower(i8 signext %12)
%14 = sub nsw i32 %11, %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %5
%17 = load i8, i8* %8
%18 = icmp eq i8 %17, 0
%19 = getelementptr inbounds i8, i8* %8, i64 1
%20 = getelementptr inbounds i8, i8* %7, i64 1
%21 = icmp eq i64 %9, 0
%22 = select i1 %18, i1 true, i1 %21
br i1 %22, label %23, label %5
23: ; preds = %16, %5, %3
%24 = phi i32 [ 0, %3 ], [ %14, %5 ], [ 0, %16 ]
ret i32 %24
}