#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 *, int, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main(void) {
  int n, i, item, a[10], position;
  printf("enter the number of elements\n");
  scanf("%d", &n);
  printf("Enter %d elements\n", n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  printf("Enter the item to search\n");
  scanf("%d", &item);
  binary_search(item, a, n, &position);
  if (position == -1)
    printf("Item %d not found\n", item);
  else
    printf("Item %d found at position %d\n", item, position);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeca(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x8(%rbp),%esi
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11aa <main+0x7a>
movslq -0xc(%rbp),%rax
lea    -0x40(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe87(%rip),%rdi
call   1050 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1175 <main+0x45>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5f(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x10(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    -0x44(%rbp),%rcx
call   1040 <binary_search@plt>
cmpl   $0xffffffff,-0x44(%rbp)
jne    11fb <main+0xcb>
mov    -0x10(%rbp),%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120f <main+0xdf>
mov    -0x10(%rbp),%esi
mov    -0x44(%rbp),%edx
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
lea    0xef7(%rip),%rdi
call   1030 <puts@plt>
lea    0xea4(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x4(%rsp),%esi
lea    0xe92(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,0x4(%rsp)
jle    11ad <main+0x6d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xe73(%rip),%r14
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <scanf@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     1190 <main+0x50>
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
lea    0xe40(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <scanf@plt>
mov    0x8(%rsp),%edi
mov    0x4(%rsp),%edx
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rcx
call   1050 <binary_search@plt>
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%esi
cmp    $0xffffffff,%edx
je     11fe <main+0xbe>
lea    0xe34(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    120c <main+0xcc>
lea    0xe11(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [30 x i8] c"enter the number of elements\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [19 x i8] c"Enter %d elements\0A\00"
@.str.3 = [26 x i8] c"Enter the item to search\0A\00"
@.str.4 = [19 x i8] c"Item %d not found\0A\00"
@.str.5 = [30 x i8] c"Item %d found at position %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [10 x i32]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %9)
store i32 0, i32* %3
br label %11
11: ; preds = %20, %0
%12 = load i32, i32* %3
%13 = load i32, i32* %2
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [10 x i32], [10 x i32]* %5, i64 0, i64 %17
%19 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %18)
br label %20
20: ; preds = %15
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %11
23: ; preds = %11
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%26 = load i32, i32* %4
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %5, i64 0, i64 0
%28 = load i32, i32* %2
%29 = call i32 @binary_search(i32 %26, i32* %27, i32 %28, i32* %6)
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* %4
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %39
35: ; preds = %23
%36 = load i32, i32* %4
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %35, %32
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [19 x i8] c"Enter %d elements\0A\00"
@.str.4 = [19 x i8] c"Item %d not found\0A\00"
@.str.5 = [30 x i8] c"Item %d found at position %d\0A\00"
@str = [29 x i8] c"enter the number of elements\00"
@str.6 = [25 x i8] c"Enter the item to search\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [10 x i32]
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast [10 x i32]* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%11 = load i32, i32* %1
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %0, %15
%16 = phi i64 [ %19, %15 ], [ 0, %0 ]
%17 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 %16
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %17)
%19 = add nuw nsw i64 %16, 1
%20 = load i32, i32* %1
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %19, %21
br i1 %22, label %15, label %23
23: ; preds = %15, %0
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%26 = load i32, i32* %2
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
%28 = load i32, i32* %1
%29 = call i32 @binary_search(i32 %26, i32* %27, i32 %28, i32* %4)
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
%32 = load i32, i32* %2
br i1 %31, label %33, label %35
33: ; preds = %23
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %37
35: ; preds = %23
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %32, i32 %30)
br label %37
37: ; preds = %35, %33
ret i32 0
}