#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 len, void *key,
                  int (*cmp)(void *a, void *b)) {
  int low = 0, high = len - 1, currentIndex;
  while (low <= high) {
    currentIndex = (high + low) / 2;
    void *currentElm = array + (currentIndex + elemSize);
    if (cmp(key, currentElm) == 0)
      return currentIndex;
    else if (cmp(key, currentElm) < 0)
      high = currentIndex - 1;
    else
      low = currentIndex + 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,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jg     11b5 <gbinarySearch+0xb5>
mov    -0x38(%rbp),%eax
add    -0x34(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
add    -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    117d <gbinarySearch+0x7d>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <gbinarySearch+0xbc>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    11a2 <gbinarySearch+0xa2>
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11ab <gbinarySearch+0xab>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11b0 <gbinarySearch+0xb0>
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    119a <gbinarySearch+0x9a>
mov    %r8,%r13
mov    %rcx,%rbp
mov    %edx,%ebx
add    $0xffffffff,%ebx
xor    %r15d,%r15d
jmp    114c <gbinarySearch+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12d
mov    %r12d,%r15d
cmp    %ebx,%r15d
jg     1190 <gbinarySearch+0x90>
lea    (%r15,%rbx,1),%eax
mov    %eax,%r12d
shr    $0x1f,%r12d
add    %eax,%r12d
sar    %r12d
movslq %r12d,%r14
add    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,%r12d
mov    %r12d,%ebx
cmp    %ebx,%r15d
jle    114c <gbinarySearch+0x4c>
mov    $0xffffffff,%eax
jmp    119a <gbinarySearch+0x9a>
mov    %r12d,%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 i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
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* %12
%16 = load i32, i32* %9
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %13
br label %18
18: ; preds = %53, %5
%19 = load i32, i32* %12
%20 = load i32, i32* %13
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %54
22: ; preds = %18
%23 = load i32, i32* %13
%24 = load i32, i32* %12
%25 = add nsw i32 %23, %24
%26 = sdiv i32 %25, 2
store i32 %26, i32* %14
%27 = load i8*, i8** %7
%28 = load i32, i32* %14
%29 = sext i32 %28 to i64
%30 = load i64, i64* %8
%31 = add i64 %29, %30
%32 = getelementptr i8, i8* %27, i64 %31
store i8* %32, i8** %15
%33 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %11
%34 = load i8*, i8** %10
%35 = load i8*, i8** %15
%36 = call i32 %33(i8* %34, i8* %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %22
%39 = load i32, i32* %14
store i32 %39, i32* %6
br label %55
40: ; preds = %22
%41 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %11
%42 = load i8*, i8** %10
%43 = load i8*, i8** %15
%44 = call i32 %41(i8* %42, i8* %43)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* %14
%48 = sub nsw i32 %47, 1
store i32 %48, i32* %13
br label %52
49: ; preds = %40
%50 = load i32, i32* %14
%51 = add nsw i32 %50, 1
store i32 %51, i32* %12
br label %52
52: ; preds = %49, %46
br label %53
53: ; preds = %52
br label %18
54: ; preds = %18
store i32 -1, i32* %6
br label %55
55: ; preds = %54, %38
%56 = load i32, i32* %6
ret i32 %56
}

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 %27, label %7
7: ; preds = %5
%8 = add nsw i32 %2, -1
br label %9
9: ; preds = %7, %19
%10 = phi i32 [ %25, %19 ], [ 0, %7 ]
%11 = phi i32 [ %24, %19 ], [ %8, %7 ]
%12 = add nsw i32 %10, %11
%13 = sdiv i32 %12, 2
%14 = sext i32 %13 to i64
%15 = add i64 %14, %1
%16 = getelementptr i8, i8* %0, i64 %15
%17 = tail call i32 %4(i8* %3, i8* %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %9
%20 = tail call i32 %4(i8* %3, i8* %16)
%21 = icmp slt i32 %20, 0
%22 = add nsw i32 %13, -1
%23 = add nsw i32 %13, 1
%24 = select i1 %21, i32 %22, i32 %11
%25 = select i1 %21, i32 %10, i32 %23
%26 = icmp sgt i32 %25, %24
br i1 %26, label %27, label %9
27: ; preds = %19, %9, %5
%28 = phi i32 [ -1, %5 ], [ %13, %9 ], [ -1, %19 ]
ret i32 %28
}