#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 */
char getchar();
int /*<<< orphan*/ pause(int *);
int /*<<< orphan*/ printArray(int, int *);
int /*<<< orphan*/ printf(char *, ...);
int binarySearch(int len, int array[], int find) {
  printf("BINARY SEARCH for %d in ", find);
  printArray(len, array);
  printf("Press ENTER to step through, enter s to (s)kip: ");
  int skip = getchar() == 's' ? 1 : 0;
  if (skip) {
    getchar();
  }
  char newline = skip ? '\n' : ' ';
  int lower = 0;
  int upper = len - 1;
  while (lower <= upper) {
    int mid = (lower + upper) / 2;
    char compare = array[mid] < find ? '<' : '>';
    if (array[mid] == find) {
      compare = '=';
    }
    printf("mid ind: %d, val: %d, %d %c %d\t", mid, array[mid], array[mid],
           compare, find);
    if (array[mid] == find) {
      printf("%d was found at index %d!\n\n", find, mid);
      return mid;
    } else if (array[mid] > find) {
      printf("Searching lower half...%c", newline);
      upper = mid - 1;
    } else {
      printf("Searching upper half...%c", newline);
      lower = mid + 1;
    }
    pause(&skip);
  }
  printf("\n%d was not found in the array\n\n", find);
  return -1;
}


x86 assembly (clang, -O0):
<binarySearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <printArray@plt>
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
movsbl %al,%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x73,%edx
cmove  %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11a8 <binarySearch+0x68>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    -0x18(%rbp),%edx
mov    $0x20,%eax
mov    $0xa,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     12e8 <binarySearch+0x1a8>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x14(%rbp),%esi
mov    $0x3e,%eax
mov    $0x3c,%ecx
cmp    %esi,%edx
cmovl  %ecx,%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jne    1223 <binarySearch+0xe3>
movb   $0x3d,-0x29(%rbp)
mov    -0x28(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
movsbl -0x29(%rbp),%r8d
mov    -0x14(%rbp),%r9d
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jne    1286 <binarySearch+0x146>
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1300 <binarySearch+0x1c0>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jle    12ba <binarySearch+0x17a>
movsbl -0x19(%rbp),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12d5 <binarySearch+0x195>
movsbl -0x19(%rbp),%esi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12da <binarySearch+0x19a>
lea    -0x18(%rbp),%rdi
call   1050 <pause@plt>
jmp    11ce <binarySearch+0x8e>
mov    -0x14(%rbp),%esi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binarySearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xea7(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <printArray@plt>
lea    0xea6(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%r15d
xor    %eax,%eax
cmp    $0x73,%r15b
sete   %al
mov    %eax,0x4(%rsp)
jne    119a <binarySearch+0x5a>
xor    %eax,%eax
call   1040 <getchar@plt>
test   %ebx,%ebx
jle    124c <binarySearch+0x10c>
cmp    $0x73,%r15b
mov    $0xa,%eax
mov    $0x20,%ecx
cmove  %eax,%ecx
mov    %ecx,(%rsp)
add    $0xffffffff,%ebx
xor    %r15d,%r15d
jmp    11e7 <binarySearch+0xa7>
xchg   %ax,%ax
lea    0xed9(%rip),%rdi
mov    (%rsp),%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%r13d
mov    %r13d,%r15d
lea    0x4(%rsp),%rdi
call   1050 <pause@plt>
cmp    %ebx,%r15d
jg     124c <binarySearch+0x10c>
lea    (%r15,%rbx,1),%eax
mov    %eax,%r13d
shr    $0x1f,%r13d
add    %eax,%r13d
sar    %r13d
movslq %r13d,%r12
mov    (%r14,%r12,4),%ecx
xor    %eax,%eax
cmp    %ebp,%ecx
setge  %al
lea    0x3c(%rax,%rax,1),%r8d
mov    $0x3d,%eax
cmove  %eax,%r8d
lea    0xe2f(%rip),%rdi
mov    %r12d,%esi
mov    %ecx,%edx
mov    %ebp,%r9d
xor    %eax,%eax
call   1030 <printf@plt>
cmp    %ebp,(%r14,%r12,4)
je     1264 <binarySearch+0x124>
jle    11c0 <binarySearch+0x80>
lea    0xe4d(%rip),%rdi
mov    (%rsp),%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0xffffffff,%r13d
mov    %r13d,%ebx
jmp    11d8 <binarySearch+0x98>
lea    0xe67(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%r13d
jmp    1277 <binarySearch+0x137>
lea    0xdff(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"BINARY SEARCH for %d in \00"
@.str.1 = [49 x i8] c"Press ENTER to step through, enter s to (s)kip: \00"
@.str.2 = [32 x i8] c"mid ind: %d, val: %d, %d %c %d\09\00"
@.str.3 = [28 x i8] c"%d was found at index %d!\0A\0A\00"
@.str.4 = [26 x i8] c"Searching lower half...%c\00"
@.str.5 = [26 x i8] c"Searching upper half...%c\00"
@.str.6 = [33 x i8] c"\0A%d was not found in the array\0A\0A\00"
define i32 @binarySearch(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%14 = load i32, i32* %7
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %5
%17 = load i32*, i32** %6
%18 = call i32 @printArray(i32 %16, i32* %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%20 = call signext i8 (...) @getchar()
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 115
%23 = zext i1 %22 to i64
%24 = select i1 %22, i32 1, i32 0
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %3
%28 = call signext i8 (...) @getchar()
br label %29
29: ; preds = %27, %3
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i32 10, i32 32
%34 = trunc i32 %33 to i8
store i8 %34, i8* %9
store i32 0, i32* %10
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
store i32 %36, i32* %11
br label %37
37: ; preds = %113, %29
%38 = load i32, i32* %10
%39 = load i32, i32* %11
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %115
41: ; preds = %37
%42 = load i32, i32* %10
%43 = load i32, i32* %11
%44 = add nsw i32 %42, %43
%45 = sdiv i32 %44, 2
store i32 %45, i32* %12
%46 = load i32*, i32** %6
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
%51 = load i32, i32* %7
%52 = icmp slt i32 %50, %51
%53 = zext i1 %52 to i64
%54 = select i1 %52, i32 60, i32 62
%55 = trunc i32 %54 to i8
store i8 %55, i8* %13
%56 = load i32*, i32** %6
%57 = load i32, i32* %12
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %56, i64 %58
%60 = load i32, i32* %59
%61 = load i32, i32* %7
%62 = icmp eq i32 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %41
store i8 61, i8* %13
br label %64
64: ; preds = %63, %41
%65 = load i32, i32* %12
%66 = load i32*, i32** %6
%67 = load i32, i32* %12
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i32, i32* %66, i64 %68
%70 = load i32, i32* %69
%71 = load i32*, i32** %6
%72 = load i32, i32* %12
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = load i8, i8* %13
%77 = sext i8 %76 to i32
%78 = load i32, i32* %7
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %65, i32 %70, i32 %75, i32 %77, i32 %78)
%80 = load i32*, i32** %6
%81 = load i32, i32* %12
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %80, i64 %82
%84 = load i32, i32* %83
%85 = load i32, i32* %7
%86 = icmp eq i32 %84, %85
br i1 %86, label %87, label %92
87: ; preds = %64
%88 = load i32, i32* %7
%89 = load i32, i32* %12
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %88, i32 %89)
%91 = load i32, i32* %12
store i32 %91, i32* %4
br label %118
92: ; preds = %64
%93 = load i32*, i32** %6
%94 = load i32, i32* %12
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i32, i32* %93, i64 %95
%97 = load i32, i32* %96
%98 = load i32, i32* %7
%99 = icmp sgt i32 %97, %98
br i1 %99, label %100, label %106
100: ; preds = %92
%101 = load i8, i8* %9
%102 = sext i8 %101 to i32
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %102)
%104 = load i32, i32* %12
%105 = sub nsw i32 %104, 1
store i32 %105, i32* %11
br label %112
106: ; preds = %92
%107 = load i8, i8* %9
%108 = sext i8 %107 to i32
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %108)
%110 = load i32, i32* %12
%111 = add nsw i32 %110, 1
store i32 %111, i32* %10
br label %112
112: ; preds = %106, %100
br label %113
113: ; preds = %112
%114 = call i32 @pause(i32* %8)
br label %37
115: ; preds = %37
%116 = load i32, i32* %7
%117 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %116)
store i32 -1, i32* %4
br label %118
118: ; preds = %115, %87
%119 = load i32, i32* %4
ret i32 %119
}

LLVM IR (-O2):
@.str = [25 x i8] c"BINARY SEARCH for %d in \00"
@.str.1 = [49 x i8] c"Press ENTER to step through, enter s to (s)kip: \00"
@.str.2 = [32 x i8] c"mid ind: %d, val: %d, %d %c %d\09\00"
@.str.3 = [28 x i8] c"%d was found at index %d!\0A\0A\00"
@.str.4 = [26 x i8] c"Searching lower half...%c\00"
@.str.5 = [26 x i8] c"Searching upper half...%c\00"
@.str.6 = [33 x i8] c"\0A%d was not found in the array\0A\0A\00"
define i32 @binarySearch(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %2)
%6 = tail call i32 @printArray(i32 %0, i32* %1)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%8 = bitcast i32* %4 to i8*
%9 = tail call signext i8 (...) @getchar()
%10 = icmp eq i8 %9, 115
%11 = zext i1 %10 to i32
store i32 %11, i32* %4
br i1 %10, label %12, label %14
12: ; preds = %3
%13 = tail call signext i8 (...) @getchar()
br label %14
14: ; preds = %12, %3
%15 = icmp slt i32 %0, 1
br i1 %15, label %49, label %16
16: ; preds = %14
%17 = select i1 %10, i32 10, i32 32
%18 = add nsw i32 %0, -1
br label %19
19: ; preds = %16, %44
%20 = phi i32 [ 0, %16 ], [ %46, %44 ]
%21 = phi i32 [ %18, %16 ], [ %45, %44 ]
%22 = add nsw i32 %20, %21
%23 = sdiv i32 %22, 2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %1, i64 %24
%26 = load i32, i32* %25
%27 = icmp slt i32 %26, %2
%28 = icmp eq i32 %26, %2
%29 = select i1 %27, i32 60, i32 62
%30 = select i1 %28, i32 61, i32 %29
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %26, i32 %26, i32 %30, i32 %2)
%32 = load i32, i32* %25
%33 = icmp eq i32 %32, %2
br i1 %33, label %34, label %36
34: ; preds = %19
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %2, i32 %23)
br label %51
36: ; preds = %19
%37 = icmp sgt i32 %32, %2
br i1 %37, label %38, label %41
38: ; preds = %36
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%40 = add nsw i32 %23, -1
br label %44
41: ; preds = %36
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%43 = add nsw i32 %23, 1
br label %44
44: ; preds = %41, %38
%45 = phi i32 [ %40, %38 ], [ %21, %41 ]
%46 = phi i32 [ %20, %38 ], [ %43, %41 ]
%47 = call i32 @pause(i32* %4)
%48 = icmp sgt i32 %46, %45
br i1 %48, label %49, label %19
49: ; preds = %44, %14
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %2)
br label %51
51: ; preds = %34, %49
%52 = phi i32 [ -1, %49 ], [ %23, %34 ]
ret i32 %52
}