#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 binsearch2(int x, int v[], int n) {
  int low, high, mid;
  int loop_iters = 0;
  low = 0;
  high = n - 1;
  mid = (low + high) / 2;
  while (low <= high) {
    loop_iters++;
    printf("mid = %d, high=%d, low=%d, v[mid]=%d\n", mid, high, low, v[mid]);
    if (x < v[mid]) {
      printf("%d < v[%d]=%d\n", x, mid, v[mid]);
      high = mid - 1;
      printf("Set high = %d\n", high);
    } else {
      printf("%d >= v[%d]=%d\n", x, mid, v[mid]);
      low = mid + 1;
      printf("Set low = %d\n", low);
    }
    mid = (low + high) / 2;
  }
  if (x == v[mid]) {
    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):
<binsearch2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1223 <binsearch2+0x113>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%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    11d4 <binsearch2+0xc4>
mov    -0x8(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120d <binsearch2+0xfd>
mov    -0x8(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
jmp    114a <binsearch2+0x3a>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    126f <binsearch2+0x15f>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1287 <binsearch2+0x177>
mov    -0x24(%rbp),%esi
lea    0xdf2(%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):
<binsearch2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,0x4(%rsp)
lea    -0x1(%rdx),%r13d
mov    %r13d,%eax
shr    $0x1f,%eax
lea    (%rdx,%rax,1),%ebx
add    $0xffffffff,%ebx
sar    %ebx
xor    %ebp,%ebp
test   %edx,%edx
jle    11dd <binsearch2+0xcd>
xor    %r12d,%r12d
jmp    118b <binsearch2+0x7b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xeed(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%ebx
lea    0xeea(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%r12d
lea    (%r12,%r13,1),%eax
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
add    $0x1,%ebp
cmp    %r13d,%r12d
jg     11dd <binsearch2+0xcd>
movslq %ebx,%r14
mov    (%r15,%r14,4),%r8d
lea    0xe67(%rip),%rdi
mov    %r14d,%esi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%r15,%r14,4),%ecx
mov    0x4(%rsp),%esi
cmp    %esi,%ecx
jle    1150 <binsearch2+0x40>
lea    0xe6a(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0xffffffff,%ebx
lea    0xe66(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%r13d
jmp    1176 <binsearch2+0x66>
movslq %ebx,%rax
mov    $0xffffffff,%r12d
mov    0x4(%rsp),%edx
cmp    %edx,(%r15,%rax,4)
jne    1203 <binsearch2+0xf3>
lea    0xe6b(%rip),%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%r12d
lea    0xe61(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12d,%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 = [15 x i8] c"%d < v[%d]=%d\0A\00"
@.str.2 = [15 x i8] c"Set high = %d\0A\00"
@.str.3 = [16 x i8] c"%d >= v[%d]=%d\0A\00"
@.str.4 = [14 x i8] c"Set low = %d\0A\00"
@.str.5 = [9 x i8] c"%d = %d\0A\00"
@.str.6 = [22 x i8] c"Loop iterations = %d\0A\00"
define i32 @binsearch2(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* %11
store i32 0, i32* %8
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %9
%14 = load i32, i32* %8
%15 = load i32, i32* %9
%16 = add nsw i32 %14, %15
%17 = sdiv i32 %16, 2
store i32 %17, i32* %10
br label %18
18: ; preds = %67, %3
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %72
22: ; preds = %18
%23 = load i32, i32* %11
%24 = add nsw i32 %23, 1
store i32 %24, i32* %11
%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 %54
41: ; preds = %22
%42 = load i32, i32* %5
%43 = load i32, i32* %10
%44 = load i32*, i32** %6
%45 = load i32, i32* %10
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %43, i32 %48)
%50 = load i32, i32* %10
%51 = sub nsw i32 %50, 1
store i32 %51, i32* %9
%52 = load i32, i32* %9
%53 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %67
54: ; preds = %22
%55 = load i32, i32* %5
%56 = load i32, i32* %10
%57 = load i32*, i32** %6
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i32, i32* %57, i64 %59
%61 = load i32, i32* %60
%62 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %55, i32 %56, i32 %61)
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
%65 = load i32, i32* %8
%66 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %54, %41
%68 = load i32, i32* %8
%69 = load i32, i32* %9
%70 = add nsw i32 %68, %69
%71 = sdiv i32 %70, 2
store i32 %71, i32* %10
br label %18
72: ; preds = %18
%73 = load i32, i32* %5
%74 = load i32*, i32** %6
%75 = load i32, i32* %10
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i32, i32* %74, i64 %76
%78 = load i32, i32* %77
%79 = icmp eq i32 %73, %78
br i1 %79, label %80, label %91
80: ; preds = %72
%81 = load i32, i32* %5
%82 = load i32*, i32** %6
%83 = load i32, i32* %10
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i32, i32* %82, i64 %84
%86 = load i32, i32* %85
%87 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %81, i32 %86)
%88 = load i32, i32* %11
%89 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %88)
%90 = load i32, i32* %10
store i32 %90, i32* %4
br label %94
91: ; preds = %72
%92 = load i32, i32* %11
%93 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %92)
store i32 -1, i32* %4
br label %94
94: ; preds = %91, %80
%95 = load i32, i32* %4
ret i32 %95
}

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