#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 strcncmp(const char *s1, const char *s2, int n) {
  int d = 0;
  while (n > 0 && (d = tolower(*s1) - tolower(*s2)) == 0) {
    --n;
    ++s1;
    ++s2;
  }
  return d;
}


x86 assembly (clang, -O0):
<strcncmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jle    1167 <strcncmp+0x57>
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,-0x18(%rbp)
cmp    $0x0,%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1177 <strcncmp+0x67>
jmp    119d <strcncmp+0x8d>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    112a <strcncmp+0x1a>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcncmp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    1158 <strcncmp+0x48>
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
add    $0x1,%r12d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <tolower@plt>
mov    %eax,%ebp
movsbl (%r14,%rbx,1),%edi
call   1030 <tolower@plt>
sub    %eax,%ebp
jne    115a <strcncmp+0x4a>
add    $0xffffffff,%r12d
add    $0x1,%rbx
cmp    $0x1,%r12d
jg     1130 <strcncmp+0x20>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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