#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 *malloc(size_t);
char *realloc(char *, size_t);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, size_t);
void buildString(char **current_text, const char *new_text) {
  size_t new_len = strlen(new_text) + 1;
  if (*current_text == NULL) {
    *current_text = malloc(new_len);
  }
  size_t current_len = strlen(*current_text);
  *current_text = realloc(*current_text, (new_len + current_len));
  strncat(*current_text, new_text, new_len);
}


x86 assembly (clang, -O0):
<buildString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1183 <buildString+0x43>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buildString>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%r15
mov    (%rbx),%rdi
test   %rdi,%rdi
jne    116f <buildString+0x2f>
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,%rdi
mov    %rax,(%rbx)
call   1030 <strlen@plt>
movslq %eax,%rsi
mov    (%rbx),%rdi
add    %r15,%rsi
call   1060 <realloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <strncat@plt>

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

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