#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 *malloc(unsigned int);
void *realloc(void *, unsigned int);
void *Athena_AssureCapacity(void *data, unsigned element_size, unsigned size,
                            unsigned *capacity) {
  unsigned c = *capacity;
  if (size > c) {
    if (c < 0x100)
      c += 0x100;
    else
      c <<= 1;
    if (!data)
      data = malloc(element_size * c);
    else
      data = realloc(data, element_size * c);
    capacity[0] = c;
  }
  return data;
}


x86 assembly (clang, -O0):
<Athena_AssureCapacity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jbe    11ae <Athena_AssureCapacity+0x8e>
cmpl   $0x100,-0x1c(%rbp)
jae    1168 <Athena_AssureCapacity+0x48>
mov    -0x1c(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x1c(%rbp)
jmp    1171 <Athena_AssureCapacity+0x51>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1191 <Athena_AssureCapacity+0x71>
mov    -0xc(%rbp),%edi
imul   -0x1c(%rbp),%edi
call   1030 <malloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11a5 <Athena_AssureCapacity+0x85>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
imul   -0x1c(%rbp),%esi
call   1040 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Athena_AssureCapacity>:
push   %rbp
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdi,%rax
mov    (%rcx),%ecx
cmp    %edx,%ecx
jae    115c <Athena_AssureCapacity+0x3c>
lea    0x100(%rcx),%edx
lea    (%rcx,%rcx,1),%ebp
cmp    $0x100,%ecx
cmovb  %edx,%ebp
imul   %ebp,%esi
test   %rax,%rax
je     1153 <Athena_AssureCapacity+0x33>
mov    %rax,%rdi
call   1040 <realloc@plt>
jmp    115a <Athena_AssureCapacity+0x3a>
mov    %esi,%edi
call   1030 <malloc@plt>
mov    %ebp,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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