#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, ...);
int binsearch(int x, int v[], int n) {
  int low, high, mid;
  low = 0;
  high = n - 1;
  int loop_iters = 0;
  while (low <= high) {
    loop_iters++;
    mid = (low + high) / 2;
    printf("mid = %d, high=%d, low=%d, v[mid]=%d\n", mid, high, low, v[mid]);
    if (x < v[mid]) {
      printf("%d < %d\n", x, v[mid]);
      high = mid - 1;
    } else if (x > v[mid]) {
      printf("%d > %d\n", x, v[mid]);
      low = mid + 1;
    } else {
      printf("%d = %d\n", x, v[mid]);
      printf("Loop iterations = %d\n", loop_iters);
      return mid;
    }
  }
  printf("Loop iterations = %d\n", loop_iters);
  return -1;
}


x86 assembly (clang, -O0):
<binsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1240 <binsearch+0x130>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rdi
mov    (%rax,%rdi,4),%r8d
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    11c0 <binsearch+0xb0>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    123b <binsearch+0x12b>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11fe <binsearch+0xee>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1236 <binsearch+0x126>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1258 <binsearch+0x148>
jmp    123b <binsearch+0x12b>
jmp    1139 <binsearch+0x29>
mov    -0x24(%rbp),%esi
lea    0xdf7(%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):
<binsearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
test   %edx,%edx
jle    11cb <binsearch+0xbb>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,0x4(%rsp)
add    $0xffffffff,%r14d
mov    $0x1,%eax
xor    %r13d,%r13d
jmp    116d <binsearch+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0xecf(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0xffffffff,%r15d
mov    %r15d,%r14d
lea    0x1(%rbp),%eax
cmp    %r14d,%r13d
jg     11c3 <binsearch+0xb3>
mov    %eax,%ebp
lea    (%r14,%r13,1),%eax
mov    %eax,%r15d
shr    $0x1f,%r15d
add    %eax,%r15d
sar    %r15d
movslq %r15d,%r12
mov    (%rbx,%r12,4),%r8d
lea    0xe72(%rip),%rdi
mov    %r12d,%esi
mov    %r14d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rbx,%r12,4),%edx
mov    0x4(%rsp),%esi
cmp    %esi,%edx
jg     1150 <binsearch+0x40>
jge    11cf <binsearch+0xbf>
lea    0xe7c(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%r15d
mov    %r15d,%r13d
jmp    1165 <binsearch+0x55>
mov    $0xffffffff,%r15d
jmp    11dd <binsearch+0xcd>
xor    %ebp,%ebp
jmp    11dd <binsearch+0xcd>
lea    0xe62(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe5d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"mid = %d, high=%d, low=%d, v[mid]=%d\0A\00"
@.str.1 = [9 x i8] c"%d < %d\0A\00"
@.str.2 = [9 x i8] c"%d > %d\0A\00"
@.str.3 = [9 x i8] c"%d = %d\0A\00"
@.str.4 = [22 x i8] c"Loop iterations = %d\0A\00"
define i32 @binsearch(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
%11 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %9
store i32 0, i32* %11
br label %14
14: ; preds = %81, %3
%15 = load i32, i32* %8
%16 = load i32, i32* %9
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %82
18: ; preds = %14
%19 = load i32, i32* %11
%20 = add nsw i32 %19, 1
store i32 %20, i32* %11
%21 = load i32, i32* %8
%22 = load i32, i32* %9
%23 = add nsw i32 %21, %22
%24 = sdiv i32 %23, 2
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = load i32, i32* %9
%27 = load i32, i32* %8
%28 = load i32*, i32** %6
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26, i32 %27, i32 %32)
%34 = load i32, i32* %5
%35 = load i32*, i32** %6
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = icmp slt i32 %34, %39
br i1 %40, label %41, label %51
41: ; preds = %18
%42 = load i32, i32* %5
%43 = load i32*, i32** %6
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %47)
%49 = load i32, i32* %10
%50 = sub nsw i32 %49, 1
store i32 %50, i32* %9
br label %81
51: ; preds = %18
%52 = load i32, i32* %5
%53 = load i32*, i32** %6
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %53, i64 %55
%57 = load i32, i32* %56
%58 = icmp sgt i32 %52, %57
br i1 %58, label %59, label %69
59: ; preds = %51
%60 = load i32, i32* %5
%61 = load i32*, i32** %6
%62 = load i32, i32* %10
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %61, i64 %63
%65 = load i32, i32* %64
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %60, i32 %65)
%67 = load i32, i32* %10
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
br label %80
69: ; preds = %51
%70 = load i32, i32* %5
%71 = load i32*, i32** %6
%72 = load i32, i32* %10
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %70, i32 %75)
%77 = load i32, i32* %11
%78 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %77)
%79 = load i32, i32* %10
store i32 %79, i32* %4
br label %85
80: ; preds = %59
br label %81
81: ; preds = %80, %41
br label %14
82: ; preds = %14
%83 = load i32, i32* %11
%84 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %83)
store i32 -1, i32* %4
br label %85
85: ; preds = %82, %69
%86 = load i32, i32* %4
ret i32 %86
}

LLVM IR (-O2):
@.str = [38 x i8] c"mid = %d, high=%d, low=%d, v[mid]=%d\0A\00"
@.str.1 = [9 x i8] c"%d < %d\0A\00"
@.str.2 = [9 x i8] c"%d > %d\0A\00"
@.str.3 = [9 x i8] c"%d = %d\0A\00"
@.str.4 = [22 x i8] c"Loop iterations = %d\0A\00"
define i32 @binsearch(i32 %0, i32* nocapture %1, i32 %2) {
%4 = icmp slt i32 %2, 1
br i1 %4, label %34, label %5
5: ; preds = %3
%6 = add nsw i32 %2, -1
br label %7
7: ; preds = %5, %30
%8 = phi i32 [ %11, %30 ], [ 0, %5 ]
%9 = phi i32 [ %32, %30 ], [ %6, %5 ]
%10 = phi i32 [ %31, %30 ], [ 0, %5 ]
%11 = add nuw nsw i32 %8, 1
%12 = add nsw i32 %9, %10
%13 = sdiv i32 %12, 2
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %1, i64 %14
%16 = load i32, i32* %15
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %9, i32 %10, i32 %16)
%18 = load i32, i32* %15
%19 = icmp sgt i32 %18, %0
br i1 %19, label %20, label %23
20: ; preds = %7
%21 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %18)
%22 = add nsw i32 %13, -1
br label %30
23: ; preds = %7
%24 = icmp slt i32 %18, %0
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %18)
%27 = add nsw i32 %13, 1
br label %30
28: ; preds = %23
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %18)
br label %34
30: ; preds = %25, %20
%31 = phi i32 [ %10, %20 ], [ %27, %25 ]
%32 = phi i32 [ %22, %20 ], [ %9, %25 ]
%33 = icmp sgt i32 %31, %32
br i1 %33, label %34, label %7
34: ; preds = %30, %3, %28
%35 = phi i32 [ %11, %28 ], [ 0, %3 ], [ %11, %30 ]
%36 = phi i32 [ %13, %28 ], [ -1, %3 ], [ -1, %30 ]
%37 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %35)
ret i32 %36
}