#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 */
double CLOCKS_PER_SEC;
int binsearch(int, int *, int);
int binsearch_fast(int, int *, int);
scalar_t__ clock();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int test_array[10];
  for (int i = 0; i < 10; i++) {
    test_array[i] = i;
  }
  test_array[3] = 4;
  int result;
  int numruns = 1000;
  clock_t begin, end;
  double time_spent, time_spent_fast;
  begin = clock();
  for (int i = 0; i < numruns; i++)
    result = binsearch(3, test_array, 10);
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("\n%f sec\tfor binsearch", time_spent);
  begin = clock();
  for (int i = 0; i < numruns; i++)
    result = binsearch_fast(3, test_array, 10);
  end = clock();
  time_spent_fast = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("\n%f sec\tfor binsearch_fast", time_spent_fast);
  printf("\n\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0xa,-0x34(%rbp)
jge    1179 <main+0x39>
mov    -0x34(%rbp),%ecx
movslq -0x34(%rbp),%rax
mov    %ecx,-0x30(%rbp,%rax,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1156 <main+0x16>
movl   $0x4,-0x24(%rbp)
movl   $0x3e8,-0x3c(%rbp)
mov    $0x0,%al
call   1050 <clock@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    11c9 <main+0x89>
lea    -0x30(%rbp),%rsi
mov    $0x3,%edi
mov    $0xa,%edx
call   1040 <binsearch@plt>
mov    %eax,-0x38(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    1199 <main+0x59>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
sub    -0x48(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2e00(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
lea    0xe03(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1246 <main+0x106>
lea    -0x30(%rbp),%rsi
mov    $0x3,%edi
mov    $0xa,%edx
call   1030 <binsearch_fast@plt>
mov    %eax,-0x38(%rbp)
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    1216 <main+0xd6>
mov    $0x0,%al
call   1050 <clock@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
sub    -0x48(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2d83(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
lea    0xd9c(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movaps 0xe9f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x900000008,%rax
mov    %rax,0x20(%rsp)
movaps 0xe94(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1060 <clock@plt>
mov    %rax,%r14
mov    $0x3e8,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x3,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <binsearch@plt>
add    $0xffffffff,%ebp
jne    11a0 <main+0x50>
xor    %eax,%eax
call   1060 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mov    0x2e18(%rip),%r15
divsd  (%r15),%xmm0
lea    0xe44(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
xor    %eax,%eax
call   1060 <clock@plt>
mov    %rax,%r14
mov    $0x3e8,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x3,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1030 <binsearch_fast@plt>
add    $0xffffffff,%ebp
jne    1200 <main+0xb0>
xor    %eax,%eax
call   1060 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r15),%xmm0
lea    0xe01(%rip),%rdi
mov    $0x1,%al
call   1070 <printf@plt>
lea    0xe0e(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str = [22 x i8] c"\0A%f sec\09for binsearch\00"
@.str.1 = [27 x i8] c"\0A%f sec\09for binsearch_fast\00"
@.str.2 = [3 x i8] c"\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
br label %12
12: ; preds = %20, %0
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 10
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i32, i32* %3
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 %18
store i32 %16, i32* %19
br label %20
20: ; preds = %15
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %12
23: ; preds = %12
%24 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 3
store i32 4, i32* %24
store i32 1000, i32* %5
%25 = call i64 (...) @clock()
store i64 %25, i64* %6
store i32 0, i32* %10
br label %26
26: ; preds = %33, %23
%27 = load i32, i32* %10
%28 = load i32, i32* %5
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%32 = call i32 @binsearch(i32 3, i32* %31, i32 10)
store i32 %32, i32* %4
br label %33
33: ; preds = %30
%34 = load i32, i32* %10
%35 = add nsw i32 %34, 1
store i32 %35, i32* %10
br label %26
36: ; preds = %26
%37 = call i64 (...) @clock()
store i64 %37, i64* %7
%38 = load i64, i64* %7
%39 = load i64, i64* %6
%40 = sub nsw i64 %38, %39
%41 = sitofp i64 %40 to double
%42 = load double, double* @CLOCKS_PER_SEC
%43 = fdiv double %41, %42
store double %43, double* %8
%44 = load double, double* %8
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), double %44)
%46 = call i64 (...) @clock()
store i64 %46, i64* %6
store i32 0, i32* %11
br label %47
47: ; preds = %54, %36
%48 = load i32, i32* %11
%49 = load i32, i32* %5
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %57
51: ; preds = %47
%52 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%53 = call i32 @binsearch_fast(i32 3, i32* %52, i32 10)
store i32 %53, i32* %4
br label %54
54: ; preds = %51
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %47
57: ; preds = %47
%58 = call i64 (...) @clock()
store i64 %58, i64* %7
%59 = load i64, i64* %7
%60 = load i64, i64* %6
%61 = sub nsw i64 %59, %60
%62 = sitofp i64 %61 to double
%63 = load double, double* @CLOCKS_PER_SEC
%64 = fdiv double %62, %63
store double %64, double* %9
%65 = load double, double* %9
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str = [22 x i8] c"\0A%f sec\09for binsearch\00"
@.str.1 = [27 x i8] c"\0A%f sec\09for binsearch_fast\00"
@str = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca [10 x i32]
%2 = bitcast [10 x i32]* %1 to i8*
%3 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%4 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 4
%5 = bitcast i32* %4 to <4 x i32>*
store <4 x i32> <i32 4, i32 5, i32 6, i32 7>, <4 x i32>* %5
%6 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 8
store i32 8, i32* %6
%7 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 9
store i32 9, i32* %7
%8 = bitcast [10 x i32]* %1 to <4 x i32>*
store <4 x i32> <i32 0, i32 1, i32 2, i32 4>, <4 x i32>* %8
%9 = tail call i64 (...) @clock()
br label %18
10: ; preds = %18
%11 = call i64 (...) @clock()
%12 = sub nsw i64 %11, %9
%13 = sitofp i64 %12 to double
%14 = load double, double* @CLOCKS_PER_SEC
%15 = fdiv double %13, %14
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), double %15)
%17 = call i64 (...) @clock()
br label %31
18: ; preds = %0, %18
%19 = phi i32 [ 0, %0 ], [ %21, %18 ]
%20 = call i32 @binsearch(i32 3, i32* %3, i32 10)
%21 = add nuw nsw i32 %19, 1
%22 = icmp eq i32 %21, 1000
br i1 %22, label %10, label %18
23: ; preds = %31
%24 = call i64 (...) @clock()
%25 = sub nsw i64 %24, %17
%26 = sitofp i64 %25 to double
%27 = load double, double* @CLOCKS_PER_SEC
%28 = fdiv double %26, %27
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double %28)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
ret i32 0
31: ; preds = %10, %31
%32 = phi i32 [ 0, %10 ], [ %34, %31 ]
%33 = call i32 @binsearch_fast(i32 3, i32* %3, i32 10)
%34 = add nuw nsw i32 %32, 1
%35 = icmp eq i32 %34, 1000
br i1 %35, label %23, label %31
}