#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 binarySearch(int *, int, int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  printf("Binary Search \n");
  printf("\n");
  int i, n, key, index;
  printf("Total Elements : ");
  scanf("%d", &n);
  printf("\n");
  if (n < 1) {
    printf("Invalid Input \n");
    printf("\n");
    return 1;
  }
  int *A = (int *)calloc(n, sizeof(int));
  printf("Number Input (Sorted Order)\n");
  for (i = 0; i < n; i++) {
    scanf("%d", (A + i));
    if (i > 0 && *(A + (i - 1)) > *(A + i)) {
      printf("Input Not Sorted \n");
      printf("\n");
      return 1;
    }
  }
  printf("\n");
  printf("Number to be Searched : ");
  scanf("%d", &key);
  printf("\n");
  index = binarySearch(A, n, key);
  if (index == -1) {
    printf("Number (%d) not found \n", key);
  } else {
    printf("Number (%d) found at Index : %d \n", key, index);
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea2(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
lea    0xf26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0xc(%rbp)
jge    11c9 <main+0x89>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1311 <main+0x1d1>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    127c <main+0x13c>
mov    -0x20(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe11(%rip),%rdi
call   1060 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
jle    1269 <main+0x129>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x20(%rbp),%rcx
movslq -0x8(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    1269 <main+0x129>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1311 <main+0x1d1>
jmp    126e <main+0x12e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ef <main+0xaf>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd83(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <scanf@plt>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1050 <binarySearch@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12e8 <main+0x1a8>
mov    -0x10(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12fc <main+0x1bc>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xef5(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7b(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rsp),%edi
test   %edi,%edi
jle    128a <main+0x12a>
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%r14
lea    0xeac(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x0,0x8(%rsp)
jle    122a <main+0xca>
mov    $0xffffffff,%r12d
xor    %ebp,%ebp
lea    0xe2c(%rip),%r15
mov    %r14,%rbx
jmp    1206 <main+0xa6>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
movslq 0x8(%rsp),%rax
add    $0x1,%r12
add    $0x4,%rbx
cmp    %rax,%rbp
jge    122a <main+0xca>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
test   %rbp,%rbp
je     11f0 <main+0x90>
mov    %r12d,%eax
mov    (%r14,%rax,4),%eax
cmp    (%rbx),%eax
jle    11f0 <main+0x90>
lea    0xe6b(%rip),%rdi
jmp    1291 <main+0x131>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xdda(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc7(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1080 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    %r14,%rdi
call   1070 <binarySearch@plt>
mov    0xc(%rsp),%esi
cmp    $0xffffffff,%eax
je     129d <main+0x13d>
lea    0xdc7(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12ad <main+0x14d>
lea    0xe14(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    12ad <main+0x14d>
lea    0xd8a(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Binary Search \0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [18 x i8] c"Total Elements : \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [16 x i8] c"Invalid Input \0A\00"
@.str.5 = [29 x i8] c"Number Input (Sorted Order)\0A\00"
@.str.6 = [19 x i8] c"Input Not Sorted \0A\00"
@.str.7 = [25 x i8] c"Number to be Searched : \00"
@.str.8 = [24 x i8] c"Number (%d) not found \0A\00"
@.str.9 = [34 x i8] c"Number (%d) found at Index : %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %74
17: ; preds = %0
%18 = load i32, i32* %3
%19 = call i64 @calloc(i32 %18, i32 4)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %2
br label %22
22: ; preds = %51, %17
%23 = load i32, i32* %2
%24 = load i32, i32* %3
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %54
26: ; preds = %22
%27 = load i32*, i32** %6
%28 = load i32, i32* %2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %30)
%32 = load i32, i32* %2
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %50
34: ; preds = %26
%35 = load i32*, i32** %6
%36 = load i32, i32* %2
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %35, i64 %38
%40 = load i32, i32* %39
%41 = load i32*, i32** %6
%42 = load i32, i32* %2
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
%46 = icmp sgt i32 %40, %45
br i1 %46, label %47, label %50
47: ; preds = %34
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %74
50: ; preds = %34, %26
br label %51
51: ; preds = %50
%52 = load i32, i32* %2
%53 = add nsw i32 %52, 1
store i32 %53, i32* %2
br label %22
54: ; preds = %22
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%57 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%59 = load i32*, i32** %6
%60 = load i32, i32* %3
%61 = load i32, i32* %4
%62 = call i32 @binarySearch(i32* %59, i32 %60, i32 %61)
store i32 %62, i32* %5
%63 = load i32, i32* %5
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %68
65: ; preds = %54
%66 = load i32, i32* %4
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i32 %66)
br label %72
68: ; preds = %54
%69 = load i32, i32* %4
%70 = load i32, i32* %5
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i32 %69, i32 %70)
br label %72
72: ; preds = %68, %65
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %74
74: ; preds = %72, %47, %14
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
@.str.2 = [18 x i8] c"Total Elements : \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.7 = [25 x i8] c"Number to be Searched : \00"
@.str.8 = [24 x i8] c"Number (%d) not found \0A\00"
@.str.9 = [34 x i8] c"Number (%d) found at Index : %d \0A\00"
@str = [15 x i8] c"Binary Search \00"
@str.10 = [28 x i8] c"Number Input (Sorted Order)\00"
@str.11 = [18 x i8] c"Input Not Sorted \00"
@str.12 = [15 x i8] c"Invalid Input \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @putchar(i32 10)
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%9 = call i32 @putchar(i32 10)
%10 = load i32, i32* %1
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.12, i64 0, i64 0))
br label %53
14: ; preds = %0
%15 = call i64 @calloc(i32 %10, i32 4)
%16 = inttoptr i64 %15 to i32*
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.10, i64 0, i64 0))
%18 = load i32, i32* %1
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %39
20: ; preds = %14, %34
%21 = phi i64 [ %35, %34 ], [ 0, %14 ]
%22 = getelementptr inbounds i32, i32* %16, i64 %21
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %22)
%24 = icmp eq i64 %21, 0
br i1 %24, label %34, label %25
25: ; preds = %20
%26 = add nuw i64 %21, 4294967295
%27 = and i64 %26, 4294967295
%28 = getelementptr inbounds i32, i32* %16, i64 %27
%29 = load i32, i32* %28
%30 = load i32, i32* %22
%31 = icmp sgt i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.11, i64 0, i64 0))
br label %53
34: ; preds = %20, %25
%35 = add nuw nsw i64 %21, 1
%36 = load i32, i32* %1
%37 = sext i32 %36 to i64
%38 = icmp slt i64 %35, %37
br i1 %38, label %20, label %39
39: ; preds = %34, %14
%40 = call i32 @putchar(i32 10)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
%42 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%43 = call i32 @putchar(i32 10)
%44 = load i32, i32* %1
%45 = load i32, i32* %2
%46 = call i32 @binarySearch(i32* %16, i32 %44, i32 %45)
%47 = icmp eq i32 %46, -1
%48 = load i32, i32* %2
br i1 %47, label %49, label %51
49: ; preds = %39
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0), i32 %48)
br label %53
51: ; preds = %39
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i32 %48, i32 %46)
br label %53
53: ; preds = %49, %51, %32, %12
%54 = phi i32 [ 1, %12 ], [ 1, %32 ], [ 0, %51 ], [ 0, %49 ]
%55 = call i32 @putchar(i32 10)
ret i32 %54
}