#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*/ exit(int);
int sanity_check_array(int *, int);
int binary_search(int *parr, int low, int high, int key) {
  int mid = 0;
  do {
    if (sanity_check_array((parr), (high)) == -1)
      exit(-1);
  } while (0);
  while (low < high) {
    mid = (high + low) / 2;
    if (key < parr[mid]) {
      high = mid;
    } else if (key > parr[mid]) {
      low = mid;
    } else if (key == parr[mid]) {
      return mid;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<binary_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <sanity_check_array@plt>
cmp    $0xffffffff,%eax
jne    115b <binary_search+0x3b>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
jmp    1160 <binary_search+0x40>
jmp    1165 <binary_search+0x45>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11ee <binary_search+0xce>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    11a1 <binary_search+0x81>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11e9 <binary_search+0xc9>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11c0 <binary_search+0xa0>
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11e4 <binary_search+0xc4>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    11df <binary_search+0xbf>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f5 <binary_search+0xd5>
jmp    11e4 <binary_search+0xc4>
jmp    11e9 <binary_search+0xc9>
jmp    1165 <binary_search+0x45>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r15
mov    %edx,%esi
call   1030 <sanity_check_array@plt>
cmp    $0xffffffff,%eax
jne    1152 <binary_search+0x32>
mov    $0xffffffff,%edi
call   1040 <exit@plt>
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebx
cmp    %ebx,%ebp
jge    1184 <binary_search+0x64>
lea    (%rbx,%rbp,1),%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
movslq %eax,%rcx
mov    (%r15,%rcx,4),%ecx
cmp    %r14d,%ecx
jg     1150 <binary_search+0x30>
jge    1180 <binary_search+0x60>
mov    %eax,%ebp
jmp    1152 <binary_search+0x32>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
jne    1152 <binary_search+0x32>
jmp    1189 <binary_search+0x69>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @binary_search(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %4
%12 = load i32*, i32** %6
%13 = load i32, i32* %8
%14 = call i32 @sanity_check_array(i32* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %11
br label %19
19: ; preds = %18
br label %20
20: ; preds = %60, %19
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %61
24: ; preds = %20
%25 = load i32, i32* %8
%26 = load i32, i32* %7
%27 = add nsw i32 %25, %26
%28 = sdiv i32 %27, 2
store i32 %28, i32* %10
%29 = load i32, i32* %9
%30 = load i32*, i32** %6
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = icmp slt i32 %29, %34
br i1 %35, label %36, label %38
36: ; preds = %24
%37 = load i32, i32* %10
store i32 %37, i32* %8
br label %60
38: ; preds = %24
%39 = load i32, i32* %9
%40 = load i32*, i32** %6
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = icmp sgt i32 %39, %44
br i1 %45, label %46, label %48
46: ; preds = %38
%47 = load i32, i32* %10
store i32 %47, i32* %7
br label %59
48: ; preds = %38
%49 = load i32, i32* %9
%50 = load i32*, i32** %6
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %53
%55 = icmp eq i32 %49, %54
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = load i32, i32* %10
store i32 %57, i32* %5
br label %62
58: ; preds = %48
br label %59
59: ; preds = %58, %46
br label %60
60: ; preds = %59, %36
br label %20
61: ; preds = %20
store i32 -1, i32* %5
br label %62
62: ; preds = %61, %56
%63 = load i32, i32* %5
ret i32 %63
}

LLVM IR (-O2):
define i32 @binary_search(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @sanity_check_array(i32* %0, i32 %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = icmp slt i32 %1, %2
br i1 %8, label %11, label %28
9: ; preds = %4
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %7, %24
%12 = phi i32 [ %26, %24 ], [ %2, %7 ]
%13 = phi i32 [ %25, %24 ], [ %1, %7 ]
%14 = add nsw i32 %12, %13
%15 = sdiv i32 %14, 2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = load i32, i32* %17
%19 = icmp sgt i32 %18, %3
br i1 %19, label %24, label %20
20: ; preds = %11
%21 = icmp slt i32 %18, %3
br i1 %21, label %24, label %22
22: ; preds = %20
%23 = icmp eq i32 %18, %3
br i1 %23, label %28, label %24
24: ; preds = %20, %11, %22
%25 = phi i32 [ %13, %22 ], [ %13, %11 ], [ %15, %20 ]
%26 = phi i32 [ %12, %22 ], [ %15, %11 ], [ %12, %20 ]
%27 = icmp slt i32 %25, %26
br i1 %27, label %11, label %28
28: ; preds = %22, %24, %7
%29 = phi i32 [ -1, %7 ], [ -1, %24 ], [ %15, %22 ]
ret i32 %29
}