#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*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
void appendstring(char **destp, char *src) {
  char *newstr;
  newstr = malloc(strlen(*destp) + strlen(src) + 1);
  strcpy(newstr, *destp);
  strcat(newstr, src);
  if (strlen(*destp) > 0)
    free(*destp);
  *destp = newstr;
}


x86 assembly (clang, -O0):
<appendstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x20(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jle    11cc <appendstring+0x7c>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendstring>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rdi),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    (%r14),%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
jle    11b2 <appendstring+0x62>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @appendstring(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%6 = load i8**, i8*** %3
%7 = load i8*, i8** %6
%8 = call i64 @strlen(i8* %7)
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
%11 = add nsw i64 %8, %10
%12 = add nsw i64 %11, 1
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = load i8**, i8*** %3
%16 = load i8*, i8** %15
%17 = call i32 @strcpy(i8* %14, i8* %16)
%18 = load i8*, i8** %5
%19 = load i8*, i8** %4
%20 = call i32 @strcat(i8* %18, i8* %19)
%21 = load i8**, i8*** %3
%22 = load i8*, i8** %21
%23 = call i64 @strlen(i8* %22)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %2
%26 = load i8**, i8*** %3
%27 = load i8*, i8** %26
%28 = call i32 @free(i8* %27)
br label %29
29: ; preds = %25, %2
%30 = load i8*, i8** %5
%31 = load i8**, i8*** %3
store i8* %30, i8** %31
ret void
}

LLVM IR (-O2):
define void @appendstring(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = add i64 %4, 1
%7 = add i64 %6, %5
%8 = tail call i8* @malloc(i64 %7)
%9 = tail call i32 @strcpy(i8* %8, i8* %3)
%10 = tail call i32 @strcat(i8* %8, i8* %1)
%11 = load i8*, i8** %0
%12 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @free(i8* %11)
br label %16
16: ; preds = %14, %2
store i8* %8, i8** %0
ret void
}