#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 binary_search(int *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int arr[200];
  int n, i, key, res_index;
  printf("----- Binary Search Demo -----");
  printf("\n Enter the size of the array : ");
  scanf("%d", &n);
  printf("\n Now enter the array (note:- must be pre-sorted )");
  for (i = 0; i < n; i++)
    scanf("%d", &arr[i]);
  printf("\n Now enter the value you intend to find in the array : \n");
  scanf("%d", &key);
  res_index = binary_search(arr, key, 0, n - 1);
  if (res_index == -1)
    printf("\n No such element exists in the given array! \n\n");
  else
    printf("\n The element %d exists at index %d in the given array. \n\n", key,
           res_index);
  printf("\n------------ End ------------ \n\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xedb(%rip),%rdi
lea    -0x334(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x338(%rbp)
mov    -0x338(%rbp),%eax
cmp    -0x334(%rbp),%eax
jge    11d0 <main+0xa0>
movslq -0x338(%rbp),%rax
lea    -0x330(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe89(%rip),%rdi
call   1050 <scanf@plt>
mov    -0x338(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x338(%rbp)
jmp    1189 <main+0x59>
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe5b(%rip),%rdi
lea    -0x33c(%rbp),%rsi
call   1050 <scanf@plt>
lea    -0x330(%rbp),%rdi
mov    -0x33c(%rbp),%esi
mov    -0x334(%rbp),%ecx
sub    $0x1,%ecx
xor    %edx,%edx
call   1040 <binary_search@plt>
mov    %eax,-0x340(%rbp)
cmpl   $0xffffffff,-0x340(%rbp)
jne    1234 <main+0x104>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    124e <main+0x11e>
mov    -0x33c(%rbp),%esi
mov    -0x340(%rbp),%edx
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x330,%rsp
lea    0xead(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xebe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed1(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <scanf@plt>
lea    0xec3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    11bd <main+0x7d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xea4(%rip),%r14
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     11a0 <main+0x60>
lea    0xeed(%rip),%rdi
call   1030 <puts@plt>
lea    0xe70(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%ecx
add    $0xffffffff,%ecx
lea    0x10(%rsp),%rdi
xor    %edx,%edx
call   1050 <binary_search@plt>
cmp    $0xffffffff,%eax
je     120c <main+0xcc>
mov    0xc(%rsp),%esi
lea    0xe75(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1218 <main+0xd8>
lea    0xef8(%rip),%rdi
call   1030 <puts@plt>
lea    0xecb(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x330,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [31 x i8] c"----- Binary Search Demo -----\00"
@.str.1 = [33 x i8] c"\0A Enter the size of the array : \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [51 x i8] c"\0A Now enter the array (note:- must be pre-sorted )\00"
@.str.4 = [58 x i8] c"\0A Now enter the value you intend to find in the array : \0A\00"
@.str.5 = [48 x i8] c"\0A No such element exists in the given array! \0A\0A\00"
@.str.6 = [59 x i8] c"\0A The element %d exists at index %d in the given array. \0A\0A\00"
@.str.7 = [34 x i8] c"\0A------------ End ------------ \0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [200 x 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 ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %11
11: ; preds = %20, %0
%12 = load i32, i32* %4
%13 = load i32, i32* %3
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [200 x i32], [200 x i32]* %2, i64 0, i64 %17
%19 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %18)
br label %20
20: ; preds = %15
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %11
23: ; preds = %11
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%26 = getelementptr inbounds [200 x i32], [200 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %5
%28 = load i32, i32* %3
%29 = sub nsw i32 %28, 1
%30 = call i32 @binary_search(i32* %26, i32 %27, i32 0, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0))
br label %39
35: ; preds = %23
%36 = load i32, i32* %5
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.6, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %35, %33
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [31 x i8] c"----- Binary Search Demo -----\00"
@.str.1 = [33 x i8] c"\0A Enter the size of the array : \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [51 x i8] c"\0A Now enter the array (note:- must be pre-sorted )\00"
@.str.6 = [59 x i8] c"\0A The element %d exists at index %d in the given array. \0A\0A\00"
@str = [57 x i8] c"\0A Now enter the value you intend to find in the array : \00"
@str.8 = [33 x i8] c"\0A------------ End ------------ \0A\00"
@str.9 = [47 x i8] c"\0A No such element exists in the given array! \0A\00"
define i32 @main() {
%1 = alloca [200 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [200 x i32]* %1 to i8*
%5 = bitcast 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 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%11 = load i32, i32* %2
%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 [200 x i32], [200 x i32]* %1, i64 0, i64 %14
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %15)
%17 = add nuw nsw i64 %14, 1
%18 = load i32, i32* %2
%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 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str, i64 0, i64 0))
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%24 = getelementptr inbounds [200 x i32], [200 x i32]* %1, i64 0, i64 0
%25 = load i32, i32* %3
%26 = load i32, i32* %2
%27 = add nsw i32 %26, -1
%28 = call i32 @binary_search(i32* %24, i32 %25, i32 0, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.9, i64 0, i64 0))
br label %35
32: ; preds = %21
%33 = load i32, i32* %3
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.6, i64 0, i64 0), i32 %33, i32 %28)
br label %35
35: ; preds = %32, %30
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}