#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 binSearch2(int *, int, int, int);
int /*<<< orphan*/ printf(char *, int);
int findInInfiniteArray(int *a, int key) {
  int val = a[0];
  int l = 0;
  int r = 1;
  while (val < key) {
    l = r;
    r = 2 * r;
    val = a[r];
    printf("high %d\n", r);
  }
  return binSearch2(a, l, r, key);
}


x86 assembly (clang, -O0):
<findInInfiniteArray>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1183 <findInInfiniteArray+0x63>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe87(%rip),%rdi
call   1030 <printf@plt>
jmp    1146 <findInInfiniteArray+0x26>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0xc(%rbp),%ecx
call   1040 <binSearch2@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findInInfiniteArray>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
cmp    %esi,(%rdi)
jge    116b <findInInfiniteArray+0x4b>
mov    $0x1,%ebp
lea    0xebf(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%ebx
lea    (%rbx,%rbx,1),%ebp
movslq %ebp,%rsi
mov    (%r15,%rsi,4),%r13d
mov    %r12,%rdi
call   1030 <printf@plt>
cmp    %r14d,%r13d
jl     1150 <findInInfiniteArray+0x30>
jmp    1172 <findInInfiniteArray+0x52>
mov    $0x1,%ebp
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <binSearch2@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"high %d\0A\00"
define i32 @findInInfiniteArray(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
%8 = load i32*, i32** %3
%9 = getelementptr inbounds i32, i32* %8, i64 0
%10 = load i32, i32* %9
store i32 %10, i32* %5
store i32 0, i32* %6
store i32 1, i32* %7
br label %11
11: ; preds = %15, %2
%12 = load i32, i32* %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = load i32, i32* %7
store i32 %16, i32* %6
%17 = load i32, i32* %7
%18 = mul nsw i32 2, %17
store i32 %18, i32* %7
%19 = load i32*, i32** %3
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
store i32 %23, i32* %5
%24 = load i32, i32* %7
%25 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %11
26: ; preds = %11
%27 = load i32*, i32** %3
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = load i32, i32* %4
%31 = call i32 @binSearch2(i32* %27, i32 %28, i32 %29, i32 %30)
ret i32 %31
}

LLVM IR (-O2):
@.str = [9 x i8] c"high %d\0A\00"
define i32 @findInInfiniteArray(i32* %0, i32 %1) {
%3 = load i32, i32* %0
%4 = icmp slt i32 %3, %1
br i1 %4, label %5, label %13
5: ; preds = %2, %5
%6 = phi i32 [ %7, %5 ], [ 1, %2 ]
%7 = shl nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = load i32, i32* %9
%11 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%12 = icmp slt i32 %10, %1
br i1 %12, label %5, label %13
13: ; preds = %5, %2
%14 = phi i32 [ 0, %2 ], [ %6, %5 ]
%15 = phi i32 [ 1, %2 ], [ %7, %5 ]
%16 = tail call i32 @binSearch2(i32* %0, i32 %14, i32 %15, i32 %1)
ret i32 %16
}