#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 findBinary(int *, int, int);
int findLinear(int *, int, int);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  int sz = 300;
  int i;
  int *array = malloc(sz * sizeof(int));
  int needle;
  int index;
  srand(time(NULL));
  for (i = 0; i < sz - 1; i++)
    array[i] = rand() % 500;
  printf("Linear search:\r\n");
  printf("Needle: %i\r\n", (needle = rand() % 10));
  index = findLinear(array, needle, sz);
  if (index > -1) {
    printf("Needle found! Index: %i\r\n", index);
    printf("     %i     %i     %i     \r\n", array[index - 1], array[index],
           array[index + 1]);
    printf("... [%i]   [%i]   [%i] ...\r\n", index - 1, index, index + 1);
  } else {
    printf("%i not found in array.\r\n");
  }
  printf("\r\nBinary search:");
  printf("Needle: %i\r\n", needle);
  index = findBinary(array, needle, sz);
  if (index > -1) {
    printf("Needle found! Index: %i\r\n", index);
    printf("     %i     %i     %i     \r\n", array[index - 1], array[index],
           array[index + 1]);
    printf("... [%i]   [%i]   [%i] ...\r\n", index - 1, index, index + 1);
  } else {
    printf("%i not found in array.\r\n");
  }
  free(array);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x12c,-0x8(%rbp)
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11f9 <main+0x79>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x1f4,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c0 <main+0x40>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,%esi
mov    %esi,-0x1c(%rbp)
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1080 <findLinear@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jle    12b1 <main+0x131>
mov    -0x20(%rbp),%esi
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%esi
sub    $0x1,%esi
mov    -0x20(%rbp),%edx
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bf <main+0x13f>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1090 <findBinary@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jle    1366 <main+0x1e6>
mov    -0x20(%rbp),%esi
lea    0xd1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%esi
sub    $0x1,%esi
mov    -0x20(%rbp),%edx
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1374 <main+0x1f4>
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x4b0,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x1f4,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    $0x12b,%rbp
jne    11c0 <main+0x30>
lea    0xe8f(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
movslq %eax,%r14
imul   $0x66666667,%r14,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%r14d
lea    0xdd2(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x12c,%edx
call   1090 <findLinear@plt>
test   %eax,%eax
js     12a0 <main+0x110>
mov    %eax,%r15d
lea    0xdb7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    -0x1(%r15),%r12d
movslq %r15d,%rax
mov    -0x4(%rbx,%rax,4),%esi
mov    %r15d,%eax
mov    %r15d,%ebp
add    $0x1,%ebp
mov    (%rbx,%rax,4),%edx
mov    0x4(%rbx,%rax,4),%ecx
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdb5(%rip),%rdi
mov    %r12d,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12ae <main+0x11e>
lea    0xdba(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd3d(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x12c,%edx
call   10a0 <findBinary@plt>
test   %eax,%eax
js     1335 <main+0x1a5>
mov    %eax,%r14d
lea    0xd22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    -0x1(%r14),%r15d
movslq %r14d,%rax
mov    -0x4(%rbx,%rax,4),%esi
mov    %r14d,%eax
mov    %r14d,%ebp
add    $0x1,%ebp
mov    (%rbx,%rax,4),%edx
mov    0x4(%rbx,%rax,4),%ecx
lea    0xd11(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd20(%rip),%rdi
mov    %r15d,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1343 <main+0x1b3>
lea    0xd25(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Linear search:\0D\0A\00"
@.str.1 = [13 x i8] c"Needle: %i\0D\0A\00"
@.str.2 = [26 x i8] c"Needle found! Index: %i\0D\0A\00"
@.str.3 = [29 x i8] c" %i %i %i \0D\0A\00"
@.str.4 = [29 x i8] c"... [%i] [%i] [%i] ...\0D\0A\00"
@.str.5 = [25 x i8] c"%i not found in array.\0D\0A\00"
@.str.6 = [17 x i8] c"\0D\0ABinary search:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 300, i32* %2
%7 = load i32, i32* %2
%8 = sext i32 %7 to i64
%9 = mul i64 %8, 4
%10 = trunc i64 %9 to i32
%11 = call i32* @malloc(i32 %10)
store i32* %11, i32** %4
%12 = call i32 @time(i32* null)
%13 = call i32 @srand(i32 %12)
store i32 0, i32* %3
br label %14
14: ; preds = %26, %0
%15 = load i32, i32* %3
%16 = load i32, i32* %2
%17 = sub nsw i32 %16, 1
%18 = icmp slt i32 %15, %17
br i1 %18, label %19, label %29
19: ; preds = %14
%20 = call i32 (...) @rand()
%21 = srem i32 %20, 500
%22 = load i32*, i32** %4
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
store i32 %21, i32* %25
br label %26
26: ; preds = %19
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %14
29: ; preds = %14
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%31 = call i32 (...) @rand()
%32 = srem i32 %31, 10
store i32 %32, i32* %5
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32*, i32** %4
%35 = load i32, i32* %5
%36 = load i32, i32* %2
%37 = call i32 @findLinear(i32* %34, i32 %35, i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, -1
br i1 %39, label %40, label %67
40: ; preds = %29
%41 = load i32, i32* %6
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%43 = load i32*, i32** %4
%44 = load i32, i32* %6
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %43, i64 %46
%48 = load i32, i32* %47
%49 = load i32*, i32** %4
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = load i32*, i32** %4
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %54, i64 %57
%59 = load i32, i32* %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %48, i32 %53, i32 %59)
%61 = load i32, i32* %6
%62 = sub nsw i32 %61, 1
%63 = load i32, i32* %6
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 1
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %62, i32 %63, i32 %65)
br label %69
67: ; preds = %29
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %40
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%71 = load i32, i32* %5
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %71)
%73 = load i32*, i32** %4
%74 = load i32, i32* %5
%75 = load i32, i32* %2
%76 = call i32 @findBinary(i32* %73, i32 %74, i32 %75)
store i32 %76, i32* %6
%77 = load i32, i32* %6
%78 = icmp sgt i32 %77, -1
br i1 %78, label %79, label %106
79: ; preds = %69
%80 = load i32, i32* %6
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %80)
%82 = load i32*, i32** %4
%83 = load i32, i32* %6
%84 = sub nsw i32 %83, 1
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %82, i64 %85
%87 = load i32, i32* %86
%88 = load i32*, i32** %4
%89 = load i32, i32* %6
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i32, i32* %88, i64 %90
%92 = load i32, i32* %91
%93 = load i32*, i32** %4
%94 = load i32, i32* %6
%95 = add nsw i32 %94, 1
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i32, i32* %93, i64 %96
%98 = load i32, i32* %97
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %87, i32 %92, i32 %98)
%100 = load i32, i32* %6
%101 = sub nsw i32 %100, 1
%102 = load i32, i32* %6
%103 = load i32, i32* %6
%104 = add nsw i32 %103, 1
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %101, i32 %102, i32 %104)
br label %108
106: ; preds = %69
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %108
108: ; preds = %106, %79
%109 = load i32*, i32** %4
%110 = call i32 @free(i32* %109)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"Needle: %i\0D\0A\00"
@.str.2 = [26 x i8] c"Needle found! Index: %i\0D\0A\00"
@.str.3 = [29 x i8] c" %i %i %i \0D\0A\00"
@.str.4 = [29 x i8] c"... [%i] [%i] [%i] ...\0D\0A\00"
@.str.5 = [25 x i8] c"%i not found in array.\0D\0A\00"
@.str.6 = [17 x i8] c"\0D\0ABinary search:\00"
@str = [16 x i8] c"Linear search:\0D\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 1200)
%2 = tail call i32 @time(i32* null)
%3 = tail call i32 @srand(i32 %2)
br label %4
4: ; preds = %0, %4
%5 = phi i64 [ 0, %0 ], [ %9, %4 ]
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 500
%8 = getelementptr inbounds i32, i32* %1, i64 %5
store i32 %7, i32* %8
%9 = add nuw nsw i64 %5, 1
%10 = icmp eq i64 %9, 299
br i1 %10, label %11, label %4
11: ; preds = %4
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 (...) @rand()
%14 = srem i32 %13, 10
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = tail call i32 @findLinear(i32* %1, i32 %14, i32 300)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %33
18: ; preds = %11
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%20 = add nsw i32 %16, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %1, i64 %21
%23 = load i32, i32* %22
%24 = zext i32 %16 to i64
%25 = getelementptr inbounds i32, i32* %1, i64 %24
%26 = load i32, i32* %25
%27 = add nuw nsw i32 %16, 1
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %1, i64 %28
%30 = load i32, i32* %29
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %26, i32 %30)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %16, i32 %27)
br label %35
33: ; preds = %11
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %18
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%38 = tail call i32 @findBinary(i32* %1, i32 %14, i32 300)
%39 = icmp sgt i32 %38, -1
br i1 %39, label %40, label %55
40: ; preds = %35
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%42 = add nsw i32 %38, -1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %1, i64 %43
%45 = load i32, i32* %44
%46 = zext i32 %38 to i64
%47 = getelementptr inbounds i32, i32* %1, i64 %46
%48 = load i32, i32* %47
%49 = add nuw nsw i32 %38, 1
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %1, i64 %50
%52 = load i32, i32* %51
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 %48, i32 %52)
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %42, i32 %38, i32 %49)
br label %57
55: ; preds = %35
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %55, %40
%58 = tail call i32 @free(i32* %1)
ret i32 0
}