#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 */
typedef int /*<<< orphan*/ arr2;

/* Variables and functions */
int binary_search(int *, int /*<<< orphan*/, int, int);
int binary_search_left(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  printf("base test: \n");
  printf("find 10: %d\n", binary_search(arr1, 0, 10, 10));
  printf("find -1: %d\n", binary_search(arr1, 0, 10, -1));
  int arr2[] = {1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7};
  int len = sizeof(arr2) / sizeof(int);
  printf("find 2: %d\n", binary_search_left(arr2, 0, len - 1, 2));
  printf("find 4: %d\n", binary_search_left(arr2, 0, len - 1, 4));
  int arr3[] = {1};
  printf("find 1: %d\n", binary_search(arr3, 0, 0, 1));
  printf("find 2: %d\n", binary_search(arr3, 0, 0, 2));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xea6(%rip),%rsi
mov    $0x2c,%edx
call   1040 <memcpy@plt>
lea    0xef1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%ecx
mov    %ecx,%edx
call   1050 <binary_search@plt>
mov    %eax,%esi
lea    0xedc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
mov    $0xffffffff,%ecx
call   1050 <binary_search@plt>
mov    %eax,%esi
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe6c(%rip),%rsi
mov    $0x2c,%edx
call   1040 <memcpy@plt>
movl   $0xb,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
mov    $0x2,%ecx
call   1060 <binary_search_left@plt>
mov    %eax,%esi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x64(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
mov    $0x4,%ecx
call   1060 <binary_search_left@plt>
mov    %eax,%esi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0xe81(%rip),%eax
mov    %eax,-0x68(%rbp)
lea    -0x68(%rbp),%rdi
xor    %edx,%edx
mov    $0x1,%ecx
mov    %edx,%esi
call   1050 <binary_search@plt>
mov    %eax,%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x68(%rbp),%rdi
xor    %edx,%edx
mov    $0x2,%ecx
mov    %edx,%esi
call   1050 <binary_search@plt>
mov    %eax,%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
movups 0xece(%rip),%xmm0
movups %xmm0,0x5c(%rsp)
movaps 0xeb6(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xe9a(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
lea    0xf28(%rip),%rdi
call   1030 <puts@plt>
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
mov    $0xa,%ecx
call   1050 <binary_search@plt>
lea    0xec5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
mov    $0xffffffff,%ecx
call   1050 <binary_search@plt>
lea    0xeae(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movups 0xe81(%rip),%xmm0
movups %xmm0,0x2c(%rsp)
movaps 0xe69(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe4d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
mov    $0x2,%ecx
call   1060 <binary_search_left@plt>
lea    0xe6e(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
mov    $0x4,%ecx
call   1060 <binary_search_left@plt>
lea    0xe53(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x1,0xc(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    $0x1,%ecx
call   1050 <binary_search@plt>
lea    0xe31(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    $0x2,%ecx
call   1050 <binary_search@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.arr1 = [11 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11]
@.str = [13 x i8] c"base test: \0A\00"
@.str.1 = [13 x i8] c"find 10: %d\0A\00"
@.str.2 = [13 x i8] c"find -1: %d\0A\00"
@__const.main.arr2 = [11 x i32] [i32 1, i32 2, i32 2, i32 3, i32 4, i32 4, i32 4, i32 5, i32 6, i32 7, i32 7]
@.str.3 = [12 x i8] c"find 2: %d\0A\00"
@.str.4 = [12 x i8] c"find 4: %d\0A\00"
@__const.main.arr3 = [1 x i32] [i32 1]
@.str.5 = [12 x i8] c"find 1: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [11 x i32]
%3 = alloca [11 x i32]
%4 = alloca i32
%5 = alloca [1 x i32]
store i32 0, i32* %1
%6 = bitcast [11 x i32]* %2 to i8*
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [11 x i32], [11 x i32]* %2, i64 0, i64 0
%9 = call i32 @binary_search(i32* %8, i32 0, i32 10, i32 10)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [11 x i32], [11 x i32]* %2, i64 0, i64 0
%12 = call i32 @binary_search(i32* %11, i32 0, i32 10, i32 -1)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = bitcast [11 x i32]* %3 to i8*
store i32 11, i32* %4
%15 = getelementptr inbounds [11 x i32], [11 x i32]* %3, i64 0, i64 0
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
%18 = call i32 @binary_search_left(i32* %15, i32 0, i32 %17, i32 2)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [11 x i32], [11 x i32]* %3, i64 0, i64 0
%21 = load i32, i32* %4
%22 = sub nsw i32 %21, 1
%23 = call i32 @binary_search_left(i32* %20, i32 0, i32 %22, i32 4)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = bitcast [1 x i32]* %5 to i8*
%26 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%27 = call i32 @binary_search(i32* %26, i32 0, i32 0, i32 1)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [1 x i32], [1 x i32]* %5, i64 0, i64 0
%30 = call i32 @binary_search(i32* %29, i32 0, i32 0, i32 2)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %30)
ret i32 0
}

LLVM IR (-O2):
@__const.main.arr1 = [11 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11]
@.str.1 = [13 x i8] c"find 10: %d\0A\00"
@.str.2 = [13 x i8] c"find -1: %d\0A\00"
@__const.main.arr2 = [11 x i32] [i32 1, i32 2, i32 2, i32 3, i32 4, i32 4, i32 4, i32 5, i32 6, i32 7, i32 7]
@.str.3 = [12 x i8] c"find 2: %d\0A\00"
@.str.4 = [12 x i8] c"find 4: %d\0A\00"
@.str.5 = [12 x i8] c"find 1: %d\0A\00"
@str = [12 x i8] c"base test: \00"
define i32 @main() {
%1 = alloca [11 x i32]
%2 = alloca [11 x i32]
%3 = alloca [1 x i32]
%4 = bitcast [11 x i32]* %1 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [11 x i32], [11 x i32]* %1, i64 0, i64 0
%7 = call i32 @binary_search(i32* %6, i32 0, i32 10, i32 10)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = call i32 @binary_search(i32* %6, i32 0, i32 10, i32 -1)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = bitcast [11 x i32]* %2 to i8*
%12 = getelementptr inbounds [11 x i32], [11 x i32]* %2, i64 0, i64 0
%13 = call i32 @binary_search_left(i32* %12, i32 0, i32 10, i32 2)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = call i32 @binary_search_left(i32* %12, i32 0, i32 10, i32 4)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = bitcast [1 x i32]* %3 to i8*
%18 = getelementptr inbounds [1 x i32], [1 x i32]* %3, i64 0, i64 0
store i32 1, i32* %18
%19 = call i32 @binary_search(i32* %18, i32 0, i32 0, i32 1)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = call i32 @binary_search(i32* %18, i32 0, i32 0, i32 2)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %21)
ret i32 0
}