#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 */
typedef int int32_t;

/* Variables and functions */
int BinarySearch(int /*<<< orphan*/, int, int *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/, char *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int32_t num, search, i = 0, result = 0;
  fprintf(stdout, "Enter the number of elements :\n");
  fscanf(stdin, "%d", &num);
  int32_t *array = (int32_t *)malloc(sizeof(int32_t) * num);
  fprintf(stdout, "Enter the elements :\n");
  while (i < num) {
    fscanf(stdin, "%d", (array + i));
    i++;
  }
  fprintf(stdout, "Enter the element to be searched :\n");
  fscanf(stdin, "%d", &search);
  result = BinarySearch(0, (num - 1), array, search);
  if (result == -1)
    fprintf(stdout, "The element %d is not present in the array\n", search);
  else
    fprintf(stdout, "The element %d is in position %d\n", search, result);
  free(array);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <fscanf@plt>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    120c <main+0xbc>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe27(%rip),%rsi
call   1040 <fscanf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ce <main+0x7e>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%ecx
xor    %edi,%edi
call   1060 <BinarySearch@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    127c <main+0x12c>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1299 <main+0x149>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e76(%rip),%r12
mov    (%r12),%edi
lea    0xe93(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2e6d(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe9a(%rip),%rsi
mov    %rsp,%rdx
call   1040 <fscanf@plt>
mov    (%rsp),%edi
shl    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    (%r12),%edi
lea    0xe7c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
cmpl   $0x0,(%rsp)
jle    11e0 <main+0x90>
xor    %ebp,%ebp
lea    0xe63(%rip),%r15
mov    %r14,%rbx
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <fscanf@plt>
add    $0x1,%rbp
movslq (%rsp),%rax
add    $0x4,%rbx
cmp    %rax,%rbp
jl     11c0 <main+0x70>
mov    (%r12),%edi
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%edi
lea    0xe23(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1040 <fscanf@plt>
mov    (%rsp),%esi
add    $0xffffffff,%esi
mov    0x4(%rsp),%ecx
xor    %edi,%edi
mov    %r14,%rdx
call   1060 <BinarySearch@plt>
mov    (%r12),%edi
mov    0x4(%rsp),%edx
cmp    $0xffffffff,%eax
je     123a <main+0xea>
lea    0xe5a(%rip),%rsi
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1248 <main+0xf8>
lea    0xe1c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [32 x i8] c"Enter the number of elements :\0A\00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [22 x i8] c"Enter the elements :\0A\00"
@.str.3 = [36 x i8] c"Enter the element to be searched :\0A\00"
@.str.4 = [44 x i8] c"The element %d is not present in the array\0A\00"
@.str.5 = [34 x i8] c"The element %d is in position %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %9
%11 = load i32, i32* @stdout
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @stdin
%14 = call i32 @fscanf(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = mul i64 4, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %10
%21 = load i32, i32* @stdout
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %27, %2
%24 = load i32, i32* %8
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i32, i32* @stdin
%29 = load i32*, i32** %10
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = call i32 @fscanf(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %32)
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %23
36: ; preds = %23
%37 = load i32, i32* @stdout
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @stdin
%40 = call i32 @fscanf(i32 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%41 = load i32, i32* %6
%42 = sub nsw i32 %41, 1
%43 = load i32*, i32** %10
%44 = load i32, i32* %7
%45 = call i32 @BinarySearch(i32 0, i32 %42, i32* %43, i32 %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %52
48: ; preds = %36
%49 = load i32, i32* @stdout
%50 = load i32, i32* %7
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %50)
br label %57
52: ; preds = %36
%53 = load i32, i32* @stdout
%54 = load i32, i32* %7
%55 = load i32, i32* %9
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %54, i32 %55)
br label %57
57: ; preds = %52, %48
%58 = load i32*, i32** %10
%59 = call i32 @free(i32* %58)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [32 x i8] c"Enter the number of elements :\0A\00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [22 x i8] c"Enter the elements :\0A\00"
@.str.3 = [36 x i8] c"Enter the element to be searched :\0A\00"
@.str.4 = [44 x i8] c"The element %d is not present in the array\0A\00"
@.str.5 = [34 x i8] c"The element %d is in position %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @stdout
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stdin
%10 = call i32 @fscanf(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%11 = load i32, i32* %3
%12 = shl i32 %11, 2
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* @stdout
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* %3
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %2, %19
%20 = phi i64 [ %24, %19 ], [ 0, %2 ]
%21 = load i32, i32* @stdin
%22 = getelementptr inbounds i32, i32* %14, i64 %20
%23 = call i32 @fscanf(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %22)
%24 = add nuw nsw i64 %20, 1
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = icmp slt i64 %24, %26
br i1 %27, label %19, label %28
28: ; preds = %19, %2
%29 = load i32, i32* @stdout
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @stdin
%32 = call i32 @fscanf(i32 %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%33 = load i32, i32* %3
%34 = add nsw i32 %33, -1
%35 = load i32, i32* %4
%36 = call i32 @BinarySearch(i32 0, i32 %34, i32* %14, i32 %35)
%37 = icmp eq i32 %36, -1
%38 = load i32, i32* @stdout
%39 = load i32, i32* %4
br i1 %37, label %40, label %42
40: ; preds = %28
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %39)
br label %44
42: ; preds = %28
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 %36)
br label %44
44: ; preds = %42, %40
%45 = call i32 @free(i32* %14)
ret i32 0
}