#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*/ assert(char const *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned long);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned long);
unsigned long strlen(char const *);
int /*<<< orphan*/ xt_core_trace(char *);
char *xt_core_string_append(char *original, const char *addition) {
  assert(addition);
  char *new_string;
  unsigned long addition_size;
  unsigned long original_size;
  unsigned long new_string_size;
  addition_size = strlen(addition);
  if (original) {
    original_size = strlen(original);
    new_string_size = original_size + addition_size;
    new_string = malloc(new_string_size + 1);
    if (new_string) {
      memcpy(new_string, original, original_size);
      memcpy(new_string + original_size, addition, addition_size);
      *(new_string + new_string_size) = '\0';
    } else {
      xt_core_trace("malloc");
    }
    free(original);
  } else {
    new_string = malloc(addition_size + 1);
    if (new_string) {
      memcpy(new_string, addition, addition_size);
      *(new_string + addition_size) = '\0';
    } else {
      xt_core_trace("malloc");
    }
  }
  return new_string;
}


x86 assembly (clang, -O0):
<xt_core_string_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1217 <xt_core_string_append+0xb7>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11fd <xt_core_string_append+0x9d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1209 <xt_core_string_append+0xa9>
lea    0xdfc(%rip),%rdi
call   1080 <xt_core_trace@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
jmp    1266 <xt_core_string_append+0x106>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1255 <xt_core_string_append+0xf5>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1261 <xt_core_string_append+0x101>
lea    0xda4(%rip),%rdi
call   1080 <xt_core_trace@plt>
jmp    1266 <xt_core_string_append+0x106>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xt_core_string_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <assert@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
test   %r15,%r15
je     11d4 <xt_core_string_append+0x74>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r12,1),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11fd <xt_core_string_append+0x9d>
mov    %r12,%rbp
add    %r13,%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
add    %r13,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%rbp,1)
jmp    1209 <xt_core_string_append+0xa9>
mov    %r12,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1223 <xt_core_string_append+0xc3>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%r12,1)
jmp    1211 <xt_core_string_append+0xb1>
lea    0xdfc(%rip),%rdi
call   1080 <xt_core_trace@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd6(%rip),%rdi
call   1080 <xt_core_trace@plt>
xor    %ebx,%ebx
jmp    1211 <xt_core_string_append+0xb1>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
define i8* @xt_core_string_append(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = call i32 @assert(i8* %9)
%11 = load i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %6
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %45
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = load i64, i64* %6
%20 = add i64 %18, %19
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = add i64 %21, 1
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %40
26: ; preds = %15
%27 = load i8*, i8** %5
%28 = load i8*, i8** %3
%29 = load i64, i64* %7
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %5
%32 = load i64, i64* %7
%33 = getelementptr inbounds i8, i8* %31, i64 %32
%34 = load i8*, i8** %4
%35 = load i64, i64* %6
%36 = call i32 @memcpy(i8* %33, i8* %34, i64 %35)
%37 = load i8*, i8** %5
%38 = load i64, i64* %8
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
br label %42
40: ; preds = %15
%41 = call i32 @xt_core_trace(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %26
%43 = load i8*, i8** %3
%44 = call i32 @free(i8* %43)
br label %62
45: ; preds = %2
%46 = load i64, i64* %6
%47 = add i64 %46, 1
%48 = call i8* @malloc(i64 %47)
store i8* %48, i8** %5
%49 = load i8*, i8** %5
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %59
51: ; preds = %45
%52 = load i8*, i8** %5
%53 = load i8*, i8** %4
%54 = load i64, i64* %6
%55 = call i32 @memcpy(i8* %52, i8* %53, i64 %54)
%56 = load i8*, i8** %5
%57 = load i64, i64* %6
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 0, i8* %58
br label %61
59: ; preds = %45
%60 = call i32 @xt_core_trace(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %61
61: ; preds = %59, %51
br label %62
62: ; preds = %61, %42
%63 = load i8*, i8** %5
ret i8* %63
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
define i8* @xt_core_string_append(i8* %0, i8* %1) {
%3 = tail call i32 @assert(i8* %1)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp eq i8* %0, null
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = tail call i64 @strlen(i8* %0)
%8 = add i64 %7, %4
%9 = add i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = tail call i32 @memcpy(i8* %10, i8* %0, i64 %7)
%14 = getelementptr inbounds i8, i8* %10, i64 %7
%15 = tail call i32 @memcpy(i8* %14, i8* %1, i64 %4)
%16 = getelementptr inbounds i8, i8* %10, i64 %8
store i8 0, i8* %16
br label %19
17: ; preds = %6
%18 = tail call i32 @xt_core_trace(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = tail call i32 @free(i8* %0)
br label %30
21: ; preds = %2
%22 = add i64 %4, 1
%23 = tail call i8* @malloc(i64 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = tail call i32 @memcpy(i8* %23, i8* %1, i64 %4)
%27 = getelementptr inbounds i8, i8* %23, i64 %4
store i8 0, i8* %27
br label %30
28: ; preds = %21
%29 = tail call i32 @xt_core_trace(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %25, %28, %19
%31 = phi i8* [ %10, %19 ], [ %23, %25 ], [ null, %28 ]
ret i8* %31
}