#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(int);
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((int)(unsigned char)s1[i]);
  for (i = 0; i < strlen(s2) + 1; i++)
    cp2[i] = tolower((int)(unsigned char)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
movzbl (%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
movzbl (%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)
movzbl 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
movzbl 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 = %37, %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 %40
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 = zext i8 %30 to i32
%32 = call signext i8 @tolower(i32 %31)
%33 = load i8*, i8** %9
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 %32, i8* %36
br label %37
37: ; preds = %25
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %19
40: ; preds = %19
store i32 0, i32* %7
br label %41
41: ; preds = %59, %40
%42 = load i32, i32* %7
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %44, 1
%46 = icmp slt i32 %42, %45
br i1 %46, label %47, label %62
47: ; preds = %41
%48 = load i8*, i8** %5
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = call signext i8 @tolower(i32 %53)
%55 = load i8*, i8** %10
%56 = load i32, i32* %7
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 %54, i8* %58
br label %59
59: ; preds = %47
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %41
62: ; preds = %41
%63 = load i8*, i8** %9
%64 = load i8*, i8** %10
%65 = load i64, i64* %6
%66 = call i32 @strncmp(i8* %63, i8* %64, i64 %65)
store i32 %66, i32* %8
%67 = load i8*, i8** %9
%68 = call i32 @free(i8* %67)
%69 = load i8*, i8** %10
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* %8
ret i32 %71
}

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 %37, label %26
15: ; preds = %3, %15
%16 = phi i64 [ %22, %15 ], [ 0, %3 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = tail call signext i8 @tolower(i32 %19)
%21 = getelementptr inbounds i8, i8* %6, i64 %16
store i8 %20, i8* %21
%22 = add nuw nsw i64 %16, 1
%23 = tail call i32 @strlen(i8* %0)
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %16, %24
br i1 %25, label %15, label %12
26: ; preds = %12, %26
%27 = phi i64 [ %33, %26 ], [ 0, %12 ]
%28 = getelementptr inbounds i8, i8* %1, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = tail call signext i8 @tolower(i32 %30)
%32 = getelementptr inbounds i8, i8* %9, i64 %27
store i8 %31, i8* %32
%33 = add nuw nsw i64 %27, 1
%34 = tail call i32 @strlen(i8* %1)
%35 = sext i32 %34 to i64
%36 = icmp slt i64 %27, %35
br i1 %36, label %26, label %37
37: ; preds = %26, %12
%38 = tail call i32 @strncmp(i8* %6, i8* %9, i64 %2)
%39 = tail call i32 @free(i8* %6)
%40 = tail call i32 @free(i8* %9)
ret i32 %38
}