#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 /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  srand(time(NULL));
  int array[16];
  int search_for = rand() % (16 * 2);
  int index;
  for (int i = 0; i < 16; i++) {
    array[i] = i * 2;
    printf("%d ", array[i]);
  }
  printf("\n search: %d\n", search_for);
  if ((index = binarySearch(array, search_for)) != -1) {
    printf("\nfound in index %d\n", index);
  } else {
    printf("\nelement not found\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-0x54(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0x10,-0x5c(%rbp)
jge    11c4 <main+0x74>
mov    -0x5c(%rbp),%ecx
shl    $0x1,%ecx
movslq -0x5c(%rbp),%rax
mov    %ecx,-0x50(%rbp,%rax,4)
movslq -0x5c(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1188 <main+0x38>
mov    -0x54(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
call   1060 <binarySearch@plt>
mov    %eax,-0x58(%rbp)
cmp    $0xffffffff,%eax
je     1203 <main+0xb3>
mov    -0x58(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1211 <main+0xc1>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
xor    %eax,%eax
call   1080 <rand@plt>
mov    %eax,%r14d
lea    0xeba(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movaps 0xe49(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rbx,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movaps 0xe12(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rbx,%rdi
mov    $0xe,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x12,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movaps 0xdda(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
mov    %rbx,%rdi
mov    $0x16,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x18,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x1a,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x1c,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movaps 0xda2(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
mov    %rbx,%rdi
mov    $0x1e,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x1f(%r14),%eax
test   %r14d,%r14d
cmovns %r14d,%eax
and    $0xffffffe0,%eax
sub    %eax,%r14d
lea    0xd8a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%rdi
mov    %r14d,%esi
call   1070 <binarySearch@plt>
cmp    $0xffffffff,%eax
je     12e6 <main+0x186>
lea    0xd77(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12f2 <main+0x192>
lea    0xd79(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d \00"
@.str.1 = [14 x i8] c"\0A search: %d\0A\00"
@.str.2 = [20 x i8] c"\0Afound in index %d\0A\00"
@.str.3 = [20 x i8] c"\0Aelement not found\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [16 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @time(i32* null)
%7 = call i32 @srand(i32 %6)
%8 = call i32 (...) @rand()
%9 = srem i32 %8, 32
store i32 %9, i32* %3
store i32 0, i32* %5
br label %10
10: ; preds = %24, %0
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 16
br i1 %12, label %13, label %27
13: ; preds = %10
%14 = load i32, i32* %5
%15 = mul nsw i32 %14, 2
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [16 x i32], [16 x i32]* %2, i64 0, i64 %17
store i32 %15, i32* %18
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [16 x i32], [16 x i32]* %2, i64 0, i64 %20
%22 = load i32, i32* %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %13
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %10
27: ; preds = %10
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [16 x i32], [16 x i32]* %2, i64 0, i64 0
%31 = load i32, i32* %3
%32 = call i32 @binarySearch(i32* %30, i32 %31)
store i32 %32, i32* %4
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32, i32* %4
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %39
37: ; preds = %27
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d \00"
@.str.1 = [14 x i8] c"\0A search: %d\0A\00"
@.str.2 = [20 x i8] c"\0Afound in index %d\0A\00"
@str = [19 x i8] c"\0Aelement not found\00"
define i32 @main() {
%1 = alloca [16 x i32]
%2 = tail call i32 @time(i32* null)
%3 = tail call i32 @srand(i32 %2)
%4 = bitcast [16 x i32]* %1 to i8*
%5 = tail call i32 (...) @rand()
%6 = getelementptr inbounds [16 x i32], [16 x i32]* %1, i64 0, i64 0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 0)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 2)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 4)
%10 = bitcast [16 x i32]* %1 to <4 x i32>*
store <4 x i32> <i32 0, i32 2, i32 4, i32 6>, <4 x i32>* %10
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 6)
%12 = getelementptr inbounds [16 x i32], [16 x i32]* %1, i64 0, i64 4
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 8)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 10)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 12)
%16 = bitcast i32* %12 to <4 x i32>*
store <4 x i32> <i32 8, i32 10, i32 12, i32 14>, <4 x i32>* %16
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 14)
%18 = getelementptr inbounds [16 x i32], [16 x i32]* %1, i64 0, i64 8
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 16)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 18)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 20)
%22 = bitcast i32* %18 to <4 x i32>*
store <4 x i32> <i32 16, i32 18, i32 20, i32 22>, <4 x i32>* %22
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 22)
%24 = getelementptr inbounds [16 x i32], [16 x i32]* %1, i64 0, i64 12
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 24)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 26)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 28)
%28 = bitcast i32* %24 to <4 x i32>*
store <4 x i32> <i32 24, i32 26, i32 28, i32 30>, <4 x i32>* %28
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 30)
%30 = srem i32 %5, 32
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = call i32 @binarySearch(i32* %6, i32 %30)
%33 = icmp eq i32 %32, -1
br i1 %33, label %36, label %34
34: ; preds = %0
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %38
36: ; preds = %0
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %36, %34
ret i32 0
}