#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 binary_search(int, int *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int main(void) {
  int sorted[] = {13, 24, 34, 46, 52, 63, 77, 89, 91, 100};
  size_t len = 10;
  size_t index;
  int key;
  printf("Array: ");
  for (index = 0; index < len; index++)
    printf("%d%s", sorted[index], ((index + 1) == len) ? "." : ", ");
  (void)putchar('\n');
  key = 46;
  printf("Key '%d' was found in position '%d'\n", key,
         binary_search(key, sorted, len));
  key = 10;
  printf("Key '%d' was found in position '%d'\n", key,
         binary_search(key, sorted, len));
  key = 100;
  printf("Key '%d' was found in position '%d'\n", key,
         binary_search(key, sorted, len));
  key = 110;
  printf("Key '%d' was found in position '%d'\n", key,
         binary_search(key, sorted, len));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xea6(%rip),%rsi
mov    $0x28,%edx
call   1050 <memcpy@plt>
movq   $0xa,-0x38(%rbp)
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    11d8 <main+0x98>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%esi
mov    -0x40(%rbp),%rcx
add    $0x1,%rcx
mov    -0x38(%rbp),%rdi
lea    0xe8c(%rip),%rdx
lea    0xe83(%rip),%rax
cmp    %rdi,%rcx
cmove  %rax,%rdx
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1182 <main+0x42>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x2e,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <binary_search@plt>
mov    -0x54(%rbp),%esi
mov    %eax,%edx
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xa,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <binary_search@plt>
mov    -0x50(%rbp),%esi
mov    %eax,%edx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x64,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <binary_search@plt>
mov    -0x4c(%rbp),%esi
mov    %eax,%edx
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x6e,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <binary_search@plt>
mov    -0x48(%rbp),%esi
mov    %eax,%edx
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0xee2(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0xec6(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xeaa(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xec7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec1(%rip),%rbx
lea    0xec1(%rip),%r14
mov    %rbx,%rdi
mov    $0xd,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x18,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x22,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x34,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x3f,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x4d,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x59,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x5b,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe16(%rip),%rdx
mov    %rbx,%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rsp,%r14
mov    $0xa,%edx
mov    $0x2e,%edi
mov    %r14,%rsi
call   1050 <binary_search@plt>
lea    0xde6(%rip),%rbx
mov    %rbx,%rdi
mov    $0x2e,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edx
mov    $0xa,%edi
mov    %r14,%rsi
call   1050 <binary_search@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edx
mov    $0x64,%edi
mov    %r14,%rsi
call   1050 <binary_search@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edx
mov    $0x6e,%edi
mov    %r14,%rsi
call   1050 <binary_search@plt>
mov    %rbx,%rdi
mov    $0x6e,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.sorted = [10 x i32] [i32 13, i32 24, i32 34, i32 46, i32 52, i32 63, i32 77, i32 89, i32 91, i32 100]
@.str = [8 x i8] c"Array: \00"
@.str.1 = [5 x i8] c"%d%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c", \00"
@.str.4 = [37 x i8] c"Key '%d' was found in position '%d'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i32]
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [10 x i32]* %2 to i8*
store i64 10, i64* %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %4
br label %8
8: ; preds = %23, %0
%9 = load i64, i64* %4
%10 = load i64, i64* %3
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %26
12: ; preds = %8
%13 = load i64, i64* %4
%14 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 %13
%15 = load i32, i32* %14
%16 = load i64, i64* %4
%17 = add i64 %16, 1
%18 = load i64, i64* %3
%19 = icmp eq i64 %17, %18
%20 = zext i1 %19 to i64
%21 = select i1 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %21)
br label %23
23: ; preds = %12
%24 = load i64, i64* %4
%25 = add i64 %24, 1
store i64 %25, i64* %4
br label %8
26: ; preds = %8
%27 = call i32 @putchar(i8 signext 10)
store i32 46, i32* %5
%28 = load i32, i32* %5
%29 = load i32, i32* %5
%30 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%31 = load i64, i64* %3
%32 = call i32 @binary_search(i32 %29, i32* %30, i64 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %32)
store i32 10, i32* %5
%34 = load i32, i32* %5
%35 = load i32, i32* %5
%36 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%37 = load i64, i64* %3
%38 = call i32 @binary_search(i32 %35, i32* %36, i64 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %34, i32 %38)
store i32 100, i32* %5
%40 = load i32, i32* %5
%41 = load i32, i32* %5
%42 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%43 = load i64, i64* %3
%44 = call i32 @binary_search(i32 %41, i32* %42, i64 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %44)
store i32 110, i32* %5
%46 = load i32, i32* %5
%47 = load i32, i32* %5
%48 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%49 = load i64, i64* %3
%50 = call i32 @binary_search(i32 %47, i32* %48, i64 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %46, i32 %50)
ret i32 0
}

LLVM IR (-O2):
@__const.main.sorted = [10 x i32] [i32 13, i32 24, i32 34, i32 46, i32 52, i32 63, i32 77, i32 89, i32 91, i32 100]
@.str = [8 x i8] c"Array: \00"
@.str.1 = [5 x i8] c"%d%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c", \00"
@.str.4 = [37 x i8] c"Key '%d' was found in position '%d'\0A\00"
define i32 @main() {
%1 = alloca [10 x i32]
%2 = bitcast [10 x i32]* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 89, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 91, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 100, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @putchar(i8 signext 10)
%16 = call i32 @binary_search(i32 46, i32* %4, i64 10)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 46, i32 %16)
%18 = call i32 @binary_search(i32 10, i32* %4, i64 10)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 10, i32 %18)
%20 = call i32 @binary_search(i32 100, i32* %4, i64 10)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 100, i32 %20)
%22 = call i32 @binary_search(i32 110, i32* %4, i64 10)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 110, i32 %22)
ret i32 0
}