#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 */
struct vector {
  int dummy;
};

/* Variables and functions */
void const *vector_get(struct vector *, int);
int vector_size(struct vector *);
int vector_bsearch(struct vector *vector, const void *value,
                   int (*comp)(const void *, const void *)) {
  const int length = vector_size(vector);
  int low = 0, high = length, i = 0;
  while (low < high) {
    i = low + (high - low) / 2;
    if (comp(vector_get(vector, i), value) < 0) {
      low = i + 1;
    } else {
      high = i;
    }
  }
  if (comp(vector_get(vector, i), value) > 0) {
    i--;
  }
  return i;
}


x86 assembly (clang, -O0):
<vector_bsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <vector_size@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11c1 <vector_bsearch+0xa1>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <vector_get@plt>
mov    %rax,%rdi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    11b6 <vector_bsearch+0x96>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11bc <vector_bsearch+0x9c>
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1154 <vector_bsearch+0x34>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <vector_get@plt>
mov    %rax,%rdi
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jle    11f4 <vector_bsearch+0xd4>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vector_bsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <vector_size@plt>
test   %eax,%eax
jle    1190 <vector_bsearch+0x70>
mov    %eax,%ebp
xor    %ebx,%ebx
jmp    1157 <vector_bsearch+0x37>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%ebp
cmp    %ebx,%ebp
jle    1193 <vector_bsearch+0x73>
mov    %ebp,%eax
sub    %ebx,%eax
mov    %eax,%r14d
shr    $0x1f,%r14d
add    %eax,%r14d
sar    %r14d
lea    (%r14,%rbx,1),%r13d
mov    %r12,%rdi
mov    %r13d,%esi
call   1030 <vector_get@plt>
mov    %rax,%rdi
mov    %r15,%rsi
call   *(%rsp)
test   %eax,%eax
jns    1150 <vector_bsearch+0x30>
add    %r14d,%ebx
add    $0x1,%ebx
cmp    %ebx,%ebp
jg     1157 <vector_bsearch+0x37>
jmp    1193 <vector_bsearch+0x73>
xor    %r13d,%r13d
mov    %r12,%rdi
mov    %r13d,%esi
call   1030 <vector_get@plt>
mov    %rax,%rdi
mov    %r15,%rsi
call   *(%rsp)
xor    %ecx,%ecx
test   %eax,%eax
setg   %cl
sub    %ecx,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.vector = type { i32 }
define i32 @vector_bsearch(%struct.vector* %0, i8* %1, i32 (i8*, i8*)* %2) {
%4 = alloca %struct.vector*
%5 = alloca i8*
%6 = alloca i32 (i8*, i8*)*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.vector* %0, %struct.vector** %4
store i8* %1, i8** %5
store i32 (i8*, i8*)* %2, i32 (i8*, i8*)** %6
%11 = load %struct.vector*, %struct.vector** %4
%12 = call i32 @vector_size(%struct.vector* %11)
store i32 %12, i32* %7
store i32 0, i32* %8
%13 = load i32, i32* %7
store i32 %13, i32* %9
store i32 0, i32* %10
br label %14
14: ; preds = %37, %3
%15 = load i32, i32* %8
%16 = load i32, i32* %9
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %38
18: ; preds = %14
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = sdiv i32 %22, 2
%24 = add nsw i32 %19, %23
store i32 %24, i32* %10
%25 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%26 = load %struct.vector*, %struct.vector** %4
%27 = load i32, i32* %10
%28 = call i8* @vector_get(%struct.vector* %26, i32 %27)
%29 = load i8*, i8** %5
%30 = call i32 %25(i8* %28, i8* %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i32, i32* %10
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
br label %37
35: ; preds = %18
%36 = load i32, i32* %10
store i32 %36, i32* %9
br label %37
37: ; preds = %35, %32
br label %14
38: ; preds = %14
%39 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%40 = load %struct.vector*, %struct.vector** %4
%41 = load i32, i32* %10
%42 = call i8* @vector_get(%struct.vector* %40, i32 %41)
%43 = load i8*, i8** %5
%44 = call i32 %39(i8* %42, i8* %43)
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = load i32, i32* %10
%48 = add nsw i32 %47, -1
store i32 %48, i32* %10
br label %49
49: ; preds = %46, %38
%50 = load i32, i32* %10
ret i32 %50
}

LLVM IR (-O2):
%struct.vector = type { i32 }
define i32 @vector_bsearch(%struct.vector* %0, i8* %1, i32 (i8*, i8*)* nocapture %2) {
%4 = tail call i32 @vector_size(%struct.vector* %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %3, %6
%7 = phi i32 [ %17, %6 ], [ %4, %3 ]
%8 = phi i32 [ %16, %6 ], [ 0, %3 ]
%9 = sub nsw i32 %7, %8
%10 = sdiv i32 %9, 2
%11 = add nsw i32 %10, %8
%12 = tail call i8* @vector_get(%struct.vector* %0, i32 %11)
%13 = tail call i32 %2(i8* %12, i8* %1)
%14 = icmp slt i32 %13, 0
%15 = add nsw i32 %11, 1
%16 = select i1 %14, i32 %15, i32 %8
%17 = select i1 %14, i32 %7, i32 %11
%18 = icmp sgt i32 %17, %16
br i1 %18, label %6, label %19
19: ; preds = %6, %3
%20 = phi i32 [ 0, %3 ], [ %11, %6 ]
%21 = tail call i8* @vector_get(%struct.vector* %0, i32 %20)
%22 = tail call i32 %2(i8* %21, i8* %1)
%23 = icmp sgt i32 %22, 0
%24 = sext i1 %23 to i32
%25 = add nsw i32 %20, %24
ret i32 %25
}