#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 *);
int /*<<< orphan*/ itolower(char *, scalar_t__, char const *, int);
char *strdup(char const *);
scalar_t__ strlen(char *);
int strncmp(char *, char *, size_t);
int istrncasecmp(const char *n1, const char *n2, size_t len, int casemap) {
  if (len == 0)
    return 0;
  char *d1 = strdup(n1);
  char *d2 = strdup(n2);
  itolower(d1, strlen(d1) + 1, n1, casemap);
  itolower(d2, strlen(d2) + 1, n2, casemap);
  int i = strncmp(d1, d2, len);
  free(d1);
  free(d2);
  return i;
}


x86 assembly (clang, -O0):
<istrncasecmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <istrncasecmp+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    1214 <istrncasecmp+0xc4>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <itolower@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <itolower@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <strncmp@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<istrncasecmp>:
test   %rdx,%rdx
je     11ea <istrncasecmp+0x9a>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
call   1070 <strdup@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbp,%rdi
mov    %r13,%rdx
mov    %r14d,%ecx
call   1050 <itolower@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r14d,%ecx
call   1050 <itolower@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <strncmp@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @istrncasecmp(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%13 = load i64, i64* %8
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 0, i32* %5
br label %44
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %10
%19 = load i8*, i8** %7
%20 = call i8* @strdup(i8* %19)
store i8* %20, i8** %11
%21 = load i8*, i8** %10
%22 = load i8*, i8** %10
%23 = call i64 @strlen(i8* %22)
%24 = add nsw i64 %23, 1
%25 = load i8*, i8** %6
%26 = load i32, i32* %9
%27 = call i32 @itolower(i8* %21, i64 %24, i8* %25, i32 %26)
%28 = load i8*, i8** %11
%29 = load i8*, i8** %11
%30 = call i64 @strlen(i8* %29)
%31 = add nsw i64 %30, 1
%32 = load i8*, i8** %7
%33 = load i32, i32* %9
%34 = call i32 @itolower(i8* %28, i64 %31, i8* %32, i32 %33)
%35 = load i8*, i8** %10
%36 = load i8*, i8** %11
%37 = load i64, i64* %8
%38 = call i32 @strncmp(i8* %35, i8* %36, i64 %37)
store i32 %38, i32* %12
%39 = load i8*, i8** %10
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %11
%42 = call i32 @free(i8* %41)
%43 = load i32, i32* %12
store i32 %43, i32* %5
br label %44
44: ; preds = %16, %15
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
define i32 @istrncasecmp(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %18, label %6
6: ; preds = %4
%7 = tail call i8* @strdup(i8* %0)
%8 = tail call i8* @strdup(i8* %1)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %7)
%10 = add nsw i64 %9, 1
%11 = tail call i32 @itolower(i8* %7, i64 %10, i8* %0, i32 %3)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%13 = add nsw i64 %12, 1
%14 = tail call i32 @itolower(i8* %8, i64 %13, i8* %1, i32 %3)
%15 = tail call i32 @strncmp(i8* %7, i8* %8, i64 %2)
%16 = tail call i32 @free(i8* %7)
%17 = tail call i32 @free(i8* %8)
br label %18
18: ; preds = %4, %6
%19 = phi i32 [ %15, %6 ], [ 0, %4 ]
ret i32 %19
}