#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 */
size_t binarySearch(int *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printHeader();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
void fig06_19() {
  int a[15];
  size_t i;
  int key;
  size_t result;
  for (i = 0; i < 15; ++i) {
    a[i] = 2 * i;
  }
  printf("%s", "Enter a number between 0 and 28: ");
  scanf("%d", &key);
  printHeader();
  result = binarySearch(a, key, 0, 15 - 1);
  if (result != -1) {
    printf("\n%d found in array element %d\n", key, result);
  } else {
    printf("\n%d not found\n", key);
  }
}


x86 assembly (clang, -O0):
<fig06_19>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movq   $0x0,-0x48(%rbp)
cmpq   $0xf,-0x48(%rbp)
jae    117e <fig06_19+0x3e>
mov    -0x48(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1150 <fig06_19+0x10>
lea    0xe7b(%rip),%rdi
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8b(%rip),%rdi
lea    -0x4c(%rbp),%rsi
call   1050 <scanf@plt>
mov    $0x0,%al
call   1060 <printHeader@plt>
lea    -0x40(%rbp),%rdi
mov    -0x4c(%rbp),%esi
xor    %edx,%edx
mov    $0xe,%ecx
call   1040 <binarySearch@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0xffffffffffffffff,-0x58(%rbp)
je     11e6 <fig06_19+0xa6>
mov    -0x4c(%rbp),%esi
mov    -0x58(%rbp),%rdx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <fig06_19+0xb7>
mov    -0x4c(%rbp),%esi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fig06_19>:
sub    $0x58,%rsp
movaps 0xeb5(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xeb9(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xebd(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movabs $0x1a00000018,%rax
mov    %rax,0x40(%rsp)
movl   $0x1c,0x48(%rsp)
lea    0xeaa(%rip),%rdi
lea    0xea6(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xeba(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
xor    %eax,%eax
call   1060 <printHeader@plt>
mov    0xc(%rsp),%esi
lea    0x10(%rsp),%rdi
xor    %edx,%edx
mov    $0xe,%ecx
call   1040 <binarySearch@plt>
mov    0xc(%rsp),%esi
cmp    $0xffffffffffffffff,%rax
je     11e1 <fig06_19+0xa1>
lea    0xe86(%rip),%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x58,%rsp
ret
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x58,%rsp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [34 x i8] c"Enter a number between 0 and 28: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [31 x i8] c"\0A%d found in array element %d\0A\00"
@.str.4 = [15 x i8] c"\0A%d not found\0A\00"
define void @fig06_19() {
%1 = alloca [15 x i32]
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
store i64 0, i64* %2
br label %5
5: ; preds = %14, %0
%6 = load i64, i64* %2
%7 = icmp ult i64 %6, 15
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = load i64, i64* %2
%10 = mul i64 2, %9
%11 = trunc i64 %10 to i32
%12 = load i64, i64* %2
%13 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 %12
store i32 %11, i32* %13
br label %14
14: ; preds = %8
%15 = load i64, i64* %2
%16 = add i64 %15, 1
store i64 %16, i64* %2
br label %5
17: ; preds = %5
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%20 = call i32 (...) @printHeader()
%21 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 0
%22 = load i32, i32* %3
%23 = call i64 @binarySearch(i32* %21, i32 %22, i32 0, i32 14)
store i64 %23, i64* %4
%24 = load i64, i64* %4
%25 = icmp ne i64 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = load i32, i32* %3
%28 = load i64, i64* %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %27, i64 %28)
br label %33
30: ; preds = %17
%31 = load i32, i32* %3
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %30, %26
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [34 x i8] c"Enter a number between 0 and 28: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [31 x i8] c"\0A%d found in array element %d\0A\00"
@.str.4 = [15 x i8] c"\0A%d not found\0A\00"
define void @fig06_19() {
%1 = alloca [15 x i32]
%2 = alloca i32
%3 = bitcast [15 x i32]* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 0
%6 = bitcast [15 x i32]* %1 to <4 x i32>*
store <4 x i32> <i32 0, i32 2, i32 4, i32 6>, <4 x i32>* %6
%7 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 4
%8 = bitcast i32* %7 to <4 x i32>*
store <4 x i32> <i32 8, i32 10, i32 12, i32 14>, <4 x i32>* %8
%9 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 8
%10 = bitcast i32* %9 to <4 x i32>*
store <4 x i32> <i32 16, i32 18, i32 20, i32 22>, <4 x i32>* %10
%11 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 12
store i32 24, i32* %11
%12 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 13
store i32 26, i32* %12
%13 = getelementptr inbounds [15 x i32], [15 x i32]* %1, i64 0, i64 14
store i32 28, i32* %13
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%16 = call i32 (...) @printHeader()
%17 = load i32, i32* %2
%18 = call i64 @binarySearch(i32* %5, i32 %17, i32 0, i32 14)
%19 = icmp eq i64 %18, -1
%20 = load i32, i32* %2
br i1 %19, label %23, label %21
21: ; preds = %0
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %20, i64 %18)
br label %25
23: ; preds = %0
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %20)
br label %25
25: ; preds = %23, %21
ret void
}