#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 /*<<< orphan*/ binary_search(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ test(int, int /*<<< orphan*/, char *);
int main(void) {
  int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int failed_tests = 0;
  failed_tests +=
      test(4, binary_search(list, 0, 9, 5), "Failed to find the midpoint");
  failed_tests +=
      test(1, binary_search(list, 0, 9, 2),
           "Failed to find a number in the bottom half of the array");
  failed_tests += test(8, binary_search(list, 0, 9, 9),
                       "Failed to find a number in the top half of the array");
  failed_tests += test(-1, binary_search(list, 0, 9, 100),
                       "Found a number that does not exist");
  if (failed_tests == 0) {
    printf("All tests passed\n");
  } else {
    printf("%d %s failed\n", failed_tests,
           failed_tests == 1 ? "test" : "tests");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xea6(%rip),%rsi
mov    $0x28,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
mov    $0x5,%ecx
call   1050 <binary_search@plt>
mov    %eax,%esi
mov    $0x4,%edi
lea    0xe9a(%rip),%rdx
call   1060 <test@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
mov    $0x2,%ecx
call   1050 <binary_search@plt>
mov    %eax,%esi
mov    $0x1,%edi
lea    0xe81(%rip),%rdx
call   1060 <test@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%ecx
mov    %ecx,%edx
call   1050 <binary_search@plt>
mov    %eax,%esi
mov    $0x8,%edi
lea    0xe87(%rip),%rdx
call   1060 <test@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
mov    $0x64,%ecx
call   1050 <binary_search@plt>
mov    %eax,%esi
mov    $0xffffffff,%edi
lea    0xe87(%rip),%rdx
call   1060 <test@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1259 <main+0x119>
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1282 <main+0x142>
mov    -0x34(%rbp),%esi
mov    -0x34(%rbp),%ecx
lea    0xe93(%rip),%rdx
lea    0xe87(%rip),%rax
cmp    $0x1,%ecx
cmove  %rax,%rdx
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0xed0(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0xeb4(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe98(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x9,%edx
mov    $0x5,%ecx
call   1050 <binary_search@plt>
lea    0xe9e(%rip),%rdx
mov    $0x4,%edi
mov    %eax,%esi
call   1060 <test@plt>
mov    %rax,%r15
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x9,%edx
mov    $0x2,%ecx
call   1050 <binary_search@plt>
lea    0xe90(%rip),%rdx
mov    $0x1,%edi
mov    %eax,%esi
call   1060 <test@plt>
mov    %rax,%rbx
add    %r15d,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x9,%edx
mov    $0x9,%ecx
call   1050 <binary_search@plt>
lea    0xe9b(%rip),%rdx
mov    $0x8,%edi
mov    %eax,%esi
call   1060 <test@plt>
mov    %rax,%r15
add    %ebx,%r15d
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x9,%edx
mov    $0x64,%ecx
call   1050 <binary_search@plt>
lea    0xea3(%rip),%rdx
mov    $0xffffffff,%edi
mov    %eax,%esi
call   1060 <test@plt>
add    %r15d,%eax
je     1246 <main+0x106>
cmp    $0x1,%eax
lea    0xeb9(%rip),%rcx
lea    0xeb7(%rip),%rdx
cmove  %rcx,%rdx
lea    0xe99(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1252 <main+0x112>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.list = [10 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@.str = [28 x i8] c"Failed to find the midpoint\00"
@.str.1 = [56 x i8] c"Failed to find a number in the bottom half of the array\00"
@.str.2 = [53 x i8] c"Failed to find a number in the top half of the array\00"
@.str.3 = [35 x i8] c"Found a number that does not exist\00"
@.str.4 = [18 x i8] c"All tests passed\0A\00"
@.str.5 = [14 x i8] c"%d %s failed\0A\00"
@.str.6 = [5 x i8] c"test\00"
@.str.7 = [6 x i8] c"tests\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i32]
%3 = alloca i32
store i32 0, i32* %1
%4 = bitcast [10 x i32]* %2 to i8*
store i32 0, i32* %3
%5 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%6 = call i32 @binary_search(i32* %5, i32 0, i32 9, i32 5)
%7 = call i64 @test(i32 4, i32 %6, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = add nsw i64 %9, %7
%11 = trunc i64 %10 to i32
store i32 %11, i32* %3
%12 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%13 = call i32 @binary_search(i32* %12, i32 0, i32 9, i32 2)
%14 = call i64 @test(i32 1, i32 %13, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = add nsw i64 %16, %14
%18 = trunc i64 %17 to i32
store i32 %18, i32* %3
%19 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%20 = call i32 @binary_search(i32* %19, i32 0, i32 9, i32 9)
%21 = call i64 @test(i32 8, i32 %20, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = add nsw i64 %23, %21
%25 = trunc i64 %24 to i32
store i32 %25, i32* %3
%26 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%27 = call i32 @binary_search(i32* %26, i32 0, i32 9, i32 100)
%28 = call i64 @test(i32 -1, i32 %27, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* %3
%30 = sext i32 %29 to i64
%31 = add nsw i64 %30, %28
%32 = trunc i64 %31 to i32
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %44
37: ; preds = %0
%38 = load i32, i32* %3
%39 = load i32, i32* %3
%40 = icmp eq i32 %39, 1
%41 = zext i1 %40 to i64
%42 = select i1 %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %38, i8* %42)
br label %44
44: ; preds = %37, %35
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@__const.main.list = [10 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@.str = [28 x i8] c"Failed to find the midpoint\00"
@.str.1 = [56 x i8] c"Failed to find a number in the bottom half of the array\00"
@.str.2 = [53 x i8] c"Failed to find a number in the top half of the array\00"
@.str.3 = [35 x i8] c"Found a number that does not exist\00"
@.str.5 = [14 x i8] c"%d %s failed\0A\00"
@.str.6 = [5 x i8] c"test\00"
@.str.7 = [6 x i8] c"tests\00"
@str = [17 x i8] c"All tests passed\00"
define i32 @main() {
%1 = alloca [10 x i32]
%2 = bitcast [10 x i32]* %1 to i8*
%3 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%4 = call i32 @binary_search(i32* %3, i32 0, i32 9, i32 5)
%5 = call i64 @test(i32 4, i32 %4, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @binary_search(i32* %3, i32 0, i32 9, i32 2)
%7 = call i64 @test(i32 1, i32 %6, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
%8 = add i64 %7, %5
%9 = call i32 @binary_search(i32* %3, i32 0, i32 9, i32 9)
%10 = call i64 @test(i32 8, i32 %9, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0))
%11 = add i64 %8, %10
%12 = call i32 @binary_search(i32* %3, i32 0, i32 9, i32 100)
%13 = call i64 @test(i32 -1, i32 %12, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%14 = add i64 %11, %13
%15 = trunc i64 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %23
19: ; preds = %0
%20 = icmp eq i32 %15, 1
%21 = select i1 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %15, i8* %21)
br label %23
23: ; preds = %19, %17
ret i32 0
}