#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 binary_search_iterative(int s[], int key, int l, int r) {
  while (l < r) {
    printf("bsi %d %d\n", l, r);
    int m = l + (r - l) / 2;
    if (key < s[m])
      r = m - 1;
    else if (s[m] < key)
      l = m + 1;
    else
      return m;
  }
  printf("bsi no exist\n");
  return -1;
}


x86 assembly (clang, -O0):
<binary_search_iterative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11bc <binary_search_iterative+0xac>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xec2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1185 <binary_search_iterative+0x75>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b7 <binary_search_iterative+0xa7>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jge    11a7 <binary_search_iterative+0x97>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b2 <binary_search_iterative+0xa2>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <binary_search_iterative+0xc1>
jmp    11b7 <binary_search_iterative+0xa7>
jmp    1125 <binary_search_iterative+0x15>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search_iterative>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    %edx,%ecx
jle    117a <binary_search_iterative+0x5a>
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xec3(%rip),%r12
jmp    1149 <binary_search_iterative+0x29>
nop
add    $0xffffffff,%eax
mov    %eax,%ebp
cmp    %ebx,%ebp
jle    117a <binary_search_iterative+0x5a>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%ecx
sub    %ebx,%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
add    %ebx,%eax
movslq %eax,%rcx
cmp    %r14d,(%r15,%rcx,4)
jg     1140 <binary_search_iterative+0x20>
jge    118b <binary_search_iterative+0x6b>
add    $0x1,%eax
mov    %eax,%ebx
cmp    %ebx,%ebp
jg     1149 <binary_search_iterative+0x29>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"bsi %d %d\0A\00"
@str = [13 x i8] c"bsi no exist\00"
define i32 @binary_search_iterative(i32* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %3, %2
br i1 %5, label %6, label %27
6: ; preds = %4, %23
%7 = phi i32 [ %25, %23 ], [ %3, %4 ]
%8 = phi i32 [ %24, %23 ], [ %2, %4 ]
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %7)
%10 = sub nsw i32 %7, %8
%11 = sdiv i32 %10, 2
%12 = add nsw i32 %11, %8
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = icmp sgt i32 %15, %1
br i1 %16, label %17, label %19
17: ; preds = %6
%18 = add nsw i32 %12, -1
br label %23
19: ; preds = %6
%20 = icmp slt i32 %15, %1
br i1 %20, label %21, label %29
21: ; preds = %19
%22 = add nsw i32 %12, 1
br label %23
23: ; preds = %17, %21
%24 = phi i32 [ %8, %17 ], [ %22, %21 ]
%25 = phi i32 [ %18, %17 ], [ %7, %21 ]
%26 = icmp sgt i32 %25, %24
br i1 %26, label %6, label %27
27: ; preds = %23, %4
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %19, %27
%30 = phi i32 [ -1, %27 ], [ %12, %19 ]
ret i32 %30
}