#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 binary_search(int *, int, int);
int linear_search(int *, int, int);
int *malloc(int);
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  int i;
  int *a = malloc(sizeof(int) * 1000000);
  for (i = 0; i < 1000000; i++) {
    *(a + i) = i + 1;
  }
  srand(time(NULL));
  int key = rand() % 1000000 + 1;
  printf("Element for search: %d\n", key);
  int found = linear_search(a, 1000000, key);
  printf("Found with linear search at position: %d\n", found);
  found = binary_search(a, 1000000, key);
  printf("Found with binary search at position: %d\n", found);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x3d0900,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xf4240,-0x8(%rbp)
jge    11c0 <main+0x50>
mov    -0x8(%rbp),%edx
add    $0x1,%edx
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1194 <main+0x24>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xf4240,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0xf4240,%esi
call   1030 <linear_search@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0xf4240,%esi
call   1080 <binary_search@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x3d0900,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
movaps 0xe79(%rip),%xmm0
movaps 0xe82(%rip),%xmm1
mov    $0xc,%eax
pcmpeqd %xmm8,%xmm8
movdqa 0xe7f(%rip),%xmm9
movdqa 0xe87(%rip),%xmm4
movdqa 0xe8f(%rip),%xmm5
movdqa 0xe97(%rip),%xmm6
nopl   0x0(%rax)
movaps %xmm1,%xmm7
shufps $0x88,%xmm0,%xmm7
movaps %xmm7,%xmm2
psubd  %xmm8,%xmm2
movaps %xmm7,%xmm3
paddd  %xmm9,%xmm3
movdqu %xmm2,-0x30(%rbx,%rax,4)
movdqu %xmm3,-0x20(%rbx,%rax,4)
movaps %xmm7,%xmm2
paddd  %xmm4,%xmm2
paddd  %xmm5,%xmm7
movdqu %xmm2,-0x10(%rbx,%rax,4)
movdqu %xmm7,(%rbx,%rax,4)
paddq  %xmm6,%xmm1
paddq  %xmm6,%xmm0
add    $0x10,%rax
cmp    $0xf424c,%rax
jne    11c0 <main+0x50>
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
xor    %eax,%eax
call   1090 <rand@plt>
cltq
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebp
add    $0x1,%ebp
lea    0xe13(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0xf4240,%esi
mov    %ebp,%edx
call   1030 <linear_search@plt>
lea    0xe0e(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0xf4240,%esi
mov    %ebp,%edx
call   1080 <binary_search@plt>
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Element for search: %d\0A\00"
@.str.1 = [42 x i8] c"Found with linear search at position: %d\0A\00"
@.str.2 = [42 x i8] c"Found with binary search at position: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32* @malloc(i32 4000000)
store i32* %6, i32** %3
store i32 0, i32* %2
br label %7
7: ; preds = %17, %0
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 1000000
br i1 %9, label %10, label %20
10: ; preds = %7
%11 = load i32, i32* %2
%12 = add nsw i32 %11, 1
%13 = load i32*, i32** %3
%14 = load i32, i32* %2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i32, i32* %13, i64 %15
store i32 %12, i32* %16
br label %17
17: ; preds = %10
%18 = load i32, i32* %2
%19 = add nsw i32 %18, 1
store i32 %19, i32* %2
br label %7
20: ; preds = %7
%21 = call i32 @time(i32* null)
%22 = call i32 @srand(i32 %21)
%23 = call i32 (...) @rand()
%24 = srem i32 %23, 1000000
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %3
%29 = load i32, i32* %4
%30 = call i32 @linear_search(i32* %28, i32 1000000, i32 %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32*, i32** %3
%34 = load i32, i32* %4
%35 = call i32 @binary_search(i32* %33, i32 1000000, i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"Element for search: %d\0A\00"
@.str.1 = [42 x i8] c"Found with linear search at position: %d\0A\00"
@.str.2 = [42 x i8] c"Found with binary search at position: %d\0A\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 4000000)
br label %2
2: ; preds = %2, %0
%3 = phi i64 [ 0, %0 ], [ %23, %2 ]
%4 = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %0 ], [ %24, %2 ]
%5 = getelementptr inbounds i32, i32* %1, i64 %3
%6 = trunc <4 x i64> %4 to <4 x i32>
%7 = add <4 x i32> %6, <i32 1, i32 1, i32 1, i32 1>
%8 = trunc <4 x i64> %4 to <4 x i32>
%9 = add <4 x i32> %8, <i32 5, i32 5, i32 5, i32 5>
%10 = bitcast i32* %5 to <4 x i32>*
store <4 x i32> %7, <4 x i32>* %10
%11 = getelementptr inbounds i32, i32* %5, i64 4
%12 = bitcast i32* %11 to <4 x i32>*
store <4 x i32> %9, <4 x i32>* %12
%13 = or i64 %3, 8
%14 = add <4 x i64> %4, <i64 8, i64 8, i64 8, i64 8>
%15 = getelementptr inbounds i32, i32* %1, i64 %13
%16 = trunc <4 x i64> %14 to <4 x i32>
%17 = add <4 x i32> %16, <i32 1, i32 1, i32 1, i32 1>
%18 = trunc <4 x i64> %14 to <4 x i32>
%19 = add <4 x i32> %18, <i32 5, i32 5, i32 5, i32 5>
%20 = bitcast i32* %15 to <4 x i32>*
store <4 x i32> %17, <4 x i32>* %20
%21 = getelementptr inbounds i32, i32* %15, i64 4
%22 = bitcast i32* %21 to <4 x i32>*
store <4 x i32> %19, <4 x i32>* %22
%23 = add nuw nsw i64 %3, 16
%24 = add <4 x i64> %4, <i64 16, i64 16, i64 16, i64 16>
%25 = icmp eq i64 %23, 1000000
br i1 %25, label %26, label %2
26: ; preds = %2
%27 = tail call i32 @time(i32* null)
%28 = tail call i32 @srand(i32 %27)
%29 = tail call i32 (...) @rand()
%30 = srem i32 %29, 1000000
%31 = add nsw i32 %30, 1
%32 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = tail call i32 @linear_search(i32* %1, i32 1000000, i32 %31)
%34 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = tail call i32 @binary_search(i32* %1, i32 1000000, i32 %31)
%36 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %35)
ret i32 0
}