#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(void) {
  int iNum, i, iFound, iaArr[10], iKey, iPos, iLow, iHigh, iMid;
  printf("\n**************************************************");
  printf("\n*\tPROGRAM TO IMPLEMENT BINARY SEARCH\t *\n");
  printf("**************************************************");
  printf("\nEnter no of elements\n");
  scanf("%d", &iNum);
  printf("\nEnter the elements in ascending order\n");
  for (i = 0; i < iNum; i++)
    scanf("%d", &iaArr[i]);
  printf("\nEnter the Key element\n");
  scanf("%d", &iKey);
  iFound = 0;
  iLow = 0;
  iHigh = iNum - 1;
  while (iLow <= iHigh) {
    iMid = (iLow + iHigh) / 2;
    if (iKey == iaArr[iMid]) {
      iPos = iMid;
      printf("\nKey element %d found at position %d\n", iKey, iPos + 1);
      return 0;
    } else {
      if (iKey < iaArr[iMid])
        iHigh = iMid - 1;
      else
        iLow = iMid + 1;
    }
  }
  printf("\nKey element not found\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xef0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xefe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf07(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
lea    0xefa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11c1 <main+0xa1>
movslq -0xc(%rbp),%rax
lea    -0x40(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xec7(%rip),%rdi
call   1040 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    118c <main+0x6c>
lea    0xed8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe9f(%rip),%rdi
lea    -0x44(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x50(%rbp),%eax
jg     127f <main+0x15f>
mov    -0x4c(%rbp),%eax
add    -0x50(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x54(%rbp)
mov    -0x44(%rbp),%eax
movslq -0x54(%rbp),%rcx
cmp    -0x40(%rbp,%rcx,4),%eax
jne    124d <main+0x12d>
mov    -0x54(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%esi
mov    -0x48(%rbp),%edx
add    $0x1,%edx
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1294 <main+0x174>
mov    -0x44(%rbp),%eax
movslq -0x54(%rbp),%rcx
cmp    -0x40(%rbp,%rcx,4),%eax
jge    126c <main+0x14c>
mov    -0x54(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1275 <main+0x155>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    127a <main+0x15a>
jmp    11f6 <main+0xd6>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
lea    0xec0(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf0f(%rip),%rdi
call   1030 <puts@plt>
lea    0xea7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf1e(%rip),%rdi
call   1030 <puts@plt>
lea    0xec0(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
lea    0xf17(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,0x8(%rsp)
jle    11bd <main+0x8d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xe95(%rip),%r14
nop
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     11a0 <main+0x70>
lea    0xeff(%rip),%rdi
call   1030 <puts@plt>
lea    0xe64(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
jle    121b <main+0xeb>
add    $0xffffffff,%eax
xor    %ecx,%ecx
mov    0xc(%rsp),%esi
jmp    11f9 <main+0xc9>
nopl   (%rax)
add    $0x1,%edx
mov    %edx,%ecx
cmp    %eax,%ecx
jg     121b <main+0xeb>
lea    (%rax,%rcx,1),%edi
mov    %edi,%edx
shr    $0x1f,%edx
add    %edi,%edx
sar    %edx
movslq %edx,%rdi
mov    0x10(%rsp,%rdi,4),%edi
cmp    %edi,%esi
je     1229 <main+0xf9>
jge    11f0 <main+0xc0>
add    $0xffffffff,%edx
mov    %edx,%eax
cmp    %eax,%ecx
jle    11f9 <main+0xc9>
lea    0xeb8(%rip),%rdi
call   1030 <puts@plt>
jmp    123a <main+0x10a>
add    $0x1,%edx
lea    0xe04(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [52 x i8] c"\0A**************************************************\00"
@.str.2 = [51 x i8] c"**************************************************\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.7 = [38 x i8] c"\0AKey element %d found at position %d\0A\00"
@str = [41 x i8] c"\0A*\09PROGRAM TO IMPLEMENT BINARY SEARCH\09 *\00"
@str.9 = [22 x i8] c"\0AEnter no of elements\00"
@str.10 = [39 x i8] c"\0AEnter the elements in ascending order\00"
@str.11 = [23 x i8] c"\0AEnter the Key element\00"
@str.12 = [23 x i8] c"\0AKey element not found\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 ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.10, i64 0, i64 0))
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %0, %15
%16 = phi i64 [ %19, %15 ], [ 0, %0 ]
%17 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 %16
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %17)
%19 = add nuw nsw i64 %16, 1
%20 = load i32, i32* %1
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %19, %21
br i1 %22, label %15, label %23
23: ; preds = %15, %0
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%26 = load i32, i32* %1
%27 = icmp slt i32 %26, 1
br i1 %27, label %50, label %28
28: ; preds = %23
%29 = add nsw i32 %26, -1
%30 = load i32, i32* %3
br label %31
31: ; preds = %28, %43
%32 = phi i32 [ %29, %28 ], [ %48, %43 ]
%33 = phi i32 [ 0, %28 ], [ %47, %43 ]
%34 = add nsw i32 %32, %33
%35 = sdiv i32 %34, 2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 %36
%38 = load i32, i32* %37
%39 = icmp eq i32 %30, %38
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = add nsw i32 %35, 1
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0), i32 %30, i32 %41)
br label %52
43: ; preds = %31
%44 = icmp slt i32 %30, %38
%45 = add nsw i32 %35, -1
%46 = add nsw i32 %35, 1
%47 = select i1 %44, i32 %33, i32 %46
%48 = select i1 %44, i32 %45, i32 %32
%49 = icmp sgt i32 %47, %48
br i1 %49, label %50, label %31
50: ; preds = %43, %23
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.12, i64 0, i64 0))
br label %52
52: ; preds = %50, %40
ret i32 0
}