#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() {
  int i, first, last, mid, length, number;
  printf("Enter the length of the array: ");
  scanf("%d", &length);
  int array[length];
  printf("Enter %d elements of the array in ascending order: \n", length);
  for (i = 0; i < length; i++)
    scanf("%d", &array[i]);
  printf("Enter number to find: ");
  scanf("%d", &number);
  first = 0;
  last = length - 1;
  while (first <= last) {
    mid = (first + last) / 2;
    if (array[mid] < number)
      first = mid + 1;
    else if (array[mid] == number) {
      printf("Found at location %d.\n", mid + 1);
      break;
    } else
      last = mid - 1;
  }
  if (first > last)
    printf("Not found\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xedc(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x18(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%esi
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11c1 <main+0xa1>
mov    -0x38(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe72(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    118c <main+0x6c>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe4a(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1270 <main+0x150>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,-0x14(%rbp)
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x1c(%rbp),%eax
jge    1230 <main+0x110>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    126b <main+0x14b>
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x1c(%rbp),%eax
jne    125d <main+0x13d>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1270 <main+0x150>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    126b <main+0x14b>
jmp    11ef <main+0xcf>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    128a <main+0x16a>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xebb(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xecd(%rip),%rdi
lea    -0x2c(%rbp),%rsi
call   1050 <scanf@plt>
mov    %rsp,%r15
mov    -0x2c(%rbp),%eax
mov    %rsp,%r12
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    -0x2c(%rbp),%esi
lea    0xea2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    11bc <main+0x8c>
xor    %r13d,%r13d
lea    0xe88(%rip),%r14
mov    %r12,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <scanf@plt>
add    $0x1,%r13
movslq -0x2c(%rbp),%rax
add    $0x4,%rbx
cmp    %rax,%r13
jl     11a0 <main+0x70>
lea    0xe95(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe4d(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jle    1219 <main+0xe9>
add    $0xffffffff,%eax
mov    -0x30(%rbp),%ecx
jmp    11f9 <main+0xc9>
nopl   0x0(%rax,%rax,1)
add    $0x1,%esi
mov    %esi,%ebx
cmp    %eax,%ebx
jg     1219 <main+0xe9>
lea    (%rbx,%rax,1),%edx
mov    %edx,%esi
shr    $0x1f,%esi
add    %edx,%esi
sar    %esi
movslq %esi,%rdx
cmp    %ecx,(%r12,%rdx,4)
jl     11f0 <main+0xc0>
je     1227 <main+0xf7>
add    $0xffffffff,%esi
mov    %esi,%eax
cmp    %eax,%ebx
jle    11f9 <main+0xc9>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
jmp    1238 <main+0x108>
add    $0x1,%esi
lea    0xe3e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [32 x i8] c"Enter the length of the array: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [53 x i8] c"Enter %d elements of the array in ascending order: \0A\00"
@.str.3 = [23 x i8] c"Enter number to find: \00"
@.str.4 = [23 x i8] c"Found at location %d.\0A\00"
@str = [10 x i8] c"Not found\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
%10 = alloca i32, i64 %8
%11 = load i32, i32* %1
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%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 i32, i32* %10, i64 %16
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%26 = load i32, i32* %1
%27 = icmp slt i32 %26, 1
br i1 %27, label %53, label %28
28: ; preds = %23
%29 = add nsw i32 %26, -1
%30 = load i32, i32* %2
br label %31
31: ; preds = %28, %46
%32 = phi i32 [ 0, %28 ], [ %48, %46 ]
%33 = phi i32 [ %29, %28 ], [ %47, %46 ]
%34 = add nsw i32 %32, %33
%35 = sdiv i32 %34, 2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %10, i64 %36
%38 = load i32, i32* %37
%39 = icmp slt i32 %38, %30
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = add nsw i32 %35, 1
br label %46
42: ; preds = %31
%43 = icmp eq i32 %38, %30
br i1 %43, label %50, label %44
44: ; preds = %42
%45 = add nsw i32 %35, -1
br label %46
46: ; preds = %44, %40
%47 = phi i32 [ %33, %40 ], [ %45, %44 ]
%48 = phi i32 [ %41, %40 ], [ %32, %44 ]
%49 = icmp sgt i32 %48, %47
br i1 %49, label %53, label %31
50: ; preds = %42
%51 = add nsw i32 %35, 1
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %51)
br label %55
53: ; preds = %46, %23
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %55
55: ; preds = %50, %53
ret i32 0
}