#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() {
  int i, last, first, middle, n, search, array[100];
  printf("Please enter the number of elements \n");
  scanf("%d", &n);
  printf("Please enter the array\n");
  for (i = 0; i <= n; i++) {
    scanf("%d", &array[i]);
  }
  printf("The entered array is\n");
  for (i = 0; i <= n; i++) {
    printf("%d \n", array[i]);
  }
  printf("Please enter the value to be searched\n");
  scanf("%d", &search);
  first = 0;
  last = n - 1;
  while (first <= last) {
    middle = (last + first) / 2;
    if (array[middle] < search)
      first = middle + 1;
    else if (array[middle] == search) {
      printf("searched element found at location %d %d \n", search, middle + 1);
      break;
    } else {
      last = middle - 1;
      middle = (first + last) / 2;
    }
  }
  if (first > last) {
    printf("oops!!! %d searched element not found\n", search);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xedf(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <scanf@plt>
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     119d <main+0x7d>
movslq -0x8(%rbp),%rax
lea    -0x1b0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe9c(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1165 <main+0x45>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     11e5 <main+0xc5>
movslq -0x8(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%esi
lea    0xed4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b2 <main+0x92>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe2c(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     12a6 <main+0x186>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%eax
cmp    -0x1c(%rbp),%eax
jge    1252 <main+0x132>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12a1 <main+0x181>
movslq -0x14(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%eax
cmp    -0x1c(%rbp),%eax
jne    1282 <main+0x162>
mov    -0x1c(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12a6 <main+0x186>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
jmp    12a1 <main+0x181>
jmp    1213 <main+0xf3>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    12c3 <main+0x1a3>
mov    -0x1c(%rbp),%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1a0,%rsp
lea    0xf12(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb1(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
lea    0xf1a(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,0x8(%rsp)
js     119d <main+0x6d>
mov    $0xffffffffffffffff,%r15
lea    0x10(%rsp),%rbx
lea    0xe81(%rip),%r14
nop
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <scanf@plt>
movslq 0x8(%rsp),%rax
add    $0x1,%r15
add    $0x4,%rbx
cmp    %rax,%r15
jl     1180 <main+0x50>
lea    0xeed(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,0x8(%rsp)
js     11dc <main+0xac>
mov    $0xffffffffffffffff,%rbx
lea    0xe6b(%rip),%r14
xchg   %ax,%ax
mov    0x14(%rsp,%rbx,4),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movslq 0x8(%rsp),%rax
add    $0x1,%rbx
cmp    %rax,%rbx
jl     11c0 <main+0x90>
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
lea    0xe11(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
jle    123b <main+0x10b>
add    $0xffffffff,%eax
xor    %ecx,%ecx
mov    0xc(%rsp),%esi
jmp    1219 <main+0xe9>
nopl   0x0(%rax)
add    $0x1,%edx
mov    %edx,%ecx
cmp    %eax,%ecx
jg     123f <main+0x10f>
lea    (%rcx,%rax,1),%edi
mov    %edi,%edx
shr    $0x1f,%edx
add    %edi,%edx
sar    %edx
movslq %edx,%rdi
cmp    %esi,0x10(%rsp,%rdi,4)
jl     1210 <main+0xe0>
je     124f <main+0x11f>
add    $0xffffffff,%edx
mov    %edx,%eax
cmp    %eax,%ecx
jle    1219 <main+0xe9>
jmp    123f <main+0x10f>
mov    0xc(%rsp),%esi
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1260 <main+0x130>
add    $0x1,%edx
lea    0xdaa(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x1a0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [38 x i8] c"Please enter the number of elements \0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [24 x i8] c"Please enter the array\0A\00"
@.str.3 = [22 x i8] c"The entered array is\0A\00"
@.str.4 = [5 x i8] c"%d \0A\00"
@.str.5 = [39 x i8] c"Please enter the value to be searched\0A\00"
@.str.6 = [43 x i8] c"searched element found at location %d %d \0A\00"
@.str.7 = [39 x i8] c"oops!!! %d searched element not found\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i32]
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 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* %6)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %12
12: ; preds = %21, %0
%13 = load i32, i32* %2
%14 = load i32, i32* %6
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load i32, i32* %2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 %18
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %19)
br label %21
21: ; preds = %16
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %12
24: ; preds = %12
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %26
26: ; preds = %36, %24
%27 = load i32, i32* %2
%28 = load i32, i32* %6
%29 = icmp sle i32 %27, %28
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i32, i32* %2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %30
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
br label %26
39: ; preds = %26
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
store i32 0, i32* %4
%42 = load i32, i32* %6
%43 = sub nsw i32 %42, 1
store i32 %43, i32* %3
br label %44
44: ; preds = %82, %39
%45 = load i32, i32* %4
%46 = load i32, i32* %3
%47 = icmp sle i32 %45, %46
br i1 %47, label %48, label %83
48: ; preds = %44
%49 = load i32, i32* %3
%50 = load i32, i32* %4
%51 = add nsw i32 %49, %50
%52 = sdiv i32 %51, 2
store i32 %52, i32* %5
%53 = load i32, i32* %5
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 %54
%56 = load i32, i32* %55
%57 = load i32, i32* %7
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %62
59: ; preds = %48
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %4
br label %82
62: ; preds = %48
%63 = load i32, i32* %5
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [100 x i32], [100 x i32]* %8, i64 0, i64 %64
%66 = load i32, i32* %65
%67 = load i32, i32* %7
%68 = icmp eq i32 %66, %67
br i1 %68, label %69, label %74
69: ; preds = %62
%70 = load i32, i32* %7
%71 = load i32, i32* %5
%72 = add nsw i32 %71, 1
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i32 %70, i32 %72)
br label %83
74: ; preds = %62
%75 = load i32, i32* %5
%76 = sub nsw i32 %75, 1
store i32 %76, i32* %3
%77 = load i32, i32* %4
%78 = load i32, i32* %3
%79 = add nsw i32 %77, %78
%80 = sdiv i32 %79, 2
store i32 %80, i32* %5
br label %81
81: ; preds = %74
br label %82
82: ; preds = %81, %59
br label %44
83: ; preds = %69, %44
%84 = load i32, i32* %4
%85 = load i32, i32* %3
%86 = icmp sgt i32 %84, %85
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = load i32, i32* %7
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0), i32 %88)
br label %90
90: ; preds = %87, %83
%91 = load i32, i32* %1
ret i32 %91
}

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