#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);
void binarySearch(int *ar, int num, int sizeOfArray) {
  int i = 0;
  int iOld = -1;
  for (i = sizeOfArray / 2; i < sizeOfArray && i >= 0;) {
    printf("%d\n", i);
    if (iOld == i)
      break;
    if (ar[i] == num) {
      printf("%d Found\n", num);
      return;
    } else if (ar[i] > num) {
      iOld = i;
      i = i - i / 2 - 1;
    } else {
      iOld = i;
      i = i + (sizeOfArray - i) / 2;
    }
  }
  printf("%d Not found\n", num);
}


x86 assembly (clang, -O0):
<binarySearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
cmp    -0x10(%rbp),%ecx
mov    %al,-0x19(%rbp)
jge    1159 <binarySearch+0x49>
cmpl   $0x0,-0x14(%rbp)
setge  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1169 <binarySearch+0x59>
jmp    121c <binarySearch+0x10c>
mov    -0x14(%rbp),%esi
lea    0xe8d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1189 <binarySearch+0x79>
jmp    121c <binarySearch+0x10c>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0xc(%rbp),%eax
jne    11b1 <binarySearch+0xa1>
mov    -0xc(%rbp),%esi
lea    0xe5d(%rip),%rdi
call   1030 <printf@plt>
jmp    122b <binarySearch+0x11b>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0xc(%rbp),%eax
jle    11ee <binarySearch+0xde>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1212 <binarySearch+0x102>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1217 <binarySearch+0x107>
jmp    113e <binarySearch+0x2e>
mov    -0xc(%rbp),%esi
lea    0xde8(%rip),%rdi
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binarySearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r12
mov    %edx,%ebp
shr    $0x1f,%ebp
add    %edx,%ebp
sar    %ebp
lea    0xedd(%rip),%rdi
cmp    %edx,%ebp
jge    11ac <binarySearch+0x9c>
mov    %edx,%ebx
cmp    $0xffffffff,%edx
jl     11ac <binarySearch+0x9c>
mov    $0xffffffff,%r14d
lea    0xeb7(%rip),%r13
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
cmp    %ebp,%r14d
je     119c <binarySearch+0x8c>
mov    %ebp,%eax
cmp    %r15d,(%r12,%rax,4)
je     11a5 <binarySearch+0x95>
jle    1180 <binarySearch+0x70>
mov    %ebp,%eax
shr    %eax
not    %eax
jmp    118d <binarySearch+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%ecx
sub    %ebp,%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
add    %ebp,%eax
cmp    %ebx,%eax
jge    119c <binarySearch+0x8c>
mov    %ebp,%r14d
mov    %eax,%ebp
test   %eax,%eax
jns    1150 <binarySearch+0x40>
lea    0xe6b(%rip),%rdi
jmp    11ac <binarySearch+0x9c>
lea    0xe58(%rip),%rdi
mov    %r15d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

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

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [10 x i8] c"%d Found\0A\00"
@.str.2 = [14 x i8] c"%d Not found\0A\00"
define void @binarySearch(i32* nocapture %0, i32 %1, i32 %2) {
%4 = sdiv i32 %2, 2
%5 = icmp slt i32 %4, %2
%6 = icmp sgt i32 %2, -2
%7 = and i1 %5, %6
br i1 %7, label %8, label %32
8: ; preds = %3, %26
%9 = phi i32 [ %10, %26 ], [ -1, %3 ]
%10 = phi i32 [ %28, %26 ], [ %4, %3 ]
%11 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %9, %10
br i1 %12, label %32, label %13
13: ; preds = %8
%14 = zext i32 %10 to i64
%15 = getelementptr inbounds i32, i32* %0, i64 %14
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, %1
br i1 %17, label %32, label %18
18: ; preds = %13
%19 = icmp sgt i32 %16, %1
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = lshr i32 %10, 1
%22 = xor i32 %21, -1
br label %26
23: ; preds = %18
%24 = sub nsw i32 %2, %10
%25 = sdiv i32 %24, 2
br label %26
26: ; preds = %20, %23
%27 = phi i32 [ %22, %20 ], [ %25, %23 ]
%28 = add i32 %27, %10
%29 = icmp slt i32 %28, %2
%30 = icmp sgt i32 %28, -1
%31 = and i1 %29, %30
br i1 %31, label %8, label %32
32: ; preds = %8, %26, %13, %3
%33 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %3 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %26 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %8 ]
%34 = tail call i32 @printf(i8* %33, i32 %1)
ret void
}