#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 *realloc(char *, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
scalar_t__ strlen(char *);
void append_string(char *string, char **pointer) {
  if (!*pointer)
    *pointer = strdup(string);
  else {
    *pointer = realloc(*pointer, strlen(*pointer) + strlen(string) + 1);
    strcat(*pointer, string);
  }
}


x86 assembly (clang, -O0):
<append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1176 <append_string+0x36>
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11c8 <append_string+0x88>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%rbx
test   %rbx,%rbx
je     1192 <append_string+0x52>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1040 <realloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <strcat@plt>
mov    %r14,%rdi
call   1060 <strdup@plt>
mov    %rax,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @append_string(i8* %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %0)
store i8* %6, i8** %1
br label %14
7: ; preds = %2
%8 = tail call i64 @strlen(i8* %3)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = add i64 %8, 1
%11 = add i64 %10, %9
%12 = tail call i8* @realloc(i8* %3, i64 %11)
store i8* %12, i8** %1
%13 = tail call i32 @strcat(i8* %12, i8* %0)
br label %14
14: ; preds = %7, %5
ret void
}