#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 BinarySearch(int *, int, int, int);
int /*<<< orphan*/ flushall();
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
void main() {
  int myarray[10] = {2, 4, 7, 8, 11, 14, 17, 19, 21, 24};
  int position, key;
  int low, high;
  int i;
  printf("\nArray is ");
  for (i = 0; i < 10; i++) {
    printf("%d ", myarray[i]);
  }
  printf("\nInput value to search for: ");
  scanf("%d", &key);
  flushall();
  low = 0;
  high = 9;
  position = BinarySearch(myarray, low, high, key);
  if (position == -1) {
    printf("\nValue %d not found\n\n", key);
  } else {
    printf("\nValue %d found at array position: %d\n\n", myarray[position],
           position);
  }
  getchar();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x30(%rbp),%rdi
lea    0xe8d(%rip),%rsi
mov    $0x28,%edx
call   1070 <memcpy@plt>
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x44(%rbp)
cmpl   $0xa,-0x44(%rbp)
jge    11c0 <main+0x60>
movslq -0x44(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1192 <main+0x32>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe7f(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <scanf@plt>
mov    $0x0,%al
call   1040 <flushall@plt>
movl   $0x0,-0x3c(%rbp)
movl   $0x9,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%edx
mov    -0x38(%rbp),%ecx
call   1060 <BinarySearch@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1228 <main+0xc8>
mov    -0x38(%rbp),%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1241 <main+0xe1>
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%esi
mov    -0x34(%rbp),%edx
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x40,%rsp
mov    0xec4(%rip),%rax
mov    %rax,0x30(%rsp)
movaps 0xea8(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe8c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xea5(%rip),%rbx
mov    %rbx,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xb,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0xe,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x11,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x13,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x15,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x18,%esi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe1b(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
xor    %eax,%eax
call   1040 <flushall@plt>
mov    0xc(%rsp),%ecx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1060 <BinarySearch@plt>
cmp    $0xffffffff,%eax
je     127d <main+0x12d>
movslq %eax,%rcx
mov    0x10(%rsp,%rcx,4),%esi
lea    0xdfb(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    128f <main+0x13f>
mov    0xc(%rsp),%esi
lea    0xdcf(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.main.myarray = [10 x i32] [i32 2, i32 4, i32 7, i32 8, i32 11, i32 14, i32 17, i32 19, i32 21, i32 24]
@.str = [11 x i8] c"\0AArray is \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [29 x i8] c"\0AInput value to search for: \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [22 x i8] c"\0AValue %d not found\0A\0A\00"
@.str.5 = [40 x i8] c"\0AValue %d found at array position: %d\0A\0A\00"
define void @main() {
%1 = alloca [10 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast [10 x i32]* %1 to i8*
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %9
9: ; preds = %18, %0
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 %14
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %12
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
br label %9
21: ; preds = %9
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%24 = call i32 (...) @flushall()
store i32 0, i32* %4
store i32 9, i32* %5
%25 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = load i32, i32* %3
%29 = call i32 @BinarySearch(i32* %25, i32 %26, i32 %27, i32 %28)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %21
%33 = load i32, i32* %3
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %42
35: ; preds = %21
%36 = load i32, i32* %2
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 %37
%39 = load i32, i32* %38
%40 = load i32, i32* %2
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 %40)
br label %42
42: ; preds = %35, %32
%43 = call i32 (...) @getchar()
ret void
}

LLVM IR (-O2):
@__const.main.myarray = [10 x i32] [i32 2, i32 4, i32 7, i32 8, i32 11, i32 14, i32 17, i32 19, i32 21, i32 24]
@.str = [11 x i8] c"\0AArray is \00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [29 x i8] c"\0AInput value to search for: \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [22 x i8] c"\0AValue %d not found\0A\0A\00"
@.str.5 = [40 x i8] c"\0AValue %d found at array position: %d\0A\0A\00"
define void @main() {
%1 = alloca [10 x i32]
%2 = alloca i32
%3 = bitcast [10 x i32]* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 2)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 7)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 8)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 11)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 14)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 17)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 19)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 21)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 24)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%19 = call i32 (...) @flushall()
%20 = load i32, i32* %2
%21 = call i32 @BinarySearch(i32* %6, i32 0, i32 9, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %0
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %24)
br label %31
26: ; preds = %0
%27 = sext i32 %21 to i64
%28 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 %27
%29 = load i32, i32* %28
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 %29, i32 %21)
br label %31
31: ; preds = %26, %23
%32 = call i32 (...) @getchar()
ret void
}