#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 scalar_t__ clock_t;

/* Variables and functions */
int binary_search(int, int *, int);
int binary_search2(int, int *, int);
scalar_t__ clock();
int /*<<< orphan*/ print_results(int, char *, double);
int main(void) {
  int data[20000];
  int index;
  int i;
  clock_t time_taken;
  for (i = 0; i < 20000; ++i)
    data[i] = i;
  time_taken = 0;
  for (i = 0, time_taken = clock(); i < 1000000; ++i)
    index = binary_search(-1, data, 20000);
  time_taken = clock() - time_taken;
  print_results(index, "binary_search()", (double)time_taken);
  time_taken = 0;
  for (i = 0, time_taken = clock(); i < 1000000; ++i)
    index = binary_search2(-1, data, 20000);
  time_taken = clock() - time_taken;
  print_results(index, "binary_search2()", (double)time_taken);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x138a0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x13898(%rbp)
cmpl   $0x4e20,-0x13898(%rbp)
jge    1194 <main+0x54>
mov    -0x13898(%rbp),%ecx
movslq -0x13898(%rbp),%rax
mov    %ecx,-0x13890(%rbp,%rax,4)
mov    -0x13898(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13898(%rbp)
jmp    115c <main+0x1c>
movq   $0x0,-0x138a0(%rbp)
movl   $0x0,-0x13898(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x138a0(%rbp)
cmpl   $0xf4240,-0x13898(%rbp)
jge    11f7 <main+0xb7>
lea    -0x13890(%rbp),%rsi
mov    $0xffffffff,%edi
mov    $0x4e20,%edx
call   1060 <binary_search@plt>
mov    %eax,-0x13894(%rbp)
mov    -0x13898(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13898(%rbp)
jmp    11b7 <main+0x77>
mov    $0x0,%al
call   1030 <clock@plt>
sub    -0x138a0(%rbp),%rax
mov    %rax,-0x138a0(%rbp)
mov    -0x13894(%rbp),%edi
cvtsi2sdq -0x138a0(%rbp),%xmm0
fe ff
lea    0xdde(%rip),%rsi
call   1040 <print_results@plt>
movq   $0x0,-0x138a0(%rbp)
movl   $0x0,-0x13898(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x138a0(%rbp)
cmpl   $0xf4240,-0x13898(%rbp)
jge    128a <main+0x14a>
lea    -0x13890(%rbp),%rsi
mov    $0xffffffff,%edi
mov    $0x4e20,%edx
call   1050 <binary_search2@plt>
mov    %eax,-0x13894(%rbp)
mov    -0x13898(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13898(%rbp)
jmp    124a <main+0x10a>
mov    $0x0,%al
call   1030 <clock@plt>
sub    -0x138a0(%rbp),%rax
mov    %rax,-0x138a0(%rbp)
mov    -0x13894(%rbp),%edi
cvtsi2sdq -0x138a0(%rbp),%xmm0
fe ff
lea    0xd5b(%rip),%rsi
call   1040 <print_results@plt>
xor    %eax,%eax
add    $0x138a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x13888,%rsp
movdqa 0xeab(%rip),%xmm0
mov    $0x24,%eax
movdqa 0xead(%rip),%xmm8
movdqa 0xeb4(%rip),%xmm9
movdqa 0xebb(%rip),%xmm10
movdqa 0xec2(%rip),%xmm11
movdqa 0xec9(%rip),%xmm12
movdqa 0xed1(%rip),%xmm6
movdqa 0xed9(%rip),%xmm7
movdqa 0xee1(%rip),%xmm1
movdqa 0xee9(%rip),%xmm2
movdqa 0xef1(%rip),%xmm3
nop
movdqa %xmm0,%xmm4
paddd  %xmm8,%xmm4
movdqa %xmm0,-0x90(%rsp,%rax,4)
ff ff
movdqa %xmm4,-0x80(%rsp,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm9,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm10,%xmm5
movdqa %xmm4,-0x70(%rsp,%rax,4)
movdqa %xmm5,-0x60(%rsp,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm11,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm12,%xmm5
movdqa %xmm4,-0x50(%rsp,%rax,4)
movdqa %xmm5,-0x40(%rsp,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm6,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm7,%xmm5
movdqa %xmm4,-0x30(%rsp,%rax,4)
movdqa %xmm5,-0x20(%rsp,%rax,4)
movdqa %xmm0,%xmm4
paddd  %xmm1,%xmm4
movdqa %xmm0,%xmm5
paddd  %xmm2,%xmm5
movdqa %xmm4,-0x10(%rsp,%rax,4)
movdqa %xmm5,(%rsp,%rax,4)
paddd  %xmm3,%xmm0
add    $0x28,%rax
cmp    $0x4e44,%rax
jne    11b0 <main+0x70>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r14
mov    $0xf4240,%ebx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xffffffff,%edi
mov    %r15,%rsi
mov    $0x4e20,%edx
call   1060 <binary_search@plt>
add    $0xffffffff,%ebx
jne    1270 <main+0x130>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
lea    0xe0e(%rip),%rsi
mov    %ebp,%edi
call   1040 <print_results@plt>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r14
mov    $0xf4240,%ebx
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    $0xffffffff,%edi
mov    %r15,%rsi
mov    $0x4e20,%edx
call   1050 <binary_search2@plt>
add    $0xffffffff,%ebx
jne    12c0 <main+0x180>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
lea    0xdce(%rip),%rsi
mov    %ebp,%edi
call   1040 <print_results@plt>
xor    %eax,%eax
add    $0x13888,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"binary_search()\00"
@.str.1 = [17 x i8] c"binary_search2()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [20000 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %4
br label %6
6: ; preds = %14, %0
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 20000
br i1 %8, label %9, label %17
9: ; preds = %6
%10 = load i32, i32* %4
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [20000 x i32], [20000 x i32]* %2, i64 0, i64 %12
store i32 %10, i32* %13
br label %14
14: ; preds = %9
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 1
store i32 %16, i32* %4
br label %6
17: ; preds = %6
store i64 0, i64* %5
store i32 0, i32* %4
%18 = call i64 (...) @clock()
store i64 %18, i64* %5
br label %19
19: ; preds = %25, %17
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 1000000
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = getelementptr inbounds [20000 x i32], [20000 x i32]* %2, i64 0, i64 0
%24 = call i32 @binary_search(i32 -1, i32* %23, i32 20000)
store i32 %24, i32* %3
br label %25
25: ; preds = %22
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %19
28: ; preds = %19
%29 = call i64 (...) @clock()
%30 = load i64, i64* %5
%31 = sub nsw i64 %29, %30
store i64 %31, i64* %5
%32 = load i32, i32* %3
%33 = load i64, i64* %5
%34 = sitofp i64 %33 to double
%35 = call i32 @print_results(i32 %32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), double %34)
store i64 0, i64* %5
store i32 0, i32* %4
%36 = call i64 (...) @clock()
store i64 %36, i64* %5
br label %37
37: ; preds = %43, %28
%38 = load i32, i32* %4
%39 = icmp slt i32 %38, 1000000
br i1 %39, label %40, label %46
40: ; preds = %37
%41 = getelementptr inbounds [20000 x i32], [20000 x i32]* %2, i64 0, i64 0
%42 = call i32 @binary_search2(i32 -1, i32* %41, i32 20000)
store i32 %42, i32* %3
br label %43
43: ; preds = %40
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %37
46: ; preds = %37
%47 = call i64 (...) @clock()
%48 = load i64, i64* %5
%49 = sub nsw i64 %47, %48
store i64 %49, i64* %5
%50 = load i32, i32* %3
%51 = load i64, i64* %5
%52 = sitofp i64 %51 to double
%53 = call i32 @print_results(i32 %50, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), double %52)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"binary_search()\00"
@.str.1 = [17 x i8] c"binary_search2()\00"
define i32 @main() {
%1 = alloca [20000 x i32]
%2 = bitcast [20000 x i32]* %1 to i8*
br label %3
3: ; preds = %3, %0
%4 = phi i64 [ 0, %0 ], [ %39, %3 ]
%5 = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %0 ], [ %40, %3 ]
%6 = add <4 x i32> %5, <i32 4, i32 4, i32 4, i32 4>
%7 = getelementptr inbounds [20000 x i32], [20000 x i32]* %1, i64 0, i64 %4
%8 = bitcast i32* %7 to <4 x i32>*
store <4 x i32> %5, <4 x i32>* %8
%9 = getelementptr inbounds i32, i32* %7, i64 4
%10 = bitcast i32* %9 to <4 x i32>*
store <4 x i32> %6, <4 x i32>* %10
%11 = add nuw nsw i64 %4, 8
%12 = add <4 x i32> %5, <i32 8, i32 8, i32 8, i32 8>
%13 = add <4 x i32> %5, <i32 12, i32 12, i32 12, i32 12>
%14 = getelementptr inbounds [20000 x i32], [20000 x i32]* %1, i64 0, i64 %11
%15 = bitcast i32* %14 to <4 x i32>*
store <4 x i32> %12, <4 x i32>* %15
%16 = getelementptr inbounds i32, i32* %14, i64 4
%17 = bitcast i32* %16 to <4 x i32>*
store <4 x i32> %13, <4 x i32>* %17
%18 = add nuw nsw i64 %4, 16
%19 = add <4 x i32> %5, <i32 16, i32 16, i32 16, i32 16>
%20 = add <4 x i32> %5, <i32 20, i32 20, i32 20, i32 20>
%21 = getelementptr inbounds [20000 x i32], [20000 x i32]* %1, i64 0, i64 %18
%22 = bitcast i32* %21 to <4 x i32>*
store <4 x i32> %19, <4 x i32>* %22
%23 = getelementptr inbounds i32, i32* %21, i64 4
%24 = bitcast i32* %23 to <4 x i32>*
store <4 x i32> %20, <4 x i32>* %24
%25 = add nuw nsw i64 %4, 24
%26 = add <4 x i32> %5, <i32 24, i32 24, i32 24, i32 24>
%27 = add <4 x i32> %5, <i32 28, i32 28, i32 28, i32 28>
%28 = getelementptr inbounds [20000 x i32], [20000 x i32]* %1, i64 0, i64 %25
%29 = bitcast i32* %28 to <4 x i32>*
store <4 x i32> %26, <4 x i32>* %29
%30 = getelementptr inbounds i32, i32* %28, i64 4
%31 = bitcast i32* %30 to <4 x i32>*
store <4 x i32> %27, <4 x i32>* %31
%32 = add nuw nsw i64 %4, 32
%33 = add <4 x i32> %5, <i32 32, i32 32, i32 32, i32 32>
%34 = add <4 x i32> %5, <i32 36, i32 36, i32 36, i32 36>
%35 = getelementptr inbounds [20000 x i32], [20000 x i32]* %1, i64 0, i64 %32
%36 = bitcast i32* %35 to <4 x i32>*
store <4 x i32> %33, <4 x i32>* %36
%37 = getelementptr inbounds i32, i32* %35, i64 4
%38 = bitcast i32* %37 to <4 x i32>*
store <4 x i32> %34, <4 x i32>* %38
%39 = add nuw nsw i64 %4, 40
%40 = add <4 x i32> %5, <i32 40, i32 40, i32 40, i32 40>
%41 = icmp eq i64 %39, 20000
br i1 %41, label %42, label %3
42: ; preds = %3
%43 = tail call i64 (...) @clock()
%44 = getelementptr inbounds [20000 x i32], [20000 x i32]* %1, i64 0, i64 0
br label %45
45: ; preds = %42, %45
%46 = phi i32 [ 0, %42 ], [ %48, %45 ]
%47 = call i32 @binary_search(i32 -1, i32* %44, i32 20000)
%48 = add nuw nsw i32 %46, 1
%49 = icmp eq i32 %48, 1000000
br i1 %49, label %50, label %45
50: ; preds = %45
%51 = call i64 (...) @clock()
%52 = sub nsw i64 %51, %43
%53 = sitofp i64 %52 to double
%54 = call i32 @print_results(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), double %53)
%55 = call i64 (...) @clock()
br label %56
56: ; preds = %50, %56
%57 = phi i32 [ 0, %50 ], [ %59, %56 ]
%58 = call i32 @binary_search2(i32 -1, i32* %44, i32 20000)
%59 = add nuw nsw i32 %57, 1
%60 = icmp eq i32 %59, 1000000
br i1 %60, label %61, label %56
61: ; preds = %56
%62 = call i64 (...) @clock()
%63 = sub nsw i64 %62, %55
%64 = sitofp i64 %63 to double
%65 = call i32 @print_results(i32 %58, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), double %64)
ret i32 0
}