#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int binarySearch(int *, int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintArray(int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int linearSearch(int *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ readArrayFile(int /*<<< orphan*/ *, int *, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  int arr[100];
  int search, linearIndex, binaryIndex;
  FILE *inFile = fopen("A1.sortedArray", "r");
  if (inFile == NULL) {
    fprintf(stderr, "Error opening file...\n");
    fprintf(stderr, "Did you run program A1 of project?\nGoodbye!\n");
    exit(1);
  }
  readArrayFile(inFile, arr, 100);
  fclose(inFile);
  srand(time(NULL));
  search = (rand() % (999 - 1)) + 1;
  linearIndex = linearSearch(arr, 100, search);
  binaryIndex = binarySearch(arr, 100, search);
  printf("Array to be searched:\n");
  fprintArray(stdout, arr, 100);
  printf("Search value = %d\n", search);
  printf("\nLinear Search:\n");
  if (linearIndex >= 0)
    printf("\tValue found at index: %d\n", linearIndex);
  else
    printf("\tValue NOT found!\n");
  printf("\nBinary Search:\n");
  if (binaryIndex >= 0)
    printf("\tValue found at index: %d\n", binaryIndex);
  else
    printf("\tValue NOT found!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe27(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x1b8(%rbp)
cmpq   $0x0,-0x1b8(%rbp)
jne    122e <main+0x6e>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x1b8(%rbp),%rdi
lea    -0x1a0(%rbp),%rsi
mov    $0x64,%edx
call   10c0 <readArrayFile@plt>
mov    -0x1b8(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    $0x3e6,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x1a4(%rbp)
lea    -0x1a0(%rbp),%rdi
mov    -0x1a4(%rbp),%edx
mov    $0x64,%esi
call   1030 <linearSearch@plt>
mov    %eax,-0x1a8(%rbp)
lea    -0x1a0(%rbp),%rdi
mov    -0x1a4(%rbp),%edx
mov    $0x64,%esi
call   10a0 <binarySearch@plt>
mov    %eax,-0x1ac(%rbp)
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
lea    -0x1a0(%rbp),%rsi
mov    $0x64,%edx
call   1070 <fprintArray@plt>
mov    -0x1a4(%rbp),%esi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x1a8(%rbp)
jl     1324 <main+0x164>
mov    -0x1a8(%rbp),%esi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1332 <main+0x172>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x1ac(%rbp)
jl     1366 <main+0x1a6>
mov    -0x1ac(%rbp),%esi
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1374 <main+0x1b4>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x198,%rsp
lea    0xe1c(%rip),%rdi
lea    0xe24(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1313 <main+0x143>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x64,%edx
call   10d0 <readArrayFile@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
xor    %eax,%eax
call   10f0 <rand@plt>
cltq
imul   $0x20d56b39,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x27,%rcx
add    %edx,%ecx
imul   $0x3e6,%ecx,%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebp
add    $0x1,%ebp
mov    %r14,%rdi
mov    $0x64,%esi
mov    %ebp,%edx
call   1030 <linearSearch@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    $0x64,%esi
mov    %ebp,%edx
call   10b0 <binarySearch@plt>
mov    %eax,%r15d
lea    0xe09(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1080 <fprintArray@plt>
lea    0xdb9(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xded(%rip),%rdi
call   1040 <puts@plt>
test   %ebx,%ebx
js     12c7 <main+0xf7>
lea    0xdac(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12d3 <main+0x103>
lea    0xdeb(%rip),%rdi
call   1040 <puts@plt>
lea    0xdcf(%rip),%rdi
call   1040 <puts@plt>
test   %r15d,%r15d
js     12f7 <main+0x127>
lea    0xd7d(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1303 <main+0x133>
lea    0xdbb(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cc6(%rip),%rbx
mov    (%rbx),%edi
lea    0xcee(%rip),%rsi
call   1090 <fprintf@plt>
mov    (%rbx),%edi
lea    0xcf7(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"A1.sortedArray\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"Error opening file...\0A\00"
@.str.3 = [45 x i8] c"Did you run program A1 of project?\0AGoodbye!\0A\00"
@.str.4 = [23 x i8] c"Array to be searched:\0A\00"
@stdout = global i32 0
@.str.5 = [19 x i8] c"Search value = %d\0A\00"
@.str.6 = [17 x i8] c"\0ALinear Search:\0A\00"
@.str.7 = [27 x i8] c"\09Value found at index: %d\0A\00"
@.str.8 = [19 x i8] c"\09Value NOT found!\0A\00"
@.str.9 = [17 x i8] c"\0ABinary Search:\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = load i32*, i32** %6
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32*, i32** %6
%18 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%19 = call i32 @readArrayFile(i32* %17, i32* %18, i32 100)
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
%22 = call i32 @time(i32* null)
%23 = call i32 @srand(i32 %22)
%24 = call i32 (...) @rand()
%25 = srem i32 %24, 998
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
%27 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%28 = load i32, i32* %3
%29 = call i32 @linearSearch(i32* %27, i32 100, i32 %28)
store i32 %29, i32* %4
%30 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%31 = load i32, i32* %3
%32 = call i32 @binarySearch(i32* %30, i32 100, i32 %31)
store i32 %32, i32* %5
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @stdout
%35 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 0
%36 = call i32 @fprintArray(i32 %34, i32* %35, i32 100)
%37 = load i32, i32* %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%40 = load i32, i32* %4
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %16
%43 = load i32, i32* %4
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %43)
br label %47
45: ; preds = %16
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
%49 = load i32, i32* %5
%50 = icmp sge i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %5
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %52)
br label %56
54: ; preds = %47
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"A1.sortedArray\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"Error opening file...\0A\00"
@.str.3 = [45 x i8] c"Did you run program A1 of project?\0AGoodbye!\0A\00"
@stdout = global i32 0
@.str.5 = [19 x i8] c"Search value = %d\0A\00"
@.str.7 = [27 x i8] c"\09Value found at index: %d\0A\00"
@str = [22 x i8] c"Array to be searched:\00"
@str.10 = [16 x i8] c"\0ALinear Search:\00"
@str.12 = [16 x i8] c"\0ABinary Search:\00"
@str.13 = [18 x i8] c"\09Value NOT found!\00"
define i32 @main() {
%1 = alloca [100 x i32]
%2 = bitcast [100 x i32]* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 0
%13 = call i32 @readArrayFile(i32* %3, i32* %12, i32 100)
%14 = call i32 @fclose(i32* %3)
%15 = call i32 @time(i32* null)
%16 = call i32 @srand(i32 %15)
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 998
%19 = add nsw i32 %18, 1
%20 = call i32 @linearSearch(i32* %12, i32 100, i32 %19)
%21 = call i32 @binarySearch(i32* %12, i32 100, i32 %19)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @stdout
%24 = call i32 @fprintArray(i32 %23, i32* %12, i32 100)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.10, i64 0, i64 0))
%27 = icmp sgt i32 %20, -1
br i1 %27, label %28, label %30
28: ; preds = %11
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %20)
br label %32
30: ; preds = %11
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.13, i64 0, i64 0))
br label %32
32: ; preds = %30, %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.12, i64 0, i64 0))
%34 = icmp sgt i32 %21, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i32 %21)
br label %39
37: ; preds = %32
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.13, i64 0, i64 0))
br label %39
39: ; preds = %37, %35
ret i32 0
}