#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 */
scalar_t__ tolower(unsigned char);
int safe_strncasecmp(const char *s1, const char *s2, size_t n) {
  size_t s;
  for (s = 0; s < n; s++) {
    if (tolower((unsigned char)s1[s]) != tolower((unsigned char)s2[s]))
      return 1;
    if (s1[s] == '\0')
      return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<safe_strncasecmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11b3 <safe_strncasecmp+0xa3>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <tolower@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <tolower@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     117c <safe_strncasecmp+0x6c>
movl   $0x1,-0x4(%rbp)
jmp    11ba <safe_strncasecmp+0xaa>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    119d <safe_strncasecmp+0x8d>
movl   $0x0,-0x4(%rbp)
jmp    11ba <safe_strncasecmp+0xaa>
jmp    11a2 <safe_strncasecmp+0x92>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    112c <safe_strncasecmp+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_strncasecmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     117a <safe_strncasecmp+0x6a>
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl -0x1(%r13,%rbp,1),%edi
call   1030 <tolower@plt>
mov    %rax,%rbx
movzbl -0x1(%r15,%rbp,1),%edi
call   1030 <tolower@plt>
cmp    %rax,%rbx
jne    1174 <safe_strncasecmp+0x64>
cmpb   $0x0,-0x1(%r13,%rbp,1)
je     117a <safe_strncasecmp+0x6a>
lea    0x1(%rbp),%rax
cmp    %r12,%rbp
mov    %rax,%rbp
jb     1140 <safe_strncasecmp+0x30>
jmp    117a <safe_strncasecmp+0x6a>
mov    $0x1,%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 @safe_strncasecmp(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %9
9: ; preds = %35, %3
%10 = load i64, i64* %8
%11 = load i64, i64* %7
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %38
13: ; preds = %9
%14 = load i8*, i8** %5
%15 = load i64, i64* %8
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = load i8, i8* %16
%18 = call i64 @tolower(i8 zeroext %17)
%19 = load i8*, i8** %6
%20 = load i64, i64* %8
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
%23 = call i64 @tolower(i8 zeroext %22)
%24 = icmp ne i64 %18, %23
br i1 %24, label %25, label %26
25: ; preds = %13
store i32 1, i32* %4
br label %39
26: ; preds = %13
%27 = load i8*, i8** %5
%28 = load i64, i64* %8
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 0, i32* %4
br label %39
34: ; preds = %26
br label %35
35: ; preds = %34
%36 = load i64, i64* %8
%37 = add i64 %36, 1
store i64 %37, i64* %8
br label %9
38: ; preds = %9
store i32 0, i32* %4
br label %39
39: ; preds = %38, %33, %25
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
define i32 @safe_strncasecmp(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3, %14
%6 = phi i64 [ %17, %14 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = tail call i64 @tolower(i8 zeroext %8)
%10 = getelementptr inbounds i8, i8* %1, i64 %6
%11 = load i8, i8* %10
%12 = tail call i64 @tolower(i8 zeroext %11)
%13 = icmp eq i64 %9, %12
br i1 %13, label %14, label %20
14: ; preds = %5
%15 = load i8, i8* %7
%16 = icmp ne i8 %15, 0
%17 = add nuw i64 %6, 1
%18 = icmp ult i64 %17, %2
%19 = select i1 %16, i1 %18, i1 false
br i1 %19, label %5, label %20
20: ; preds = %5, %14, %3
%21 = phi i32 [ 0, %3 ], [ 0, %14 ], [ 1, %5 ]
ret i32 %21
}