#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 /*<<< orphan*/ free(char *);
char *safe_malloc(int);
int strlen(char const *);
int strncmp(char *, char *, size_t);
char tolower(char const);
int strncasecmp(const char *s1, const char *s2, size_t n) {
  int i, ret;
  char *cp1, *cp2;
  cp1 = safe_malloc(strlen(s1) + 1);
  cp2 = safe_malloc(strlen(s2) + 1);
  for (i = 0; i < strlen(s1) + 1; i++)
    cp1[i] = tolower(s1[i]);
  for (i = 0; i < strlen(s2) + 1; i++)
    cp2[i] = tolower(s2[i]);
  ret = strncmp(cp1, cp2, n);
  free(cp1);
  free(cp2);
  return ret;
}


x86 assembly (clang, -O0):
<strncasecmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1050 <safe_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1050 <safe_malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11e4 <strncasecmp+0x94>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1070 <tolower@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1199 <strncasecmp+0x49>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1236 <strncasecmp+0xe6>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1070 <tolower@plt>
mov    %al,%dl
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11eb <strncasecmp+0x9b>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncmp@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncasecmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%rbx
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <safe_malloc@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <safe_malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
js     11c2 <strncasecmp+0x72>
mov    $0xffffffffffffffff,%rbp
nopw   0x0(%rax,%rax,1)
movsbl 0x1(%rbx,%rbp,1),%edi
call   1070 <tolower@plt>
mov    %al,0x1(%r15,%rbp,1)
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
add    $0x1,%rbp
cmp    %rax,%rbp
jl     11a0 <strncasecmp+0x50>
mov    %r13,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
js     1203 <strncasecmp+0xb3>
mov    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movsbl 0x1(%r13,%rbx,1),%edi
call   1070 <tolower@plt>
mov    %al,0x1(%r12,%rbx,1)
mov    %r13,%rdi
call   1060 <strlen@plt>
cltq
add    $0x1,%rbx
cmp    %rax,%rbx
jl     11e0 <strncasecmp+0x90>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <strncmp@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %12, 1
%14 = call i8* @safe_malloc(i32 %13)
store i8* %14, i8** %9
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, 1
%18 = call i8* @safe_malloc(i32 %17)
store i8* %18, i8** %10
store i32 0, i32* %7
br label %19
19: ; preds = %36, %3
%20 = load i32, i32* %7
%21 = load i8*, i8** %4
%22 = call i32 @strlen(i8* %21)
%23 = add nsw i32 %22, 1
%24 = icmp slt i32 %20, %23
br i1 %24, label %25, label %39
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = call signext i8 @tolower(i8 signext %30)
%32 = load i8*, i8** %9
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
br label %36
36: ; preds = %25
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %19
39: ; preds = %19
store i32 0, i32* %7
br label %40
40: ; preds = %57, %39
%41 = load i32, i32* %7
%42 = load i8*, i8** %5
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %43, 1
%45 = icmp slt i32 %41, %44
br i1 %45, label %46, label %60
46: ; preds = %40
%47 = load i8*, i8** %5
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = load i8, i8* %50
%52 = call signext i8 @tolower(i8 signext %51)
%53 = load i8*, i8** %10
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 %52, i8* %56
br label %57
57: ; preds = %46
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
store i32 %59, i32* %7
br label %40
60: ; preds = %40
%61 = load i8*, i8** %9
%62 = load i8*, i8** %10
%63 = load i64, i64* %6
%64 = call i32 @strncmp(i8* %61, i8* %62, i64 %63)
store i32 %64, i32* %8
%65 = load i8*, i8** %9
%66 = call i32 @free(i8* %65)
%67 = load i8*, i8** %10
%68 = call i32 @free(i8* %67)
%69 = load i32, i32* %8
ret i32 %69
}

LLVM IR (-O2):
define i32 @strncasecmp(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 1
%6 = tail call i8* @safe_malloc(i32 %5)
%7 = tail call i32 @strlen(i8* %1)
%8 = add nsw i32 %7, 1
%9 = tail call i8* @safe_malloc(i32 %8)
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %15, %3
%13 = tail call i32 @strlen(i8* %1)
%14 = icmp slt i32 %13, 0
br i1 %14, label %35, label %25
15: ; preds = %3, %15
%16 = phi i64 [ %21, %15 ], [ 0, %3 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = tail call signext i8 @tolower(i8 signext %18)
%20 = getelementptr inbounds i8, i8* %6, i64 %16
store i8 %19, i8* %20
%21 = add nuw nsw i64 %16, 1
%22 = tail call i32 @strlen(i8* %0)
%23 = sext i32 %22 to i64
%24 = icmp slt i64 %16, %23
br i1 %24, label %15, label %12
25: ; preds = %12, %25
%26 = phi i64 [ %31, %25 ], [ 0, %12 ]
%27 = getelementptr inbounds i8, i8* %1, i64 %26
%28 = load i8, i8* %27
%29 = tail call signext i8 @tolower(i8 signext %28)
%30 = getelementptr inbounds i8, i8* %9, i64 %26
store i8 %29, i8* %30
%31 = add nuw nsw i64 %26, 1
%32 = tail call i32 @strlen(i8* %1)
%33 = sext i32 %32 to i64
%34 = icmp slt i64 %26, %33
br i1 %34, label %25, label %35
35: ; preds = %25, %12
%36 = tail call i32 @strncmp(i8* %6, i8* %9, i64 %2)
%37 = tail call i32 @free(i8* %6)
%38 = tail call i32 @free(i8* %9)
ret i32 %36
}