#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 binary_search(int *, int, int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, int, int);
int sanity_check_array(int *, int);
int _get_position(int *parr, int key) {
  int low, high, val;
  do {
    if (sanity_check_array((parr), (1)) == -1)
      exit(-1);
  } while (0);
  low = 0;
  high = 1;
  val = -1;
  while (low < high) {
    printf("\nlow:=%d, high:=%d", low, high);
    if (key == parr[high]) {
      val = high;
      break;
    } else if (key > parr[high]) {
      low = high;
      high = high * 2;
    } else {
      printf("\n bin search, low:=%d, high:=%d", low, high);
      val = binary_search(parr, low, high, key);
      break;
    }
  }
  return val;
}


x86 assembly (clang, -O0):
<_get_position>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1030 <sanity_check_array@plt>
cmp    $0xffffffff,%eax
jne    1170 <_get_position+0x30>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    1175 <_get_position+0x35>
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1225 <_get_position+0xe5>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    11c7 <_get_position+0x87>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1225 <_get_position+0xe5>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11ef <_get_position+0xaf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121b <_get_position+0xdb>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0xc(%rbp),%ecx
call   1050 <binary_search@plt>
mov    %eax,-0x18(%rbp)
jmp    1225 <_get_position+0xe5>
jmp    1220 <_get_position+0xe0>
jmp    118a <_get_position+0x4a>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_position>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    $0x1,%ebx
mov    $0x1,%esi
call   1030 <sanity_check_array@plt>
cmp    $0xffffffff,%eax
je     11d9 <_get_position+0x99>
xor    %r13d,%r13d
lea    0xe91(%rip),%r12
nop
mov    %ebx,%ebp
mov    %r12,%rdi
mov    %r13d,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
mov    %ebx,%eax
cmp    %r14d,(%r15,%rax,4)
je     1199 <_get_position+0x59>
jge    11aa <_get_position+0x6a>
lea    0x0(%rbp,%rbp,1),%ebx
mov    %ebp,%r13d
cmp    %ebx,%ebp
jb     1170 <_get_position+0x30>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe62(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r13d,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <binary_search@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [19 x i8] c"\0Alow:=%d, high:=%d\00"
@.str.1 = [32 x i8] c"\0A bin search, low:=%d, high:=%d\00"
define i32 @_get_position(i32* %0, i32 %1) {
%3 = tail call i32 @sanity_check_array(i32* %0, i32 1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2, %17
%8 = phi i32 [ %18, %17 ], [ 1, %2 ]
%9 = phi i32 [ %8, %17 ], [ 0, %2 ]
%10 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %8)
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, %1
br i1 %14, label %23, label %15
15: ; preds = %7
%16 = icmp slt i32 %13, %1
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = shl nsw i32 %8, 1
%19 = icmp ult i32 %8, %18
br i1 %19, label %7, label %23
20: ; preds = %15
%21 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %8)
%22 = tail call i32 @binary_search(i32* %0, i32 %9, i32 %8, i32 %1)
br label %23
23: ; preds = %7, %17, %20
%24 = phi i32 [ %22, %20 ], [ %8, %7 ], [ -1, %17 ]
ret i32 %24
}