#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);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ system(char *);
int main() {
  int a[100], i, num, item, loc = -1;
  system("cls");
  printf("Enter array size:");
  scanf("%d", &num);
  printf("\n Enter array elements:");
  for (i = 0; i < num; i++)
    scanf("%d", &a[i]);
  printf("\nEnter element to search:");
  scanf("%d", &item);
  loc = binarySearch(a, num, item);
  if (loc != -1)
    printf("\nElement found at postion: %d\n", loc);
  else
    printf("\nElement not found.\n");
  system("pause");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x1b0(%rbp)
ff ff ff
lea    0xe9d(%rip),%rdi
call   1030 <system@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe99(%rip),%rdi
lea    -0x1a8(%rbp),%rsi
call   1060 <scanf@plt>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1a4(%rbp)
mov    -0x1a4(%rbp),%eax
cmp    -0x1a8(%rbp),%eax
jge    11e8 <main+0xa8>
movslq -0x1a4(%rbp),%rax
lea    -0x1a0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe47(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x1a4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1a4(%rbp)
jmp    11a1 <main+0x61>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe19(%rip),%rdi
lea    -0x1ac(%rbp),%rsi
call   1060 <scanf@plt>
lea    -0x1a0(%rbp),%rdi
mov    -0x1a8(%rbp),%esi
mov    -0x1ac(%rbp),%edx
call   1050 <binarySearch@plt>
mov    %eax,-0x1b0(%rbp)
cmpl   $0xffffffff,-0x1b0(%rbp)
je     124d <main+0x10d>
mov    -0x1b0(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125b <main+0x11b>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe1d(%rip),%rdi
call   1030 <system@plt>
xor    %eax,%eax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1a0,%rsp
lea    0xe9d(%rip),%rdi
call   1040 <system@plt>
lea    0xe95(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe99(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <scanf@plt>
lea    0xe8b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    11cd <main+0x7d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xe6c(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     11b0 <main+0x60>
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe34(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0x10(%rsp),%rdi
call   1060 <binarySearch@plt>
cmp    $0xffffffff,%eax
je     1215 <main+0xc5>
lea    0xe41(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1221 <main+0xd1>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
lea    0xe42(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x1a0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"cls\00"
@.str.1 = [18 x i8] c"Enter array size:\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [24 x i8] c"\0A Enter array elements:\00"
@.str.4 = [26 x i8] c"\0AEnter element to search:\00"
@.str.5 = [31 x i8] c"\0AElement found at postion: %d\0A\00"
@.str.6 = [21 x i8] c"\0AElement not found.\0A\00"
@.str.7 = [6 x i8] c"pause\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %6
%7 = call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 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* %4)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %11
11: ; preds = %20, %0
%12 = load i32, i32* %3
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [100 x i32], [100 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* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %11
23: ; preds = %11
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 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 [100 x i32], [100 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %4
%28 = load i32, i32* %5
%29 = call i32 @binarySearch(i32* %26, i32 %27, i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %33)
br label %37
35: ; preds = %23
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"cls\00"
@.str.1 = [18 x i8] c"Enter array size:\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [24 x i8] c"\0A Enter array elements:\00"
@.str.4 = [26 x i8] c"\0AEnter element to search:\00"
@.str.5 = [31 x i8] c"\0AElement found at postion: %d\0A\00"
@.str.7 = [6 x i8] c"pause\00"
@str = [20 x i8] c"\0AElement not found.\00"
define i32 @main() {
%1 = alloca [100 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [100 x i32]* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 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 ([24 x i8], [24 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 [100 x i32], [100 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, 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 [100 x i32], [100 x i32]* %1, i64 0, i64 0
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = call i32 @binarySearch(i32* %24, i32 %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %31, label %29
29: ; preds = %21
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %27)
br label %33
31: ; preds = %21
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %31, %29
%34 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}