int binary_search(int *array, int data, int length) {
  int low = 0;
  int high = low + length;
  int mid = 0;
  if (length == 0)
    return (~0);
  printf("%s:"
         "start: low %d mid %d high %d arr %d\n",
         __FUNCTION__, low, mid, high, array[mid]);
  while (low < high) {
    printf("%s:"
           "inside: low %d mid %d high %d arr %d\n",
           __FUNCTION__, low, mid, high, array[mid]);
    mid = low + (high - low) / 2;
    if (data == array[mid])
      break;
    if (data < array[mid]) {
      high = mid;
    } else {
      low = mid + 1;
    }
  }
  printf("%s:"
         "end: low %d mid %d high %d arr %d data %d\n",
         __FUNCTION__, low, mid, high, array[mid], data);
  if (array[mid] == data) {
    printf("%s:"
           "bsearch found %d at %d\n",
           __FUNCTION__, data, high);
    return mid;
  } else if (data > array[mid])
    return ~(mid + 1);
  else
    return ~mid;
}


x86 assembly (clang, -O0):
<binary_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    114f <binary_search+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ae <binary_search+0x19e>
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rsi
mov    (%rax,%rsi,4),%r9d
lea    0xe94(%rip),%rdi
lea    0xeb5(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1215 <binary_search+0x105>
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rsi
mov    (%rax,%rsi,4),%r9d
lea    0xe93(%rip),%rdi
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    11e8 <binary_search+0xd8>
jmp    1215 <binary_search+0x105>
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1207 <binary_search+0xf7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1210 <binary_search+0x100>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117a <binary_search+0x6a>
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rsi
mov    (%rax,%rsi,4),%r9d
mov    -0x14(%rbp),%eax
lea    0xe2a(%rip),%rdi
lea    0xdec(%rip),%rsi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jne    1280 <binary_search+0x170>
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe26(%rip),%rdi
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ae <binary_search+0x19e>
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    12a5 <binary_search+0x195>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    12ae <binary_search+0x19e>
mov    -0x24(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     11b0 <binary_search+0xa0>
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
mov    (%rdi),%r9d
lea    0xecb(%rip),%rdi
lea    0xeec(%rip),%rsi
xor    %r12d,%r12d
xor    %edx,%edx
xor    %ecx,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r15),%r9d
test   %ebx,%ebx
jle    11b7 <binary_search+0xa7>
xor    %ebp,%ebp
xor    %r12d,%r12d
jmp    1168 <binary_search+0x58>
nopl   0x0(%rax,%rax,1)
mov    %r13d,%ebx
cmp    %r12d,%r13d
jle    11c1 <binary_search+0xb1>
lea    0xec7(%rip),%rdi
lea    0xeb2(%rip),%rsi
mov    %r12d,%edx
mov    %ebp,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%eax
sub    %r12d,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
add    %r12d,%ebp
movslq %ebp,%rax
mov    (%r15,%rax,4),%r9d
cmp    %r14d,%r9d
je     11be <binary_search+0xae>
mov    %ebp,%r13d
jg     1160 <binary_search+0x50>
lea    0x1(%rbp),%r12d
mov    %ebx,%r13d
jmp    1160 <binary_search+0x50>
mov    $0xffffffff,%ebp
jmp    1217 <binary_search+0x107>
mov    %ebx,%r13d
xor    %ebp,%ebp
jmp    11c1 <binary_search+0xb1>
mov    %ebx,%r13d
movslq %ebp,%rbx
mov    %r14d,(%rsp)
lea    0xe90(%rip),%rdi
lea    0xe52(%rip),%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
mov    %r13d,%r8d
xor    %eax,%eax
call   1030 <printf@plt>
cmp    %r14d,(%r15,%rbx,4)
jne    1208 <binary_search+0xf8>
lea    0xe9b(%rip),%rdi
lea    0xe2f(%rip),%rsi
mov    %r14d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1217 <binary_search+0x107>
jge    1215 <binary_search+0x105>
mov    $0xfffffffe,%eax
sub    %ebp,%eax
mov    %eax,%ebp
jmp    1217 <binary_search+0x107>
not    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"%s:start: low %d mid %d high %d arr %d\0A\00"
@__FUNCTION__.binary_search = [14 x i8] c"binary_search\00"
@.str.1 = [41 x i8] c"%s:inside: low %d mid %d high %d arr %d\0A\00"
@.str.2 = [46 x i8] c"%s:end: low %d mid %d high %d arr %d data %d\0A\00"
@.str.3 = [27 x i8] c"%s:bsearch found %d at %d\0A\00"
define i32 @binary_search(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
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = add nsw i32 %11, %12
store i32 %13, i32* %9
store i32 0, i32* %10
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %107
17: ; preds = %3
%18 = load i32, i32* %8
%19 = load i32, i32* %10
%20 = load i32, i32* %9
%21 = load i32*, i32** %5
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 %18, i32 %19, i32 %20, i32 %25)
br label %27
27: ; preds = %68, %17
%28 = load i32, i32* %8
%29 = load i32, i32* %9
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %69
31: ; preds = %27
%32 = load i32, i32* %8
%33 = load i32, i32* %10
%34 = load i32, i32* %9
%35 = load i32*, i32** %5
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 %32, i32 %33, i32 %34, i32 %39)
%41 = load i32, i32* %8
%42 = load i32, i32* %9
%43 = load i32, i32* %8
%44 = sub nsw i32 %42, %43
%45 = sdiv i32 %44, 2
%46 = add nsw i32 %41, %45
store i32 %46, i32* %10
%47 = load i32, i32* %6
%48 = load i32*, i32** %5
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %48, i64 %50
%52 = load i32, i32* %51
%53 = icmp eq i32 %47, %52
br i1 %53, label %54, label %55
54: ; preds = %31
br label %69
55: ; preds = %31
%56 = load i32, i32* %6
%57 = load i32*, i32** %5
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
%61 = load i32, i32* %60
%62 = icmp slt i32 %56, %61
br i1 %62, label %63, label %65
63: ; preds = %55
%64 = load i32, i32* %10
store i32 %64, i32* %9
br label %68
65: ; preds = %55
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %68
68: ; preds = %65, %63
br label %27
69: ; preds = %54, %27
%70 = load i32, i32* %8
%71 = load i32, i32* %10
%72 = load i32, i32* %9
%73 = load i32*, i32** %5
%74 = load i32, i32* %10
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32, i32* %73, i64 %75
%77 = load i32, i32* %76
%78 = load i32, i32* %6
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 %70, i32 %71, i32 %72, i32 %77, i32 %78)
%80 = load i32*, i32** %5
%81 = load i32, i32* %10
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %80, i64 %82
%84 = load i32, i32* %83
%85 = load i32, i32* %6
%86 = icmp eq i32 %84, %85
br i1 %86, label %87, label %92
87: ; preds = %69
%88 = load i32, i32* %6
%89 = load i32, i32* %9
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 %88, i32 %89)
%91 = load i32, i32* %10
store i32 %91, i32* %4
br label %107
92: ; preds = %69
%93 = load i32, i32* %6
%94 = load i32*, i32** %5
%95 = load i32, i32* %10
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i32, i32* %94, i64 %96
%98 = load i32, i32* %97
%99 = icmp sgt i32 %93, %98
br i1 %99, label %100, label %104
100: ; preds = %92
%101 = load i32, i32* %10
%102 = add nsw i32 %101, 1
%103 = xor i32 %102, -1
store i32 %103, i32* %4
br label %107
104: ; preds = %92
%105 = load i32, i32* %10
%106 = xor i32 %105, -1
store i32 %106, i32* %4
br label %107
107: ; preds = %104, %100, %87, %16
%108 = load i32, i32* %4
ret i32 %108
}

LLVM IR (-O2):
@.str = [40 x i8] c"%s:start: low %d mid %d high %d arr %d\0A\00"
@__FUNCTION__.binary_search = [14 x i8] c"binary_search\00"
@.str.1 = [41 x i8] c"%s:inside: low %d mid %d high %d arr %d\0A\00"
@.str.2 = [46 x i8] c"%s:end: low %d mid %d high %d arr %d data %d\0A\00"
@.str.3 = [27 x i8] c"%s:bsearch found %d at %d\0A\00"
define i32 @binary_search(i32* nocapture %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %47, label %5
5: ; preds = %3
%6 = load i32, i32* %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 0, i32 0, i32 %2, i32 %6)
%8 = icmp sgt i32 %2, 0
%9 = load i32, i32* %0
br i1 %8, label %10, label %29
10: ; preds = %5, %23
%11 = phi i32 [ %21, %23 ], [ %9, %5 ]
%12 = phi i32 [ %18, %23 ], [ 0, %5 ]
%13 = phi i32 [ %27, %23 ], [ %2, %5 ]
%14 = phi i32 [ %26, %23 ], [ 0, %5 ]
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 %14, i32 %12, i32 %13, i32 %11)
%16 = sub nsw i32 %13, %14
%17 = sdiv i32 %16, 2
%18 = add nsw i32 %17, %14
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %0, i64 %19
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, %1
br i1 %22, label %29, label %23
23: ; preds = %10
%24 = icmp sgt i32 %21, %1
%25 = add nsw i32 %18, 1
%26 = select i1 %24, i32 %14, i32 %25
%27 = select i1 %24, i32 %18, i32 %13
%28 = icmp sgt i32 %27, %26
br i1 %28, label %10, label %29
29: ; preds = %23, %10, %5
%30 = phi i32 [ %9, %5 ], [ %21, %10 ], [ %21, %23 ]
%31 = phi i32 [ 0, %5 ], [ %26, %23 ], [ %14, %10 ]
%32 = phi i32 [ %2, %5 ], [ %27, %23 ], [ %13, %10 ]
%33 = phi i32 [ 0, %5 ], [ %18, %10 ], [ %18, %23 ]
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %0, i64 %34
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 %31, i32 %33, i32 %32, i32 %30, i32 %1)
%37 = load i32, i32* %35
%38 = icmp eq i32 %37, %1
br i1 %38, label %39, label %41
39: ; preds = %29
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0), i32 %1, i32 %32)
br label %47
41: ; preds = %29
%42 = icmp slt i32 %37, %1
br i1 %42, label %43, label %45
43: ; preds = %41
%44 = sub i32 -2, %33
br label %47
45: ; preds = %41
%46 = xor i32 %33, -1
br label %47
47: ; preds = %3, %45, %43, %39
%48 = phi i32 [ %33, %39 ], [ %44, %43 ], [ %46, %45 ], [ -1, %3 ]
ret i32 %48
}