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


x86 assembly (clang, -O0):
<binary_search_recursive>:
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    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    115b <binary_search_recursive+0x3b>
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <binary_search_recursive+0x100>
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    11c1 <binary_search_recursive+0xa1>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%edx
sub    $0x1,%edx
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
call   1040 <binary_search_recursive@plt>
mov    %eax,-0x4(%rbp)
jmp    1220 <binary_search_recursive+0x100>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jge    1209 <binary_search_recursive+0xe9>
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    -0x1c(%rbp),%edx
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
mov    -0x1c(%rbp),%ecx
call   1040 <binary_search_recursive@plt>
mov    %eax,-0x4(%rbp)
jmp    1220 <binary_search_recursive+0x100>
mov    -0x20(%rbp),%esi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search_recursive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    %edx,%ecx
jge    1151 <binary_search_recursive+0x31>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ecx,%r15d
mov    %edx,%r13d
mov    %esi,%r14d
mov    %rdi,%r12
nopl   (%rax)
mov    %r15d,%ebx
sub    %r13d,%ebx
mov    %ebx,%ebp
shr    $0x1f,%ebp
add    %ebx,%ebp
sar    %ebp
add    %r13d,%ebp
movslq %ebp,%rax
cmp    %r14d,(%r12,%rax,4)
jle    119b <binary_search_recursive+0x7b>
add    $0xffffffff,%ebp
lea    0xe7b(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%r15d
cmp    $0x1,%ebx
jg     1160 <binary_search_recursive+0x40>
jmp    112f <binary_search_recursive+0xf>
jge    11bf <binary_search_recursive+0x9f>
lea    0x1(%rbp),%r13d
lea    0xe58(%rip),%rdi
mov    %r13d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %ebp,%r15d
jg     1160 <binary_search_recursive+0x40>
jmp    112f <binary_search_recursive+0xf>
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1140 <binary_search_recursive+0x20>

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

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