#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);
int bsearch(int *arr, int size, int item) {
  int middle_item;
  int start = 0, end = size - 1;
  int middle;
  while (start < end) {
    middle = (start + end) / 2;
    printf("Start %d Middle %d End %d\n", start, middle, end);
    middle_item = *(arr + middle);
    if (middle_item == item)
      return middle;
    else {
      if (middle_item > item) {
        end = middle - 1;
      } else
        start = middle + 1;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<bsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11b6 <bsearch+0xa6>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xea1(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1189 <bsearch+0x79>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <bsearch+0xad>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11a3 <bsearch+0x93>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ac <bsearch+0x9c>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11b1 <bsearch+0xa1>
jmp    1132 <bsearch+0x22>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
cmp    $0x2,%esi
jl     118e <bsearch+0x7e>
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r13
add    $0xffffffff,%ebp
xor    %ebx,%ebx
lea    0xec7(%rip),%r12
jmp    114b <bsearch+0x3b>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14d
mov    %r14d,%ebx
cmp    %ebp,%ebx
jge    1184 <bsearch+0x74>
lea    (%rbx,%rbp,1),%eax
mov    %eax,%r14d
shr    $0x1f,%r14d
add    %eax,%r14d
sar    %r14d
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1030 <printf@plt>
movslq %r14d,%rax
mov    0x0(%r13,%rax,4),%eax
cmp    %r15d,%eax
je     118b <bsearch+0x7b>
jle    1140 <bsearch+0x30>
add    $0xffffffff,%r14d
mov    %r14d,%ebp
cmp    %ebp,%ebx
jl     114b <bsearch+0x3b>
mov    $0xffffffff,%eax
jmp    118e <bsearch+0x7e>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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