#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__ realloc(char *, unsigned long);
unsigned long strlen(char *);
char *tl_str_append_char(char **str, const char c) {
  unsigned long len = strlen(*str);
  ++len;
  char *result = NULL;
  result = (char *)realloc(*str, (len + 1) * sizeof(char));
  if (!result) {
    return NULL;
  }
  result[len - 1] = c;
  result[len] = '\0';
  *str = result;
  return *str;
}


x86 assembly (clang, -O0):
<tl_str_append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
shl    $0x0,%rsi
call   1040 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118a <tl_str_append_char+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    11be <tl_str_append_char+0x9e>
mov    -0x11(%rbp),%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tl_str_append_char>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
mov    (%rdi),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
lea    0x2(%rax),%rsi
mov    %rbx,%rdi
call   1040 <realloc@plt>
test   %rax,%rax
je     115a <tl_str_append_char+0x3a>
mov    %r14b,(%rax,%rbp,1)
movb   $0x0,0x1(%rax,%rbp,1)
mov    %rax,(%r15)
jmp    115c <tl_str_append_char+0x3c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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