#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 *);
long *realloc(long *, int);
int /*<<< orphan*/ stderr;
int array_add(long *array, int element, int *size, int *alloc) {
  if (*size == *alloc) {
    *alloc *= 2;
    long *temp = realloc(array, *alloc * sizeof *array);
    if (!temp) {
      fprintf(stderr, "Couldn't realloc\n");
      exit(EXIT_FAILURE);
    }
    array = temp;
  }
  array[*size] = element;
  return (*size)++;
}


x86 assembly (clang, -O0):
<array_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jne    11b4 <array_add+0x84>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ac <array_add+0x7c>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
movslq -0xc(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_add>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%ebp
mov    (%rdx),%esi
cmp    (%rcx),%esi
jne    1155 <array_add+0x25>
lea    (%rsi,%rsi,1),%eax
mov    %eax,(%rcx)
shl    $0x4,%esi
call   1040 <realloc@plt>
test   %rax,%rax
je     116b <array_add+0x3b>
mov    %rax,%rdi
mov    (%rbx),%esi
movslq %ebp,%rcx
movslq %esi,%rax
mov    %rcx,(%rdi,%rax,8)
lea    0x1(%rax),%ecx
mov    %ecx,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Couldn't realloc\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @array_add(i64* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64*
store i64* %0, i64** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%10 = load i32*, i32** %7
%11 = load i32, i32* %10
%12 = load i32*, i32** %8
%13 = load i32, i32* %12
%14 = icmp eq i32 %11, %13
br i1 %14, label %15, label %35
15: ; preds = %4
%16 = load i32*, i32** %8
%17 = load i32, i32* %16
%18 = mul nsw i32 %17, 2
store i32 %18, i32* %16
%19 = load i64*, i64** %5
%20 = load i32*, i32** %8
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 8
%24 = trunc i64 %23 to i32
%25 = call i64* @realloc(i64* %19, i32 %24)
store i64* %25, i64** %9
%26 = load i64*, i64** %9
%27 = icmp ne i64* %26, null
br i1 %27, label %33, label %28
28: ; preds = %15
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %15
%34 = load i64*, i64** %9
store i64* %34, i64** %5
br label %35
35: ; preds = %33, %4
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = load i64*, i64** %5
%39 = load i32*, i32** %7
%40 = load i32, i32* %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i64, i64* %38, i64 %41
store i64 %37, i64* %42
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = add nsw i32 %44, 1
store i32 %45, i32* %43
ret i32 %44
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Couldn't realloc\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @array_add(i64* %0, i32 %1, i32* nocapture %2, i32* nocapture %3) {
%5 = load i32, i32* %2
%6 = load i32, i32* %3
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %20
8: ; preds = %4
%9 = shl nsw i32 %5, 1
store i32 %9, i32* %3
%10 = shl i32 %5, 4
%11 = tail call i64* @realloc(i64* %0, i32 %10)
%12 = icmp eq i64* %11, null
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = load i32, i32* %2
br label %20
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %13, %4
%21 = phi i32 [ %5, %4 ], [ %14, %13 ]
%22 = phi i64* [ %0, %4 ], [ %11, %13 ]
%23 = sext i32 %1 to i64
%24 = sext i32 %21 to i64
%25 = getelementptr inbounds i64, i64* %22, i64 %24
store i64 %23, i64* %25
%26 = add nsw i32 %21, 1
store i32 %26, i32* %2
ret i32 %21
}