#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 strcasecmp(char const *, char const *);
int strcmp(char const *, char const *);
size_t strlen(char *);
bool endswith(char *haystack, char *needle, bool case_sensitive) {
  int (*cmp)(const char *, const char *) = strcmp;
  if (!case_sensitive) {
    cmp = strcasecmp;
  }
  size_t haystack_len = strlen(haystack);
  size_t needle_len = strlen(needle);
  return needle_len <= haystack_len &&
         cmp(haystack + haystack_len - needle_len, needle) == 0;
}


x86 assembly (clang, -O0):
<endswith>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2eb6(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1143 <endswith+0x33>
mov    0x2e91(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x28(%rbp),%rcx
mov    %al,-0x31(%rbp)
ja     1194 <endswith+0x84>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rdi
add    -0x28(%rbp),%rdi
xor    %ecx,%ecx
sub    -0x30(%rbp),%rcx
add    %rcx,%rdi
mov    -0x10(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<endswith>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
xor    %ecx,%ecx
cmp    %r15,%rax
ja     115f <endswith+0x4f>
test   %ebp,%ebp
jne    1143 <endswith+0x33>
mov    0x2e8f(%rip),%rcx
jmp    114a <endswith+0x3a>
mov    0x2e96(%rip),%rcx
add    %r15,%rbx
sub    %rax,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   *%rcx
xor    %ecx,%ecx
test   %eax,%eax
sete   %cl
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @endswith(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32 (i8*, i8*)*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 (i8*, i8*)* @strcmp, i32 (i8*, i8*)** %7
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 (i8*, i8*)* @strcasecmp, i32 (i8*, i8*)** %7
br label %13
13: ; preds = %12, %3
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %8
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %9
%18 = load i64, i64* %9
%19 = load i64, i64* %8
%20 = icmp ule i64 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %13
%22 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %7
%23 = load i8*, i8** %4
%24 = load i64, i64* %8
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i64, i64* %9
%27 = sub i64 0, %26
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8*, i8** %5
%30 = call i32 %22(i8* %28, i8* %29)
%31 = icmp eq i32 %30, 0
br label %32
32: ; preds = %21, %13
%33 = phi i1 [ false, %13 ], [ %31, %21 ]
%34 = zext i1 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
define i32 @endswith(i8* %0, i8* %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = icmp ugt i64 %5, %4
br i1 %6, label %16, label %7
7: ; preds = %3
%8 = icmp eq i32 %2, 0
%9 = select i1 %8, i32 (i8*, i8*)* @strcasecmp, i32 (i8*, i8*)* @strcmp
%10 = getelementptr inbounds i8, i8* %0, i64 %4
%11 = sub i64 0, %5
%12 = getelementptr inbounds i8, i8* %10, i64 %11
%13 = tail call i32 %9(i8* %12, i8* %1)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %7, %3
%17 = phi i32 [ 0, %3 ], [ %15, %7 ]
ret i32 %17
}