#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 binary(int a[], int low, int high) {
  int mid, flag = 0, key;
  printf("enter for search\n");
  scanf("%d", &key);
  while (low <= high) {
    mid = ((low + high) / 2);
    if (a[mid] == key) {
      printf("%d is found", key);
      flag = 1;
      break;
    } else if (a[mid] > key) {
      high = mid - 1;
    } else if (a[mid] < key) {
      low = mid + 1;
    }
  }
  if (flag == 0) {
    printf("not found\n");
  }
}


x86 assembly (clang, -O0):
<binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xec4(%rip),%rdi
lea    -0x24(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     11f3 <binary+0xd3>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x24(%rbp),%eax
jne    11a5 <binary+0x85>
mov    -0x24(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x20(%rbp)
jmp    11f3 <binary+0xd3>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x24(%rbp),%eax
jle    11c7 <binary+0xa7>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11e9 <binary+0xc9>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x24(%rbp),%eax
jge    11e4 <binary+0xc4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e9 <binary+0xc9>
jmp    11ee <binary+0xce>
jmp    1157 <binary+0x37>
cmpl   $0x0,-0x20(%rbp)
jne    120b <binary+0xeb>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
cmp    %ebx,%r15d
jg     11a2 <binary+0x72>
mov    0xc(%rsp),%esi
nopw   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
movslq %eax,%rcx
mov    (%r14,%rcx,4),%ecx
cmp    %esi,%ecx
je     11b0 <binary+0x80>
lea    -0x1(%rax),%edx
add    $0x1,%eax
cmp    %esi,%ecx
cmovge %r15d,%eax
cmovg  %edx,%ebx
cmovg  %r15d,%eax
mov    %eax,%r15d
cmp    %ebx,%eax
jle    1170 <binary+0x40>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
jmp    11be <binary+0x8e>
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"enter for search\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [12 x i8] c"%d is found\00"
@.str.3 = [11 x i8] c"not found\0A\00"
define i32 @binary(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %10)
br label %13
13: ; preds = %56, %3
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %57
17: ; preds = %13
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = add nsw i32 %18, %19
%21 = sdiv i32 %20, 2
store i32 %21, i32* %8
%22 = load i32*, i32** %5
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
%26 = load i32, i32* %25
%27 = load i32, i32* %10
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = load i32, i32* %10
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %30)
store i32 1, i32* %9
br label %57
32: ; preds = %17
%33 = load i32*, i32** %5
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
%38 = load i32, i32* %10
%39 = icmp sgt i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, 1
store i32 %42, i32* %7
br label %55
43: ; preds = %32
%44 = load i32*, i32** %5
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = load i32, i32* %10
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %54
51: ; preds = %43
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
br label %54
54: ; preds = %51, %43
br label %55
55: ; preds = %54, %40
br label %56
56: ; preds = %55
br label %13
57: ; preds = %29, %13
%58 = load i32, i32* %9
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %57
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [12 x i8] c"%d is found\00"
@str = [17 x i8] c"enter for search\00"
@str.4 = [10 x i8] c"not found\00"
define i32 @binary(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%8 = icmp sgt i32 %1, %2
br i1 %8, label %31, label %9
9: ; preds = %3
%10 = load i32, i32* %4
br label %11
11: ; preds = %9, %22
%12 = phi i32 [ %1, %9 ], [ %29, %22 ]
%13 = phi i32 [ %2, %9 ], [ %28, %22 ]
%14 = add nsw i32 %12, %13
%15 = sdiv i32 %14, 2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, %10
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %33
22: ; preds = %11
%23 = icmp sgt i32 %18, %10
%24 = add nsw i32 %15, -1
%25 = icmp slt i32 %18, %10
%26 = add nsw i32 %15, 1
%27 = select i1 %25, i32 %26, i32 %12
%28 = select i1 %23, i32 %24, i32 %13
%29 = select i1 %23, i32 %12, i32 %27
%30 = icmp sgt i32 %29, %28
br i1 %30, label %31, label %11
31: ; preds = %22, %3
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
br label %33
33: ; preds = %20, %31
ret i32 undef
}