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


x86 assembly (clang, -O0):
<arg_strncasecmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e8 <arg_strncasecmp+0xd8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movzbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
je     11a8 <arg_strncasecmp+0x98>
mov    -0x28(%rbp),%rax
movzbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x30(%rbp)
movzbl -0x1(%rax),%edi
call   1030 <tolower@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11ef <arg_strncasecmp+0xdf>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    11c8 <arg_strncasecmp+0xb8>
jmp    11e3 <arg_strncasecmp+0xd3>
jmp    11cd <arg_strncasecmp+0xbd>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    113f <arg_strncasecmp+0x2f>
jmp    11e8 <arg_strncasecmp+0xd8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<arg_strncasecmp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1161 <arg_strncasecmp+0x51>
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
add    $0xffffffffffffffff,%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
movzbl (%r12,%rbx,1),%edi
call   1030 <tolower@plt>
mov    %eax,%ebp
movzbl (%r14,%rbx,1),%edi
call   1030 <tolower@plt>
cmp    %eax,%ebp
jne    1165 <arg_strncasecmp+0x55>
xor    %ebp,%ebp
cmpb   $0x0,(%r12,%rbx,1)
je     117d <arg_strncasecmp+0x6d>
lea    0x1(%rbx),%rax
cmp    %rbx,%r15
mov    %rax,%rbx
jne    1130 <arg_strncasecmp+0x20>
jmp    117d <arg_strncasecmp+0x6d>
xor    %ebp,%ebp
jmp    117d <arg_strncasecmp+0x6d>
movzbl (%r12,%rbx,1),%edi
call   1030 <tolower@plt>
mov    %eax,%ebp
movzbl (%r14,%rbx,1),%edi
call   1030 <tolower@plt>
sub    %eax,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @arg_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*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %46
12: ; preds = %3
%13 = load i8*, i8** %5
store i8* %13, i8** %8
%14 = load i8*, i8** %6
store i8* %14, i8** %9
br label %15
15: ; preds = %41, %12
%16 = load i8*, i8** %8
%17 = load i8, i8* %16
%18 = call i32 @tolower(i8 zeroext %17)
%19 = load i8*, i8** %9
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %9
%21 = load i8, i8* %19
%22 = call i32 @tolower(i8 zeroext %21)
%23 = icmp ne i32 %18, %22
br i1 %23, label %24, label %33
24: ; preds = %15
%25 = load i8*, i8** %8
%26 = load i8, i8* %25
%27 = call i32 @tolower(i8 zeroext %26)
%28 = load i8*, i8** %9
%29 = getelementptr inbounds i8, i8* %28, i32 -1
store i8* %29, i8** %9
%30 = load i8, i8* %29
%31 = call i32 @tolower(i8 zeroext %30)
%32 = sub nsw i32 %27, %31
store i32 %32, i32* %4
br label %47
33: ; preds = %15
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %8
%36 = load i8, i8* %34
%37 = zext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
br label %45
40: ; preds = %33
br label %41
41: ; preds = %40
%42 = load i64, i64* %7
%43 = add i64 %42, -1
store i64 %43, i64* %7
%44 = icmp ne i64 %43, 0
br i1 %44, label %15, label %45
45: ; preds = %41, %39
br label %46
46: ; preds = %45, %3
store i32 0, i32* %4
br label %47
47: ; preds = %46, %24
%48 = load i32, i32* %4
ret i32 %48
}

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