#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 n, i, a[10], key, low, high, mid, flag = 0;
  printf("\n Enter the no. of elements : ");
  scanf("%d", &n);
  printf("\n Enter %d elements in ascending order ", n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  printf("\n Enter the key element to search : ");
  scanf("%d", &key);
  low = 0;
  high = n - 1;
  while (low <= high) {
    mid = (low + high) / 2;
    if (key == a[mid]) {
      flag = 1;
      break;
    }
    if (key > a[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  if (flag == 1)
    printf("\n Successful Search ");
  else
    printf("\n Unsuccessful Search");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x54(%rbp)
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed4(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%esi
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11a1 <main+0x81>
movslq -0xc(%rbp),%rax
lea    -0x40(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe91(%rip),%rdi
call   1040 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116c <main+0x4c>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe69(%rip),%rdi
lea    -0x44(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jg     1236 <main+0x116>
mov    -0x48(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x50(%rbp)
mov    -0x44(%rbp),%eax
movslq -0x50(%rbp),%rcx
cmp    -0x40(%rbp,%rcx,4),%eax
jne    1209 <main+0xe9>
movl   $0x1,-0x54(%rbp)
jmp    1236 <main+0x116>
mov    -0x44(%rbp),%eax
movslq -0x50(%rbp),%rcx
cmp    -0x40(%rbp,%rcx,4),%eax
jle    1228 <main+0x108>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1231 <main+0x111>
mov    -0x50(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11cf <main+0xaf>
cmpl   $0x1,-0x54(%rbp)
jne    1253 <main+0x133>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1261 <main+0x141>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
lea    0xed0(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xee1(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x8(%rsp),%esi
lea    0xecf(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    118d <main+0x6d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xeb0(%rip),%r14
nop
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     1170 <main+0x50>
lea    0xeb6(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe7b(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x8(%rsp),%eax
lea    0xecb(%rip),%rdi
test   %eax,%eax
jle    1200 <main+0xe0>
add    $0xffffffff,%eax
mov    0xc(%rsp),%edx
lea    0xea4(%rip),%r8
jmp    11d9 <main+0xb9>
nopl   (%rax)
add    $0xffffffff,%esi
mov    %esi,%eax
cmp    %eax,%ebx
jg     1200 <main+0xe0>
lea    (%rax,%rbx,1),%ecx
mov    %ecx,%esi
shr    $0x1f,%esi
add    %ecx,%esi
sar    %esi
movslq %esi,%rcx
mov    0x10(%rsp,%rcx,4),%ecx
cmp    %ecx,%edx
je     11fd <main+0xdd>
jle    11d0 <main+0xb0>
add    $0x1,%esi
mov    %esi,%ebx
cmp    %eax,%ebx
jle    11d9 <main+0xb9>
jmp    1200 <main+0xe0>
mov    %r8,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [31 x i8] c"\0A Enter the no. of elements : \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [40 x i8] c"\0A Enter %d elements in ascending order \00"
@.str.3 = [37 x i8] c"\0A Enter the key element to search : \00"
@.str.4 = [21 x i8] c"\0A Successful Search \00"
@.str.5 = [22 x i8] c"\0A Unsuccessful Search\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i32]
%3 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast [10 x i32]* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 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 ([40 x i8], [40 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 [10 x i32], [10 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 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 = load i32, i32* %1
%25 = icmp slt i32 %24, 1
br i1 %25, label %45, label %26
26: ; preds = %21
%27 = add nsw i32 %24, -1
%28 = load i32, i32* %3
br label %29
29: ; preds = %26, %38
%30 = phi i32 [ %27, %26 ], [ %43, %38 ]
%31 = phi i32 [ 0, %26 ], [ %42, %38 ]
%32 = add nsw i32 %30, %31
%33 = sdiv i32 %32, 2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = icmp eq i32 %28, %36
br i1 %37, label %45, label %38
38: ; preds = %29
%39 = icmp sgt i32 %28, %36
%40 = add nsw i32 %33, 1
%41 = add nsw i32 %33, -1
%42 = select i1 %39, i32 %40, i32 %31
%43 = select i1 %39, i32 %30, i32 %41
%44 = icmp sgt i32 %42, %43
br i1 %44, label %45, label %29
45: ; preds = %38, %29, %21
%46 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), %21 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), %29 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), %38 ]
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %46)
ret i32 0
}