#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 data[], int len, int target) {
  int low = 0;
  int mid = 0;
  int high = len - 1;
  printf("search %d l=%d h=%d \n", target, low, high);
  while (low <= high) {
    mid = low + ((high - low) / 2);
    printf("l=%d, h=%d, m=%d \n", low, high, mid);
    if (data[mid] == target) {
      return mid;
    } else if (data[mid] > target) {
      high = mid - 1;
    } else {
      low = mid + 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,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xeb7(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     11e1 <bsearch+0xd1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x18(%rbp),%eax
jne    11ac <bsearch+0x9c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <bsearch+0xd8>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x18(%rbp),%eax
jle    11ce <bsearch+0xbe>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d7 <bsearch+0xc7>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11dc <bsearch+0xcc>
jmp    114e <bsearch+0x3e>
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    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%r14
lea    -0x1(%rbp),%r15d
lea    0xed2(%rip),%rdi
xor    %ebx,%ebx
mov    %edx,%esi
xor    %edx,%edx
mov    %r15d,%ecx
call   1030 <printf@plt>
mov    $0xffffffff,%eax
test   %ebp,%ebp
jle    119b <bsearch+0x8b>
lea    0xeca(%rip),%r13
jmp    115a <bsearch+0x4a>
xchg   %ax,%ax
add    $0x1,%ebp
mov    %ebp,%ebx
cmp    %ebx,%r15d
jl     1192 <bsearch+0x82>
mov    %r15d,%eax
sub    %ebx,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
add    %ebx,%ebp
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
call   1030 <printf@plt>
movslq %ebp,%rax
mov    (%r14,%rax,4),%eax
cmp    %r12d,%eax
je     1199 <bsearch+0x89>
jle    1150 <bsearch+0x40>
add    $0xffffffff,%ebp
mov    %ebp,%r15d
cmp    %ebx,%r15d
jge    115a <bsearch+0x4a>
mov    $0xffffffff,%eax
jmp    119b <bsearch+0x8b>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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