#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 *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned long);
int /*<<< orphan*/ memcpy(char *, char *, unsigned long);
unsigned long strlen(char *);
int /*<<< orphan*/ xt_core_trace(char *);
char *xt_core_string_append_n(char *original, char *addition,
                              unsigned long addition_size) {
  assert(addition);
  char *new_string;
  unsigned long original_size;
  unsigned long new_string_size;
  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_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
cmpq   $0x0,-0x8(%rbp)
je     120e <xt_core_string_append_n+0xae>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f4 <xt_core_string_append_n+0x94>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1200 <xt_core_string_append_n+0xa0>
lea    0xe05(%rip),%rdi
call   1080 <xt_core_trace@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
jmp    125d <xt_core_string_append_n+0xfd>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     124c <xt_core_string_append_n+0xec>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1258 <xt_core_string_append_n+0xf8>
lea    0xdad(%rip),%rdi
call   1080 <xt_core_trace@plt>
jmp    125d <xt_core_string_append_n+0xfd>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xt_core_string_append_n>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsi,%rdi
call   1050 <assert@plt>
test   %r12,%r12
je     11ca <xt_core_string_append_n+0x6a>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r14,1),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f0 <xt_core_string_append_n+0x90>
lea    (%r14,%r13,1),%rbp
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
add    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%rbp,1)
jmp    11fc <xt_core_string_append_n+0x9c>
lea    0x1(%r14),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1216 <xt_core_string_append_n+0xb6>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%rbx,%r14,1)
jmp    1204 <xt_core_string_append_n+0xa4>
lea    0xe09(%rip),%rdi
call   1080 <xt_core_trace@plt>
mov    %r12,%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    0xde3(%rip),%rdi
call   1080 <xt_core_trace@plt>
xor    %ebx,%ebx
jmp    1204 <xt_core_string_append_n+0xa4>

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

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
define i8* @xt_core_string_append_n(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 @assert(i8* %1)
%5 = icmp eq i8* %0, null
br i1 %5, label %21, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %0)
%8 = add i64 %7, %2
%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 %2)
%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 = %3
%22 = add i64 %2, 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 %2)
%27 = getelementptr inbounds i8, i8* %23, i64 %2
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
}