#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 */

/* Variables and functions */
int binarySearch(int *, int, int);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  int n = 1024 * 1024 * 512, *a = malloc(n * sizeof(int)), i, res;
  for (i = 0; i < n; i++)
    a[i] = i;
  int nl = 1024 * 1024 * 8;
  srand(time(NULL));
  int *lookups = malloc(nl * sizeof(int));
  for (i = 0; i < nl; i++) {
    lookups[i] = rand() % n;
  }
  for (i = 0; i < nl; i++)
    res = binarySearch(a, n, lookups[i]);
  free(a);
  free(lookups);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x20000000,-0x8(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11b8 <main+0x58>
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1190 <main+0x30>
movl   $0x800000,-0x1c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1219 <main+0xb9>
mov    $0x0,%al
call   1080 <rand@plt>
cltd
idivl  -0x8(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e9 <main+0x89>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1254 <main+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
call   1060 <binarySearch@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1220 <main+0xc0>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x80000000,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
movdqa 0xe86(%rip),%xmm0
mov    $0x1c,%eax
movdqa 0xe88(%rip),%xmm8
movdqa 0xe8f(%rip),%xmm9
movdqa 0xe96(%rip),%xmm10
movdqa 0xe9e(%rip),%xmm4
movdqa 0xea6(%rip),%xmm5
movdqa 0xeae(%rip),%xmm6
movdqa 0xeb6(%rip),%xmm7
movdqa 0xebe(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movdqa %xmm0,%xmm2
paddd  %xmm8,%xmm2
movdqu %xmm0,-0x70(%r15,%rax,4)
movdqu %xmm2,-0x60(%r15,%rax,4)
movdqa %xmm0,%xmm2
paddd  %xmm9,%xmm2
movdqa %xmm0,%xmm3
paddd  %xmm10,%xmm3
movdqu %xmm2,-0x50(%r15,%rax,4)
movdqu %xmm3,-0x40(%r15,%rax,4)
movdqa %xmm0,%xmm2
paddd  %xmm4,%xmm2
movdqa %xmm0,%xmm3
paddd  %xmm5,%xmm3
movdqu %xmm2,-0x30(%r15,%rax,4)
movdqu %xmm3,-0x20(%r15,%rax,4)
movdqa %xmm0,%xmm2
paddd  %xmm6,%xmm2
movdqa %xmm0,%xmm3
paddd  %xmm7,%xmm3
movdqu %xmm2,-0x10(%r15,%rax,4)
movdqu %xmm3,(%r15,%rax,4)
paddd  %xmm1,%xmm0
add    $0x20,%rax
cmp    $0x2000001c,%rax
jne    11d0 <main+0x70>
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0x2000000,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1080 <rand@plt>
lea    0x1fffffff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xe0000000,%ecx
sub    %ecx,%eax
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    $0x800000,%rbx
jne    1280 <main+0x120>
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r14,%rbx,4),%edx
mov    %r15,%rdi
mov    $0x20000000,%esi
call   1060 <binarySearch@plt>
add    $0x1,%rbx
cmp    $0x800000,%rbx
jne    12b0 <main+0x150>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %1
store i32 536870912, i32* %2
%8 = load i32, i32* %2
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 4
%11 = trunc i64 %10 to i32
%12 = call i32* @malloc(i32 %11)
store i32* %12, i32** %3
store i32 0, i32* %4
br label %13
13: ; preds = %23, %0
%14 = load i32, i32* %4
%15 = load i32, i32* %2
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i32, i32* %4
%19 = load i32*, i32** %3
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
store i32 %18, i32* %22
br label %23
23: ; preds = %17
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %13
26: ; preds = %13
store i32 8388608, i32* %6
%27 = call i32 @time(i32* null)
%28 = call i32 @srand(i32 %27)
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = mul i64 %30, 4
%32 = trunc i64 %31 to i32
%33 = call i32* @malloc(i32 %32)
store i32* %33, i32** %7
store i32 0, i32* %4
br label %34
34: ; preds = %46, %26
%35 = load i32, i32* %4
%36 = load i32, i32* %6
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %49
38: ; preds = %34
%39 = call i32 (...) @rand()
%40 = load i32, i32* %2
%41 = srem i32 %39, %40
%42 = load i32*, i32** %7
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
store i32 %41, i32* %45
br label %46
46: ; preds = %38
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %34
49: ; preds = %34
store i32 0, i32* %4
br label %50
50: ; preds = %63, %49
%51 = load i32, i32* %4
%52 = load i32, i32* %6
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %66
54: ; preds = %50
%55 = load i32*, i32** %3
%56 = load i32, i32* %2
%57 = load i32*, i32** %7
%58 = load i32, i32* %4
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
%61 = load i32, i32* %60
%62 = call i32 @binarySearch(i32* %55, i32 %56, i32 %61)
store i32 %62, i32* %5
br label %63
63: ; preds = %54
%64 = load i32, i32* %4
%65 = add nsw i32 %64, 1
store i32 %65, i32* %4
br label %50
66: ; preds = %50
%67 = load i32*, i32** %3
%68 = call i32 @free(i32* %67)
%69 = load i32*, i32** %7
%70 = call i32 @free(i32* %69)
ret i32 0
}

LLVM IR (-O2):
define i32 @main() {
%1 = tail call i32* @malloc(i32 -2147483648)
br label %2
2: ; preds = %2, %0
%3 = phi i64 [ 0, %0 ], [ %31, %2 ]
%4 = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %0 ], [ %32, %2 ]
%5 = add <4 x i32> %4, <i32 4, i32 4, i32 4, i32 4>
%6 = getelementptr inbounds i32, i32* %1, i64 %3
%7 = bitcast i32* %6 to <4 x i32>*
store <4 x i32> %4, <4 x i32>* %7
%8 = getelementptr inbounds i32, i32* %6, i64 4
%9 = bitcast i32* %8 to <4 x i32>*
store <4 x i32> %5, <4 x i32>* %9
%10 = or i64 %3, 8
%11 = add <4 x i32> %4, <i32 8, i32 8, i32 8, i32 8>
%12 = add <4 x i32> %4, <i32 12, i32 12, i32 12, i32 12>
%13 = getelementptr inbounds i32, i32* %1, i64 %10
%14 = bitcast i32* %13 to <4 x i32>*
store <4 x i32> %11, <4 x i32>* %14
%15 = getelementptr inbounds i32, i32* %13, i64 4
%16 = bitcast i32* %15 to <4 x i32>*
store <4 x i32> %12, <4 x i32>* %16
%17 = or i64 %3, 16
%18 = add <4 x i32> %4, <i32 16, i32 16, i32 16, i32 16>
%19 = add <4 x i32> %4, <i32 20, i32 20, i32 20, i32 20>
%20 = getelementptr inbounds i32, i32* %1, i64 %17
%21 = bitcast i32* %20 to <4 x i32>*
store <4 x i32> %18, <4 x i32>* %21
%22 = getelementptr inbounds i32, i32* %20, i64 4
%23 = bitcast i32* %22 to <4 x i32>*
store <4 x i32> %19, <4 x i32>* %23
%24 = or i64 %3, 24
%25 = add <4 x i32> %4, <i32 24, i32 24, i32 24, i32 24>
%26 = add <4 x i32> %4, <i32 28, i32 28, i32 28, i32 28>
%27 = getelementptr inbounds i32, i32* %1, i64 %24
%28 = bitcast i32* %27 to <4 x i32>*
store <4 x i32> %25, <4 x i32>* %28
%29 = getelementptr inbounds i32, i32* %27, i64 4
%30 = bitcast i32* %29 to <4 x i32>*
store <4 x i32> %26, <4 x i32>* %30
%31 = add nuw nsw i64 %3, 32
%32 = add <4 x i32> %4, <i32 32, i32 32, i32 32, i32 32>
%33 = icmp eq i64 %31, 536870912
br i1 %33, label %34, label %2
34: ; preds = %2
%35 = tail call i32 @time(i32* null)
%36 = tail call i32 @srand(i32 %35)
%37 = tail call i32* @malloc(i32 33554432)
br label %38
38: ; preds = %34, %38
%39 = phi i64 [ 0, %34 ], [ %43, %38 ]
%40 = tail call i32 (...) @rand()
%41 = srem i32 %40, 536870912
%42 = getelementptr inbounds i32, i32* %37, i64 %39
store i32 %41, i32* %42
%43 = add nuw nsw i64 %39, 1
%44 = icmp eq i64 %43, 8388608
br i1 %44, label %45, label %38
45: ; preds = %38, %45
%46 = phi i64 [ %50, %45 ], [ 0, %38 ]
%47 = getelementptr inbounds i32, i32* %37, i64 %46
%48 = load i32, i32* %47
%49 = tail call i32 @binarySearch(i32* %1, i32 536870912, i32 %48)
%50 = add nuw nsw i64 %46, 1
%51 = icmp eq i64 %50, 8388608
br i1 %51, label %52, label %45
52: ; preds = %45
%53 = tail call i32 @free(i32* %1)
%54 = tail call i32 @free(i32* %37)
ret i32 0
}