#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, int max) {
  int diff = 0;
  for (; max && !(diff = tolower(*s2) - tolower(*s1)) && *s1; max--, s1++, s2++)
    ;
  return diff;
}


x86 assembly (clang, -O0):
<strncasecmp>:
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)
je     117a <strncasecmp+0x6a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    -0x20(%rbp),%ecx
sub    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
jne    117a <strncasecmp+0x6a>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118a <strncasecmp+0x7a>
jmp    11b5 <strncasecmp+0xa5>
jmp    118f <strncasecmp+0x7f>
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 <strncasecmp+0x1a>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncasecmp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
je     1161 <strncasecmp+0x51>
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
add    $0xffffffff,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%edi
call   1030 <tolower@plt>
mov    %eax,%ebp
movsbl (%r12,%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    %ebx,%r15d
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, 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 = %28, %3
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %8
%12 = load i8*, i8** %5
%13 = load i8, i8* %12
%14 = call i32 @tolower(i8 signext %13)
%15 = load i8*, i8** %4
%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 ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %11
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br label %25
25: ; preds = %20, %11, %8
%26 = phi i1 [ false, %11 ], [ false, %8 ], [ %24, %20 ]
br i1 %26, label %27, label %35
27: ; preds = %25
br label %28
28: ; preds = %27
%29 = load i32, i32* %6
%30 = add nsw i32 %29, -1
store i32 %30, i32* %6
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %5
br label %8
35: ; preds = %25
%36 = load i32, i32* %7
ret i32 %36
}

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