#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 *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int strlen(char *);
void append(char **base, char *str) {
  char *oldbase;
  char *newbase;
  int alloclen;
  oldbase = *base;
  if (!oldbase) {
    newbase = strdup(str);
    if (!newbase) {
      fprintf(stderr, "ERROR: Failed to strdup %s\n", str);
      exit(EXIT_FAILURE);
    }
  } else {
    alloclen = strlen(oldbase) + strlen(str) + 2;
    newbase = (char *)malloc(alloclen);
    if (!newbase) {
      fprintf(stderr, "ERROR: Failed to allocate %d bytes\n", alloclen);
      exit(EXIT_FAILURE);
    }
    snprintf(newbase, alloclen, "%s %s\n", oldbase, str);
    free(oldbase);
  }
  *base = newbase;
}


x86 assembly (clang, -O0):
<append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dc <append+0x6c>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d7 <append+0x67>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1261 <append+0xf1>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123d <append+0xcd>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xded(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%r12
test   %r12,%r12
je     11ce <append+0x5e>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x2,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11ea <append+0x7a>
mov    %rax,%rbx
lea    0xe8c(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r12,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    11de <append+0x6e>
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1205 <append+0x95>
mov    %rbx,(%r15)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    121f <append+0xaf>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"ERROR: Failed to strdup %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"ERROR: Failed to allocate %d bytes\0A\00"
@.str.2 = [7 x i8] c"%s %s\0A\00"
define void @append(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8** %0, i8*** %3
store i8* %1, i8** %4
%8 = load i8**, i8*** %3
%9 = load i8*, i8** %8
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %24, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %4
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %12
br label %50
24: ; preds = %2
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %26, %28
%30 = add nsw i32 %29, 2
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = icmp ne i8* %34, null
br i1 %35, label %42, label %36
36: ; preds = %24
%37 = load i32, i32* @stderr
%38 = load i32, i32* %7
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %24
%43 = load i8*, i8** %6
%44 = load i32, i32* %7
%45 = load i8*, i8** %5
%46 = load i8*, i8** %4
%47 = call i32 @snprintf(i8* %43, i32 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %45, i8* %46)
%48 = load i8*, i8** %5
%49 = call i32 @free(i8* %48)
br label %50
50: ; preds = %42, %23
%51 = load i8*, i8** %6
%52 = load i8**, i8*** %3
store i8* %51, i8** %52
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"ERROR: Failed to strdup %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [36 x i8] c"ERROR: Failed to allocate %d bytes\0A\00"
@.str.2 = [7 x i8] c"%s %s\0A\00"
define void @append(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %29
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %1)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = tail call i32 @strlen(i8* %3)
%15 = tail call i32 @strlen(i8* %1)
%16 = add i32 %14, 2
%17 = add i32 %16, %15
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %13
%27 = tail call i32 @snprintf(i8* %19, i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* %1)
%28 = tail call i32 @free(i8* %3)
br label %29
29: ; preds = %5, %26
%30 = phi i8* [ %19, %26 ], [ %6, %5 ]
store i8* %30, i8** %0
ret void
}