#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 binary_search_iterative(int *, int, int /*<<< orphan*/, int);
int binary_search_recursive(int *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  int s[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int key, idx;
  key = 8;
  printf("binary search recursive: %d\n", key);
  idx = binary_search_recursive(s, key, 0, 9);
  key = 11;
  printf("binary search recursive: %d\n", key);
  idx = binary_search_recursive(s, key, 0, 9);
  key = 8;
  printf("binary search iterative: %d\n", key);
  idx = binary_search_iterative(s, key, 0, 9);
  key = 11;
  printf("binary search iterative: %d\n", key);
  idx = binary_search_iterative(s, key, 0, 9);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xe9f(%rip),%rsi
mov    $0x28,%edx
call   1040 <memcpy@plt>
movl   $0x8,-0x44(%rbp)
mov    -0x44(%rbp),%esi
lea    0xeac(%rip),%rdi
call   1030 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1050 <binary_search_recursive@plt>
mov    %eax,-0x48(%rbp)
movl   $0xb,-0x44(%rbp)
mov    -0x44(%rbp),%esi
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1050 <binary_search_recursive@plt>
mov    %eax,-0x48(%rbp)
movl   $0x8,-0x44(%rbp)
mov    -0x44(%rbp),%esi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1060 <binary_search_iterative@plt>
mov    %eax,-0x48(%rbp)
movl   $0xb,-0x44(%rbp)
mov    -0x44(%rbp),%esi
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1060 <binary_search_iterative@plt>
mov    %eax,-0x48(%rbp)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0xee0(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0xec4(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xea8(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xec5(%rip),%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   1030 <printf@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x8,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1040 <binary_search_recursive@plt>
mov    %r14,%rdi
mov    $0xb,%esi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    $0xb,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1040 <binary_search_recursive@plt>
lea    0xe96(%rip),%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    $0x8,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1050 <binary_search_iterative@plt>
mov    %rbx,%rdi
mov    $0xb,%esi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    $0xb,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1050 <binary_search_iterative@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.s = [10 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@.str = [29 x i8] c"binary search recursive: %d\0A\00"
@.str.1 = [29 x i8] c"binary search iterative: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10 x i32]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [10 x i32]* %6 to i8*
store i32 8, i32* %7
%10 = load i32, i32* %7
%11 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [10 x i32], [10 x i32]* %6, i64 0, i64 0
%13 = load i32, i32* %7
%14 = call i32 @binary_search_recursive(i32* %12, i32 %13, i32 0, i32 9)
store i32 %14, i32* %8
store i32 11, i32* %7
%15 = load i32, i32* %7
%16 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [10 x i32], [10 x i32]* %6, i64 0, i64 0
%18 = load i32, i32* %7
%19 = call i32 @binary_search_recursive(i32* %17, i32 %18, i32 0, i32 9)
store i32 %19, i32* %8
store i32 8, i32* %7
%20 = load i32, i32* %7
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [10 x i32], [10 x i32]* %6, i64 0, i64 0
%23 = load i32, i32* %7
%24 = call i32 @binary_search_iterative(i32* %22, i32 %23, i32 0, i32 9)
store i32 %24, i32* %8
store i32 11, i32* %7
%25 = load i32, i32* %7
%26 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %6, i64 0, i64 0
%28 = load i32, i32* %7
%29 = call i32 @binary_search_iterative(i32* %27, i32 %28, i32 0, i32 9)
store i32 %29, i32* %8
ret i32 0
}

LLVM IR (-O2):
@__const.main.s = [10 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@.str = [29 x i8] c"binary search recursive: %d\0A\00"
@.str.1 = [29 x i8] c"binary search iterative: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i32]
%4 = bitcast [10 x i32]* %3 to i8*
%5 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 8)
%6 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
%7 = call i32 @binary_search_recursive(i32* %6, i32 8, i32 0, i32 9)
%8 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 11)
%9 = call i32 @binary_search_recursive(i32* %6, i32 11, i32 0, i32 9)
%10 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 8)
%11 = call i32 @binary_search_iterative(i32* %6, i32 8, i32 0, i32 9)
%12 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 11)
%13 = call i32 @binary_search_iterative(i32* %6, i32 11, i32 0, i32 9)
ret i32 0
}