#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*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int aeron_array_add(uint8_t **array, size_t element_size, size_t new_length,
                    uint8_t *restrict element_to_add) {
  if (aeron_reallocf((void **)array, element_size * new_length) < 0) {
    aeron_set_err(ENOMEM, "%s", "could not array add");
    return -1;
  }
  memcpy(*array + ((new_length - 1) * element_size), element_to_add,
         element_size);
  return 0;
}


x86 assembly (clang, -O0):
<aeron_array_add>:
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    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
call   1040 <aeron_reallocf@plt>
cmp    $0x0,%rax
jge    118c <aeron_array_add+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    11bb <aeron_array_add+0x8b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
imul   -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeron_array_add>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rdx,%rsi
imul   %r15,%rsi
call   1040 <aeron_reallocf@plt>
test   %rax,%rax
js     1177 <aeron_array_add+0x47>
add    $0xffffffffffffffff,%rbx
imul   %r15,%rbx
shl    $0x2,%rbx
add    (%r12),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    1198 <aeron_array_add+0x68>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
lea    0xe75(%rip),%rdx
call   1030 <aeron_set_err@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [20 x i8] c"could not array add\00"
define i32 @aeron_array_add(i32** %0, i64 %1, i64 %2, i32* noalias %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
store i32** %0, i32*** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%10 = load i32**, i32*** %6
%11 = bitcast i32** %10 to i8**
%12 = load i64, i64* %7
%13 = load i64, i64* %8
%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 ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %31
20: ; preds = %4
%21 = load i32**, i32*** %6
%22 = load i32*, i32** %21
%23 = load i64, i64* %8
%24 = sub i64 %23, 1
%25 = load i64, i64* %7
%26 = mul i64 %24, %25
%27 = getelementptr inbounds i32, i32* %22, i64 %26
%28 = load i32*, i32** %9
%29 = load i64, i64* %7
%30 = call i32 @memcpy(i32* %27, i32* %28, i64 %29)
store i32 0, i32* %5
br label %31
31: ; preds = %20, %17
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [20 x i8] c"could not array add\00"
define i32 @aeron_array_add(i32** %0, i64 %1, i64 %2, i32* noalias %3) {
%5 = bitcast i32** %0 to i8**
%6 = mul i64 %2, %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 ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %18
12: ; preds = %4
%13 = load i32*, i32** %0
%14 = add i64 %2, -1
%15 = mul i64 %14, %1
%16 = getelementptr inbounds i32, i32* %13, i64 %15
%17 = tail call i32 @memcpy(i32* %16, i32* %3, i64 %1)
br label %18
18: ; preds = %12, %9
%19 = phi i32 [ -1, %9 ], [ 0, %12 ]
ret i32 %19
}