#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 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ abort();
int record_cmp_artist(struct record *, struct record *);
int record_cmp_bpm(struct record *, struct record *);
size_t bin_search(struct record **base, size_t n, struct record *item, int sort,
                  bool *found) {
  int r;
  size_t mid;
  struct record *x;
  if (n == 0) {
    *found = false;
    return 0;
  }
  mid = n / 2;
  x = base[mid];
  switch (sort) {
  case 0:
    r = record_cmp_artist(item, x);
    break;
  case 1:
    r = record_cmp_bpm(item, x);
    break;
  case 2:
  default:
    abort();
  }
  if (r < 0)
    return bin_search(base, mid, item, sort, found);
  if (r > 0) {
    return mid + 1 + bin_search(base + mid + 1, n - mid - 1, item, sort, found);
  }
  *found = true;
  return mid;
}


x86 assembly (clang, -O0):
<bin_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117d <bin_search+0x3d>
mov    -0x30(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1298 <bin_search+0x158>
mov    -0x18(%rbp),%rax
shr    %rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4c(%rbp)
test   %eax,%eax
je     11cd <bin_search+0x8d>
jmp    11ab <bin_search+0x6b>
mov    -0x4c(%rbp),%eax
sub    $0x1,%eax
je     11e2 <bin_search+0xa2>
jmp    11bc <bin_search+0x7c>
mov    -0x4c(%rbp),%eax
sub    $0x2,%eax
je     11f7 <bin_search+0xb7>
jmp    11fc <bin_search+0xbc>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1050 <record_cmp_artist@plt>
mov    %eax,-0x34(%rbp)
jmp    1203 <bin_search+0xc3>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1040 <record_cmp_bpm@plt>
mov    %eax,-0x34(%rbp)
jmp    1203 <bin_search+0xc3>
jmp    11fc <bin_search+0xbc>
mov    $0x0,%al
call   1030 <abort@plt>
cmpl   $0x0,-0x34(%rbp)
jge    122e <bin_search+0xee>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
call   1060 <bin_search@plt>
mov    %rax,-0x8(%rbp)
jmp    1298 <bin_search+0x158>
cmpl   $0x0,-0x34(%rbp)
jle    1286 <bin_search+0x146>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
add    $0x8,%rdi
mov    -0x18(%rbp),%rsi
sub    -0x40(%rbp),%rsi
sub    $0x1,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
call   1060 <bin_search@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1298 <bin_search+0x158>
mov    -0x30(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%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    %r8,(%rsp)
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
xor    %r13d,%r13d
test   %r14,%r14
jne    1171 <bin_search+0x41>
jmp    11ac <bin_search+0x7c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1040 <record_cmp_bpm@plt>
test   %eax,%eax
jns    1190 <bin_search+0x60>
test   %r14,%r14
je     11ac <bin_search+0x7c>
mov    %r14,%rbx
shr    %r14
mov    (%r12,%r14,8),%rsi
cmp    $0x1,%ebp
je     1160 <bin_search+0x30>
test   %ebp,%ebp
jne    11d3 <bin_search+0xa3>
mov    %r15,%rdi
call   1050 <record_cmp_artist@plt>
test   %eax,%eax
js     116c <bin_search+0x3c>
je     11cc <bin_search+0x9c>
lea    (%r12,%r14,8),%r12
add    $0x8,%r12
add    %r14,%r13
add    $0x1,%r13
not    %r14
add    %rbx,%r14
test   %r14,%r14
jne    1171 <bin_search+0x41>
xor    %eax,%eax
xor    %r14d,%r14d
mov    (%rsp),%rcx
mov    %eax,(%rcx)
add    %r13,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%eax
jmp    11b1 <bin_search+0x81>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.record = type { i32 }
define i64 @bin_search(%struct.record** %0, i64 %1, %struct.record* %2, i32 %3, i32* %4) {
%6 = alloca i64
%7 = alloca %struct.record**
%8 = alloca i64
%9 = alloca %struct.record*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i64
%14 = alloca %struct.record*
store %struct.record** %0, %struct.record*** %7
store i64 %1, i64* %8
store %struct.record* %2, %struct.record** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%15 = load i64, i64* %8
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = load i32*, i32** %11
store i32 0, i32* %18
store i64 0, i64* %6
br label %70
19: ; preds = %5
%20 = load i64, i64* %8
%21 = udiv i64 %20, 2
store i64 %21, i64* %13
%22 = load %struct.record**, %struct.record*** %7
%23 = load i64, i64* %13
%24 = getelementptr inbounds %struct.record*, %struct.record** %22, i64 %23
%25 = load %struct.record*, %struct.record** %24
store %struct.record* %25, %struct.record** %14
%26 = load i32, i32* %10
switch i32 %26, label %36 [
i32 0, label %27
i32 1, label %31
i32 2, label %35
]
27: ; preds = %19
%28 = load %struct.record*, %struct.record** %9
%29 = load %struct.record*, %struct.record** %14
%30 = call i32 @record_cmp_artist(%struct.record* %28, %struct.record* %29)
store i32 %30, i32* %12
br label %38
31: ; preds = %19
%32 = load %struct.record*, %struct.record** %9
%33 = load %struct.record*, %struct.record** %14
%34 = call i32 @record_cmp_bpm(%struct.record* %32, %struct.record* %33)
store i32 %34, i32* %12
br label %38
35: ; preds = %19
br label %36
36: ; preds = %19, %35
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %31, %27
%39 = load i32, i32* %12
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load %struct.record**, %struct.record*** %7
%43 = load i64, i64* %13
%44 = load %struct.record*, %struct.record** %9
%45 = load i32, i32* %10
%46 = load i32*, i32** %11
%47 = call i64 @bin_search(%struct.record** %42, i64 %43, %struct.record* %44, i32 %45, i32* %46)
store i64 %47, i64* %6
br label %70
48: ; preds = %38
%49 = load i32, i32* %12
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %67
51: ; preds = %48
%52 = load i64, i64* %13
%53 = add i64 %52, 1
%54 = load %struct.record**, %struct.record*** %7
%55 = load i64, i64* %13
%56 = getelementptr inbounds %struct.record*, %struct.record** %54, i64 %55
%57 = getelementptr inbounds %struct.record*, %struct.record** %56, i64 1
%58 = load i64, i64* %8
%59 = load i64, i64* %13
%60 = sub i64 %58, %59
%61 = sub i64 %60, 1
%62 = load %struct.record*, %struct.record** %9
%63 = load i32, i32* %10
%64 = load i32*, i32** %11
%65 = call i64 @bin_search(%struct.record** %57, i64 %61, %struct.record* %62, i32 %63, i32* %64)
%66 = add i64 %53, %65
store i64 %66, i64* %6
br label %70
67: ; preds = %48
%68 = load i32*, i32** %11
store i32 1, i32* %68
%69 = load i64, i64* %13
store i64 %69, i64* %6
br label %70
70: ; preds = %67, %51, %41, %17
%71 = load i64, i64* %6
ret i64 %71
}

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