#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 BSearchRecHelper(void const *, void const *, int /*<<< orphan*/, size_t,
                     size_t, int (*)(void const *, void const *));
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int BSearchRec(const void *key, const void *base, size_t itemCount,
               size_t itemSize, int (*cmp)(const void *, const void *)) {
  static const int ERROR = -2;
  static const int NOT_FOUND = -1;
  if (!key) {
    errno = EINVAL;
    return ERROR;
  }
  if (!base) {
    errno = EINVAL;
    return ERROR;
  }
  if (!cmp) {
    errno = EINVAL;
    return ERROR;
  }
  return BSearchRecHelper(key, base, 0, itemCount, itemSize, cmp);
}


x86 assembly (clang, -O0):
<BSearchRec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1155 <BSearchRec+0x45>
mov    0x2e9a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e99(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffe,-0x4(%rbp)
jmp    11c5 <BSearchRec+0xb5>
cmpq   $0x0,-0x18(%rbp)
jne    117e <BSearchRec+0x6e>
mov    0x2e71(%rip),%rax
mov    (%rax),%ecx
mov    0x2e70(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffe,-0x4(%rbp)
jmp    11c5 <BSearchRec+0xb5>
cmpq   $0x0,-0x30(%rbp)
jne    11a7 <BSearchRec+0x97>
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
mov    0x2e47(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffe,-0x4(%rbp)
jmp    11c5 <BSearchRec+0xb5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
xor    %edx,%edx
call   1030 <BSearchRecHelper@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BSearchRec>:
test   %rdi,%rdi
je     112f <BSearchRec+0x1f>
test   %rsi,%rsi
je     112f <BSearchRec+0x1f>
mov    %r8,%r9
test   %r8,%r8
je     112f <BSearchRec+0x1f>
mov    %rcx,%r8
mov    %rdx,%rcx
xor    %edx,%edx
jmp    1030 <BSearchRecHelper@plt>
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    0x2ea1(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xfffffffe,%eax
ret

LLVM IR (-O0):
@BSearchRec.ERROR = internal constant i32 -2
@BSearchRec.NOT_FOUND = internal constant i32 -1
@EINVAL = global i32 0
@errno = global i32 0
define i32 @BSearchRec(i8* %0, i8* %1, i64 %2, i64 %3, i32 (i8*, i8*)* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32 (i8*, i8*)*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i32 (i8*, i8*)* %4, i32 (i8*, i8*)** %11
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %5
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i32 -2, i32* %6
br label %33
16: ; preds = %5
%17 = load i8*, i8** %8
%18 = icmp ne i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
store i32 -2, i32* %6
br label %33
21: ; preds = %16
%22 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %11
%23 = icmp ne i32 (i8*, i8*)* %22, null
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = load i32, i32* @EINVAL
store i32 %25, i32* @errno
store i32 -2, i32* %6
br label %33
26: ; preds = %21
%27 = load i8*, i8** %7
%28 = load i8*, i8** %8
%29 = load i64, i64* %9
%30 = load i64, i64* %10
%31 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %11
%32 = call i32 @BSearchRecHelper(i8* %27, i8* %28, i32 0, i64 %29, i64 %30, i32 (i8*, i8*)* %31)
store i32 %32, i32* %6
br label %33
33: ; preds = %26, %24, %19, %14
%34 = load i32, i32* %6
ret i32 %34
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @BSearchRec(i8* %0, i8* %1, i64 %2, i64 %3, i32 (i8*, i8*)* %4) {
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = load i32, i32* @EINVAL
store i32 %8, i32* @errno
br label %19
9: ; preds = %5
%10 = icmp eq i8* %1, null
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %19
13: ; preds = %9
%14 = icmp eq i32 (i8*, i8*)* %4, null
br i1 %14, label %15, label %17
15: ; preds = %13
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
br label %19
17: ; preds = %13
%18 = tail call i32 @BSearchRecHelper(i8* %0, i8* %1, i32 0, i64 %2, i64 %3, i32 (i8*, i8*)* %4)
br label %19
19: ; preds = %17, %15, %11, %7
%20 = phi i32 [ %18, %17 ], [ -2, %15 ], [ -2, %11 ], [ -2, %7 ]
ret i32 %20
}