#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*/ x;

/* Variables and functions */
int binary_search(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  int x[] = {2, 3, 5, 9, 12, 13, 24, 31, 72, 85, 99, 105};
  int len = sizeof(x) / sizeof(int), i = 6;
  for (int j = 0; j < 106; j++) {
    printf("%i exists in the array at index %i\n", j,
           binary_search(x, 0, len - 1, j));
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xeb6(%rip),%rsi
mov    $0x30,%edx
call   1040 <memcpy@plt>
movl   $0xc,-0x44(%rbp)
movl   $0x6,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpl   $0x6a,-0x4c(%rbp)
jge    11ac <main+0x7c>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
sub    $0x1,%edx
mov    -0x4c(%rbp),%ecx
xor    %esi,%esi
call   1050 <binary_search@plt>
mov    -0x50(%rbp),%esi
mov    %eax,%edx
lea    0xe97(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1169 <main+0x39>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
movaps 0xef0(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xed4(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xeb8(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %ebx,%ebx
mov    %rsp,%r14
lea    0xed8(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xb,%edx
mov    %ebx,%ecx
call   1040 <binary_search@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <printf@plt>
add    $0x1,%ebx
cmp    $0x6a,%ebx
jne    1160 <main+0x40>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.x = [12 x i32] [i32 2, i32 3, i32 5, i32 9, i32 12, i32 13, i32 24, i32 31, i32 72, i32 85, i32 99, i32 105]
@.str = [36 x i8] c"%i exists in the array at index %i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [12 x i32]* %2 to i8*
store i32 12, i32* %3
store i32 6, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %18, %0
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 106
br i1 %9, label %10, label %21
10: ; preds = %7
%11 = load i32, i32* %5
%12 = getelementptr inbounds [12 x i32], [12 x i32]* %2, i64 0, i64 0
%13 = load i32, i32* %3
%14 = sub nsw i32 %13, 1
%15 = load i32, i32* %5
%16 = call i32 @binary_search(i32* %12, i32 0, i32 %14, i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %16)
br label %18
18: ; preds = %10
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
store i32 %20, i32* %5
br label %7
21: ; preds = %7
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@__const.main.x = [12 x i32] [i32 2, i32 3, i32 5, i32 9, i32 12, i32 13, i32 24, i32 31, i32 72, i32 85, i32 99, i32 105]
@.str = [36 x i8] c"%i exists in the array at index %i\0A\00"
define i32 @main() {
%1 = alloca [12 x i32]
%2 = bitcast [12 x i32]* %1 to i8*
%3 = getelementptr inbounds [12 x i32], [12 x i32]* %1, i64 0, i64 0
br label %5
4: ; preds = %5
ret i32 0
5: ; preds = %0, %5
%6 = phi i32 [ 0, %0 ], [ %9, %5 ]
%7 = call i32 @binary_search(i32* %3, i32 0, i32 11, i32 %6)
%8 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = add nuw nsw i32 %6, 1
%10 = icmp eq i32 %9, 106
br i1 %10, label %4, label %5
}