#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*/ toAppend;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
char *append(char *original, int originalSize, char *toAppend) {
  if (original == NULL) {
    original = malloc(sizeof(toAppend));
  }
  if (original == NULL) {
    return NULL;
  }
  original = (char *)realloc(original, (originalSize + 1) * sizeof(char));
  if (original == NULL) {
    fprintf(stderr, " No hay más memoria.\n");
    return NULL;
  }
  original[originalSize] = *toAppend;
  return original;
}


x86 assembly (clang, -O0):
<append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115c <append+0x2c>
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1174 <append+0x44>
movq   $0x0,-0x8(%rbp)
jmp    11d5 <append+0xa5>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bc <append+0x8c>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d5 <append+0xa5>
mov    -0x20(%rbp),%rax
mov    (%rax),%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebx
test   %rdi,%rdi
jne    1150 <append+0x20>
mov    $0x8,%edi
call   1040 <malloc@plt>
mov    %rax,%rdi
test   %rax,%rax
je     117d <append+0x4d>
lea    0x1(%rbx),%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     1168 <append+0x38>
mov    (%r14),%cl
movslq %ebx,%rdx
mov    %cl,(%rax,%rdx,1)
jmp    117f <append+0x4f>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c" No hay m\C3\A1s memoria.\0A\00"
define i8* @append(i8* %0, i32 %1, i8* nocapture %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call dereferenceable_or_null(8) i8* @malloc(i32 8)
%7 = icmp eq i8* %6, null
br i1 %7, label %21, label %8
8: ; preds = %3, %5
%9 = phi i8* [ %6, %5 ], [ %0, %3 ]
%10 = add nsw i32 %1, 1
%11 = tail call i64 @realloc(i8* %9, i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %21
17: ; preds = %8
%18 = load i8, i8* %2
%19 = sext i32 %1 to i64
%20 = getelementptr inbounds i8, i8* %12, i64 %19
store i8 %18, i8* %20
br label %21
21: ; preds = %5, %17, %14
%22 = phi i8* [ null, %14 ], [ %12, %17 ], [ null, %5 ]
ret i8* %22
}