#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 */
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *realloc(char *, int);
int strlen(char const *);
char *appendToString(char *dest, const char *src) {
  int destlen;
  int srclen = strlen(src);
  if (dest == NULL) {
    dest = malloc(srclen + 1);
    memset(dest, 0, srclen + 1);
    destlen = 0;
  } else {
    destlen = strlen(dest);
    dest = realloc(dest, destlen + srclen + 1);
  }
  memcpy(dest + destlen, src, srclen);
  dest[destlen + srclen] = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<appendToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a3 <appendToString+0x53>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x14(%rbp)
jmp    11c5 <appendToString+0x75>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    -0x18(%rbp),%esi
add    $0x1,%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    -0x18(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendToString>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
test   %rbx,%rbx
je     118d <appendToString+0x3d>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
lea    (%r15,%r12,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbx
jmp    11aa <appendToString+0x5a>
lea    0x1(%r15),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
xor    %r12d,%r12d
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
movslq %r12d,%rbp
lea    (%rbx,%rbp,1),%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
add    %r15d,%ebp
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @appendToString(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %6
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* %6
%13 = add nsw i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
%18 = call i32 @memset(i8* %15, i32 0, i32 %17)
store i32 0, i32* %5
br label %28
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %5
%22 = load i8*, i8** %3
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = add nsw i32 %23, %24
%26 = add nsw i32 %25, 1
%27 = call i8* @realloc(i8* %22, i32 %26)
store i8* %27, i8** %3
br label %28
28: ; preds = %19, %11
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8*, i8** %4
%34 = load i32, i32* %6
%35 = call i32 @memcpy(i8* %32, i8* %33, i32 %34)
%36 = load i8*, i8** %3
%37 = load i32, i32* %5
%38 = load i32, i32* %6
%39 = add nsw i32 %37, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %36, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
define i8* @appendToString(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = add nsw i32 %3, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i32 @memset(i8* %7, i32 0, i32 %6)
br label %14
9: ; preds = %2
%10 = tail call i32 @strlen(i8* %0)
%11 = add i32 %3, 1
%12 = add i32 %11, %10
%13 = tail call i8* @realloc(i8* %0, i32 %12)
br label %14
14: ; preds = %9, %5
%15 = phi i32 [ 0, %5 ], [ %10, %9 ]
%16 = phi i8* [ %7, %5 ], [ %13, %9 ]
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = tail call i32 @memcpy(i8* %18, i8* %1, i32 %3)
%20 = add nsw i32 %15, %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %16, i64 %21
store i8 0, i8* %22
ret i8* %16
}