#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, int);
int BinarySearch(int array[], int min_index, int max_index, int key) {
  int position = -1;
  int mid;
  while (min_index <= max_index) {
    mid = (min_index + max_index) / 2;
    printf("array[%d]= %d\n", mid, array[mid]);
    if (key == array[mid]) {
      position = mid;
      return position;
    } else if (key < array[mid])
      max_index = mid - 1;
    else
      min_index = mid + 1;
  }
  return position;
}


x86 assembly (clang, -O0):
<BinarySearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     11bd <BinarySearch+0xad>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xea2(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    1188 <BinarySearch+0x78>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c3 <BinarySearch+0xb3>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    11aa <BinarySearch+0x9a>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b3 <BinarySearch+0xa3>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b8 <BinarySearch+0xa8>
jmp    112c <BinarySearch+0x1c>
mov    -0x20(%rbp),%eax
mov    %eax,-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
cmp    %edx,%esi
jle    1126 <BinarySearch+0x16>
mov    $0xffffffff,%eax
jmp    118c <BinarySearch+0x7c>
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r12
jmp    114b <BinarySearch+0x3b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r13d
mov    %r13d,%ebx
cmp    %ebp,%ebx
jg     111f <BinarySearch+0xf>
lea    (%rbx,%rbp,1),%eax
mov    %eax,%r13d
shr    $0x1f,%r13d
add    %eax,%r13d
sar    %r13d
movslq %r13d,%r14
mov    (%r12,%r14,4),%edx
lea    0xe97(%rip),%rdi
mov    %r14d,%esi
call   1030 <printf@plt>
mov    (%r12,%r14,4),%eax
cmp    %r15d,%eax
je     1189 <BinarySearch+0x79>
jle    1140 <BinarySearch+0x30>
add    $0xffffffff,%r13d
mov    %r13d,%ebp
cmp    %ebp,%ebx
jle    114b <BinarySearch+0x3b>
jmp    111f <BinarySearch+0xf>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"array[%d]= %d\0A\00"
define i32 @BinarySearch(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
%11 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 -1, i32* %10
br label %12
12: ; preds = %53, %4
%13 = load i32, i32* %7
%14 = load i32, i32* %8
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %54
16: ; preds = %12
%17 = load i32, i32* %7
%18 = load i32, i32* %8
%19 = add nsw i32 %17, %18
%20 = sdiv i32 %19, 2
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = load i32*, i32** %6
%23 = load i32, i32* %11
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
%26 = load i32, i32* %25
%27 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %26)
%28 = load i32, i32* %9
%29 = load i32*, i32** %6
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = icmp eq i32 %28, %33
br i1 %34, label %35, label %38
35: ; preds = %16
%36 = load i32, i32* %11
store i32 %36, i32* %10
%37 = load i32, i32* %10
store i32 %37, i32* %5
br label %56
38: ; preds = %16
%39 = load i32, i32* %9
%40 = load i32*, i32** %6
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
%44 = load i32, i32* %43
%45 = icmp slt i32 %39, %44
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = load i32, i32* %11
%48 = sub nsw i32 %47, 1
store i32 %48, i32* %8
br label %52
49: ; preds = %38
%50 = load i32, i32* %11
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %52
52: ; preds = %49, %46
br label %53
53: ; preds = %52
br label %12
54: ; preds = %12
%55 = load i32, i32* %10
store i32 %55, i32* %5
br label %56
56: ; preds = %54, %35
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
@.str = [15 x i8] c"array[%d]= %d\0A\00"
define i32 @BinarySearch(i32* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %1, %2
br i1 %5, label %24, label %6
6: ; preds = %4, %17
%7 = phi i32 [ %22, %17 ], [ %1, %4 ]
%8 = phi i32 [ %21, %17 ], [ %2, %4 ]
%9 = add nsw i32 %7, %8
%10 = sdiv i32 %9, 2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = load i32, i32* %12
%14 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %13)
%15 = load i32, i32* %12
%16 = icmp eq i32 %15, %3
br i1 %16, label %24, label %17
17: ; preds = %6
%18 = icmp sgt i32 %15, %3
%19 = add nsw i32 %10, -1
%20 = add nsw i32 %10, 1
%21 = select i1 %18, i32 %19, i32 %8
%22 = select i1 %18, i32 %7, i32 %20
%23 = icmp sgt i32 %22, %21
br i1 %23, label %24, label %6
24: ; preds = %6, %17, %4
%25 = phi i32 [ -1, %4 ], [ -1, %17 ], [ %10, %6 ]
ret i32 %25
}