#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 */
scalar_t__ malloc(size_t);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
void list_add_str(char **pstr1, char *str2) {
  if (!str2)
    return;
  size_t len = strlen(str2);
  if (!(*pstr1)) {
    *pstr1 = (char *)malloc(len + 1);
    strcpy(*pstr1, str2);
  } else {
    *pstr1 = (char *)realloc(*pstr1, strlen(*pstr1) + len + 2);
    strcat(*pstr1, ",");
    strcat(*pstr1, str2);
  }
}


x86 assembly (clang, -O0):
<list_add_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1170 <list_add_str+0x20>
jmp    120f <list_add_str+0xbf>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11b7 <list_add_str+0x67>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    120f <list_add_str+0xbf>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rsi
add    -0x18(%rbp),%rsi
add    $0x2,%rsi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    0xe06(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_add_str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11b6 <list_add_str+0x66>
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    (%r14),%rbx
test   %rbx,%rbx
je     11c2 <list_add_str+0x72>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x2,%rsi
mov    %rbx,%rdi
call   1060 <realloc@plt>
mov    %rax,(%r14)
lea    0xe68(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcat@plt>
mov    (%r14),%rdi
mov    %r12,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1070 <strcat@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
add    $0x1,%r15
mov    %r15,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %r12,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <strcpy@plt>

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

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