#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 *);
main() {
  int n;
  int i;
  int item;
  do {
    printf("Enter the number of elements\n");
    scanf("%d", &n);
  } while (n < 1);
  int arr[n];
  printf("\nEnter the elements in ascending order...\n");
  for (i = 0; i < n; i++) {
    printf("\nEnter the element\n");
    scanf("%d", &arr[i]);
  }
  printf("\nArray is: {");
  for (i = 0; i < n; i++)
    printf(" %d ", arr[i]);
  printf("}\n");
  printf("\nEnter the item to be searched\n");
  scanf("%d", &item);
  int start = 0, end = n - 1, mid;
  while (start <= end) {
    mid = (start + end) / 2;
    if (arr[mid] == item) {
      printf("\n%d found at position %d\n", item, mid + 1);
      break;
    } else if (arr[mid] > item)
      end = mid - 1;
    else
      start = mid + 1;
  }
  if (start > end)
    printf("\n%d not found!\n", item);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeda(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
cmpl   $0x1,-0x8(%rbp)
jl     112f <main+0xf>
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    0xe9c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11d6 <main+0xb6>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe5b(%rip),%rdi
call   1040 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1193 <main+0x73>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    121e <main+0xfe>
mov    -0x38(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11eb <main+0xcb>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xddd(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jg     12de <main+0x1be>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,-0x2c(%rbp)
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x10(%rbp),%eax
jne    12a9 <main+0x189>
mov    -0x10(%rbp),%esi
mov    -0x2c(%rbp),%edx
add    $0x1,%edx
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12de <main+0x1be>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x10(%rbp),%eax
jle    12cb <main+0x1ab>
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12d4 <main+0x1b4>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12d9 <main+0x1b9>
jmp    125a <main+0x13a>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    12fb <main+0x1db>
mov    -0x10(%rbp),%esi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xef7(%rip),%r14
lea    0xeb1(%rip),%r15
lea    -0x2c(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <scanf@plt>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jle    1160 <main+0x30>
mov    %rsp,-0x38(%rbp)
mov    %rsp,%r13
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xec2(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    11e4 <main+0xb4>
xor    %r12d,%r12d
lea    0xef8(%rip),%r14
lea    0xe4a(%rip),%r15
mov    %r13,%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <scanf@plt>
add    $0x1,%r12
movslq -0x2c(%rbp),%rax
add    $0x4,%rbx
cmp    %rax,%r12
jl     11c0 <main+0x90>
lea    0xe18(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    122c <main+0xfc>
lea    0xe11(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13,%rbx,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
movslq -0x2c(%rbp),%rax
cmp    %rax,%rbx
jl     1210 <main+0xe0>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb5(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jle    129e <main+0x16e>
add    $0xffffffff,%eax
xor    %ecx,%ecx
mov    -0x30(%rbp),%esi
jmp    1279 <main+0x149>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%edx
mov    %edx,%ecx
cmp    %eax,%ecx
jg     12a1 <main+0x171>
lea    (%rax,%rcx,1),%edi
mov    %edi,%edx
shr    $0x1f,%edx
add    %edi,%edx
sar    %edx
movslq %edx,%rdi
mov    0x0(%r13,%rdi,4),%edi
cmp    %esi,%edi
je     12b1 <main+0x181>
jle    1270 <main+0x140>
add    $0xffffffff,%edx
mov    %edx,%eax
cmp    %eax,%ecx
jle    1279 <main+0x149>
jmp    12a1 <main+0x171>
mov    -0x30(%rbp),%esi
lea    0xd87(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12c2 <main+0x192>
add    $0x1,%edx
lea    0xd5a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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 = [43 x i8] c"\0AEnter the elements in ascending order...\0A\00"
@.str.3 = [20 x i8] c"\0AEnter the element\0A\00"
@.str.4 = [13 x i8] c"\0AArray is: {\00"
@.str.5 = [5 x i8] c" %d \00"
@.str.6 = [3 x i8] c"}\0A\00"
@.str.7 = [32 x i8] c"\0AEnter the item to be searched\0A\00"
@.str.8 = [26 x i8] c"\0A%d found at position %d\0A\00"
@.str.9 = [16 x i8] c"\0A%d not found!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
br label %10
10: ; preds = %13, %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
br label %13
13: ; preds = %10
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 1
br i1 %15, label %10, label %16
16: ; preds = %13
%17 = load i32, i32* %2
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %5
%20 = alloca i32, i64 %18
store i64 %18, i64* %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %22
22: ; preds = %32, %16
%23 = load i32, i32* %3
%24 = load i32, i32* %2
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %22
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %20, i64 %29
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %30)
br label %32
32: ; preds = %26
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %22
35: ; preds = %22
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %37
37: ; preds = %47, %35
%38 = load i32, i32* %3
%39 = load i32, i32* %2
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %50
41: ; preds = %37
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %20, i64 %43
%45 = load i32, i32* %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %41
%48 = load i32, i32* %3
%49 = add nsw i32 %48, 1
store i32 %49, i32* %3
br label %37
50: ; preds = %37
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
%53 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
store i32 0, i32* %7
%54 = load i32, i32* %2
%55 = sub nsw i32 %54, 1
store i32 %55, i32* %8
br label %56
56: ; preds = %90, %50
%57 = load i32, i32* %7
%58 = load i32, i32* %8
%59 = icmp sle i32 %57, %58
br i1 %59, label %60, label %91
60: ; preds = %56
%61 = load i32, i32* %7
%62 = load i32, i32* %8
%63 = add nsw i32 %61, %62
%64 = sdiv i32 %63, 2
store i32 %64, i32* %9
%65 = load i32, i32* %9
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i32, i32* %20, i64 %66
%68 = load i32, i32* %67
%69 = load i32, i32* %4
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %76
71: ; preds = %60
%72 = load i32, i32* %4
%73 = load i32, i32* %9
%74 = add nsw i32 %73, 1
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %72, i32 %74)
br label %91
76: ; preds = %60
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32, i32* %20, i64 %78
%80 = load i32, i32* %79
%81 = load i32, i32* %4
%82 = icmp sgt i32 %80, %81
br i1 %82, label %83, label %86
83: ; preds = %76
%84 = load i32, i32* %9
%85 = sub nsw i32 %84, 1
store i32 %85, i32* %8
br label %89
86: ; preds = %76
%87 = load i32, i32* %9
%88 = add nsw i32 %87, 1
store i32 %88, i32* %7
br label %89
89: ; preds = %86, %83
br label %90
90: ; preds = %89
br label %56
91: ; preds = %71, %56
%92 = load i32, i32* %7
%93 = load i32, i32* %8
%94 = icmp sgt i32 %92, %93
br i1 %94, label %95, label %98
95: ; preds = %91
%96 = load i32, i32* %4
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i32 %96)
br label %98
98: ; preds = %95, %91
%99 = load i8*, i8** %5
%100 = load i32, i32* %1
ret i32 %100
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.4 = [13 x i8] c"\0AArray is: {\00"
@.str.5 = [5 x i8] c" %d \00"
@.str.8 = [26 x i8] c"\0A%d found at position %d\0A\00"
@.str.9 = [16 x i8] c"\0A%d not found!\0A\00"
@str = [29 x i8] c"Enter the number of elements\00"
@str.10 = [42 x i8] c"\0AEnter the elements in ascending order...\00"
@str.11 = [2 x i8] c"}\00"
@str.12 = [31 x i8] c"\0AEnter the item to be searched\00"
@str.13 = [19 x i8] c"\0AEnter the element\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
br label %5
5: ; preds = %5, %0
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 1
br i1 %9, label %5, label %10
10: ; preds = %5
%11 = zext i32 %8 to i64
%12 = call i8* @llvm.stacksave()
%13 = alloca i32, i64 %11
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.10, i64 0, i64 0))
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %10, %17
%18 = phi i64 [ %22, %17 ], [ 0, %10 ]
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.13, i64 0, i64 0))
%20 = getelementptr inbounds i32, i32* %13, i64 %18
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %20)
%22 = add nuw nsw i64 %18, 1
%23 = load i32, i32* %1
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %22, %24
br i1 %25, label %17, label %26
26: ; preds = %17, %10
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32, i32* %1
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %26, %30
%31 = phi i64 [ %35, %30 ], [ 0, %26 ]
%32 = getelementptr inbounds i32, i32* %13, i64 %31
%33 = load i32, i32* %32
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = add nuw nsw i64 %31, 1
%36 = load i32, i32* %1
%37 = sext i32 %36 to i64
%38 = icmp slt i64 %35, %37
br i1 %38, label %30, label %39
39: ; preds = %30, %26
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.11, i64 0, i64 0))
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.12, i64 0, i64 0))
%42 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%43 = load i32, i32* %1
%44 = icmp slt i32 %43, 1
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = load i32, i32* %2
br label %69
47: ; preds = %39
%48 = add nsw i32 %43, -1
%49 = load i32, i32* %2
br label %50
50: ; preds = %47, %59
%51 = phi i32 [ %48, %47 ], [ %64, %59 ]
%52 = phi i32 [ 0, %47 ], [ %63, %59 ]
%53 = add nsw i32 %51, %52
%54 = sdiv i32 %53, 2
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %13, i64 %55
%57 = load i32, i32* %56
%58 = icmp eq i32 %57, %49
br i1 %58, label %66, label %59
59: ; preds = %50
%60 = icmp sgt i32 %57, %49
%61 = add nsw i32 %54, -1
%62 = add nsw i32 %54, 1
%63 = select i1 %60, i32 %52, i32 %62
%64 = select i1 %60, i32 %61, i32 %51
%65 = icmp sgt i32 %63, %64
br i1 %65, label %69, label %50
66: ; preds = %50
%67 = add nsw i32 %54, 1
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %49, i32 %67)
br label %72
69: ; preds = %59, %45
%70 = phi i32 [ %46, %45 ], [ %49, %59 ]
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i32 %70)
br label %72
72: ; preds = %66, %69
ret i32 0
}