#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*/ dynstr_copy(char **, char const *, size_t);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
void dynstr_append_string(char **this_ptr, const char *new_str,
                          size_t max_size) {
  char *temp = NULL;
  size_t new_size;
  if (NULL == (*this_ptr)) {
    dynstr_copy(this_ptr, new_str, max_size);
    return;
  }
  if (!new_str || !(*new_str))
    return;
  new_size = strlen(*this_ptr) + strlen(new_str);
  if (max_size < new_size)
    return;
  temp = calloc(1, new_size + 1);
  strcpy(temp, (*this_ptr));
  strcat(temp, new_str);
  free(*this_ptr);
  (*this_ptr) = temp;
  temp = NULL;
}


x86 assembly (clang, -O0):
<dynstr_append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
jne    11a1 <dynstr_append_string+0x41>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <dynstr_copy@plt>
jmp    124a <dynstr_append_string+0xea>
cmpq   $0x0,-0x10(%rbp)
je     11b9 <dynstr_append_string+0x59>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11be <dynstr_append_string+0x5e>
jmp    124a <dynstr_append_string+0xea>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11f8 <dynstr_append_string+0x98>
jmp    124a <dynstr_append_string+0xea>
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@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_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rdi),%rdi
test   %rdi,%rdi
je     11dc <dynstr_append_string+0x7c>
test   %rbx,%rbx
je     11d0 <dynstr_append_string+0x70>
cmpb   $0x0,(%rbx)
je     11d0 <dynstr_append_string+0x70>
call   1060 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    %r12,%rax
cmp    %r15,%rax
ja     11d0 <dynstr_append_string+0x70>
add    $0x1,%rax
mov    $0x1,%edi
mov    %rax,%rsi
call   1070 <calloc@plt>
mov    %rax,%r15
mov    (%r14),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    %r15,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <dynstr_copy@plt>

LLVM IR (-O0):
define void @dynstr_append_string(i8** %0, i8* %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 %17
12: ; preds = %3
%13 = load i8**, i8*** %4
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = call i32 @dynstr_copy(i8** %13, i8* %14, i64 %15)
br label %52
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %20, %17
br label %52
25: ; preds = %20
%26 = load i8**, i8*** %4
%27 = load i8*, i8** %26
%28 = call i64 @strlen(i8* %27)
%29 = load i8*, i8** %5
%30 = call i64 @strlen(i8* %29)
%31 = add i64 %28, %30
store i64 %31, i64* %8
%32 = load i64, i64* %6
%33 = load i64, i64* %8
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %25
br label %52
36: ; preds = %25
%37 = load i64, i64* %8
%38 = add i64 %37, 1
%39 = call i8* @calloc(i32 1, i64 %38)
store i8* %39, i8** %7
%40 = load i8*, i8** %7
%41 = load i8**, i8*** %4
%42 = load i8*, i8** %41
%43 = call i32 @strcpy(i8* %40, i8* %42)
%44 = load i8*, i8** %7
%45 = load i8*, i8** %5
%46 = call i32 @strcat(i8* %44, i8* %45)
%47 = load i8**, i8*** %4
%48 = load i8*, i8** %47
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %7
%51 = load i8**, i8*** %4
store i8* %50, i8** %51
store i8* null, i8** %7
br label %52
52: ; preds = %36, %35, %24, %12
ret void
}

LLVM IR (-O2):
define void @dynstr_append_string(i8** %0, i8* %1, i64 %2) {
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @dynstr_copy(i8** %0, i8* %1, i64 %2)
br label %26
8: ; preds = %3
%9 = icmp eq i8* %1, null
br i1 %9, label %26, label %10
10: ; preds = %8
%11 = load i8, i8* %1
%12 = icmp eq i8 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %10
%14 = tail call i64 @strlen(i8* %4)
%15 = tail call i64 @strlen(i8* %1)
%16 = add i64 %15, %14
%17 = icmp ugt i64 %16, %2
br i1 %17, label %26, label %18
18: ; preds = %13
%19 = add i64 %16, 1
%20 = tail call i8* @calloc(i32 1, i64 %19)
%21 = load i8*, i8** %0
%22 = tail call i32 @strcpy(i8* %20, i8* %21)
%23 = tail call i32 @strcat(i8* %20, i8* %1)
%24 = load i8*, i8** %0
%25 = tail call i32 @free(i8* %24)
store i8* %20, i8** %0
br label %26
26: ; preds = %13, %8, %10, %18, %6
ret void
}