#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, mid, low, a[20], n, up, item;
  printf("Enter no. of elements\n");
  scanf("%d", &n);
  printf("Enter elements in the array\n");
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  printf("Enter the element to be searched\n");
  scanf("%d", &item);
  low = 0;
  up = n - 1;
  while (low <= up) {
    mid = (low + up) / 2;
    if (a[mid] > item) {
      up = mid - 1;
    } else if (a[mid] < item) {
      low = mid + 1;
    } else {
      printf("Found at index %d", mid);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf21(%rip),%rdi
lea    -0x64(%rbp),%rsi
call   1040 <scanf@plt>
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x64(%rbp),%eax
jge    1197 <main+0x77>
movslq -0x8(%rbp),%rax
lea    -0x60(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xee1(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1162 <main+0x42>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb9(%rip),%rdi
lea    -0x6c(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x64(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x68(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x68(%rbp),%eax
jg     1240 <main+0x120>
mov    -0x10(%rbp),%eax
add    -0x68(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%eax
cmp    -0x6c(%rbp),%eax
jle    1201 <main+0xe1>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    123b <main+0x11b>
movslq -0xc(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%eax
cmp    -0x6c(%rbp),%eax
jge    1220 <main+0x100>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1236 <main+0x116>
mov    -0xc(%rbp),%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1240 <main+0x120>
jmp    123b <main+0x11b>
jmp    11c5 <main+0xa5>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x60,%rsp
lea    0xed2(%rip),%rdi
call   1030 <puts@plt>
lea    0xec3(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
lea    0xecb(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,0x8(%rsp)
jle    119d <main+0x6d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xe98(%rip),%r14
nopw   0x0(%rax,%rax,1)
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    0xea0(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5f(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
jle    1209 <main+0xd9>
add    $0xffffffff,%eax
xor    %edx,%edx
mov    0xc(%rsp),%ecx
jmp    11d9 <main+0xa9>
nopl   (%rax)
add    $0xffffffff,%esi
mov    %esi,%eax
cmp    %eax,%edx
jg     1209 <main+0xd9>
lea    (%rax,%rdx,1),%edi
mov    %edi,%esi
shr    $0x1f,%esi
add    %edi,%esi
sar    %esi
movslq %esi,%rdi
cmp    %ecx,0x10(%rsp,%rdi,4)
jg     11d0 <main+0xa0>
jge    11fb <main+0xcb>
add    $0x1,%esi
mov    %esi,%edx
cmp    %eax,%edx
jle    11d9 <main+0xa9>
jmp    1209 <main+0xd9>
lea    0xdfe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Enter no. of elements\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [29 x i8] c"Enter elements in the array\0A\00"
@.str.3 = [34 x i8] c"Enter the element to be searched\0A\00"
@.str.4 = [18 x i8] c"Found at index %d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [20 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 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 ([29 x i8], [29 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 slt 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 [20 x i32], [20 x i32]* %5, 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 ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
store i32 0, i32* %4
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 1
store i32 %28, i32* %7
br label %29
29: ; preds = %61, %24
%30 = load i32, i32* %4
%31 = load i32, i32* %7
%32 = icmp sle i32 %30, %31
br i1 %32, label %33, label %62
33: ; preds = %29
%34 = load i32, i32* %4
%35 = load i32, i32* %7
%36 = add nsw i32 %34, %35
%37 = sdiv i32 %36, 2
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [20 x i32], [20 x i32]* %5, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* %8
%43 = icmp sgt i32 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %33
%45 = load i32, i32* %3
%46 = sub nsw i32 %45, 1
store i32 %46, i32* %7
br label %61
47: ; preds = %33
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [20 x i32], [20 x i32]* %5, i64 0, i64 %49
%51 = load i32, i32* %50
%52 = load i32, i32* %8
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %4
br label %60
57: ; preds = %47
%58 = load i32, i32* %3
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %62
60: ; preds = %54
br label %61
61: ; preds = %60, %44
br label %29
62: ; preds = %57, %29
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"Found at index %d\00"
@str = [22 x i8] c"Enter no. of elements\00"
@str.5 = [28 x i8] c"Enter elements in the array\00"
@str.6 = [33 x i8] c"Enter the element to be searched\00"
define i32 @main() {
%1 = alloca [20 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [20 x i32]* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 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 ([28 x i8], [28 x i8]* @str.5, 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 [20 x i32], [20 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 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%23 = load i32, i32* %2
%24 = icmp slt i32 %23, 1
br i1 %24, label %49, label %25
25: ; preds = %20
%26 = add nsw i32 %23, -1
%27 = load i32, i32* %3
br label %28
28: ; preds = %25, %45
%29 = phi i32 [ %26, %25 ], [ %47, %45 ]
%30 = phi i32 [ 0, %25 ], [ %46, %45 ]
%31 = add nsw i32 %29, %30
%32 = sdiv i32 %31, 2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [20 x i32], [20 x i32]* %1, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = icmp sgt i32 %35, %27
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = add nsw i32 %32, -1
br label %45
39: ; preds = %28
%40 = icmp slt i32 %35, %27
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = add nsw i32 %32, 1
br label %45
43: ; preds = %39
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %49
45: ; preds = %41, %37
%46 = phi i32 [ %30, %37 ], [ %42, %41 ]
%47 = phi i32 [ %38, %37 ], [ %29, %41 ]
%48 = icmp sgt i32 %46, %47
br i1 %48, label %49, label %28
49: ; preds = %45, %20, %43
ret i32 0
}