#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*/ c;
typedef int /*<<< orphan*/ b;

/* Variables and functions */
int BinarySearch(int *, int, int /*<<< orphan*/, int);
int findmin(int *, int);
int /*<<< orphan*/ printf(char *, ...);
void test_binary_search(void) {
  int a[10] = {15, 21, 39, 44, 52, 71, 94, 154, 204, 322};
  int b[10] = {9, 11, 22, 31, 45, 53, 69, 71, 88, 94};
  int c[10] = {4, 11, 3, 19, 20, 2, 74, 1, 32, 41};
  int len = sizeof(b) / sizeof(b[0]);
  printf("Binary Search:\n");
  int pos = BinarySearch(b, 22, 0, 9);
  printf("The key was found at position %d\n", pos);
  int pos_two = BinarySearch(b, 71, 0, 9);
  printf("The key was found at position %d\n", pos_two);
  len = sizeof(c) / sizeof(c[0]);
  int min = findmin(c, len);
  printf("The minimum value found was %d\n", min);
}


x86 assembly (clang, -O0):
<test_binary_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
lea    -0x30(%rbp),%rdi
lea    0xeaa(%rip),%rsi
mov    $0x28,%edx
call   1050 <memcpy@plt>
lea    -0x60(%rbp),%rdi
lea    0xec5(%rip),%rsi
mov    $0x28,%edx
call   1050 <memcpy@plt>
lea    -0x90(%rbp),%rdi
lea    0xedd(%rip),%rsi
mov    $0x28,%edx
call   1050 <memcpy@plt>
movl   $0xa,-0x94(%rbp)
lea    0xeea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    $0x16,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1040 <BinarySearch@plt>
mov    %eax,-0x98(%rbp)
mov    -0x98(%rbp),%esi
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    $0x47,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1040 <BinarySearch@plt>
mov    %eax,-0x9c(%rbp)
mov    -0x9c(%rbp),%esi
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xa,-0x94(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x94(%rbp),%esi
call   1060 <findmin@plt>
mov    %eax,-0xa0(%rbp)
mov    -0xa0(%rbp),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_binary_search>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    0xed2(%rip),%rax
mov    %rax,0x50(%rsp)
movaps 0xeb6(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe9a(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
mov    0xede(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0xec2(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xea6(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xf05(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    $0x16,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1050 <BinarySearch@plt>
lea    0xe9e(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x47,%esi
xor    %edx,%edx
mov    $0x9,%ecx
call   1050 <BinarySearch@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%rdi
mov    $0xa,%esi
call   1060 <findmin@plt>
lea    0xe80(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.test_binary_search.a = [10 x i32] [i32 15, i32 21, i32 39, i32 44, i32 52, i32 71, i32 94, i32 154, i32 204, i32 322]
@__const.test_binary_search.b = [10 x i32] [i32 9, i32 11, i32 22, i32 31, i32 45, i32 53, i32 69, i32 71, i32 88, i32 94]
@__const.test_binary_search.c = [10 x i32] [i32 4, i32 11, i32 3, i32 19, i32 20, i32 2, i32 74, i32 1, i32 32, i32 41]
@.str = [16 x i8] c"Binary Search:\0A\00"
@.str.1 = [34 x i8] c"The key was found at position %d\0A\00"
@.str.2 = [32 x i8] c"The minimum value found was %d\0A\00"
define void @test_binary_search() {
%1 = alloca [10 x i32]
%2 = alloca [10 x i32]
%3 = alloca [10 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast [10 x i32]* %1 to i8*
%9 = bitcast [10 x i32]* %2 to i8*
%10 = bitcast [10 x i32]* %3 to i8*
store i32 10, i32* %4
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%13 = call i32 @BinarySearch(i32* %12, i32 22, i32 0, i32 9)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%17 = call i32 @BinarySearch(i32* %16, i32 71, i32 0, i32 9)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 10, i32* %4
%20 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
%21 = load i32, i32* %4
%22 = call i32 @findmin(i32* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %23)
ret void
}

LLVM IR (-O2):
@__const.test_binary_search.b = [10 x i32] [i32 9, i32 11, i32 22, i32 31, i32 45, i32 53, i32 69, i32 71, i32 88, i32 94]
@__const.test_binary_search.c = [10 x i32] [i32 4, i32 11, i32 3, i32 19, i32 20, i32 2, i32 74, i32 1, i32 32, i32 41]
@.str.1 = [34 x i8] c"The key was found at position %d\0A\00"
@.str.2 = [32 x i8] c"The minimum value found was %d\0A\00"
@str = [15 x i8] c"Binary Search:\00"
define void @test_binary_search() {
%1 = alloca [10 x i32]
%2 = alloca [10 x i32]
%3 = bitcast [10 x i32]* %1 to i8*
%4 = bitcast [10 x i32]* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%7 = call i32 @BinarySearch(i32* %6, i32 22, i32 0, i32 9)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = call i32 @BinarySearch(i32* %6, i32 71, i32 0, i32 9)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%12 = call i32 @findmin(i32* %11, i32 10)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %12)
ret void
}