#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*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main(void) {
  int n, a[30], item, i, j, mid, top, bottom;
  printf("Enter how many elements you want:\n");
  scanf("%d", &n);
  printf("Enter the %d elements in ascending order\n", n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  printf("\nEnter the item to  search\n");
  scanf("%d", &item);
  bottom = 1;
  top = n;
  do {
    mid = (bottom + top) / 2;
    if (item < a[mid])
      top = mid - 1;
    else if (item > a[mid])
      bottom = mid + 1;
  } while (item != a[mid] && bottom <= top);
  if (item == a[mid]) {
    printf("Binary search successfull!!\n");
    printf("\n %d found in position: %d\n", item, mid + 1);
  } else {
    printf("\n  Search failed\n %d not found\n", item);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xedc(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%esi
lea    0xecc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x88(%rbp)
mov    -0x88(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ac <main+0x8c>
movslq -0x88(%rbp),%rax
lea    -0x80(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe90(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x88(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x88(%rbp)
jmp    116b <main+0x4b>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe62(%rip),%rdi
lea    -0x84(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x1,-0x98(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x94(%rbp)
mov    -0x98(%rbp),%eax
add    -0x94(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x90(%rbp)
mov    -0x84(%rbp),%eax
movslq -0x90(%rbp),%rcx
cmp    -0x80(%rbp,%rcx,4),%eax
jge    1225 <main+0x105>
mov    -0x90(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x94(%rbp)
jmp    1250 <main+0x130>
mov    -0x84(%rbp),%eax
movslq -0x90(%rbp),%rcx
cmp    -0x80(%rbp,%rcx,4),%eax
jle    124b <main+0x12b>
mov    -0x90(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x98(%rbp)
jmp    1250 <main+0x130>
jmp    1255 <main+0x135>
mov    -0x84(%rbp),%ecx
movslq -0x90(%rbp),%rdx
xor    %eax,%eax
cmp    -0x80(%rbp,%rdx,4),%ecx
mov    %al,-0x99(%rbp)
je     1289 <main+0x169>
mov    -0x98(%rbp),%eax
cmp    -0x94(%rbp),%eax
setle  %al
mov    %al,-0x99(%rbp)
mov    -0x99(%rbp),%al
test   $0x1,%al
jne    11e0 <main+0xc0>
mov    -0x84(%rbp),%eax
movslq -0x90(%rbp),%rcx
cmp    -0x80(%rbp,%rcx,4),%eax
jne    12de <main+0x1be>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x84(%rbp),%esi
mov    -0x90(%rbp),%edx
add    $0x1,%edx
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f2 <main+0x1d2>
mov    -0x84(%rbp),%esi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
lea    0xf26(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb1(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%esi
lea    0xe9f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    119d <main+0x6d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xe80(%rip),%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     1180 <main+0x50>
lea    0xee7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe50(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%eax
mov    $0x1,%edx
mov    0xc(%rsp),%esi
nopw   0x0(%rax,%rax,1)
mov    %edx,%edi
mov    %eax,%r8d
lea    (%rdi,%r8,1),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rbx
mov    0x10(%rsp,%rbx,4),%ecx
lea    -0x1(%rbx),%eax
add    $0x1,%ebx
cmp    %ecx,%esi
cmovg  %ebx,%edx
cmovge %r8d,%eax
cmovl  %edi,%edx
je     1201 <main+0xd1>
cmp    %eax,%edx
jle    11d0 <main+0xa0>
cmp    %ecx,%esi
jne    1227 <main+0xf7>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%esi
lea    0xe11(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1235 <main+0x105>
lea    0xe1b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Enter how many elements you want:\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [42 x i8] c"Enter the %d elements in ascending order\0A\00"
@.str.3 = [28 x i8] c"\0AEnter the item to search\0A\00"
@.str.4 = [29 x i8] c"Binary search successfull!!\0A\00"
@.str.5 = [28 x i8] c"\0A %d found in position: %d\0A\00"
@.str.6 = [32 x i8] c"\0A Search failed\0A %d not found\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [30 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%12 = load i32, i32* %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %12)
store i32 0, i32* %5
br label %14
14: ; preds = %23, %0
%15 = load i32, i32* %5
%16 = load i32, i32* %2
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [30 x i32], [30 x i32]* %3, i64 0, i64 %20
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %21)
br label %23
23: ; preds = %18
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %14
26: ; preds = %14
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
store i32 1, i32* %9
%29 = load i32, i32* %2
store i32 %29, i32* %8
br label %30
30: ; preds = %67, %26
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = add nsw i32 %31, %32
%34 = sdiv i32 %33, 2
store i32 %34, i32* %7
%35 = load i32, i32* %4
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [30 x i32], [30 x i32]* %3, i64 0, i64 %37
%39 = load i32, i32* %38
%40 = icmp slt i32 %35, %39
br i1 %40, label %41, label %44
41: ; preds = %30
%42 = load i32, i32* %7
%43 = sub nsw i32 %42, 1
store i32 %43, i32* %8
br label %55
44: ; preds = %30
%45 = load i32, i32* %4
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [30 x i32], [30 x i32]* %3, i64 0, i64 %47
%49 = load i32, i32* %48
%50 = icmp sgt i32 %45, %49
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %54
54: ; preds = %51, %44
br label %55
55: ; preds = %54, %41
br label %56
56: ; preds = %55
%57 = load i32, i32* %4
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [30 x i32], [30 x i32]* %3, i64 0, i64 %59
%61 = load i32, i32* %60
%62 = icmp ne i32 %57, %61
br i1 %62, label %63, label %67
63: ; preds = %56
%64 = load i32, i32* %9
%65 = load i32, i32* %8
%66 = icmp sle i32 %64, %65
br label %67
67: ; preds = %63, %56
%68 = phi i1 [ false, %56 ], [ %66, %63 ]
br i1 %68, label %30, label %69
69: ; preds = %67
%70 = load i32, i32* %4
%71 = load i32, i32* %7
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [30 x i32], [30 x i32]* %3, i64 0, i64 %72
%74 = load i32, i32* %73
%75 = icmp eq i32 %70, %74
br i1 %75, label %76, label %82
76: ; preds = %69
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%78 = load i32, i32* %4
%79 = load i32, i32* %7
%80 = add nsw i32 %79, 1
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %78, i32 %80)
br label %85
82: ; preds = %69
%83 = load i32, i32* %4
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %83)
br label %85
85: ; preds = %82, %76
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [42 x i8] c"Enter the %d elements in ascending order\0A\00"
@.str.5 = [28 x i8] c"\0A %d found in position: %d\0A\00"
@.str.6 = [32 x i8] c"\0A Search failed\0A %d not found\0A\00"
@str = [34 x i8] c"Enter how many elements you want:\00"
@str.7 = [27 x i8] c"\0AEnter the item to search\00"
@str.8 = [28 x i8] c"Binary search successfull!!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [30 x i32]
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast [30 x i32]* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 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* %1)
%9 = load i32, i32* %1
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %0, %13
%14 = phi i64 [ %17, %13 ], [ 0, %0 ]
%15 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 %14
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %15)
%17 = add nuw nsw i64 %14, 1
%18 = load i32, i32* %1
%19 = sext i32 %18 to i64
%20 = icmp slt i64 %17, %19
br i1 %20, label %13, label %21
21: ; preds = %13, %0
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%24 = load i32, i32* %1
%25 = load i32, i32* %3
br label %26
26: ; preds = %26, %21
%27 = phi i32 [ %24, %21 ], [ %39, %26 ]
%28 = phi i32 [ 1, %21 ], [ %40, %26 ]
%29 = add nsw i32 %28, %27
%30 = sdiv i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [30 x i32], [30 x i32]* %2, i64 0, i64 %31
%33 = load i32, i32* %32
%34 = icmp slt i32 %25, %33
%35 = add nsw i32 %30, -1
%36 = icmp sgt i32 %25, %33
%37 = add nsw i32 %30, 1
%38 = select i1 %36, i32 %37, i32 %28
%39 = select i1 %34, i32 %35, i32 %27
%40 = select i1 %34, i32 %28, i32 %38
%41 = icmp ne i32 %25, %33
%42 = icmp sle i32 %40, %39
%43 = select i1 %41, i1 %42, i1 false
br i1 %43, label %26, label %44
44: ; preds = %26
%45 = icmp eq i32 %25, %33
br i1 %45, label %46, label %50
46: ; preds = %44
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.8, i64 0, i64 0))
%48 = load i32, i32* %3
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %48, i32 %37)
br label %52
50: ; preds = %44
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %25)
br label %52
52: ; preds = %50, %46
ret i32 0
}