#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 /*<<< orphan*/ buf;

/* Variables and functions */
int bsearch(int *, int, int, int *);
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  int buf[] = {0, 1, 1, 2, 3, 3, 4, 5, 6, 10};
  int pos = 0;
  bool res;
  res = bsearch(buf, sizeof(buf) / sizeof(int), -1, &pos);
  printf("Test 1 res is %d pos is %d\n", res, pos);
  res = bsearch(buf, sizeof(buf) / sizeof(int), 2, &pos);
  printf("Test 2 res is %d pos is %d\n", res, pos);
  res = bsearch(buf, sizeof(buf) / sizeof(int), 10, &pos);
  printf("Test 3 res is %d pos is %d\n", res, pos);
  res = bsearch(buf, sizeof(buf) / sizeof(int), 7, &pos);
  printf("Test 4 res is %d pos is %d\n", res, pos);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xeb6(%rip),%rsi
mov    $0x28,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0xa,%esi
mov    $0xffffffff,%edx
lea    -0x34(%rbp),%rcx
call   1050 <bsearch@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xea6(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    $0xa,%esi
mov    $0x2,%edx
lea    -0x34(%rbp),%rcx
call   1050 <bsearch@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xe96(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    $0xa,%edx
lea    -0x34(%rbp),%rcx
mov    %edx,%esi
call   1050 <bsearch@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    $0xa,%esi
mov    $0x7,%edx
lea    -0x34(%rbp),%rcx
call   1050 <bsearch@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xe79(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0xef2(%rip),%rax
mov    %rax,0x30(%rsp)
movaps 0xed6(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xeba(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r14
mov    %rbx,%rdi
mov    $0xa,%esi
mov    $0xffffffff,%edx
mov    %r14,%rcx
call   1040 <bsearch@plt>
mov    0xc(%rsp),%edx
lea    0xeab(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1040 <bsearch@plt>
mov    0xc(%rsp),%edx
lea    0xea0(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    $0xa,%edx
mov    %r14,%rcx
call   1040 <bsearch@plt>
mov    0xc(%rsp),%edx
lea    0xe95(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    $0x7,%edx
mov    %r14,%rcx
call   1040 <bsearch@plt>
mov    0xc(%rsp),%edx
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.buf = [10 x i32] [i32 0, i32 1, i32 1, i32 2, i32 3, i32 3, i32 4, i32 5, i32 6, i32 10]
@.str = [28 x i8] c"Test 1 res is %d pos is %d\0A\00"
@.str.1 = [28 x i8] c"Test 2 res is %d pos is %d\0A\00"
@.str.2 = [28 x i8] c"Test 3 res is %d pos is %d\0A\00"
@.str.3 = [28 x i8] c"Test 4 res is %d pos is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i32]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast [10 x i32]* %2 to i8*
store i32 0, i32* %3
%6 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%7 = call i32 @bsearch(i32* %6, i32 10, i32 -1, i32* %3)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = load i32, i32* %3
%10 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%12 = call i32 @bsearch(i32* %11, i32 10, i32 2, i32* %3)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i32, i32* %3
%15 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
%16 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%17 = call i32 @bsearch(i32* %16, i32 10, i32 10, i32* %3)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = load i32, i32* %3
%20 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
%21 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%22 = call i32 @bsearch(i32* %21, i32 10, i32 7, i32* %3)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %24)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [10 x i32] [i32 0, i32 1, i32 1, i32 2, i32 3, i32 3, i32 4, i32 5, i32 6, i32 10]
@.str = [28 x i8] c"Test 1 res is %d pos is %d\0A\00"
@.str.1 = [28 x i8] c"Test 2 res is %d pos is %d\0A\00"
@.str.2 = [28 x i8] c"Test 3 res is %d pos is %d\0A\00"
@.str.3 = [28 x i8] c"Test 4 res is %d pos is %d\0A\00"
define i32 @main() {
%1 = alloca [10 x i32]
%2 = alloca i32
%3 = bitcast [10 x i32]* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%6 = call i32 @bsearch(i32* %5, i32 10, i32 -1, i32* %2)
%7 = load i32, i32* %2
%8 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = call i32 @bsearch(i32* %5, i32 10, i32 2, i32* %2)
%10 = load i32, i32* %2
%11 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %10)
%12 = call i32 @bsearch(i32* %5, i32 10, i32 10, i32* %2)
%13 = load i32, i32* %2
%14 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13)
%15 = call i32 @bsearch(i32* %5, i32 10, i32 7, i32* %2)
%16 = load i32, i32* %2
%17 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %16)
ret i32 0
}