#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*/ asprintf(char **, char *, char const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fanout_error(char *);
char *realloc(char *, int);
char *strcat(char *, char const *);
int strlen(char const *);
char *str_append(char *target, const char *data) {
  char *newtarget;
  if (data == NULL) {
    return target;
  }
  if (target == NULL) {
    asprintf(&target, "%s", data);
    return target;
  }
  int len = strlen(target) + strlen(data) + 1;
  newtarget = realloc(target, len);
  if (newtarget == NULL) {
    fanout_error("ERROR unable to allocate memory");
    exit(EXIT_FAILURE);
  }
  newtarget = strcat(newtarget, data);
  return newtarget;
}


x86 assembly (clang, -O0):
<str_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1188 <str_append+0x28>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1224 <str_append+0xc4>
cmpq   $0x0,-0x10(%rbp)
jne    11b4 <str_append+0x54>
mov    -0x18(%rbp),%rdx
lea    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1060 <asprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1224 <str_append+0xc4>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120b <str_append+0xab>
lea    0xe0b(%rip),%rdi
call   1030 <fanout_error@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_append>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %rdi,0x8(%rsp)
test   %rsi,%rsi
je     11ce <str_append+0x6e>
mov    %rsi,%r14
test   %rbx,%rbx
je     11b5 <str_append+0x55>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
je     11db <str_append+0x7b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
jmp    11ce <str_append+0x6e>
lea    0xe44(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %r14,%rdx
call   1060 <asprintf@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe21(%rip),%rdi
call   1030 <fanout_error@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [32 x i8] c"ERROR unable to allocate memory\00"
@EXIT_FAILURE = global i32 0
define i8* @str_append(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i8*, i8** %4
store i8* %11, i8** %3
br label %40
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %4
store i8* %18, i8** %3
br label %40
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
%22 = load i8*, i8** %5
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %21, %23
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
%26 = load i8*, i8** %4
%27 = load i32, i32* %7
%28 = call i8* @realloc(i8* %26, i32 %27)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %19
%32 = call i32 @fanout_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %19
%36 = load i8*, i8** %6
%37 = load i8*, i8** %5
%38 = call i8* @strcat(i8* %36, i8* %37)
store i8* %38, i8** %6
%39 = load i8*, i8** %6
store i8* %39, i8** %3
br label %40
40: ; preds = %35, %15, %10
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [32 x i8] c"ERROR unable to allocate memory\00"
@EXIT_FAILURE = global i32 0
define i8* @str_append(i8* %0, i8* %1) {
%3 = alloca i8*
store i8* %0, i8** %3
%4 = icmp eq i8* %1, null
br i1 %4, label %23, label %5
5: ; preds = %2
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = load i8*, i8** %3
br label %23
10: ; preds = %5
%11 = tail call i32 @strlen(i8* %0)
%12 = tail call i32 @strlen(i8* %1)
%13 = add i32 %11, 1
%14 = add i32 %13, %12
%15 = tail call i8* @realloc(i8* %0, i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = tail call i32 @fanout_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
%22 = tail call i8* @strcat(i8* %15, i8* %1)
br label %23
23: ; preds = %2, %21, %7
%24 = phi i8* [ %9, %7 ], [ %15, %21 ], [ %0, %2 ]
ret i8* %24
}