#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 */
void *
freecell_solver_bsearch(void *key, void *void_array, size_t len, size_t width,
                        int (*compare)(const void *, const void *, void *),
                        void *context, int *found) {
  int low = 0;
  int high = len - 1;
  int mid;
  int result;
  char *array = void_array;
  while (low <= high) {
    mid = ((low + high) >> 1);
    result = compare(key, (void *)(array + mid * width), context);
    if (result < 0) {
      high = mid - 1;
    } else if (result > 0) {
      low = mid + 1;
    } else {
      *found = 1;
      return (void *)(array + mid * width);
    }
  }
  *found = 0;
  return ((void *)(array + (high + 1) * width));
}


x86 assembly (clang, -O0):
<freecell_solver_bsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jg     11d4 <freecell_solver_bsearch+0xd4>
mov    -0x3c(%rbp),%eax
add    -0x40(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x50(%rbp),%rsi
movslq -0x44(%rbp),%rcx
imul   -0x28(%rbp),%rcx
add    %rcx,%rsi
mov    -0x38(%rbp),%rdx
call   *%rax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    118f <freecell_solver_bsearch+0x8f>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11cf <freecell_solver_bsearch+0xcf>
cmpl   $0x0,-0x48(%rbp)
jle    11a7 <freecell_solver_bsearch+0xa7>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ca <freecell_solver_bsearch+0xca>
mov    0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x50(%rbp),%rax
movslq -0x44(%rbp),%rcx
imul   -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11f7 <freecell_solver_bsearch+0xf7>
jmp    11cf <freecell_solver_bsearch+0xcf>
jmp    113e <freecell_solver_bsearch+0x3e>
mov    0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freecell_solver_bsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,0x8(%rsp)
mov    %rcx,(%rsp)
mov    %rdx,%rbx
mov    %rsi,%r14
add    $0xffffffff,%ebx
js     116f <freecell_solver_bsearch+0x6f>
mov    %rdi,%rbp
xor    %r12d,%r12d
jmp    113c <freecell_solver_bsearch+0x3c>
nop
add    $0xffffffff,%r13d
mov    %r13d,%ebx
cmp    %ebx,%r12d
jg     116f <freecell_solver_bsearch+0x6f>
lea    (%r12,%rbx,1),%r13d
sar    %r13d
movslq %r13d,%r15
imul   (%rsp),%r15
add    %r14,%r15
mov    %rbp,%rdi
mov    %r15,%rsi
mov    0x10(%rsp),%rdx
call   *0x8(%rsp)
test   %eax,%eax
js     1130 <freecell_solver_bsearch+0x30>
je     118d <freecell_solver_bsearch+0x8d>
add    $0x1,%r13d
mov    %r13d,%r12d
cmp    %ebx,%r12d
jle    113c <freecell_solver_bsearch+0x3c>
mov    0x50(%rsp),%rax
movl   $0x0,(%rax)
add    $0x1,%ebx
movslq %ebx,%rax
imul   (%rsp),%rax
add    %rax,%r14
mov    %r14,%r15
jmp    1198 <freecell_solver_bsearch+0x98>
mov    0x50(%rsp),%rax
movl   $0x1,(%rax)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @freecell_solver_bsearch(i8* %0, i8* %1, i64 %2, i64 %3, i32 (i8*, i8*, i8*)* %4, i8* %5, i32* %6) {
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32 (i8*, i8*, i8*)*
%14 = alloca i8*
%15 = alloca i32*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i8*
store i8* %0, i8** %9
store i8* %1, i8** %10
store i64 %2, i64* %11
store i64 %3, i64* %12
store i32 (i8*, i8*, i8*)* %4, i32 (i8*, i8*, i8*)** %13
store i8* %5, i8** %14
store i32* %6, i32** %15
store i32 0, i32* %16
%21 = load i64, i64* %11
%22 = sub i64 %21, 1
%23 = trunc i64 %22 to i32
store i32 %23, i32* %17
%24 = load i8*, i8** %10
store i8* %24, i8** %20
br label %25
25: ; preds = %64, %7
%26 = load i32, i32* %16
%27 = load i32, i32* %17
%28 = icmp sle i32 %26, %27
br i1 %28, label %29, label %65
29: ; preds = %25
%30 = load i32, i32* %16
%31 = load i32, i32* %17
%32 = add nsw i32 %30, %31
%33 = ashr i32 %32, 1
store i32 %33, i32* %18
%34 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %13
%35 = load i8*, i8** %9
%36 = load i8*, i8** %20
%37 = load i32, i32* %18
%38 = sext i32 %37 to i64
%39 = load i64, i64* %12
%40 = mul i64 %38, %39
%41 = getelementptr inbounds i8, i8* %36, i64 %40
%42 = load i8*, i8** %14
%43 = call i32 %34(i8* %35, i8* %41, i8* %42)
store i32 %43, i32* %19
%44 = load i32, i32* %19
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %29
%47 = load i32, i32* %18
%48 = sub nsw i32 %47, 1
store i32 %48, i32* %17
br label %64
49: ; preds = %29
%50 = load i32, i32* %19
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = load i32, i32* %18
%54 = add nsw i32 %53, 1
store i32 %54, i32* %16
br label %63
55: ; preds = %49
%56 = load i32*, i32** %15
store i32 1, i32* %56
%57 = load i8*, i8** %20
%58 = load i32, i32* %18
%59 = sext i32 %58 to i64
%60 = load i64, i64* %12
%61 = mul i64 %59, %60
%62 = getelementptr inbounds i8, i8* %57, i64 %61
store i8* %62, i8** %8
br label %74
63: ; preds = %52
br label %64
64: ; preds = %63, %46
br label %25
65: ; preds = %25
%66 = load i32*, i32** %15
store i32 0, i32* %66
%67 = load i8*, i8** %20
%68 = load i32, i32* %17
%69 = add nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = load i64, i64* %12
%72 = mul i64 %70, %71
%73 = getelementptr inbounds i8, i8* %67, i64 %72
store i8* %73, i8** %8
br label %74
74: ; preds = %65, %55
%75 = load i8*, i8** %8
ret i8* %75
}

LLVM IR (-O2):
define i8* @freecell_solver_bsearch(i8* %0, i8* %1, i64 %2, i64 %3, i32 (i8*, i8*, i8*)* nocapture %4, i8* %5, i32* nocapture writeonly %6) {
%8 = trunc i64 %2 to i32
%9 = add i32 %8, -1
%10 = icmp slt i32 %9, 0
br i1 %10, label %32, label %11
11: ; preds = %7, %28
%12 = phi i32 [ %30, %28 ], [ 0, %7 ]
%13 = phi i32 [ %29, %28 ], [ %9, %7 ]
%14 = add nsw i32 %12, %13
%15 = ashr i32 %14, 1
%16 = sext i32 %15 to i64
%17 = mul i64 %16, %3
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = tail call i32 %4(i8* %0, i8* %18, i8* %5)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = add nsw i32 %15, -1
br label %28
23: ; preds = %11
%24 = icmp eq i32 %19, 0
br i1 %24, label %27, label %25
25: ; preds = %23
%26 = add nsw i32 %15, 1
br label %28
27: ; preds = %23
store i32 1, i32* %6
br label %38
28: ; preds = %25, %21
%29 = phi i32 [ %22, %21 ], [ %13, %25 ]
%30 = phi i32 [ %12, %21 ], [ %26, %25 ]
%31 = icmp sgt i32 %30, %29
br i1 %31, label %32, label %11
32: ; preds = %28, %7
%33 = phi i32 [ %9, %7 ], [ %29, %28 ]
store i32 0, i32* %6
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = mul i64 %35, %3
%37 = getelementptr inbounds i8, i8* %1, i64 %36
br label %38
38: ; preds = %32, %27
%39 = phi i8* [ %18, %27 ], [ %37, %32 ]
ret i8* %39
}