#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(char);
bool startswith(char *haystack, char *needle, bool case_sensitive) {
  while (true) {
    char nchar = *needle++;
    char hchar = *haystack++;
    if (nchar == 0) {
      return true;
    } else if (hchar == 0) {
      return false;
    }
    if (case_sensitive) {
      nchar = (char)tolower(nchar);
      hchar = (char)tolower(hchar);
    }
    if (nchar != hchar) {
      return false;
    }
  }
}


x86 assembly (clang, -O0):
<startswith>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%al
mov    %al,-0x1d(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    %al,-0x1e(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x0,%eax
jne    1164 <startswith+0x54>
movl   $0x1,-0x4(%rbp)
jmp    11c5 <startswith+0xb5>
movsbl -0x1e(%rbp),%eax
cmp    $0x0,%eax
jne    117d <startswith+0x6d>
movl   $0x0,-0x4(%rbp)
jmp    11c5 <startswith+0xb5>
jmp    1182 <startswith+0x72>
cmpl   $0x0,-0x1c(%rbp)
je     11a4 <startswith+0x94>
movsbl -0x1d(%rbp),%edi
call   1030 <tolower@plt>
mov    %al,-0x1d(%rbp)
movsbl -0x1e(%rbp),%edi
call   1030 <tolower@plt>
mov    %al,-0x1e(%rbp)
movsbl -0x1d(%rbp),%eax
movsbl -0x1e(%rbp),%ecx
cmp    %ecx,%eax
je     11c0 <startswith+0xb0>
movl   $0x0,-0x4(%rbp)
jmp    11c5 <startswith+0xb5>
jmp    1123 <startswith+0x13>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startswith>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,0x4(%rsp)
mov    %rsi,%r12
mov    %rdi,%r15
xor    %r14d,%r14d
xor    %r13d,%r13d
jmp    1139 <startswith+0x29>
nopl   (%rax)
add    $0x1,%r14
cmp    %bpl,%bl
jne    1172 <startswith+0x62>
movzbl (%r12,%r14,1),%ebx
test   %bl,%bl
je     116c <startswith+0x5c>
movzbl (%r15,%r14,1),%ebp
test   %bpl,%bpl
je     1172 <startswith+0x62>
cmpl   $0x0,0x4(%rsp)
je     1130 <startswith+0x20>
movsbl %bl,%edi
call   1030 <tolower@plt>
mov    %rax,%rbx
movsbl %bpl,%edi
call   1030 <tolower@plt>
mov    %rax,%rbp
jmp    1130 <startswith+0x20>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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