#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 */
size_t array_next_count(size_t);
void *realloc(void *, size_t);
int array_std_reserve(void *baton, void **mem, size_t elm_size, size_t want,
                      size_t *have) {
  if (*have >= want) {
    return 0;
  }
  size_t next_size = array_next_count(want);
  void *mem_new = realloc(*mem, next_size * elm_size);
  if (mem_new != NULL) {
    *mem = mem_new;
    *have = next_size;
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<array_std_reserve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jb     1159 <array_std_reserve+0x39>
movl   $0x0,-0x4(%rbp)
jmp    11b3 <array_std_reserve+0x93>
mov    -0x28(%rbp),%rdi
call   1030 <array_next_count@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
imul   -0x20(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11ac <array_std_reserve+0x8c>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11b3 <array_std_reserve+0x93>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_std_reserve>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r12d,%r12d
cmp    %rcx,(%r8)
jae    1166 <array_std_reserve+0x46>
mov    %r8,%rbx
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rcx,%rdi
call   1030 <array_next_count@plt>
mov    %rax,%r15
mov    (%r14),%rdi
imul   %rax,%rbp
mov    %rbp,%rsi
call   1040 <realloc@plt>
test   %rax,%rax
je     1160 <array_std_reserve+0x40>
mov    %rax,(%r14)
mov    %r15,(%rbx)
jmp    1166 <array_std_reserve+0x46>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @array_std_reserve(i8* %0, i8** %1, i64 %2, i64 %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i8*
store i8* %0, i8** %7
store i8** %1, i8*** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64* %4, i64** %11
%14 = load i64*, i64** %11
%15 = load i64, i64* %14
%16 = load i64, i64* %10
%17 = icmp uge i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %5
store i32 0, i32* %6
br label %36
19: ; preds = %5
%20 = load i64, i64* %10
%21 = call i64 @array_next_count(i64 %20)
store i64 %21, i64* %12
%22 = load i8**, i8*** %8
%23 = load i8*, i8** %22
%24 = load i64, i64* %12
%25 = load i64, i64* %9
%26 = mul i64 %24, %25
%27 = call i8* @realloc(i8* %23, i64 %26)
store i8* %27, i8** %13
%28 = load i8*, i8** %13
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %35
30: ; preds = %19
%31 = load i8*, i8** %13
%32 = load i8**, i8*** %8
store i8* %31, i8** %32
%33 = load i64, i64* %12
%34 = load i64*, i64** %11
store i64 %33, i64* %34
store i32 0, i32* %6
br label %36
35: ; preds = %19
store i32 -1, i32* %6
br label %36
36: ; preds = %35, %30, %18
%37 = load i32, i32* %6
ret i32 %37
}

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