#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 *, size_t);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
char *str_add(char *old, const char *value) {
  size_t len;
  char *new;
  len = 0;
  if (old != NULL)
    len = strlen(old);
  new = realloc(old, len + 1 + strlen(value) + 1);
  if (new == NULL)
    return old;
  if (old == NULL)
    strcpy(new, "");
  else
    strcat(new, ",");
  strcat(new, value);
  return new;
}


x86 assembly (clang, -O0):
<str_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1170 <str_add+0x30>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bd <str_add+0x7d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1202 <str_add+0xc2>
cmpq   $0x0,-0x10(%rbp)
jne    11dd <str_add+0x9d>
mov    -0x28(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1030 <strcpy@plt>
jmp    11ed <str_add+0xad>
mov    -0x28(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_add>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
je     1161 <str_add+0x21>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    $0x2,%rbx
jmp    1166 <str_add+0x26>
mov    $0x2,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %rax,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <realloc@plt>
test   %rax,%rax
je     119a <str_add+0x5a>
mov    %rax,%rbx
test   %r15,%r15
je     119f <str_add+0x5f>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
jmp    11ae <str_add+0x6e>
mov    %r15,%rbx
jmp    11b9 <str_add+0x79>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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