#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 CLOCKS_PER_SEC;
scalar_t__ clock();
int newBinSearch(int, int *, int);
int originalBinSearch(int, int *, int);
int /*<<< orphan*/ printf(char *, int, ...);
int rand();
int /*<<< orphan*/ sort(int *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  int find, max, indexFound;
  max = 1000;
  srand(time(NULL));
  find = rand() % max;
  int values[max];
  printf("Randomly find %d\n", find);
  for (int i = 0; i < max; i++) {
    values[i] = rand() % (max + 1);
    if (values[i] == find) {
      values[i] += rand() % (max - find);
    }
  }
  int randIndex = rand() % (max + 1);
  values[randIndex] = find;
  sort(values, max);
  for (int i = 0; i < max; i++) {
    printf("i: %d, v[i]: %d\n", i, values[i]);
  }
  clock_t start, stop;
  start = clock();
  indexFound = originalBinSearch(find, values, max);
  stop = clock();
  printf("Original took %Lf cycles with %d cycles per second.\n",
         (long double)(stop - start), CLOCKS_PER_SEC);
  printf("Found %d at %d.\n", values[indexFound], indexFound);
  start = clock();
  indexFound = newBinSearch(find, values, max);
  stop = clock();
  printf("New took %Lf cycles with %d cycles per second.\n",
         (long double)(stop - start), CLOCKS_PER_SEC);
  printf("Found %d at %d.\n", values[indexFound], indexFound);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x3e8,-0xc(%rbp)
xor    %eax,%eax
mov    %eax,-0x54(%rbp)
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    -0x54(%rbp),%eax
call   10a0 <rand@plt>
mov    -0xc(%rbp),%ecx
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1261 <main+0xe1>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    -0x50(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x8(%rbp),%eax
jne    124e <main+0xce>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    -0xc(%rbp),%ecx
sub    -0x8(%rbp),%ecx
cltd
idiv   %ecx
mov    -0x50(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    (%rax,%rcx,4),%edx
mov    %edx,(%rax,%rcx,4)
jmp    1253 <main+0xd3>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11f9 <main+0x79>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    -0x50(%rbp),%rdi
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x8(%rbp),%ecx
movslq -0x28(%rbp),%rax
mov    %ecx,(%rdi,%rax,4)
mov    -0xc(%rbp),%esi
call   1050 <sort@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12c7 <main+0x147>
mov    -0x50(%rbp),%rax
mov    -0x2c(%rbp),%esi
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1291 <main+0x111>
xor    %eax,%eax
mov    %al,-0x55(%rbp)
call   1030 <clock@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%edx
call   1090 <originalBinSearch@plt>
mov    %eax,%ecx
mov    -0x55(%rbp),%al
mov    %ecx,-0x10(%rbp)
call   1030 <clock@plt>
mov    %rax,%rcx
mov    -0x55(%rbp),%al
mov    %rcx,-0x40(%rbp)
mov    -0x40(%rbp),%esi
mov    -0x38(%rbp),%ecx
sub    %ecx,%esi
mov    0x2cde(%rip),%rcx
mov    (%rcx),%edx
lea    0xd10(%rip),%rdi
call   1040 <printf@plt>
mov    -0x50(%rbp),%rcx
mov    -0x55(%rbp),%al
movslq -0x10(%rbp),%rsi
mov    %esi,%edx
mov    (%rcx,%rsi,4),%esi
lea    0xd29(%rip),%rdi
call   1040 <printf@plt>
mov    -0x55(%rbp),%al
call   1030 <clock@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%edx
call   1080 <newBinSearch@plt>
mov    %eax,%ecx
mov    -0x55(%rbp),%al
mov    %ecx,-0x10(%rbp)
call   1030 <clock@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
fildll -0x48(%rbp)
fstp   %st(0)
mov    %eax,%esi
mov    0x2c6b(%rip),%rax
mov    (%rax),%edx
lea    0xce3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x10(%rbp),%edx
lea    0xcb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xfa8,%rsp
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %eax,%eax
call   10a0 <rand@plt>
movslq %eax,%r12
imul   $0x10624dd3,%r12,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
imul   $0x3e8,%eax,%eax
sub    %eax,%r12d
lea    0xe33(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3e8,%ebx
sub    %r12d,%ebx
xor    %ebp,%ebp
jmp    11fd <main+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbp
cmp    $0x3e8,%rbp
je     123c <main+0xbc>
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x4178749f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x28,%rcx
add    %edx,%ecx
imul   $0x3e9,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,(%rsp,%rbp,4)
cmp    %r12d,%eax
jne    11f0 <main+0x70>
xor    %eax,%eax
call   10a0 <rand@plt>
cltd
idiv   %ebx
add    %r12d,%edx
mov    %edx,(%rsp,%rbp,4)
jmp    11f0 <main+0x70>
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x4178749f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x28,%rcx
add    %edx,%ecx
imul   $0x3e9,%ecx,%ecx
sub    %ecx,%eax
cltq
mov    %r12d,(%rsp,%rax,4)
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1050 <sort@plt>
lea    0xd97(%rip),%r14
xor    %ebx,%ebx
nopl   (%rax)
mov    (%rsp,%rbx,4),%edx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    $0x3e8,%rbx
jne    1280 <main+0x100>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1090 <originalBinSearch@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <clock@plt>
sub    %ebx,%eax
mov    0x2d1d(%rip),%r13
mov    0x0(%r13),%edx
lea    0xd4d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movslq %ebp,%rdx
mov    (%rsp,%rdx,4),%esi
lea    0xd6c(%rip),%r15
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%rbx
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1080 <newBinSearch@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <clock@plt>
sub    %ebx,%eax
mov    0x0(%r13),%edx
lea    0xd43(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movslq %ebp,%rdx
mov    (%rsp,%rdx,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0xfa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Randomly find %d\0A\00"
@.str.1 = [17 x i8] c"i: %d, v[i]: %d\0A\00"
@.str.2 = [53 x i8] c"Original took %Lf cycles with %d cycles per second.\0A\00"
@CLOCKS_PER_SEC = global i32 0
@.str.3 = [17 x i8] c"Found %d at %d.\0A\00"
@.str.4 = [48 x i8] c"New took %Lf cycles with %d cycles per second.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %1
store i32 1000, i32* %3
%12 = call i32 @time(i32* null)
%13 = call i32 @srand(i32 %12)
%14 = call i32 (...) @rand()
%15 = load i32, i32* %3
%16 = srem i32 %14, %15
store i32 %16, i32* %2
%17 = load i32, i32* %3
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %5
%20 = alloca i32, i64 %18
store i64 %18, i64* %6
%21 = load i32, i32* %2
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 0, i32* %7
br label %23
23: ; preds = %53, %0
%24 = load i32, i32* %7
%25 = load i32, i32* %3
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %56
27: ; preds = %23
%28 = call i32 (...) @rand()
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
%31 = srem i32 %28, %30
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %20, i64 %33
store i32 %31, i32* %34
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %20, i64 %36
%38 = load i32, i32* %37
%39 = load i32, i32* %2
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %52
41: ; preds = %27
%42 = call i32 (...) @rand()
%43 = load i32, i32* %3
%44 = load i32, i32* %2
%45 = sub nsw i32 %43, %44
%46 = srem i32 %42, %45
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %20, i64 %48
%50 = load i32, i32* %49
%51 = add nsw i32 %50, %46
store i32 %51, i32* %49
br label %52
52: ; preds = %41, %27
br label %53
53: ; preds = %52
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
br label %23
56: ; preds = %23
%57 = call i32 (...) @rand()
%58 = load i32, i32* %3
%59 = add nsw i32 %58, 1
%60 = srem i32 %57, %59
store i32 %60, i32* %8
%61 = load i32, i32* %2
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %20, i64 %63
store i32 %61, i32* %64
%65 = load i32, i32* %3
%66 = call i32 @sort(i32* %20, i32 %65)
store i32 0, i32* %9
br label %67
67: ; preds = %78, %56
%68 = load i32, i32* %9
%69 = load i32, i32* %3
%70 = icmp slt i32 %68, %69
br i1 %70, label %71, label %81
71: ; preds = %67
%72 = load i32, i32* %9
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i32, i32* %20, i64 %74
%76 = load i32, i32* %75
%77 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %72, i32 %76)
br label %78
78: ; preds = %71
%79 = load i32, i32* %9
%80 = add nsw i32 %79, 1
store i32 %80, i32* %9
br label %67
81: ; preds = %67
%82 = call i64 (...) @clock()
store i64 %82, i64* %10
%83 = load i32, i32* %2
%84 = load i32, i32* %3
%85 = call i32 @originalBinSearch(i32 %83, i32* %20, i32 %84)
store i32 %85, i32* %4
%86 = call i64 (...) @clock()
store i64 %86, i64* %11
%87 = load i64, i64* %11
%88 = load i64, i64* %10
%89 = sub nsw i64 %87, %88
%90 = sitofp i64 %89 to x86_fp80
%91 = fptosi x86_fp80 %90 to i32
%92 = load i32, i32* @CLOCKS_PER_SEC
%93 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %91, i32 %92)
%94 = load i32, i32* %4
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i32, i32* %20, i64 %95
%97 = load i32, i32* %96
%98 = load i32, i32* %4
%99 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %97, i32 %98)
%100 = call i64 (...) @clock()
store i64 %100, i64* %10
%101 = load i32, i32* %2
%102 = load i32, i32* %3
%103 = call i32 @newBinSearch(i32 %101, i32* %20, i32 %102)
store i32 %103, i32* %4
%104 = call i64 (...) @clock()
store i64 %104, i64* %11
%105 = load i64, i64* %11
%106 = load i64, i64* %10
%107 = sub nsw i64 %105, %106
%108 = sitofp i64 %107 to x86_fp80
%109 = fptosi x86_fp80 %108 to i32
%110 = load i32, i32* @CLOCKS_PER_SEC
%111 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0), i32 %109, i32 %110)
%112 = load i32, i32* %4
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds i32, i32* %20, i64 %113
%115 = load i32, i32* %114
%116 = load i32, i32* %4
%117 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %115, i32 %116)
store i32 0, i32* %1
%118 = load i8*, i8** %5
%119 = load i32, i32* %1
ret i32 %119
}

LLVM IR (-O2):
@.str = [18 x i8] c"Randomly find %d\0A\00"
@.str.1 = [17 x i8] c"i: %d, v[i]: %d\0A\00"
@.str.2 = [53 x i8] c"Original took %Lf cycles with %d cycles per second.\0A\00"
@CLOCKS_PER_SEC = global i32 0
@.str.3 = [17 x i8] c"Found %d at %d.\0A\00"
@.str.4 = [48 x i8] c"New took %Lf cycles with %d cycles per second.\0A\00"
define i32 @main() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i32 @srand(i32 %1)
%3 = tail call i32 (...) @rand()
%4 = srem i32 %3, 1000
%5 = alloca [1000 x i32]
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%7 = sub nsw i32 1000, %4
br label %15
8: ; preds = %25
%9 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 0
%10 = tail call i32 (...) @rand()
%11 = srem i32 %10, 1001
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 %12
store i32 %4, i32* %13
%14 = call i32 @sort(i32* %9, i32 1000)
br label %51
15: ; preds = %0, %25
%16 = phi i64 [ 0, %0 ], [ %26, %25 ]
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 1001
%19 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 %16
store i32 %18, i32* %19
%20 = icmp eq i32 %18, %4
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = tail call i32 (...) @rand()
%23 = srem i32 %22, %7
%24 = add nsw i32 %4, %23
store i32 %24, i32* %19
br label %25
25: ; preds = %15, %21
%26 = add nuw nsw i64 %16, 1
%27 = icmp eq i64 %26, 1000
br i1 %27, label %8, label %15
28: ; preds = %51
%29 = call i64 (...) @clock()
%30 = call i32 @originalBinSearch(i32 %4, i32* %9, i32 1000)
%31 = call i64 (...) @clock()
%32 = sub nsw i64 %31, %29
%33 = trunc i64 %32 to i32
%34 = load i32, i32* @CLOCKS_PER_SEC
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %34)
%36 = sext i32 %30 to i64
%37 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 %36
%38 = load i32, i32* %37
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %38, i32 %30)
%40 = call i64 (...) @clock()
%41 = call i32 @newBinSearch(i32 %4, i32* %9, i32 1000)
%42 = call i64 (...) @clock()
%43 = sub nsw i64 %42, %40
%44 = trunc i64 %43 to i32
%45 = load i32, i32* @CLOCKS_PER_SEC
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0), i32 %44, i32 %45)
%47 = sext i32 %41 to i64
%48 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 %47
%49 = load i32, i32* %48
%50 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 %41)
ret i32 0
51: ; preds = %8, %51
%52 = phi i64 [ 0, %8 ], [ %57, %51 ]
%53 = getelementptr inbounds [1000 x i32], [1000 x i32]* %5, i64 0, i64 %52
%54 = load i32, i32* %53
%55 = trunc i64 %52 to i32
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %55, i32 %54)
%57 = add nuw nsw i64 %52, 1
%58 = icmp eq i64 %57, 1000
br i1 %58, label %28, label %51
}