#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 *, ...);
void b_search(int *vec, int val, int length) {
  int init, end, middle;
  init = 0;
  end = length - 1;
  int i;
  for (i = 0; end >= init; i++) {
    middle = (init + end) / 2;
    if (val > *(vec + middle))
      init = middle + 1;
    else if (val < *(vec + middle))
      end = middle - 1;
    else {
      printf("\nFound value %d after %d searches\n", *(vec + middle), i + 1);
      return;
    }
  }
  printf("I didn't find this value\n");
}


x86 assembly (clang, -O0):
<b_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11d6 <b_search+0xc6>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    1178 <b_search+0x68>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c3 <b_search+0xb3>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    119a <b_search+0x8a>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11be <b_search+0xae>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e4 <b_search+0xd4>
jmp    11c3 <b_search+0xb3>
jmp    11c8 <b_search+0xb8>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1139 <b_search+0x29>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b_search>:
test   %edx,%edx
jle    116f <b_search+0x4f>
mov    %edx,%r8d
add    $0xffffffff,%r8d
xor    %r9d,%r9d
mov    $0x1,%edx
jmp    114e <b_search+0x2e>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%eax
mov    %eax,%r9d
add    $0x1,%edx
cmp    %r9d,%r8d
jl     116f <b_search+0x4f>
lea    (%r9,%r8,1),%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
movslq %eax,%rcx
mov    (%rdi,%rcx,4),%ecx
cmp    %esi,%ecx
jl     1140 <b_search+0x20>
jle    117b <b_search+0x5b>
add    $0xffffffff,%eax
mov    %eax,%r8d
jmp    1146 <b_search+0x26>
lea    0xead(%rip),%rdi
jmp    1030 <puts@plt>
lea    0xe7e(%rip),%rdi
mov    %ecx,%esi
xor    %eax,%eax
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"\0AFound value %d after %d searches\0A\00"
@.str.1 = [26 x i8] c"I didn't find this value\0A\00"
define void @b_search(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** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %8
store i32 0, i32* %10
br label %13
13: ; preds = %54, %3
%14 = load i32, i32* %8
%15 = load i32, i32* %7
%16 = icmp sge i32 %14, %15
br i1 %16, label %17, label %57
17: ; preds = %13
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = add nsw i32 %18, %19
%21 = sdiv i32 %20, 2
store i32 %21, i32* %9
%22 = load i32, i32* %5
%23 = load i32*, i32** %4
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = icmp sgt i32 %22, %27
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %53
32: ; preds = %17
%33 = load i32, i32* %5
%34 = load i32*, i32** %4
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = icmp slt i32 %33, %38
br i1 %39, label %40, label %43
40: ; preds = %32
%41 = load i32, i32* %9
%42 = sub nsw i32 %41, 1
store i32 %42, i32* %8
br label %52
43: ; preds = %32
%44 = load i32*, i32** %4
%45 = load i32, i32* %9
%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 = add nsw i32 %49, 1
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %48, i32 %50)
br label %59
52: ; preds = %40
br label %53
53: ; preds = %52, %29
br label %54
54: ; preds = %53
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %13
57: ; preds = %13
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %59
59: ; preds = %57, %43
ret void
}

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