int binary_search(int *array, int size, int element) {
  if (!array) {
    printf("You passed NULL into function: %s()\n", __FUNCTION__);
    return -1;
  }
  int low = 0;
  int mid = 0;
  int high = 0;
  for (low = 0, high = size - 1; low <= high;) {
    mid = (low + high) / 2;
    if (array[mid] < element) {
      low = mid + 1;
    } else if (array[mid] > element) {
      high = mid - 1;
    } else {
      return mid;
    }
  }
  return -1;
}


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)
cmpq   $0x0,-0x10(%rbp)
jne    114e <binary_search+0x3e>
lea    0xecc(%rip),%rdi
lea    0xeea(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <binary_search+0xe0>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jg     11e9 <binary_search+0xd9>
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x18(%rbp),%eax
jge    11b2 <binary_search+0xa2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11e4 <binary_search+0xd4>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x18(%rbp),%eax
jle    11d4 <binary_search+0xc4>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11df <binary_search+0xcf>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <binary_search+0xe0>
jmp    11e4 <binary_search+0xd4>
jmp    1173 <binary_search+0x63>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search>:
push   %rax
test   %rdi,%rdi
je     1165 <binary_search+0x55>
mov    $0xffffffff,%eax
test   %esi,%esi
jle    1163 <binary_search+0x53>
add    $0xffffffff,%esi
xor    %r8d,%r8d
jmp    113c <binary_search+0x2c>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r10d
mov    %r10d,%r8d
cmp    %esi,%r8d
jg     1163 <binary_search+0x53>
lea    (%rsi,%r8,1),%r9d
mov    %r9d,%r10d
shr    $0x1f,%r10d
add    %r9d,%r10d
sar    %r10d
movslq %r10d,%rcx
cmp    %edx,(%rdi,%rcx,4)
jl     1130 <binary_search+0x20>
jle    1181 <binary_search+0x71>
add    $0xffffffff,%r10d
mov    %r10d,%esi
cmp    %esi,%r8d
jle    113c <binary_search+0x2c>
pop    %rcx
ret
lea    0xe94(%rip),%rdi
lea    0xeb2(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
mov    %r10d,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@.str = [37 x i8] c"You passed NULL into function: %s()\0A\00"
@__FUNCTION__.binary_search = [14 x i8] c"binary_search\00"
define i32 @binary_search(i32* %0, i32 %1, i32 %2) {
%4 = icmp eq i32* %0, null
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.binary_search, i64 0, i64 0))
br label %30
7: ; preds = %3
%8 = icmp slt i32 %1, 1
br i1 %8, label %30, label %9
9: ; preds = %7
%10 = add nsw i32 %1, -1
br label %11
11: ; preds = %9, %26
%12 = phi i32 [ %28, %26 ], [ %10, %9 ]
%13 = phi i32 [ %27, %26 ], [ 0, %9 ]
%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 slt i32 %18, %2
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = add nsw i32 %15, 1
br label %26
22: ; preds = %11
%23 = icmp sgt i32 %18, %2
br i1 %23, label %24, label %30
24: ; preds = %22
%25 = add nsw i32 %15, -1
br label %26
26: ; preds = %24, %20
%27 = phi i32 [ %21, %20 ], [ %13, %24 ]
%28 = phi i32 [ %12, %20 ], [ %25, %24 ]
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %11
30: ; preds = %26, %22, %7, %5
%31 = phi i32 [ -1, %5 ], [ -1, %7 ], [ -1, %26 ], [ %15, %22 ]
ret i32 %31
}