#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 */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
char *strdup(char *);
int strlen(char *);
void append_string(char **s, char *val) {
  int len;
  char *ss;
  if ((*s) == NULL) {
    (*s) = strdup(val);
    return;
  }
  len = strlen((*s)) + strlen(val) + 2;
  ss = (char *)malloc(len);
  if (ss == NULL)
    return;
  ss[0] = '\0';
  strcat(ss, (*s));
  strcat(ss, " ");
  strcat(ss, val);
  free((*s));
  (*s) = ss;
}


x86 assembly (clang, -O0):
<append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1186 <append_string+0x36>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1212 <append_string+0xc2>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <append_string+0x77>
jmp    1212 <append_string+0xc2>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_string>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdi),%rdi
test   %rdi,%rdi
je     11b7 <append_string+0x67>
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c5 <append_string+0x75>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    (%r15),%rsi
mov    %rax,%rdi
call   1060 <strcat@plt>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
jmp    11c2 <append_string+0x72>
mov    %r14,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %rbx,(%r15)
pop    %rbx
pop    %r14
pop    %r15
ret

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

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