#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 /*<<< orphan*/ printf(char *, ...);
int randomNumberGenerator(int);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int count, first, last, middle, n, search, array[100];
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (count = 0; count < n; count++) {
    array[count] = randomNumberGenerator(n);
  }
  printf("Enter value to find\n");
  scanf("%d", &search);
  first = 0;
  last = n - 1;
  middle = (first + last) / 2;
  while (first <= last) {
    if (array[middle] < search)
      first = middle + 1;
    else if (array[middle] == search) {
      printf("%d FOUND at location %d.\n", search, middle + 1);
      break;
    } else
      last = middle - 1;
    middle = (first + last) / 2;
  }
  if (first > last)
    printf("%d NOT FOUND\n", search);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xec3(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x18(%rbp),%esi
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11a7 <main+0x77>
mov    -0x18(%rbp),%edi
call   1030 <randomNumberGenerator@plt>
mov    %eax,%ecx
movslq -0x8(%rbp),%rax
mov    %ecx,-0x1b0(%rbp,%rax,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1178 <main+0x48>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe5e(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   1050 <scanf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1268 <main+0x138>
movslq -0x14(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%eax
cmp    -0x1c(%rbp),%eax
jge    1214 <main+0xe4>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1252 <main+0x122>
movslq -0x14(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%eax
cmp    -0x1c(%rbp),%eax
jne    1244 <main+0x114>
mov    -0x1c(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1268 <main+0x138>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1252 <main+0x122>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
jmp    11e6 <main+0xb6>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    1285 <main+0x155>
mov    -0x1c(%rbp),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x1a0,%rsp
lea    0xeef(%rip),%rdi
call   1030 <puts@plt>
lea    0xea5(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x8(%rsp),%esi
lea    0xe93(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
test   %edi,%edi
jle    11a7 <main+0x67>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
call   1040 <randomNumberGenerator@plt>
mov    %eax,0x10(%rsp,%rbx,4)
add    $0x1,%rbx
movslq 0x8(%rsp),%rdi
cmp    %rdi,%rbx
jl     1190 <main+0x50>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
lea    0xe46(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
test   %edi,%edi
jle    1207 <main+0xc7>
add    $0xffffffff,%edi
xor    %eax,%eax
mov    %edi,%ecx
jmp    11ec <main+0xac>
nopl   0x0(%rax)
add    $0x1,%edx
mov    %edx,%eax
lea    (%rcx,%rax,1),%edi
cmp    %ecx,%eax
jg     1207 <main+0xc7>
mov    %edi,%edx
shr    $0x1f,%edx
add    %edi,%edx
sar    %edx
movslq %edx,%rdi
cmp    %esi,0x10(%rsp,%rdi,4)
jl     11e0 <main+0xa0>
je     1217 <main+0xd7>
add    $0xffffffff,%edx
mov    %edx,%ecx
jmp    11e5 <main+0xa5>
lea    0xe22(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1228 <main+0xe8>
add    $0x1,%edx
lea    0xdf5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x1a0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Enter number of elements\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [19 x i8] c"Enter %d integers\0A\00"
@.str.3 = [21 x i8] c"Enter value to find\0A\00"
@.str.4 = [26 x i8] c"%d FOUND at location %d.\0A\00"
@.str.5 = [14 x i8] c"%d NOT FOUND\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i32]
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%11 = load i32, i32* %6
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %11)
store i32 0, i32* %2
br label %13
13: ; preds = %23, %0
%14 = load i32, i32* %2
%15 = load i32, i32* %6
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = load i32, i32* %6
%19 = call i32 @randomNumberGenerator(i32 %18)
%20 = load i32, i32* %2
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 %21
store i32 %19, i32* %22
br label %23
23: ; preds = %17
%24 = load i32, i32* %2
%25 = add nsw i32 %24, 1
store i32 %25, i32* %2
br label %13
26: ; preds = %13
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
store i32 0, i32* %3
%29 = load i32, i32* %6
%30 = sub nsw i32 %29, 1
store i32 %30, i32* %4
%31 = load i32, i32* %3
%32 = load i32, i32* %4
%33 = add nsw i32 %31, %32
%34 = sdiv i32 %33, 2
store i32 %34, i32* %5
br label %35
35: ; preds = %65, %26
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = icmp sle i32 %36, %37
br i1 %38, label %39, label %70
39: ; preds = %35
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 %41
%43 = load i32, i32* %42
%44 = load i32, i32* %7
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %65
49: ; preds = %39
%50 = load i32, i32* %5
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 %51
%53 = load i32, i32* %52
%54 = load i32, i32* %7
%55 = icmp eq i32 %53, %54
br i1 %55, label %56, label %61
56: ; preds = %49
%57 = load i32, i32* %7
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %57, i32 %59)
br label %70
61: ; preds = %49
%62 = load i32, i32* %5
%63 = sub nsw i32 %62, 1
store i32 %63, i32* %4
br label %64
64: ; preds = %61
br label %65
65: ; preds = %64, %46
%66 = load i32, i32* %3
%67 = load i32, i32* %4
%68 = add nsw i32 %66, %67
%69 = sdiv i32 %68, 2
store i32 %69, i32* %5
br label %35
70: ; preds = %56, %35
%71 = load i32, i32* %3
%72 = load i32, i32* %4
%73 = icmp sgt i32 %71, %72
br i1 %73, label %74, label %77
74: ; preds = %70
%75 = load i32, i32* %7
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %75)
br label %77
77: ; preds = %74, %70
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [19 x i8] c"Enter %d integers\0A\00"
@.str.4 = [26 x i8] c"%d FOUND at location %d.\0A\00"
@.str.5 = [14 x i8] c"%d NOT FOUND\0A\00"
@str = [25 x i8] c"Enter number of elements\00"
@str.6 = [20 x i8] c"Enter value to find\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i32]
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast [100 x i32]* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%9 = load i32, i32* %1
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %0, %13
%14 = phi i64 [ %18, %13 ], [ 0, %0 ]
%15 = phi i32 [ %19, %13 ], [ %11, %0 ]
%16 = call i32 @randomNumberGenerator(i32 %15)
%17 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 %14
store i32 %16, i32* %17
%18 = add nuw nsw i64 %14, 1
%19 = load i32, i32* %1
%20 = sext i32 %19 to i64
%21 = icmp slt i64 %18, %20
br i1 %21, label %13, label %22
22: ; preds = %13, %0
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%25 = load i32, i32* %1
%26 = add nsw i32 %25, -1
%27 = icmp slt i32 %25, 1
%28 = load i32, i32* %2
br i1 %27, label %52, label %29
29: ; preds = %22, %44
%30 = phi i32 [ %47, %44 ], [ %26, %22 ]
%31 = phi i32 [ %46, %44 ], [ %26, %22 ]
%32 = phi i32 [ %45, %44 ], [ 0, %22 ]
%33 = sdiv i32 %30, 2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [100 x i32], [100 x i32]* %3, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = icmp slt i32 %36, %28
br i1 %37, label %38, label %40
38: ; preds = %29
%39 = add nsw i32 %33, 1
br label %44
40: ; preds = %29
%41 = icmp eq i32 %36, %28
br i1 %41, label %49, label %42
42: ; preds = %40
%43 = add nsw i32 %33, -1
br label %44
44: ; preds = %42, %38
%45 = phi i32 [ %39, %38 ], [ %32, %42 ]
%46 = phi i32 [ %31, %38 ], [ %43, %42 ]
%47 = add nsw i32 %46, %45
%48 = icmp sgt i32 %45, %46
br i1 %48, label %52, label %29
49: ; preds = %40
%50 = add nsw i32 %33, 1
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %50)
br label %54
52: ; preds = %44, %22
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %28)
br label %54
54: ; preds = %49, %52
ret i32 0
}