#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
void *malloc(size_t const);
void *realloc(void *, size_t const);
int /*<<< orphan*/ stderr;
void vmg_adjust_vector_capacity(void **v, int *cap, const int required,
                                const size_t elementSize) {
  if (required <= (*cap))
    return;
  if (((*v) == NULL) || ((*cap) == 0)) {
    (*v) = malloc(elementSize * required);
    if ((*v) == NULL)
      goto ERROR;
    (*cap) = required;
    return;
  } else {
    (*cap) *= 2;
    if ((*cap) < required)
      (*cap) = required;
    void *other = realloc((*v), elementSize * (*cap));
    if (other == NULL)
      goto ERROR;
    (*v) = other;
  }
  return;
ERROR:
  fprintf(stderr, "ERROR: No memory left. Could not allocate/resize vector\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<vmg_adjust_vector_capacity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jg     116b <vmg_adjust_vector_capacity+0x2b>
jmp    1247 <vmg_adjust_vector_capacity+0x107>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1186 <vmg_adjust_vector_capacity+0x46>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11c2 <vmg_adjust_vector_capacity+0x82>
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rax
imul   %rax,%rdi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11b4 <vmg_adjust_vector_capacity+0x74>
jmp    1224 <vmg_adjust_vector_capacity+0xe4>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1247 <vmg_adjust_vector_capacity+0x107>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jge    11e5 <vmg_adjust_vector_capacity+0xa5>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
imul   %rax,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1214 <vmg_adjust_vector_capacity+0xd4>
jmp    1224 <vmg_adjust_vector_capacity+0xe4>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1247 <vmg_adjust_vector_capacity+0x107>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmg_adjust_vector_capacity>:
push   %rbp
push   %r14
push   %rbx
mov    (%rsi),%eax
cmp    %edx,%eax
jge    1193 <vmg_adjust_vector_capacity+0x53>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rdi),%rdi
test   %rdi,%rdi
je     117d <vmg_adjust_vector_capacity+0x3d>
test   %eax,%eax
je     117d <vmg_adjust_vector_capacity+0x3d>
add    %eax,%eax
cmp    %ebp,%eax
cmovl  %ebp,%eax
mov    %eax,(%rbx)
movslq %eax,%rsi
imul   %rcx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     1198 <vmg_adjust_vector_capacity+0x58>
mov    %rax,(%r14)
jmp    1193 <vmg_adjust_vector_capacity+0x53>
movslq %ebp,%rdi
imul   %rcx,%rdi
call   1040 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1198 <vmg_adjust_vector_capacity+0x58>
mov    %ebp,(%rbx)
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [57 x i8] c"ERROR: No memory left. Could not allocate/resize vector\0A\00"
@EXIT_FAILURE = global i32 0
define void @vmg_adjust_vector_capacity(i8** %0, i32* %1, i32 %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i64 %3, i64* %8
%10 = load i32, i32* %7
%11 = load i32*, i32** %6
%12 = load i32, i32* %11
%13 = icmp sle i32 %10, %12
br i1 %13, label %14, label %15
14: ; preds = %4
br label %69
15: ; preds = %4
%16 = load i8**, i8*** %5
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32*, i32** %6
%21 = load i32, i32* %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %19, %15
%24 = load i64, i64* %8
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = mul i64 %24, %26
%28 = call i8* @malloc(i64 %27)
%29 = load i8**, i8*** %5
store i8* %28, i8** %29
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %23
br label %64
34: ; preds = %23
%35 = load i32, i32* %7
%36 = load i32*, i32** %6
store i32 %35, i32* %36
br label %69
37: ; preds = %19
%38 = load i32*, i32** %6
%39 = load i32, i32* %38
%40 = mul nsw i32 %39, 2
store i32 %40, i32* %38
%41 = load i32*, i32** %6
%42 = load i32, i32* %41
%43 = load i32, i32* %7
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i32, i32* %7
%47 = load i32*, i32** %6
store i32 %46, i32* %47
br label %48
48: ; preds = %45, %37
%49 = load i8**, i8*** %5
%50 = load i8*, i8** %49
%51 = load i64, i64* %8
%52 = load i32*, i32** %6
%53 = load i32, i32* %52
%54 = sext i32 %53 to i64
%55 = mul i64 %51, %54
%56 = call i8* @realloc(i8* %50, i64 %55)
store i8* %56, i8** %9
%57 = load i8*, i8** %9
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %60
59: ; preds = %48
br label %64
60: ; preds = %48
%61 = load i8*, i8** %9
%62 = load i8**, i8*** %5
store i8* %61, i8** %62
br label %63
63: ; preds = %60
br label %69
64: ; preds = %59, %33
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %63, %34, %14
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [57 x i8] c"ERROR: No memory left. Could not allocate/resize vector\0A\00"
@EXIT_FAILURE = global i32 0
define void @vmg_adjust_vector_capacity(i8** nocapture %0, i32* nocapture %1, i32 %2, i64 %3) {
%5 = load i32, i32* %1
%6 = icmp slt i32 %5, %2
br i1 %6, label %7, label %32
7: ; preds = %4
%8 = load i8*, i8** %0
%9 = icmp eq i8* %8, null
%10 = icmp eq i32 %5, 0
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = sext i32 %2 to i64
%14 = mul i64 %13, %3
%15 = tail call i8* @malloc(i64 %14)
store i8* %15, i8** %0
%16 = icmp eq i8* %15, null
br i1 %16, label %27, label %17
17: ; preds = %12
store i32 %2, i32* %1
br label %32
18: ; preds = %7
%19 = shl nsw i32 %5, 1
%20 = icmp slt i32 %19, %2
%21 = select i1 %20, i32 %2, i32 %19
store i32 %21, i32* %1
%22 = sext i32 %21 to i64
%23 = mul i64 %22, %3
%24 = tail call i8* @realloc(i8* %8, i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %18
store i8* %24, i8** %0
br label %32
27: ; preds = %18, %12
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %26, %4, %17
ret void
}