#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 A[100], n, i, s, mid, l, r;
  printf("Enter Number of Elements: ");
  scanf("%d", &n);
  printf("Enter %d Elements:\n", n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);
  printf("Enter Element to Search: ");
  scanf("%d", &s);
  l = 0;
  r = n;
  while (l <= r) {
    mid = (l + r) / 2;
    if (A[mid] > s)
      r = mid - 1;
    else if (A[mid] < s)
      l = mid + 1;
    else
      break;
  }
  if (l <= r)
    printf("%d found at index %d\n", s, mid + 1);
  else
    printf("%d not found\n", s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed4(%rip),%rdi
lea    -0x1a4(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x1a4(%rbp),%esi
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1a8(%rbp)
mov    -0x1a8(%rbp),%eax
cmp    -0x1a4(%rbp),%eax
jge    11b8 <main+0x98>
movslq -0x1a8(%rbp),%rax
lea    -0x1a0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe7c(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x1a8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1a8(%rbp)
jmp    1171 <main+0x51>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe4e(%rip),%rdi
lea    -0x1ac(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x1b4(%rbp)
mov    -0x1a4(%rbp),%eax
mov    %eax,-0x1b8(%rbp)
mov    -0x1b4(%rbp),%eax
cmp    -0x1b8(%rbp),%eax
jg     1286 <main+0x166>
mov    -0x1b4(%rbp),%eax
add    -0x1b8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1b0(%rbp)
movslq -0x1b0(%rbp),%rax
mov    -0x1a0(%rbp,%rax,4),%eax
cmp    -0x1ac(%rbp),%eax
jle    1249 <main+0x129>
mov    -0x1b0(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1b8(%rbp)
jmp    1281 <main+0x161>
movslq -0x1b0(%rbp),%rax
mov    -0x1a0(%rbp,%rax,4),%eax
cmp    -0x1ac(%rbp),%eax
jge    1277 <main+0x157>
mov    -0x1b0(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b4(%rbp)
jmp    127c <main+0x15c>
jmp    1286 <main+0x166>
jmp    1281 <main+0x161>
jmp    11ef <main+0xcf>
mov    -0x1b4(%rbp),%eax
cmp    -0x1b8(%rbp),%eax
jg     12ba <main+0x19a>
mov    -0x1ac(%rbp),%esi
mov    -0x1b0(%rbp),%edx
add    $0x1,%edx
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ce <main+0x1ae>
mov    -0x1ac(%rbp),%esi
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x1a0,%rsp
lea    0xecd(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xeda(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x8(%rsp),%esi
lea    0xec8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmpl   $0x0,0x8(%rsp)
jle    119d <main+0x7d>
lea    0x10(%rsp),%r15
xor    %ebx,%ebx
lea    0xea9(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r15
cmp    %rax,%rbx
jl     1180 <main+0x60>
lea    0xe8e(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe67(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1040 <scanf@plt>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%esi
test   %eax,%eax
jns    11d9 <main+0xb9>
jmp    11fd <main+0xdd>
nopl   0x0(%rax)
add    $0xffffffff,%edx
mov    %edx,%eax
cmp    %eax,%ebx
jg     11fd <main+0xdd>
lea    (%rax,%rbx,1),%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
add    %ecx,%edx
sar    %edx
movslq %edx,%rcx
mov    0x10(%rsp,%rcx,4),%ecx
cmp    %esi,%ecx
jg     11d0 <main+0xb0>
add    $0x1,%edx
cmp    %esi,%ecx
jge    120d <main+0xed>
mov    %edx,%ebx
cmp    %eax,%ebx
jle    11d9 <main+0xb9>
lea    0xe5e(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    121b <main+0xfb>
lea    0xe38(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x1a0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [27 x i8] c"Enter Number of Elements: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [20 x i8] c"Enter %d Elements:\0A\00"
@.str.3 = [26 x i8] c"Enter Element to Search: \00"
@.str.4 = [22 x i8] c"%d found at index %d\0A\00"
@.str.5 = [14 x i8] c"%d not found\0A\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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 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* %2)
%9 = load i32, i32* %2
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%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.1, 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.3, i64 0, i64 0))
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%24 = load i32, i32* %2
%25 = icmp slt i32 %24, 0
%26 = load i32, i32* %3
br i1 %25, label %47, label %27
27: ; preds = %21, %41
%28 = phi i32 [ %43, %41 ], [ %24, %21 ]
%29 = phi i32 [ %42, %41 ], [ 0, %21 ]
%30 = add nsw i32 %28, %29
%31 = sdiv i32 %30, 2
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = icmp sgt i32 %34, %26
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = add nsw i32 %31, -1
br label %41
38: ; preds = %27
%39 = icmp slt i32 %34, %26
%40 = add nsw i32 %31, 1
br i1 %39, label %41, label %45
41: ; preds = %38, %36
%42 = phi i32 [ %29, %36 ], [ %40, %38 ]
%43 = phi i32 [ %37, %36 ], [ %28, %38 ]
%44 = icmp sgt i32 %42, %43
br i1 %44, label %47, label %27
45: ; preds = %38
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %40)
br label %49
47: ; preds = %41, %21
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %26)
br label %49
49: ; preds = %47, %45
ret i32 0
}