#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 */
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
scalar_t__ strlen(char *);
void append_string(char **dest, char *source) {
  char buffer[4096];
  sprintf(buffer, "%s%s", *dest, source);
  *dest = (char *)malloc(strlen(buffer) + 1);
  sprintf(*dest, "%s", buffer);
}


x86 assembly (clang, -O0):
<append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rcx
lea    0xea4(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    -0x1010(%rbp),%rdx
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_string>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rcx
mov    %rdi,%rbx
mov    (%rdi),%rdx
lea    0xea6(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [3 x i8] c"%s\00"
define void @append_string(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca [4096 x i8]
store i8** %0, i8*** %3
store i8* %1, i8** %4
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = load i8*, i8** %4
%10 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9)
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%12 = call i64 @strlen(i8* %11)
%13 = add nsw i64 %12, 1
%14 = call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = load i8**, i8*** %3
store i8* %15, i8** %16
%17 = load i8**, i8*** %3
%18 = load i8*, i8** %17
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%20 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %19)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define void @append_string(i8** nocapture %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i8*, i8** %0
%6 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %1)
%7 = call i64 @strlen(i8* %4)
%8 = add nsw i64 %7, 1
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %0
%11 = call i8* @strcpy(i8* dereferenceable(1) %10, i8* %4)
ret void
}