#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 */
char *calloc(int, size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int strlen(char *);
void dynstr_append_char(char **this_ptr, char new_char, size_t max_size) {
  char *temp = NULL;
  size_t new_size;
  if (NULL == (*this_ptr)) {
    (*this_ptr) = calloc(1, 2);
    sprintf((*this_ptr), "%c", new_char);
    return;
  }
  new_size = strlen(*this_ptr) + 1;
  if (max_size < new_size)
    return;
  temp = calloc(1, new_size + 1);
  sprintf(temp, "%s%c", (*this_ptr), new_char);
  free(*this_ptr);
  (*this_ptr) = temp;
  temp = NULL;
}


x86 assembly (clang, -O0):
<dynstr_append_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
jne    11a4 <dynstr_append_char+0x64>
mov    $0x1,%edi
mov    $0x2,%esi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movsbl -0x9(%rbp),%edx
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    121e <dynstr_append_char+0xde>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11cc <dynstr_append_char+0x8c>
jmp    121e <dynstr_append_char+0xde>
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
movsbl -0x9(%rbp),%ecx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x20(%rbp)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dynstr_append_char>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     119b <dynstr_append_char+0x5b>
mov    %rdx,%rbp
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rsi
cmp    %rbp,%rsi
ja     11b4 <dynstr_append_char+0x74>
add    $0x1,%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rbp
mov    (%rbx),%rdx
movsbl %r14b,%ecx
lea    0xe7c(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    %rbp,(%rbx)
jmp    11b4 <dynstr_append_char+0x74>
mov    $0x2,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,(%rbx)
mov    %r14b,(%rax)
movb   $0x0,0x1(%rax)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
@.str.1 = [5 x i8] c"%s%c\00"
define void @dynstr_append_char(i8** %0, i8 signext %1, i64 %2) {
%4 = alloca i8**
%5 = alloca i8
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8** %0, i8*** %4
store i8 %1, i8* %5
store i64 %2, i64* %6
store i8* null, i8** %7
%9 = load i8**, i8*** %4
%10 = load i8*, i8** %9
%11 = icmp eq i8* null, %10
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = call i8* @calloc(i32 1, i64 2)
%14 = load i8**, i8*** %4
store i8* %13, i8** %14
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = load i8, i8* %5
%18 = sext i8 %17 to i32
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %45
20: ; preds = %3
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, 1
%25 = sext i32 %24 to i64
store i64 %25, i64* %8
%26 = load i64, i64* %6
%27 = load i64, i64* %8
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %20
br label %45
30: ; preds = %20
%31 = load i64, i64* %8
%32 = add i64 %31, 1
%33 = call i8* @calloc(i32 1, i64 %32)
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = load i8**, i8*** %4
%36 = load i8*, i8** %35
%37 = load i8, i8* %5
%38 = sext i8 %37 to i32
%39 = call i32 (i8*, i8*, ...) @sprintf(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %36, i32 %38)
%40 = load i8**, i8*** %4
%41 = load i8*, i8** %40
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %7
%44 = load i8**, i8*** %4
store i8* %43, i8** %44
store i8* null, i8** %7
br label %45
45: ; preds = %30, %29, %12
ret void
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%s%c\00"
define void @dynstr_append_char(i8** nocapture %0, i8 signext %1, i64 %2) {
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i8* @calloc(i32 1, i64 2)
store i8* %7, i8** %0
store i8 %1, i8* %7
%8 = getelementptr i8, i8* %7, i64 1
store i8 0, i8* %8
br label %22
9: ; preds = %3
%10 = tail call i32 @strlen(i8* %4)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = icmp ugt i64 %12, %2
br i1 %13, label %22, label %14
14: ; preds = %9
%15 = add nsw i64 %12, 1
%16 = tail call i8* @calloc(i32 1, i64 %15)
%17 = load i8*, i8** %0
%18 = sext i8 %1 to i32
%19 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %16, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %17, i32 %18)
%20 = load i8*, i8** %0
%21 = tail call i32 @free(i8* %20)
store i8* %16, i8** %0
br label %22
22: ; preds = %9, %14, %6
ret void
}