#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 */
scalar_t__ array_grow(int **, int *, int);
int /*<<< orphan*/ memset(int *, int, int);
int available_index(int **list, int *cap) {
  int i, orig_cap;
  for (i = 0; i < *cap; ++i)
    if ((*list)[i] == -1)
      return i;
  orig_cap = *cap;
  if (array_grow(list, cap, sizeof(int)) < 0)
    return -1;
  memset((*list) + orig_cap, -1, (*cap - orig_cap) * sizeof(int));
  return orig_cap;
}


x86 assembly (clang, -O0):
<available_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    1179 <available_index+0x59>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    1166 <available_index+0x46>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <available_index+0xbd>
jmp    116b <available_index+0x4b>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1137 <available_index+0x17>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <array_grow@plt>
cmp    $0x0,%rax
jge    11aa <available_index+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <available_index+0xbd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
sub    -0x20(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edx
mov    $0xffffffff,%esi
call   1040 <memset@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<available_index>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
movslq (%rsi),%rbx
test   %rbx,%rbx
jle    114f <available_index+0x2f>
mov    %ebx,%eax
mov    (%r15),%rdx
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
cmpl   $0xffffffff,(%rdx,%rcx,4)
je     1182 <available_index+0x62>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1140 <available_index+0x20>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <array_grow@plt>
test   %rax,%rax
js     1186 <available_index+0x66>
mov    %rbx,%rdi
shl    $0x2,%rdi
add    (%r15),%rdi
mov    (%r14),%edx
sub    %ebx,%edx
shl    $0x2,%edx
mov    $0xffffffff,%esi
call   1040 <memset@plt>
jmp    118b <available_index+0x6b>
mov    %ecx,%ebx
jmp    118b <available_index+0x6b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @available_index(i32** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32** %0, i32*** %4
store i32* %1, i32** %5
store i32 0, i32* %6
br label %8
8: ; preds = %24, %2
%9 = load i32, i32* %6
%10 = load i32*, i32** %5
%11 = load i32, i32* %10
%12 = icmp slt i32 %9, %11
br i1 %12, label %13, label %27
13: ; preds = %8
%14 = load i32**, i32*** %4
%15 = load i32*, i32** %14
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %15, i64 %17
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = load i32, i32* %6
store i32 %22, i32* %3
br label %50
23: ; preds = %13
br label %24
24: ; preds = %23
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
br label %8
27: ; preds = %8
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
store i32 %29, i32* %7
%30 = load i32**, i32*** %4
%31 = load i32*, i32** %5
%32 = call i64 @array_grow(i32** %30, i32* %31, i32 4)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 -1, i32* %3
br label %50
35: ; preds = %27
%36 = load i32**, i32*** %4
%37 = load i32*, i32** %36
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32*, i32** %5
%42 = load i32, i32* %41
%43 = load i32, i32* %7
%44 = sub nsw i32 %42, %43
%45 = sext i32 %44 to i64
%46 = mul i64 %45, 4
%47 = trunc i64 %46 to i32
%48 = call i32 @memset(i32* %40, i32 -1, i32 %47)
%49 = load i32, i32* %7
store i32 %49, i32* %3
br label %50
50: ; preds = %35, %34, %21
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
define i32 @available_index(i32** %0, i32* %1) {
%3 = load i32, i32* %1
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = load i32*, i32** %0
%7 = zext i32 %3 to i64
br label %8
8: ; preds = %5, %13
%9 = phi i64 [ 0, %5 ], [ %14, %13 ]
%10 = getelementptr inbounds i32, i32* %6, i64 %9
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, -1
br i1 %12, label %27, label %13
13: ; preds = %8
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %16, label %8
16: ; preds = %13, %2
%17 = tail call i64 @array_grow(i32** %0, i32* %1, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %16
%20 = load i32*, i32** %0
%21 = sext i32 %3 to i64
%22 = getelementptr inbounds i32, i32* %20, i64 %21
%23 = load i32, i32* %1
%24 = sub nsw i32 %23, %3
%25 = shl i32 %24, 2
%26 = tail call i32 @memset(i32* %22, i32 -1, i32 %25)
br label %29
27: ; preds = %8
%28 = trunc i64 %9 to i32
br label %29
29: ; preds = %27, %16, %19
%30 = phi i32 [ %3, %19 ], [ -1, %16 ], [ %28, %27 ]
ret i32 %30
}