#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 *);
char *appendToString(char *dest, const char *src, int len) {
  int destlen;
  if (dest == NULL) {
    dest = malloc(len + 1);
    memset(dest, 0, len + 1);
    destlen = 0;
  } else {
    destlen = strlen(dest);
    dest = realloc(dest, destlen + len + 1);
  }
  memcpy(dest + destlen, src, len);
  dest[destlen + len] = '\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    %edx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119a <appendToString+0x4a>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x18(%rbp)
jmp    11bc <appendToString+0x6c>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
add    -0x14(%rbp),%esi
add    $0x1,%esi
call   1070 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    -0x14(%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    %edx,%r15d
mov    %rsi,%r14
test   %rdi,%rdi
je     1182 <appendToString+0x32>
mov    %rdi,%rbx
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    119f <appendToString+0x4f>
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, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %18
10: ; preds = %3
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
%17 = call i32 @memset(i8* %14, i32 0, i32 %16)
store i32 0, i32* %7
br label %27
18: ; preds = %3
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %7
%21 = load i8*, i8** %4
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = add nsw i32 %22, %23
%25 = add nsw i32 %24, 1
%26 = call i8* @realloc(i8* %21, i32 %25)
store i8* %26, i8** %4
br label %27
27: ; preds = %18, %10
%28 = load i8*, i8** %4
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = call i32 @memcpy(i8* %31, i8* %32, i32 %33)
%35 = load i8*, i8** %4
%36 = load i32, i32* %7
%37 = load i32, i32* %6
%38 = add nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %35, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %4
ret i8* %41
}

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