#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 */
struct record_t {
  int dummy;
};

/* Variables and functions */
int record_cmp(struct record_t *, struct record_t *);
size_t bin_search(struct record_t **base, size_t n, struct record_t *item,
                  bool *found) {
  int r;
  size_t mid;
  if (n == 0) {
    *found = false;
    return 0;
  }
  mid = n / 2;
  r = record_cmp(item, base[mid]);
  if (r < 0)
    return bin_search(base, mid, item, found);
  if (r > 0)
    return mid + 1 + bin_search(base + mid + 1, n - mid - 1, item, found);
  *found = true;
  return mid;
}


x86 assembly (clang, -O0):
<bin_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115a <bin_search+0x3a>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    120d <bin_search+0xed>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <record_cmp@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11a6 <bin_search+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <bin_search@plt>
mov    %rax,-0x8(%rbp)
jmp    120d <bin_search+0xed>
cmpl   $0x0,-0x2c(%rbp)
jle    11fb <bin_search+0xdb>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
add    $0x8,%rdi
mov    -0x18(%rbp),%rsi
sub    -0x38(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <bin_search@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    120d <bin_search+0xed>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin_search>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
test   %rbx,%rbx
je     1164 <bin_search+0x54>
mov    %rbx,%rbp
shr    %rbx
mov    (%r12,%rbx,8),%rsi
mov    %r15,%rdi
call   1030 <record_cmp@plt>
test   %eax,%eax
js     1130 <bin_search+0x20>
je     1180 <bin_search+0x70>
lea    (%r12,%rbx,8),%r12
add    $0x8,%r12
add    %rbx,%r13
add    $0x1,%r13
not    %rbx
add    %rbp,%rbx
jmp    1130 <bin_search+0x20>
xor    %eax,%eax
xor    %ebx,%ebx
mov    %eax,(%r14)
add    %r13,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%eax
jmp    1168 <bin_search+0x58>

LLVM IR (-O0):
%struct.record_t = type { i32 }
define i64 @bin_search(%struct.record_t** %0, i64 %1, %struct.record_t* %2, i32* %3) {
%5 = alloca i64
%6 = alloca %struct.record_t**
%7 = alloca i64
%8 = alloca %struct.record_t*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
store %struct.record_t** %0, %struct.record_t*** %6
store i64 %1, i64* %7
store %struct.record_t* %2, %struct.record_t** %8
store i32* %3, i32** %9
%12 = load i64, i64* %7
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = load i32*, i32** %9
store i32 0, i32* %15
store i64 0, i64* %5
br label %54
16: ; preds = %4
%17 = load i64, i64* %7
%18 = udiv i64 %17, 2
store i64 %18, i64* %11
%19 = load %struct.record_t*, %struct.record_t** %8
%20 = load %struct.record_t**, %struct.record_t*** %6
%21 = load i64, i64* %11
%22 = getelementptr inbounds %struct.record_t*, %struct.record_t** %20, i64 %21
%23 = load %struct.record_t*, %struct.record_t** %22
%24 = call i32 @record_cmp(%struct.record_t* %19, %struct.record_t* %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %16
%28 = load %struct.record_t**, %struct.record_t*** %6
%29 = load i64, i64* %11
%30 = load %struct.record_t*, %struct.record_t** %8
%31 = load i32*, i32** %9
%32 = call i64 @bin_search(%struct.record_t** %28, i64 %29, %struct.record_t* %30, i32* %31)
store i64 %32, i64* %5
br label %54
33: ; preds = %16
%34 = load i32, i32* %10
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %51
36: ; preds = %33
%37 = load i64, i64* %11
%38 = add i64 %37, 1
%39 = load %struct.record_t**, %struct.record_t*** %6
%40 = load i64, i64* %11
%41 = getelementptr inbounds %struct.record_t*, %struct.record_t** %39, i64 %40
%42 = getelementptr inbounds %struct.record_t*, %struct.record_t** %41, i64 1
%43 = load i64, i64* %7
%44 = load i64, i64* %11
%45 = sub i64 %43, %44
%46 = sub i64 %45, 1
%47 = load %struct.record_t*, %struct.record_t** %8
%48 = load i32*, i32** %9
%49 = call i64 @bin_search(%struct.record_t** %42, i64 %46, %struct.record_t* %47, i32* %48)
%50 = add i64 %38, %49
store i64 %50, i64* %5
br label %54
51: ; preds = %33
%52 = load i32*, i32** %9
store i32 1, i32* %52
%53 = load i64, i64* %11
store i64 %53, i64* %5
br label %54
54: ; preds = %51, %36, %27, %14
%55 = load i64, i64* %5
ret i64 %55
}

LLVM IR (-O2):
%struct.record_t = type { i32 }
define i64 @bin_search(%struct.record_t** %0, i64 %1, %struct.record_t* %2, i32* %3) {
br label %5
5: ; preds = %20, %4
%6 = phi i64 [ %26, %20 ], [ 0, %4 ]
%7 = phi %struct.record_t** [ %22, %20 ], [ %0, %4 ]
%8 = phi i64 [ %24, %20 ], [ %1, %4 ]
br label %9
9: ; preds = %5, %12
%10 = phi i64 [ %13, %12 ], [ %8, %5 ]
%11 = icmp eq i64 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %9
%13 = lshr i64 %10, 1
%14 = getelementptr inbounds %struct.record_t*, %struct.record_t** %7, i64 %13
%15 = load %struct.record_t*, %struct.record_t** %14
%16 = tail call i32 @record_cmp(%struct.record_t* %2, %struct.record_t* %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %9, label %18
18: ; preds = %12
%19 = icmp eq i32 %16, 0
br i1 %19, label %27, label %20
20: ; preds = %18
%21 = getelementptr inbounds %struct.record_t*, %struct.record_t** %7, i64 1
%22 = getelementptr inbounds %struct.record_t*, %struct.record_t** %21, i64 %13
%23 = xor i64 %13, -1
%24 = add i64 %10, %23
%25 = add i64 %6, 1
%26 = add i64 %25, %13
br label %5
27: ; preds = %18, %9
%28 = phi i32 [ 0, %9 ], [ 1, %18 ]
%29 = phi i64 [ 0, %9 ], [ %13, %18 ]
store i32 %28, i32* %3
%30 = add i64 %29, %6
ret i64 %30
}