#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 gBinarySearch(void *array, size_t elemSize, int arrayLen, void *target,
                  int (*cmp)(const void *, const void *)) {
  void *currElem;
  int low = 0, mid, high = arrayLen - 1;
  while (low <= high) {
    mid = low + ((high - low) / 2);
    currElem = (char *)array + (mid * elemSize);
    if (cmp(target, currElem) == 0)
      return mid;
    else if (cmp(target, currElem) < 0)
      high = mid - 1;
    else
      low = mid + 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<gBinarySearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x44(%rbp),%eax
jg     11c3 <gBinarySearch+0xc3>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%eax
sub    -0x3c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x40(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    118b <gBinarySearch+0x8b>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <gBinarySearch+0xca>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    11b0 <gBinarySearch+0xb0>
mov    -0x40(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11b9 <gBinarySearch+0xb9>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11be <gBinarySearch+0xbe>
jmp    112b <gBinarySearch+0x2b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gBinarySearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,0x8(%rsp)
mov    $0xffffffff,%eax
test   %edx,%edx
jle    1199 <gBinarySearch+0x99>
mov    %r8,%r13
mov    %rcx,%rbp
mov    %edx,%r12d
add    $0xffffffff,%r12d
xor    %r15d,%r15d
jmp    114b <gBinarySearch+0x4b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebx
mov    %ebx,%r15d
cmp    %r15d,%r12d
jl     1190 <gBinarySearch+0x90>
mov    %r12d,%eax
sub    %r15d,%eax
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
add    %r15d,%ebx
movslq %ebx,%r14
imul   0x10(%rsp),%r14
add    0x8(%rsp),%r14
mov    %rbp,%rdi
mov    %r14,%rsi
call   *%r13
test   %eax,%eax
je     1197 <gBinarySearch+0x97>
mov    %rbp,%rdi
mov    %r14,%rsi
call   *%r13
test   %eax,%eax
jns    1140 <gBinarySearch+0x40>
add    $0xffffffff,%ebx
mov    %ebx,%r12d
cmp    %r15d,%r12d
jge    114b <gBinarySearch+0x4b>
mov    $0xffffffff,%eax
jmp    1199 <gBinarySearch+0x99>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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