#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 */
void *realloc(char *, int);
int vec_expand(char **buf, int *length, int *cap, int memsz) {
  if (*length + 1 > *cap) {
    void *ptr;
    int n = (*cap == 0) ? 1 : *cap << 1;
    ptr = realloc(*buf, n * memsz);
    if (ptr == NULL) {
      return -1;
    }
    *buf = (char *)ptr;
    *cap = n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<vec_expand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jle    11aa <vec_expand+0x9a>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1156 <vec_expand+0x46>
mov    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1162 <vec_expand+0x52>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
shl    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x34(%rbp),%esi
imul   -0x24(%rbp),%esi
call   1030 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1196 <vec_expand+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <vec_expand+0xa1>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vec_expand>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rdx),%eax
xor    %r15d,%r15d
cmp    %eax,(%rsi)
jl     1152 <vec_expand+0x42>
mov    %rdx,%rbx
mov    %rdi,%r14
lea    (%rax,%rax,1),%edx
test   %eax,%eax
mov    $0x1,%ebp
cmovne %edx,%ebp
mov    (%rdi),%rdi
imul   %ebp,%ecx
mov    %ecx,%esi
call   1030 <realloc@plt>
test   %rax,%rax
je     114c <vec_expand+0x3c>
mov    %rax,(%r14)
mov    %ebp,(%rbx)
jmp    1152 <vec_expand+0x42>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vec_expand(i8** %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8** %0, i8*** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%12 = load i32*, i32** %7
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 1
%15 = load i32*, i32** %8
%16 = load i32, i32* %15
%17 = icmp sgt i32 %14, %16
br i1 %17, label %18, label %43
18: ; preds = %4
%19 = load i32*, i32** %8
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
br label %27
23: ; preds = %18
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = shl i32 %25, 1
br label %27
27: ; preds = %23, %22
%28 = phi i32 [ 1, %22 ], [ %26, %23 ]
store i32 %28, i32* %11
%29 = load i8**, i8*** %6
%30 = load i8*, i8** %29
%31 = load i32, i32* %11
%32 = load i32, i32* %9
%33 = mul nsw i32 %31, %32
%34 = call i8* @realloc(i8* %30, i32 %33)
store i8* %34, i8** %10
%35 = load i8*, i8** %10
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %38
37: ; preds = %27
store i32 -1, i32* %5
br label %44
38: ; preds = %27
%39 = load i8*, i8** %10
%40 = load i8**, i8*** %6
store i8* %39, i8** %40
%41 = load i32, i32* %11
%42 = load i32*, i32** %8
store i32 %41, i32* %42
br label %43
43: ; preds = %38, %4
store i32 0, i32* %5
br label %44
44: ; preds = %43, %37
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
define i32 @vec_expand(i8** nocapture %0, i32* nocapture %1, i32* nocapture %2, i32 %3) {
%5 = load i32, i32* %1
%6 = load i32, i32* %2
%7 = icmp slt i32 %5, %6
br i1 %7, label %17, label %8
8: ; preds = %4
%9 = icmp eq i32 %6, 0
%10 = shl i32 %6, 1
%11 = select i1 %9, i32 1, i32 %10
%12 = load i8*, i8** %0
%13 = mul nsw i32 %11, %3
%14 = tail call i8* @realloc(i8* %12, i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %8
store i8* %14, i8** %0
store i32 %11, i32* %2
br label %17
17: ; preds = %4, %16, %8
%18 = phi i32 [ -1, %8 ], [ 0, %16 ], [ 0, %4 ]
ret i32 %18
}