#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 c, first, last, middle, n, search, array[100];
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
  printf("Enter value to find\n");
  scanf("%d", &search);
  first = 0;
  last = n - 1;
  middle = (first + last) / 2;
  while (first <= last) {
    if (array[middle] < search)
      first = middle + 1;
    else if (array[middle] == search) {
      printf("%d found at location %d.\n", search, middle + 1);
      break;
    } else
      last = middle - 1;
    middle = (first + last) / 2;
  }
  if (first > last)
    printf("Not found! %d is not present in the list.\n", search);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed3(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x18(%rbp),%esi
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11a0 <main+0x80>
movslq -0x8(%rbp),%rax
lea    -0x1b0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe8d(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1168 <main+0x48>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe65(%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
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1261 <main+0x141>
movslq -0x14(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%eax
cmp    -0x1c(%rbp),%eax
jge    120d <main+0xed>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    124b <main+0x12b>
movslq -0x14(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%eax
cmp    -0x1c(%rbp),%eax
jne    123d <main+0x11d>
mov    -0x1c(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1261 <main+0x141>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    124b <main+0x12b>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
jmp    11df <main+0xbf>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    127e <main+0x15e>
mov    -0x1c(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@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    0xf18(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb1(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%esi
lea    0xe9f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    119d <main+0x6d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xe80(%rip),%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     1180 <main+0x50>
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
lea    0xe50(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%edi
mov    0xc(%rsp),%esi
test   %edi,%edi
jle    11f7 <main+0xc7>
add    $0xffffffff,%edi
xor    %eax,%eax
mov    %edi,%ecx
jmp    11dc <main+0xac>
nop
add    $0x1,%edx
mov    %edx,%eax
lea    (%rcx,%rax,1),%edi
cmp    %ecx,%eax
jg     11f7 <main+0xc7>
mov    %edi,%edx
shr    $0x1f,%edx
add    %edi,%edx
sar    %edx
movslq %edx,%rdi
cmp    %esi,0x10(%rsp,%rdi,4)
jl     11d0 <main+0xa0>
je     1207 <main+0xd7>
add    $0xffffffff,%edx
mov    %edx,%ecx
jmp    11d5 <main+0xa5>
lea    0xe32(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1218 <main+0xe8>
add    $0x1,%edx
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x1a0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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