#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 */
int stub1(void const *, void const *, void *);
void *mybsearch(const void *key, void **array, size_t nmemb,
                int (*compar)(const void *, const void *, void *data),
                void *data, int *pos) {
  int l, u, idx;
  int comparison;
  l = 0;
  u = nmemb;
  while (l < u) {
    idx = (l + u) / 2;
    comparison = (*compar)(key, array[idx], data);
    if (comparison < 0)
      u = idx;
    else if (comparison > 0)
      l = idx + 1;
    else {
      *pos = idx;
      return array[idx];
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<mybsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
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
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    11bd <mybsearch+0xbd>
mov    -0x3c(%rbp),%eax
add    -0x40(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
movslq -0x44(%rbp),%rdx
mov    (%rcx,%rdx,8),%rsi
mov    -0x30(%rbp),%rdx
call   *%rax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    117d <mybsearch+0x7d>
mov    -0x44(%rbp),%eax
mov    %eax,-0x40(%rbp)
jmp    11b8 <mybsearch+0xb8>
cmpl   $0x0,-0x48(%rbp)
jle    1195 <mybsearch+0x95>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11b3 <mybsearch+0xb3>
mov    -0x44(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x8(%rbp)
jmp    11c5 <mybsearch+0xc5>
jmp    11b8 <mybsearch+0xb8>
jmp    112e <mybsearch+0x2e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mybsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %rcx,0x8(%rsp)
mov    %rdx,%rbp
xor    %eax,%eax
test   %ebp,%ebp
jle    117d <mybsearch+0x7d>
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %r9,(%rsp)
xor    %r15d,%r15d
jmp    1138 <mybsearch+0x38>
mov    %r12d,%ebp
cmp    %ebp,%r15d
jge    116e <mybsearch+0x6e>
lea    (%r15,%rbp,1),%eax
mov    %eax,%r12d
shr    $0x1f,%r12d
add    %eax,%r12d
sar    %r12d
movslq %r12d,%r13
mov    (%rbx,%r13,8),%rsi
mov    %r14,%rdi
mov    0x10(%rsp),%rdx
call   *0x8(%rsp)
test   %eax,%eax
js     1130 <mybsearch+0x30>
je     1172 <mybsearch+0x72>
add    $0x1,%r12d
mov    %r12d,%r15d
cmp    %ebp,%r15d
jl     1138 <mybsearch+0x38>
xor    %eax,%eax
jmp    117d <mybsearch+0x7d>
mov    (%rsp),%rax
mov    %r12d,(%rax)
mov    (%rbx,%r13,8),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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