#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 binsearchFast(int, int *, int);
scalar_t__ clock();
int /*<<< orphan*/ printf(char *, int);
main() {
  clock_t start, finish;
  int i, v[500000];
  for (i = 0; i < 500000; i++)
    v[i] = i;
  start = clock();
  int p;
  for (i = 0; i < 500000; i++) {
    p = binsearch(i, v, 500000);
  }
  finish = clock();
  printf("binsearch duration: %f\n",
         ((double)(finish - start)) / CLOCKS_PER_SEC);
  start = clock();
  for (i = 0; i < 500000; i++) {
    p = binsearchFast(i, v, 500000);
  }
  finish = clock();
  printf("binsearchFast duration: %f\n",
         ((double)(finish - start)) / CLOCKS_PER_SEC);
  for (i = -10000; i < 500000; i++) {
    p = binsearchFast(i, v, 500000);
    if (i < 0 && p != -1)
      break;
    else if (i >= 0 && p != i)
      break;
  }
  printf("i: %d\n", i);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1e84b0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x7a120,-0x1c(%rbp)
jge    1182 <main+0x42>
mov    -0x1c(%rbp),%ecx
movslq -0x1c(%rbp),%rax
mov    %ecx,-0x1e84a0(%rbp,%rax,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1159 <main+0x19>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x7a120,-0x1c(%rbp)
jge    11c9 <main+0x89>
mov    -0x1c(%rbp),%edi
lea    -0x1e84a0(%rbp),%rsi
mov    $0x7a120,%edx
call   1030 <binsearch@plt>
mov    %eax,-0x1e84a4(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1194 <main+0x54>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2e00(%rip),%rax
divsd  (%rax),%xmm0
cvttsd2si %xmm0,%esi
lea    0xe09(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x7a120,-0x1c(%rbp)
jge    1243 <main+0x103>
mov    -0x1c(%rbp),%edi
lea    -0x1e84a0(%rbp),%rsi
mov    $0x7a120,%edx
call   1060 <binsearchFast@plt>
mov    %eax,-0x1e84a4(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120e <main+0xce>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2d86(%rip),%rax
divsd  (%rax),%xmm0
cvttsd2si %xmm0,%esi
lea    0xda7(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffd8f0,-0x1c(%rbp)
cmpl   $0x7a120,-0x1c(%rbp)
jge    12f6 <main+0x1b6>
mov    -0x1c(%rbp),%edi
lea    -0x1e84a0(%rbp),%rsi
mov    $0x7a120,%edx
call   1060 <binsearchFast@plt>
mov    %eax,-0x1e84a4(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12c0 <main+0x180>
cmpl   $0xffffffff,-0x1e84a4(%rbp)
je     12c0 <main+0x180>
jmp    12f6 <main+0x1b6>
cmpl   $0x0,-0x1c(%rbp)
jl     12de <main+0x19e>
mov    -0x1e84a4(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12de <main+0x19e>
jmp    12f6 <main+0x1b6>
jmp    12e3 <main+0x1a3>
jmp    12e8 <main+0x1a8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    127d <main+0x13d>
mov    -0x1c(%rbp),%esi
lea    0xd34(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1e84b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1e8488,%rsp
movdqa 0xeaa(%rip),%xmm0
mov    $0x24,%eax
movdqa 0xeac(%rip),%xmm8
movdqa 0xeb3(%rip),%xmm9
movdqa 0xeba(%rip),%xmm10
movdqa 0xec1(%rip),%xmm11
movdqa 0xec8(%rip),%xmm12
movdqa 0xed0(%rip),%xmm6
movdqa 0xed8(%rip),%xmm7
movdqa 0xee0(%rip),%xmm1
movdqa 0xee8(%rip),%xmm2
movdqa 0xef0(%rip),%xmm3
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    $0x7a144,%rax
jne    11b0 <main+0x70>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%r14
mov    %rsp,%r15
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x7a120,%edx
call   1030 <binsearch@plt>
add    $0x1,%ebx
cmp    $0x7a120,%ebx
jne    1260 <main+0x120>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mov    0x2d53(%rip),%r12
divsd  (%r12),%xmm0
cvttsd2si %xmm0,%esi
lea    0xe0a(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%r14
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x7a120,%edx
call   1060 <binsearchFast@plt>
add    $0x1,%ebx
cmp    $0x7a120,%ebx
jne    12c0 <main+0x180>
xor    %eax,%eax
call   1040 <clock@plt>
sub    %r14,%rax
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
divsd  (%r12),%xmm0
cvttsd2si %xmm0,%esi
lea    0xdcb(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffd8f0,%ebx
mov    %rsp,%r14
jmp    131b <main+0x1db>
nopl   0x0(%rax)
add    $0x1,%ebx
cmp    $0x7a120,%ebx
je     133d <main+0x1fd>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x7a120,%edx
call   1060 <binsearchFast@plt>
test   %ebx,%ebx
jns    1333 <main+0x1f3>
cmp    $0xffffffff,%eax
jne    1342 <main+0x202>
test   %ebx,%ebx
js     1310 <main+0x1d0>
cmp    %eax,%ebx
je     1310 <main+0x1d0>
jmp    1342 <main+0x202>
mov    $0x7a120,%ebx
lea    0xd9b(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x1e8488,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"binsearch duration: %f\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.1 = [28 x i8] c"binsearchFast duration: %f\0A\00"
@.str.2 = [7 x i8] c"i: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
%5 = alloca [500000 x i32]
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
br label %7
7: ; preds = %15, %0
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 500000
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = load i32, i32* %4
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [500000 x i32], [500000 x i32]* %5, i64 0, i64 %13
store i32 %11, i32* %14
br label %15
15: ; preds = %10
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
store i32 %17, i32* %4
br label %7
18: ; preds = %7
%19 = call i64 (...) @clock()
store i64 %19, i64* %2
store i32 0, i32* %4
br label %20
20: ; preds = %27, %18
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 500000
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i32, i32* %4
%25 = getelementptr inbounds [500000 x i32], [500000 x i32]* %5, i64 0, i64 0
%26 = call i32 @binsearch(i32 %24, i32* %25, i32 500000)
store i32 %26, i32* %6
br label %27
27: ; preds = %23
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %20
30: ; preds = %20
%31 = call i64 (...) @clock()
store i64 %31, i64* %3
%32 = load i64, i64* %3
%33 = load i64, i64* %2
%34 = sub nsw i64 %32, %33
%35 = sitofp i64 %34 to double
%36 = load double, double* @CLOCKS_PER_SEC
%37 = fdiv double %35, %36
%38 = fptosi double %37 to i32
%39 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %38)
%40 = call i64 (...) @clock()
store i64 %40, i64* %2
store i32 0, i32* %4
br label %41
41: ; preds = %48, %30
%42 = load i32, i32* %4
%43 = icmp slt i32 %42, 500000
br i1 %43, label %44, label %51
44: ; preds = %41
%45 = load i32, i32* %4
%46 = getelementptr inbounds [500000 x i32], [500000 x i32]* %5, i64 0, i64 0
%47 = call i32 @binsearchFast(i32 %45, i32* %46, i32 500000)
store i32 %47, i32* %6
br label %48
48: ; preds = %44
%49 = load i32, i32* %4
%50 = add nsw i32 %49, 1
store i32 %50, i32* %4
br label %41
51: ; preds = %41
%52 = call i64 (...) @clock()
store i64 %52, i64* %3
%53 = load i64, i64* %3
%54 = load i64, i64* %2
%55 = sub nsw i64 %53, %54
%56 = sitofp i64 %55 to double
%57 = load double, double* @CLOCKS_PER_SEC
%58 = fdiv double %56, %57
%59 = fptosi double %58 to i32
%60 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %59)
store i32 -10000, i32* %4
br label %61
61: ; preds = %84, %51
%62 = load i32, i32* %4
%63 = icmp slt i32 %62, 500000
br i1 %63, label %64, label %87
64: ; preds = %61
%65 = load i32, i32* %4
%66 = getelementptr inbounds [500000 x i32], [500000 x i32]* %5, i64 0, i64 0
%67 = call i32 @binsearchFast(i32 %65, i32* %66, i32 500000)
store i32 %67, i32* %6
%68 = load i32, i32* %4
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %64
%71 = load i32, i32* %6
%72 = icmp ne i32 %71, -1
br i1 %72, label %73, label %74
73: ; preds = %70
br label %87
74: ; preds = %70, %64
%75 = load i32, i32* %4
%76 = icmp sge i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %74
%78 = load i32, i32* %6
%79 = load i32, i32* %4
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %82
81: ; preds = %77
br label %87
82: ; preds = %77, %74
br label %83
83: ; preds = %82
br label %84
84: ; preds = %83
%85 = load i32, i32* %4
%86 = add nsw i32 %85, 1
store i32 %86, i32* %4
br label %61
87: ; preds = %81, %73, %61
%88 = load i32, i32* %4
%89 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %88)
%90 = load i32, i32* %1
ret i32 %90
}

LLVM IR (-O2):
@.str = [24 x i8] c"binsearch duration: %f\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.1 = [28 x i8] c"binsearchFast duration: %f\0A\00"
@.str.2 = [7 x i8] c"i: %d\0A\00"
define i32 @main() {
%1 = alloca [500000 x i32]
%2 = bitcast [500000 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 [500000 x i32], [500000 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 [500000 x i32], [500000 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 [500000 x i32], [500000 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 [500000 x i32], [500000 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 [500000 x i32], [500000 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, 500000
br i1 %41, label %42, label %3
42: ; preds = %3
%43 = tail call i64 (...) @clock()
%44 = getelementptr inbounds [500000 x i32], [500000 x i32]* %1, i64 0, i64 0
br label %45
45: ; preds = %42, %45
%46 = phi i32 [ 0, %42 ], [ %48, %45 ]
%47 = call i32 @binsearch(i32 %46, i32* %44, i32 500000)
%48 = add nuw nsw i32 %46, 1
%49 = icmp eq i32 %48, 500000
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 = load double, double* @CLOCKS_PER_SEC
%55 = fdiv double %53, %54
%56 = fptosi double %55 to i32
%57 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %56)
%58 = call i64 (...) @clock()
br label %59
59: ; preds = %50, %59
%60 = phi i32 [ 0, %50 ], [ %62, %59 ]
%61 = call i32 @binsearchFast(i32 %60, i32* %44, i32 500000)
%62 = add nuw nsw i32 %60, 1
%63 = icmp eq i32 %62, 500000
br i1 %63, label %64, label %59
64: ; preds = %59
%65 = call i64 (...) @clock()
%66 = sub nsw i64 %65, %58
%67 = sitofp i64 %66 to double
%68 = load double, double* @CLOCKS_PER_SEC
%69 = fdiv double %67, %68
%70 = fptosi double %69 to i32
%71 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %70)
br label %72
72: ; preds = %64, %81
%73 = phi i32 [ -10000, %64 ], [ %82, %81 ]
%74 = call i32 @binsearchFast(i32 %73, i32* %44, i32 500000)
%75 = icmp slt i32 %73, 0
%76 = icmp ne i32 %74, -1
%77 = select i1 %75, i1 %76, i1 false
br i1 %77, label %84, label %78
78: ; preds = %72
%79 = icmp eq i32 %74, %73
%80 = select i1 %75, i1 true, i1 %79
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = add nsw i32 %73, 1
%83 = icmp eq i32 %82, 500000
br i1 %83, label %84, label %72
84: ; preds = %78, %72, %81
%85 = phi i32 [ %73, %78 ], [ %73, %72 ], [ 500000, %81 ]
%86 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %85)
ret i32 0
}