#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 a[100], j, mid, n, i = 0, key, flag = 0;
  printf("Enter number of elements");
  scanf("%d", &n);
  printf("Enter element \n");
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  j = n - 1;
  i = 0;
  printf("Enter key to search ");
  scanf("%d", &key);
  while (i <= j) {
    mid = i + j / 2;
    if (a[mid] == key) {
      flag = 1;
      break;
    } else if (a[mid] < key)
      i = mid + 1;
    else
      j = mid - 1;
  }
  if (flag == 1)
    printf("Item found in array\n");
  else
    printf("Item not found..\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1b0(%rbp)
movl   $0x0,-0x1b8(%rbp)
lea    0xeb3(%rip),%rdi
call   1030 <printf@plt>
lea    0xec0(%rip),%rdi
lea    -0x1ac(%rbp),%rsi
call   1040 <scanf@plt>
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x1b0(%rbp)
mov    -0x1b0(%rbp),%eax
cmp    -0x1ac(%rbp),%eax
jge    11c2 <main+0xa2>
movslq -0x1b0(%rbp),%rax
lea    -0x1a0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe70(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x1b0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b0(%rbp)
jmp    117b <main+0x5b>
mov    -0x1ac(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1a4(%rbp)
movl   $0x0,-0x1b0(%rbp)
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
lea    0xe2b(%rip),%rdi
lea    -0x1b4(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x1b0(%rbp),%eax
cmp    -0x1a4(%rbp),%eax
jg     12a6 <main+0x186>
mov    -0x1b0(%rbp),%eax
mov    %eax,-0x1bc(%rbp)
mov    -0x1a4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1bc(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1a8(%rbp)
movslq -0x1a8(%rbp),%rax
mov    -0x1a0(%rbp,%rax,4),%eax
cmp    -0x1b4(%rbp),%eax
jne    125f <main+0x13f>
movl   $0x1,-0x1b8(%rbp)
jmp    12a6 <main+0x186>
movslq -0x1a8(%rbp),%rax
mov    -0x1a0(%rbp,%rax,4),%eax
cmp    -0x1b4(%rbp),%eax
jge    128d <main+0x16d>
mov    -0x1a8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b0(%rbp)
jmp    129c <main+0x17c>
mov    -0x1a8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1a4(%rbp)
jmp    12a1 <main+0x181>
jmp    11fa <main+0xda>
cmpl   $0x1,-0x1b8(%rbp)
jne    12c4 <main+0x1a4>
lea    0xd87(%rip),%rdi
call   1030 <printf@plt>
jmp    12d0 <main+0x1b0>
lea    0xd8b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1a8,%rsp
lea    0xebb(%rip),%rdi
call   1040 <printf@plt>
lea    0xec8(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
lea    0xecf(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%r15d
test   %r15d,%r15d
jle    119d <main+0x6d>
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
lea    0xe9a(%rip),%r14
nop
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%r15
add    $0x4,%r12
cmp    %r15,%rbx
jl     1180 <main+0x50>
lea    0xe78(%rip),%rdi
call   1040 <printf@plt>
lea    0xe69(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
lea    0xe7f(%rip),%rdi
test   %r15d,%r15d
jle    1214 <main+0xe4>
add    $0xffffffff,%r15d
xor    %edx,%edx
mov    0xc(%rsp),%ecx
lea    0xe7a(%rip),%rax
jmp    11eb <main+0xbb>
nopl   0x0(%rax)
add    $0xffffffff,%esi
mov    %esi,%r15d
cmp    %r15d,%edx
jg     1214 <main+0xe4>
mov    %r15d,%esi
shr    $0x1f,%esi
add    %r15d,%esi
sar    %esi
add    %edx,%esi
movslq %esi,%rbx
mov    0x10(%rsp,%rbx,4),%ebx
cmp    %ecx,%ebx
je     1211 <main+0xe1>
jge    11e0 <main+0xb0>
add    $0x1,%esi
mov    %esi,%edx
cmp    %r15d,%edx
jle    11eb <main+0xbb>
jmp    1214 <main+0xe4>
mov    %rax,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Enter number of elements\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [16 x i8] c"Enter element \0A\00"
@.str.3 = [21 x i8] c"Enter key to search \00"
@.str.4 = [21 x i8] c"Item found in array\0A\00"
@.str.5 = [18 x i8] c"Item not found..\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
store i32 0, i32* %8
%9 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 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* %5)
%11 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %12
12: ; preds = %21, %0
%13 = load i32, i32* %6
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [100 x i32], [100 x i32]* %2, 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* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
br label %12
24: ; preds = %12
%25 = load i32, i32* %5
%26 = sub nsw i32 %25, 1
store i32 %26, i32* %3
store i32 0, i32* %6
%27 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 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* %7)
br label %29
29: ; preds = %59, %24
%30 = load i32, i32* %6
%31 = load i32, i32* %3
%32 = icmp sle i32 %30, %31
br i1 %32, label %33, label %60
33: ; preds = %29
%34 = load i32, i32* %6
%35 = load i32, i32* %3
%36 = sdiv i32 %35, 2
%37 = add nsw i32 %34, %36
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* %7
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %33
store i32 1, i32* %8
br label %60
45: ; preds = %33
%46 = load i32, i32* %4
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [100 x i32], [100 x i32]* %2, i64 0, i64 %47
%49 = load i32, i32* %48
%50 = load i32, i32* %7
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %45
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %58
55: ; preds = %45
%56 = load i32, i32* %4
%57 = sub nsw i32 %56, 1
store i32 %57, i32* %3
br label %58
58: ; preds = %55, %52
br label %59
59: ; preds = %58
br label %29
60: ; preds = %44, %29
%61 = load i32, i32* %8
%62 = icmp eq i32 %61, 1
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %67
65: ; preds = %60
%66 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %65, %63
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str = [25 x i8] c"Enter number of elements\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [21 x i8] c"Enter key to search \00"
@str = [15 x i8] c"Enter element \00"
@str.6 = [17 x i8] c"Item not found..\00"
@str.7 = [20 x i8] c"Item found in array\00"
define i32 @main() {
%1 = alloca [100 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [100 x i32]* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 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 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %0, %12
%13 = phi i64 [ %16, %12 ], [ 0, %0 ]
%14 = getelementptr inbounds [100 x i32], [100 x i32]* %1, 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* %2
%18 = sext i32 %17 to i64
%19 = icmp slt i64 %16, %18
br i1 %19, label %12, label %20
20: ; preds = %12, %0
%21 = phi i32 [ %10, %0 ], [ %17, %12 ]
%22 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, 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 = icmp slt i32 %21, 1
br i1 %24, label %44, label %25
25: ; preds = %20
%26 = add nsw i32 %21, -1
%27 = load i32, i32* %3
br label %28
28: ; preds = %25, %37
%29 = phi i32 [ 0, %25 ], [ %42, %37 ]
%30 = phi i32 [ %26, %25 ], [ %41, %37 ]
%31 = sdiv i32 %30, 2
%32 = add nsw i32 %29, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = icmp eq i32 %35, %27
br i1 %36, label %44, label %37
37: ; preds = %28
%38 = icmp slt i32 %35, %27
%39 = add nsw i32 %32, 1
%40 = add nsw i32 %32, -1
%41 = select i1 %38, i32 %30, i32 %40
%42 = select i1 %38, i32 %39, i32 %29
%43 = icmp sgt i32 %42, %41
br i1 %43, label %44, label %28
44: ; preds = %37, %28, %20
%45 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0), %20 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0), %28 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0), %37 ]
%46 = call i32 @puts(i8* dereferenceable(1) %45)
ret i32 0
}