#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 */
typedef int off_t;

/* Variables and functions */
void *tvu_bsearch_entry(void const *, size_t, int);
void *tvu_bsearch_near(const void *key, const void *base, size_t nel,
                       size_t width,
                       int (*compar)(const void *, const void *)) {
  off_t imin = 0;
  off_t imax = nel - 1;
  off_t imid;
  while (imin < imax) {
    imid = (imin + imax) / 2;
    if (compar(key, tvu_bsearch_entry(base, width, imid)) > 0) {
      imin = imid + 1;
    } else {
      imax = imid;
    }
  }
  if ((compar(key, tvu_bsearch_entry(base, width, imin)) < 0) & (imin > 0)) {
    imin--;
  }
  return tvu_bsearch_entry(base, width, imin);
}


x86 assembly (clang, -O0):
<tvu_bsearch_near>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    11aa <tvu_bsearch_near+0x9a>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <tvu_bsearch_entry@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
mov    -0x40(%rbp),%rax
call   *%rax
cmp    $0x0,%eax
jle    119f <tvu_bsearch_near+0x8f>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a5 <tvu_bsearch_near+0x95>
mov    -0x34(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    113e <tvu_bsearch_near+0x2e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <tvu_bsearch_entry@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rsi
mov    -0x50(%rbp),%rax
call   *%rax
cmp    $0x0,%eax
setl   %al
and    $0x1,%al
movzbl %al,%eax
cmpl   $0x0,-0x2c(%rbp)
setg   %cl
and    $0x1,%cl
movzbl %cl,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1203 <tvu_bsearch_near+0xf3>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <tvu_bsearch_entry@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tvu_bsearch_near>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,(%rsp)
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%r13
add    $0xffffffff,%ebp
xor    %ebx,%ebx
test   %ebp,%ebp
jg     1147 <tvu_bsearch_near+0x37>
jmp    117d <tvu_bsearch_near+0x6d>
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%ebp
cmp    %ebp,%ebx
jge    117d <tvu_bsearch_near+0x6d>
lea    (%rbx,%rbp,1),%eax
mov    %eax,%r14d
shr    $0x1f,%r14d
add    %eax,%r14d
sar    %r14d
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <tvu_bsearch_entry@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   *(%rsp)
test   %eax,%eax
jle    1140 <tvu_bsearch_near+0x30>
add    $0x1,%r14d
mov    %r14d,%ebx
cmp    %ebp,%ebx
jl     1147 <tvu_bsearch_near+0x37>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <tvu_bsearch_entry@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   *(%rsp)
xor    %edx,%edx
test   %ebx,%ebx
setg   %dl
shl    $0x1f,%edx
and    %eax,%edx
sar    $0x1f,%edx
add    %ebx,%edx
mov    %r12,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <tvu_bsearch_entry@plt>

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

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