#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 findoccupied(int);
int findunoccupied(int);
int /*<<< orphan*/ overflow(int);
int findhole(int size) {
  int starthole = 0, endhole;
  for (;;) {
    if (starthole + size > 2047)
      break;
    starthole = findunoccupied(starthole);
    if (starthole == ((int)-1))
      break;
    endhole = findoccupied(starthole);
    if (endhole == ((int)-1))
      endhole = 2047 + 1;
    if ((endhole - starthole) >= size)
      return starthole;
    starthole = endhole;
  }
  overflow(17);
  return 0;
}


x86 assembly (clang, -O0):
<findhole>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    -0x8(%rbp),%eax
cmp    $0x7ff,%eax
jle    1158 <findhole+0x28>
jmp    11b3 <findhole+0x83>
mov    -0xc(%rbp),%edi
call   1040 <findunoccupied@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1172 <findhole+0x42>
jmp    11b3 <findhole+0x83>
mov    -0xc(%rbp),%edi
call   1030 <findoccupied@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    118e <findhole+0x5e>
movl   $0x800,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     11a8 <findhole+0x78>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c4 <findhole+0x94>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1142 <findhole+0x12>
mov    $0x11,%edi
call   1050 <overflow@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findhole>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
mov    $0x800,%r14d
xchg   %ax,%ax
lea    (%rax,%rbx,1),%ecx
cmp    $0x7ff,%ecx
jg     1171 <findhole+0x41>
mov    %eax,%edi
call   1040 <findunoccupied@plt>
cmp    $0xffffffff,%eax
je     1171 <findhole+0x41>
mov    %eax,%ebp
mov    %eax,%edi
call   1030 <findoccupied@plt>
cmp    $0xffffffff,%eax
cmove  %r14d,%eax
mov    %eax,%ecx
sub    %ebp,%ecx
cmp    %ebx,%ecx
jl     1140 <findhole+0x10>
jmp    117d <findhole+0x4d>
mov    $0x11,%edi
call   1050 <overflow@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @findhole(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
br label %6
6: ; preds = %32, %1
%7 = load i32, i32* %4
%8 = load i32, i32* %3
%9 = add nsw i32 %7, %8
%10 = icmp sgt i32 %9, 2047
br i1 %10, label %11, label %12
11: ; preds = %6
br label %34
12: ; preds = %6
%13 = load i32, i32* %4
%14 = call i32 @findunoccupied(i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %12
br label %34
18: ; preds = %12
%19 = load i32, i32* %4
%20 = call i32 @findoccupied(i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 2048, i32* %5
br label %24
24: ; preds = %23, %18
%25 = load i32, i32* %5
%26 = load i32, i32* %4
%27 = sub nsw i32 %25, %26
%28 = load i32, i32* %3
%29 = icmp sge i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = load i32, i32* %4
store i32 %31, i32* %2
br label %36
32: ; preds = %24
%33 = load i32, i32* %5
store i32 %33, i32* %4
br label %6
34: ; preds = %17, %11
%35 = call i32 @overflow(i32 17)
store i32 0, i32* %2
br label %36
36: ; preds = %34, %30
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
define i32 @findhole(i32 %0) {
br label %2
2: ; preds = %9, %1
%3 = phi i32 [ 0, %1 ], [ %12, %9 ]
%4 = add nsw i32 %3, %0
%5 = icmp sgt i32 %4, 2047
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = tail call i32 @findunoccupied(i32 %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = tail call i32 @findoccupied(i32 %7)
%11 = icmp eq i32 %10, -1
%12 = select i1 %11, i32 2048, i32 %10
%13 = sub nsw i32 %12, %7
%14 = icmp slt i32 %13, %0
br i1 %14, label %2, label %17
15: ; preds = %6, %2
%16 = tail call i32 @overflow(i32 17)
br label %17
17: ; preds = %9, %15
%18 = phi i32 [ 0, %15 ], [ %7, %9 ]
ret i32 %18
}