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

/* Variables and functions */
int CLOCKS_PER_SEC;
int binsearch(int, int *, int);
int binsearch_alt(int, int *, int);
int clock();
int /*<<< orphan*/ printf(char *, int, int);
int main(void) {
  clock_t timer;
  int pos = 0;
  int i;
  int v[1000000] = {0};
  for (i = 0; i < 1000000; ++i)
    v[i] = i;
  timer = clock();
  for (i = 0; i < 1000000; ++i) {
    pos = binsearch(-1, v, 1000000);
  }
  timer = clock() - timer;
  printf("originl results:\n\ttimer: %lu clocks or %lu seconds\n", timer,
         timer / CLOCKS_PER_SEC);
  timer = clock();
  for (i = 0; i < 1000000; ++i) {
    pos = binsearch_alt(-1, v, 1000000);
  }
  timer = clock() - timer;
  printf("alternative results:\n\ttimer: %lu clocks or %lu seconds\n", timer,
         timer / CLOCKS_PER_SEC);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3d0910,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x3d0910(%rbp),%rdi
xor    %esi,%esi
mov    $0x3d0900,%edx
call   1060 <memset@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jge    11ac <main+0x5c>
mov    -0x10(%rbp),%ecx
movslq -0x10(%rbp),%rax
mov    %ecx,-0x3d0910(%rbp,%rax,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1183 <main+0x33>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jge    11f1 <main+0xa1>
lea    -0x3d0910(%rbp),%rsi
mov    $0xffffffff,%edi
mov    $0xf4240,%edx
call   1030 <binsearch@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11bd <main+0x6d>
mov    $0x0,%al
call   1040 <clock@plt>
sub    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%eax
mov    0x2ddd(%rip),%rcx
cltd
idivl  (%rcx)
mov    %eax,%edx
lea    0xde9(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <clock@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jge    1261 <main+0x111>
lea    -0x3d0910(%rbp),%rsi
mov    $0xffffffff,%edi
mov    $0xf4240,%edx
call   1070 <binsearch_alt@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    122d <main+0xdd>
mov    $0x0,%al
call   1040 <clock@plt>
sub    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x8(%rbp),%eax
mov    0x2d6d(%rip),%rcx
cltd
idivl  (%rcx)
mov    %eax,%edx
lea    0xdad(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x3d0910,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3d0908,%rsp
mov    %rsp,%rdi
mov    $0x3d0900,%edx
xor    %esi,%esi
call   1060 <memset@plt>
movdqa 0xe8c(%rip),%xmm0
mov    $0x24,%eax
movdqa 0xe8e(%rip),%xmm8
movdqa 0xe95(%rip),%xmm9
movdqa 0xe9c(%rip),%xmm10
movdqa 0xea3(%rip),%xmm11
movdqa 0xeaa(%rip),%xmm12
movdqa 0xeb2(%rip),%xmm6
movdqa 0xeba(%rip),%xmm7
movdqa 0xec2(%rip),%xmm1
movdqa 0xeca(%rip),%xmm2
movdqa 0xed2(%rip),%xmm3
xchg   %ax,%ax
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    $0xf4264,%rax
jne    11d0 <main+0x80>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %eax,%r14d
mov    $0xf4240,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xffffffff,%edi
mov    %rbx,%rsi
mov    $0xf4240,%edx
call   1030 <binsearch@plt>
add    $0xffffffff,%ebp
jne    1290 <main+0x140>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %eax,%esi
sub    %r14d,%esi
mov    0x2d2e(%rip),%r14
mov    %esi,%eax
cltd
idivl  (%r14)
lea    0xde9(%rip),%rdi
mov    %eax,%edx
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %eax,%r15d
mov    $0xf4240,%ebx
mov    %rsp,%rbp
mov    $0xffffffff,%edi
mov    %rbp,%rsi
mov    $0xf4240,%edx
call   1070 <binsearch_alt@plt>
add    $0xffffffff,%ebx
jne    12e0 <main+0x190>
xor    %eax,%eax
call   1040 <clock@plt>
mov    %eax,%esi
sub    %r15d,%esi
mov    %esi,%eax
cltd
idivl  (%r14)
lea    0xdd4(%rip),%rdi
mov    %eax,%edx
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x3d0908,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [52 x i8] c"originl results:\0A\09timer: %lu clocks or %lu seconds\0A\00"
@CLOCKS_PER_SEC = global i32 0
@.str.1 = [56 x i8] c"alternative results:\0A\09timer: %lu clocks or %lu seconds\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1000000 x i32]
store i32 0, i32* %1
store i32 0, i32* %3
%6 = bitcast [1000000 x i32]* %5 to i8*
store i32 0, i32* %4
br label %7
7: ; preds = %15, %0
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 1000000
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 [1000000 x i32], [1000000 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 i32 (...) @clock()
store i32 %19, i32* %2
store i32 0, i32* %4
br label %20
20: ; preds = %26, %18
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 1000000
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = getelementptr inbounds [1000000 x i32], [1000000 x i32]* %5, i64 0, i64 0
%25 = call i32 @binsearch(i32 -1, i32* %24, i32 1000000)
store i32 %25, i32* %3
br label %26
26: ; preds = %23
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %20
29: ; preds = %20
%30 = call i32 (...) @clock()
%31 = load i32, i32* %2
%32 = sub nsw i32 %30, %31
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = load i32, i32* %2
%35 = load i32, i32* @CLOCKS_PER_SEC
%36 = sdiv i32 %34, %35
%37 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %36)
%38 = call i32 (...) @clock()
store i32 %38, i32* %2
store i32 0, i32* %4
br label %39
39: ; preds = %45, %29
%40 = load i32, i32* %4
%41 = icmp slt i32 %40, 1000000
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = getelementptr inbounds [1000000 x i32], [1000000 x i32]* %5, i64 0, i64 0
%44 = call i32 @binsearch_alt(i32 -1, i32* %43, i32 1000000)
store i32 %44, i32* %3
br label %45
45: ; preds = %42
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
store i32 %47, i32* %4
br label %39
48: ; preds = %39
%49 = call i32 (...) @clock()
%50 = load i32, i32* %2
%51 = sub nsw i32 %49, %50
store i32 %51, i32* %2
%52 = load i32, i32* %2
%53 = load i32, i32* %2
%54 = load i32, i32* @CLOCKS_PER_SEC
%55 = sdiv i32 %53, %54
%56 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %52, i32 %55)
ret i32 0
}

LLVM IR (-O2):
@.str = [52 x i8] c"originl results:\0A\09timer: %lu clocks or %lu seconds\0A\00"
@CLOCKS_PER_SEC = global i32 0
@.str.1 = [56 x i8] c"alternative results:\0A\09timer: %lu clocks or %lu seconds\0A\00"
define i32 @main() {
%1 = alloca [1000000 x i32]
%2 = bitcast [1000000 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 [1000000 x i32], [1000000 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 [1000000 x i32], [1000000 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 [1000000 x i32], [1000000 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 [1000000 x i32], [1000000 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 [1000000 x i32], [1000000 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, 1000000
br i1 %41, label %42, label %3
42: ; preds = %3
%43 = tail call i32 (...) @clock()
%44 = getelementptr inbounds [1000000 x i32], [1000000 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 1000000)
%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 i32 (...) @clock()
%52 = sub nsw i32 %51, %43
%53 = load i32, i32* @CLOCKS_PER_SEC
%54 = sdiv i32 %52, %53
%55 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %52, i32 %54)
%56 = call i32 (...) @clock()
br label %57
57: ; preds = %50, %57
%58 = phi i32 [ 0, %50 ], [ %60, %57 ]
%59 = call i32 @binsearch_alt(i32 -1, i32* %44, i32 1000000)
%60 = add nuw nsw i32 %58, 1
%61 = icmp eq i32 %60, 1000000
br i1 %61, label %62, label %57
62: ; preds = %57
%63 = call i32 (...) @clock()
%64 = sub nsw i32 %63, %56
%65 = load i32, i32* @CLOCKS_PER_SEC
%66 = sdiv i32 %64, %65
%67 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %64, i32 %66)
ret i32 0
}