#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);
scalar_t__ realloc(char *, size_t);
size_t strlen(char *);
extern void append_char(char **str, char c) {
  char *b = NULL;
  if (*str == NULL) {
    b = (char *)malloc(2 * sizeof(char));
    b[0] = c;
    b[1] = '\0';
  } else {
    char *d = *str;
    size_t len = strlen(d);
    b = (char *)realloc(*str, len + sizeof(char) + 1);
    b[len] = c;
    b[len + 1] = '\0';
  }
  *str = b;
}


x86 assembly (clang, -O0):
<append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    117c <append_char+0x4c>
mov    $0x2,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x9(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11cb <append_char+0x9b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x9(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    (%rdi),%rbp
test   %rbp,%rbp
je     116a <append_char+0x3a>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
lea    0x2(%rax),%rsi
mov    %rbp,%rdi
call   1050 <realloc@plt>
mov    %r14b,(%rax,%rbx,1)
lea    (%rax,%rbx,1),%rcx
add    $0x1,%rcx
jmp    117e <append_char+0x4e>
mov    $0x2,%edi
call   1040 <malloc@plt>
mov    %r14b,(%rax)
mov    %rax,%rcx
add    $0x1,%rcx
movb   $0x0,(%rcx)
mov    %rax,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @append_char(i8** %0, i8 signext %1) {
%3 = alloca i8**
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8** %0, i8*** %3
store i8 %1, i8* %4
store i8* null, i8** %5
%8 = load i8**, i8*** %3
%9 = load i8*, i8** %8
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = call i64 @malloc(i32 2)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %5
%14 = load i8, i8* %4
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 %14, i8* %16
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8 0, i8* %18
br label %39
19: ; preds = %2
%20 = load i8**, i8*** %3
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %7
%24 = load i8**, i8*** %3
%25 = load i8*, i8** %24
%26 = load i64, i64* %7
%27 = add i64 %26, 1
%28 = add i64 %27, 1
%29 = call i64 @realloc(i8* %25, i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %5
%31 = load i8, i8* %4
%32 = load i8*, i8** %5
%33 = load i64, i64* %7
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 %31, i8* %34
%35 = load i8*, i8** %5
%36 = load i64, i64* %7
%37 = add i64 %36, 1
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %19, %11
%40 = load i8*, i8** %5
%41 = load i8**, i8*** %3
store i8* %40, i8** %41
ret void
}

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