#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 */
unsigned long CLOCKS_PER_SEC;
int binsearch(int, int *, int);
int binsearch2(int, int *, int);
scalar_t__ clock();
int /*<<< orphan*/ printf(char *, unsigned long, ...);
int main() {
  int test_data[20000];
  int index;
  int n = -1;
  int i;
  clock_t time_taken;
  for (i = 0; i < 20000; ++i)
    test_data[i] = i;
  for (i = 0, time_taken = clock(); i < 100000; ++i) {
    index = binsearch(n, test_data, 20000);
  }
  time_taken = clock() - time_taken;
  if (index < 0)
    printf("Elem %d not found.\n", n);
  else
    printf("%d found at index %d.\n", n, index);
  printf("binsearch() took %lu clocks (%lu seconds)\n",
         (unsigned long)time_taken, (unsigned long)time_taken / CLOCKS_PER_SEC);
  for (i = 0, time_taken = clock(); i < 100000; ++i) {
    index = binsearch2(n, test_data, 20000);
  }
  time_taken = clock() - time_taken;
  if (index < 0)
    printf("Elem %d not found.\n", n);
  else
    printf("%d found at index %d.\n", n, index);
  printf("binsearch2() took %lu clocks (%lu seconds)\n",
         (unsigned long)time_taken, (unsigned long)time_taken / CLOCKS_PER_SEC);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x138b0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x13898(%rbp)
ff ff ff
movl   $0x0,-0x1389c(%rbp)
cmpl   $0x4e20,-0x1389c(%rbp)
jge    119e <main+0x5e>
mov    -0x1389c(%rbp),%ecx
movslq -0x1389c(%rbp),%rax
mov    %ecx,-0x13890(%rbp,%rax,4)
mov    -0x1389c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1389c(%rbp)
jmp    1166 <main+0x26>
movl   $0x0,-0x1389c(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x138a8(%rbp)
cmpl   $0x186a0,-0x1389c(%rbp)
jge    11f7 <main+0xb7>
mov    -0x13898(%rbp),%edi
lea    -0x13890(%rbp),%rsi
mov    $0x4e20,%edx
call   1030 <binsearch@plt>
mov    %eax,-0x13894(%rbp)
mov    -0x1389c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1389c(%rbp)
jmp    11b6 <main+0x76>
mov    $0x0,%al
call   1040 <clock@plt>
sub    -0x138a8(%rbp),%rax
mov    %rax,-0x138a8(%rbp)
cmpl   $0x0,-0x13894(%rbp)
jge    1233 <main+0xf3>
movslq -0x13898(%rbp),%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124e <main+0x10e>
movslq -0x13898(%rbp),%rsi
mov    -0x13894(%rbp),%edx
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x138a8(%rbp),%rsi
mov    -0x138a8(%rbp),%rax
mov    0x2d85(%rip),%rcx
xor    %edx,%edx
divq   (%rcx)
mov    %rax,%rdx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x1389c(%rbp)
mov    $0x0,%al
call   1040 <clock@plt>
mov    %rax,-0x138a8(%rbp)
cmpl   $0x186a0,-0x1389c(%rbp)
jge    12d2 <main+0x192>
mov    -0x13898(%rbp),%edi
lea    -0x13890(%rbp),%rsi
mov    $0x4e20,%edx
call   1060 <binsearch2@plt>
mov    %eax,-0x13894(%rbp)
mov    -0x1389c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1389c(%rbp)
jmp    1291 <main+0x151>
mov    $0x0,%al
call   1040 <clock@plt>
sub    -0x138a8(%rbp),%rax
mov    %rax,-0x138a8(%rbp)
cmpl   $0x0,-0x13894(%rbp)
jge    130e <main+0x1ce>
movslq -0x13898(%rbp),%rsi
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1329 <main+0x1e9>
movslq -0x13898(%rbp),%rsi
mov    -0x13894(%rbp),%edx
lea    0xcf2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x138a8(%rbp),%rsi
mov    -0x138a8(%rbp),%rax
mov    0x2caa(%rip),%rcx
xor    %edx,%edx
divq   (%rcx)
mov    %rax,%rdx
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x138b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x13880,%rsp
movdqa 0xea9(%rip),%xmm0
mov    $0x24,%eax
movdqa 0xeab(%rip),%xmm8
movdqa 0xeb2(%rip),%xmm9
movdqa 0xeb9(%rip),%xmm10
movdqa 0xec0(%rip),%xmm11
movdqa 0xec7(%rip),%xmm12
movdqa 0xecf(%rip),%xmm6
movdqa 0xed7(%rip),%xmm7
movdqa 0xedf(%rip),%xmm1
movdqa 0xee7(%rip),%xmm2
movdqa 0xeef(%rip),%xmm3
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
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    11c0 <main+0x80>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%r14
mov    $0x186a0,%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   1030 <binsearch@plt>
add    $0xffffffff,%ebx
jne    1280 <main+0x140>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%rbx
sub    %r14,%rbx
test   %ebp,%ebp
js     12c3 <main+0x183>
lea    0xe13(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12d8 <main+0x198>
lea    0xde6(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d09(%rip),%r15
mov    (%r15),%rcx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     12f8 <main+0x1b8>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    12fe <main+0x1be>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
lea    0xdd6(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%r14
mov    $0x186a0,%ebx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xffffffff,%edi
mov    %r12,%rsi
mov    $0x4e20,%edx
call   1060 <binsearch2@plt>
add    $0xffffffff,%ebx
jne    1330 <main+0x1f0>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <clock@plt>
mov    %rax,%rbx
sub    %r14,%rbx
test   %ebp,%ebp
js     1373 <main+0x233>
lea    0xd63(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1388 <main+0x248>
lea    0xd36(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%rcx
mov    %rbx,%rax
or     %rcx,%rax
shr    $0x20,%rax
je     13a1 <main+0x261>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    13a7 <main+0x267>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
lea    0xd58(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x13880,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Elem %d not found.\0A\00"
@.str.1 = [23 x i8] c"%d found at index %d.\0A\00"
@.str.2 = [43 x i8] c"binsearch() took %lu clocks (%lu seconds)\0A\00"
@CLOCKS_PER_SEC = global i64 0
@.str.3 = [44 x i8] c"binsearch2() took %lu clocks (%lu seconds)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [20000 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
store i32 -1, i32* %4
store i32 0, i32* %5
br label %7
7: ; preds = %15, %0
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 20000
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = load i32, i32* %5
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [20000 x i32], [20000 x i32]* %2, i64 0, i64 %13
store i32 %11, i32* %14
br label %15
15: ; preds = %10
%16 = load i32, i32* %5
%17 = add nsw i32 %16, 1
store i32 %17, i32* %5
br label %7
18: ; preds = %7
store i32 0, i32* %5
%19 = call i64 (...) @clock()
store i64 %19, i64* %6
br label %20
20: ; preds = %27, %18
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 100000
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i32, i32* %4
%25 = getelementptr inbounds [20000 x i32], [20000 x i32]* %2, i64 0, i64 0
%26 = call i32 @binsearch(i32 %24, i32* %25, i32 20000)
store i32 %26, i32* %3
br label %27
27: ; preds = %23
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %20
30: ; preds = %20
%31 = call i64 (...) @clock()
%32 = load i64, i64* %6
%33 = sub nsw i64 %31, %32
store i64 %33, i64* %6
%34 = load i32, i32* %3
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32, i32* %4
%38 = sext i32 %37 to i64
%39 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %38)
br label %45
40: ; preds = %30
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = load i32, i32* %3
%44 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %42, i32 %43)
br label %45
45: ; preds = %40, %36
%46 = load i64, i64* %6
%47 = load i64, i64* %6
%48 = load i64, i64* @CLOCKS_PER_SEC
%49 = udiv i64 %47, %48
%50 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i64 %46, i64 %49)
store i32 0, i32* %5
%51 = call i64 (...) @clock()
store i64 %51, i64* %6
br label %52
52: ; preds = %59, %45
%53 = load i32, i32* %5
%54 = icmp slt i32 %53, 100000
br i1 %54, label %55, label %62
55: ; preds = %52
%56 = load i32, i32* %4
%57 = getelementptr inbounds [20000 x i32], [20000 x i32]* %2, i64 0, i64 0
%58 = call i32 @binsearch2(i32 %56, i32* %57, i32 20000)
store i32 %58, i32* %3
br label %59
59: ; preds = %55
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %5
br label %52
62: ; preds = %52
%63 = call i64 (...) @clock()
%64 = load i64, i64* %6
%65 = sub nsw i64 %63, %64
store i64 %65, i64* %6
%66 = load i32, i32* %3
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %62
%69 = load i32, i32* %4
%70 = sext i32 %69 to i64
%71 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %70)
br label %77
72: ; preds = %62
%73 = load i32, i32* %4
%74 = sext i32 %73 to i64
%75 = load i32, i32* %3
%76 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %74, i32 %75)
br label %77
77: ; preds = %72, %68
%78 = load i64, i64* %6
%79 = load i64, i64* %6
%80 = load i64, i64* @CLOCKS_PER_SEC
%81 = udiv i64 %79, %80
%82 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64 %78, i64 %81)
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"Elem %d not found.\0A\00"
@.str.1 = [23 x i8] c"%d found at index %d.\0A\00"
@.str.2 = [43 x i8] c"binsearch() took %lu clocks (%lu seconds)\0A\00"
@CLOCKS_PER_SEC = global i64 0
@.str.3 = [44 x i8] c"binsearch2() took %lu clocks (%lu seconds)\0A\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 @binsearch(i32 -1, i32* %44, i32 20000)
%48 = add nuw nsw i32 %46, 1
%49 = icmp eq i32 %48, 100000
br i1 %49, label %50, label %45
50: ; preds = %45
%51 = call i64 (...) @clock()
%52 = sub nsw i64 %51, %43
%53 = icmp slt i32 %47, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 -1)
br label %58
56: ; preds = %50
%57 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 -1, i32 %47)
br label %58
58: ; preds = %56, %54
%59 = load i64, i64* @CLOCKS_PER_SEC
%60 = udiv i64 %52, %59
%61 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i64 %52, i64 %60)
%62 = call i64 (...) @clock()
br label %63
63: ; preds = %58, %63
%64 = phi i32 [ 0, %58 ], [ %66, %63 ]
%65 = call i32 @binsearch2(i32 -1, i32* %44, i32 20000)
%66 = add nuw nsw i32 %64, 1
%67 = icmp eq i32 %66, 100000
br i1 %67, label %68, label %63
68: ; preds = %63
%69 = call i64 (...) @clock()
%70 = sub nsw i64 %69, %62
%71 = icmp slt i32 %65, 0
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 -1)
br label %76
74: ; preds = %68
%75 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 -1, i32 %65)
br label %76
76: ; preds = %74, %72
%77 = load i64, i64* @CLOCKS_PER_SEC
%78 = udiv i64 %70, %77
%79 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i64 %70, i64 %78)
ret i32 0
}