#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 mservcli_strnicmp(const char *str1, const char *str2, int n) {
  while (n-- > 0) {
    if (tolower(*str1) != tolower(*str2++))
      return (tolower(*str1) - tolower(*--str2));
    if (*str1++ == '\0')
      break;
  }
  return (0);
}


x86 assembly (clang, -O0):
<mservcli_strnicmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x1c(%rbp)
cmp    $0x0,%eax
jle    11c5 <mservcli_strnicmp+0xb5>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
je     11a0 <mservcli_strnicmp+0x90>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
movsbl -0x1(%rax),%edi
call   1030 <tolower@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11cc <mservcli_strnicmp+0xbc>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11c0 <mservcli_strnicmp+0xb0>
jmp    11c5 <mservcli_strnicmp+0xb5>
jmp    1123 <mservcli_strnicmp+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mservcli_strnicmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r12
xor    %r14d,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
jle    117a <mservcli_strnicmp+0x6a>
movsbl (%r12,%rbx,1),%edi
call   1030 <tolower@plt>
mov    %eax,%r13d
movsbl (%r15,%rbx,1),%edi
call   1030 <tolower@plt>
cmp    %eax,%r13d
jne    1160 <mservcli_strnicmp+0x50>
add    $0xffffffff,%ebp
cmpb   $0x0,(%r12,%rbx,1)
lea    0x1(%rbx),%rbx
jne    1130 <mservcli_strnicmp+0x20>
jmp    117a <mservcli_strnicmp+0x6a>
movsbl (%r12,%rbx,1),%edi
call   1030 <tolower@plt>
mov    %eax,%r14d
movsbl (%r15,%rbx,1),%edi
call   1030 <tolower@plt>
sub    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mservcli_strnicmp(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
br label %8
8: ; preds = %37, %3
%9 = load i32, i32* %7
%10 = add nsw i32 %9, -1
store i32 %10, i32* %7
%11 = icmp sgt i32 %9, 0
br i1 %11, label %12, label %38
12: ; preds = %8
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = call i32 @tolower(i8 signext %14)
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %6
%18 = load i8, i8* %16
%19 = call i32 @tolower(i8 signext %18)
%20 = icmp ne i32 %15, %19
br i1 %20, label %21, label %30
21: ; preds = %12
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = call i32 @tolower(i8 signext %23)
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %6
%27 = load i8, i8* %26
%28 = call i32 @tolower(i8 signext %27)
%29 = sub nsw i32 %24, %28
store i32 %29, i32* %4
br label %39
30: ; preds = %12
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
%33 = load i8, i8* %31
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %30
br label %38
37: ; preds = %30
br label %8
38: ; preds = %36, %8
store i32 0, i32* %4
br label %39
39: ; preds = %38, %21
%40 = load i32, i32* %4
ret i32 %40
}

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