#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
scalar_t__ aeron_reallocf(void **, size_t);
int /*<<< orphan*/ aeron_set_err(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, size_t);
int aeron_array_ensure_capacity(uint8_t **array, size_t element_size,
                                size_t old_capacity, size_t new_capacity) {
  if (aeron_reallocf((void **)array, new_capacity * element_size) < 0) {
    aeron_set_err(ENOMEM, "%s", "could not ensure capacity");
    return -1;
  }
  memset(*array + (old_capacity * element_size), 0,
         (new_capacity - old_capacity) * element_size);
  return 0;
}


x86 assembly (clang, -O0):
<aeron_array_ensure_capacity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
imul   -0x18(%rbp),%rsi
call   1050 <aeron_reallocf@plt>
cmp    $0x0,%rax
jge    118c <aeron_array_ensure_capacity+0x5c>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
lea    0xe88(%rip),%rdx
call   1030 <aeron_set_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <aeron_array_ensure_capacity+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
sub    -0x20(%rbp),%rdx
imul   -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeron_array_ensure_capacity>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rcx,%rsi
imul   %r15,%rsi
call   1050 <aeron_reallocf@plt>
test   %rax,%rax
js     1179 <aeron_array_ensure_capacity+0x49>
sub    %rbx,%r14
imul   %r15,%rbx
shl    $0x2,%rbx
add    (%r12),%rbx
imul   %r15,%r14
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
jmp    119a <aeron_array_ensure_capacity+0x6a>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
lea    0xe73(%rip),%rdx
call   1030 <aeron_set_err@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [26 x i8] c"could not ensure capacity\00"
define i32 @aeron_array_ensure_capacity(i32** %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32** %0, i32*** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%10 = load i32**, i32*** %6
%11 = bitcast i32** %10 to i8**
%12 = load i64, i64* %9
%13 = load i64, i64* %7
%14 = mul i64 %12, %13
%15 = call i64 @aeron_reallocf(i8** %11, i64 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i32, i32* @ENOMEM
%19 = call i32 @aeron_set_err(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %33
20: ; preds = %4
%21 = load i32**, i32*** %6
%22 = load i32*, i32** %21
%23 = load i64, i64* %8
%24 = load i64, i64* %7
%25 = mul i64 %23, %24
%26 = getelementptr inbounds i32, i32* %22, i64 %25
%27 = load i64, i64* %9
%28 = load i64, i64* %8
%29 = sub i64 %27, %28
%30 = load i64, i64* %7
%31 = mul i64 %29, %30
%32 = call i32 @memset(i32* %26, i32 0, i64 %31)
store i32 0, i32* %5
br label %33
33: ; preds = %20, %17
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [26 x i8] c"could not ensure capacity\00"
define i32 @aeron_array_ensure_capacity(i32** %0, i64 %1, i64 %2, i64 %3) {
%5 = bitcast i32** %0 to i8**
%6 = mul i64 %3, %1
%7 = tail call i64 @aeron_reallocf(i8** %5, i64 %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @ENOMEM
%11 = tail call i32 @aeron_set_err(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %19
12: ; preds = %4
%13 = load i32*, i32** %0
%14 = mul i64 %2, %1
%15 = getelementptr inbounds i32, i32* %13, i64 %14
%16 = sub i64 %3, %2
%17 = mul i64 %16, %1
%18 = tail call i32 @memset(i32* %15, i32 0, i64 %17)
br label %19
19: ; preds = %12, %9
%20 = phi i32 [ -1, %9 ], [ 0, %12 ]
ret i32 %20
}