#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(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
char *append_char(char *text, char c) {
  char buffer[4096];
  sprintf(buffer, "%s", text);
  int len = strlen(buffer) + 1;
  buffer[len] = c;
  buffer[len + 1] = c;
  char *ret = (char *)malloc(strlen(buffer) + 1);
  sprintf(ret, "%s", buffer);
  return ret;
}


x86 assembly (clang, -O0):
<append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xea9(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1014(%rbp)
mov    -0x9(%rbp),%cl
movslq -0x1014(%rbp),%rax
mov    %cl,-0x1010(%rbp,%rax,1)
mov    -0x9(%rbp),%cl
mov    -0x1014(%rbp),%eax
add    $0x1,%eax
cltq
mov    %cl,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rdi
lea    -0x1010(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x1020(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_char>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%ebx
mov    %rdi,%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
mov    %bl,0x1(%rax,%r14,1)
mov    %bl,0x2(%rax,%r14,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i8* @append_char(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca [4096 x i8]
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i8 %1, i8* %4
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %12, 1
store i32 %13, i32* %6
%14 = load i8, i8* %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %16
store i8 %14, i8* %17
%18 = load i8, i8* %4
%19 = load i32, i32* %6
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %21
store i8 %18, i8* %22
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 1
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %7
ret i8* %31
}

LLVM IR (-O2):
define i8* @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 = call i8* @strcpy(i8* %4, i8* dereferenceable(1) %0)
%6 = call i32 @strlen(i8* %4)
%7 = add nsw i32 %6, 1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %8
store i8 %1, i8* %9
%10 = add nsw i32 %6, 2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %11
store i8 %1, i8* %12
%13 = call i32 @strlen(i8* %4)
%14 = add nsw i32 %13, 1
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = call i8* @strcpy(i8* dereferenceable(1) %16, i8* %4)
ret i8* %16
}