#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 */
struct Listy {
  int dummy;
};

/* Variables and functions */
int binarySearch(struct Listy *, int, int, int);
int getElementAt(struct Listy *, int);
int findIndex(struct Listy *list, int x) {
  int index = 1;
  while (getElementAt(list, index) && getElementAt(list, index) < x) {
    index *= 2;
  }
  return binarySearch(list, x, index / 2, index);
}


x86 assembly (clang, -O0):
<findIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <getElementAt@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
je     1167 <findIndex+0x47>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <getElementAt@plt>
cmp    -0xc(%rbp),%eax
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1177 <findIndex+0x57>
jmp    1185 <findIndex+0x65>
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <findIndex+0x16>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x10(%rbp),%ecx
call   1040 <binarySearch@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findIndex>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    $0x1,%ebp
mov    $0x1,%esi
call   1030 <getElementAt@plt>
test   %eax,%eax
je     116f <findIndex+0x4f>
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <getElementAt@plt>
cmp    %r14d,%eax
jge    116f <findIndex+0x4f>
add    %ebp,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <getElementAt@plt>
test   %eax,%eax
jne    1150 <findIndex+0x30>
mov    %ebp,%edx
shr    %edx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %ebp,%ecx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <binarySearch@plt>

LLVM IR (-O0):
%struct.Listy = type { i32 }
define i32 @findIndex(%struct.Listy* %0, i32 %1) {
%3 = alloca %struct.Listy*
%4 = alloca i32
%5 = alloca i32
store %struct.Listy* %0, %struct.Listy** %3
store i32 %1, i32* %4
store i32 1, i32* %5
br label %6
6: ; preds = %19, %2
%7 = load %struct.Listy*, %struct.Listy** %3
%8 = load i32, i32* %5
%9 = call i32 @getElementAt(%struct.Listy* %7, i32 %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %6
%12 = load %struct.Listy*, %struct.Listy** %3
%13 = load i32, i32* %5
%14 = call i32 @getElementAt(%struct.Listy* %12, i32 %13)
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br label %17
17: ; preds = %11, %6
%18 = phi i1 [ false, %6 ], [ %16, %11 ]
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = load i32, i32* %5
%21 = mul nsw i32 %20, 2
store i32 %21, i32* %5
br label %6
22: ; preds = %17
%23 = load %struct.Listy*, %struct.Listy** %3
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = sdiv i32 %25, 2
%27 = load i32, i32* %5
%28 = call i32 @binarySearch(%struct.Listy* %23, i32 %24, i32 %26, i32 %27)
ret i32 %28
}

LLVM IR (-O2):
%struct.Listy = type { i32 }
define i32 @findIndex(%struct.Listy* %0, i32 %1) {
%3 = tail call i32 @getElementAt(%struct.Listy* %0, i32 1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %13, label %5
5: ; preds = %2, %9
%6 = phi i32 [ %10, %9 ], [ 1, %2 ]
%7 = tail call i32 @getElementAt(%struct.Listy* %0, i32 %6)
%8 = icmp slt i32 %7, %1
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = shl nsw i32 %6, 1
%11 = tail call i32 @getElementAt(%struct.Listy* %0, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %5
13: ; preds = %5, %9, %2
%14 = phi i32 [ 1, %2 ], [ %10, %9 ], [ %6, %5 ]
%15 = lshr i32 %14, 1
%16 = tail call i32 @binarySearch(%struct.Listy* %0, i32 %1, i32 %15, i32 %14)
ret i32 %16
}