#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_char(char **dest, char c) {
  char buffer[4096];
  sprintf(buffer, "%s%c", *dest, c);
  *dest = (char *)malloc(strlen(buffer) + 1);
  sprintf(*dest, "%s", buffer);
}


x86 assembly (clang, -O0):
<append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
movsbl -0x9(%rbp),%ecx
lea    0xea2(%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    0xe69(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_char>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%ecx
mov    %rdi,%rbx
mov    (%rdi),%rdx
lea    0xea7(%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%c\00"
@.str.1 = [3 x i8] c"%s\00"
define void @append_char(i8** %0, i8 signext %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 = sext i8 %9 to i32
%11 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %10)
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %13, 1
%15 = call i64 @malloc(i64 %14)
%16 = inttoptr i64 %15 to i8*
%17 = load i8**, i8*** %3
store i8* %16, i8** %17
%18 = load i8**, i8*** %3
%19 = load i8*, i8** %18
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%21 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %20)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%c\00"
define void @append_char(i8** nocapture %0, i8 signext %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 = sext i8 %1 to i32
%7 = 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, i32 %6)
%8 = call i64 @strlen(i8* %4)
%9 = add nsw i64 %8, 1
%10 = tail call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %0
%12 = call i8* @strcpy(i8* dereferenceable(1) %11, i8* %4)
ret void
}