#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 *);
char *malloc(size_t);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char *str_append(char **pp, const char *sep, const char *str) {
  char *res, *p;
  size_t len = 0;
  p = *pp;
  if (p) {
    len = strlen(p) + strlen(sep);
  }
  res = malloc(len + strlen(str) + 1);
  if (p) {
    strcpy(res, p);
    strcat(res, sep);
  }
  strcpy(res + len, str);
  free(p);
  return *pp = res;
}


x86 assembly (clang, -O0):
<str_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a6 <str_append+0x56>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f0 <str_append+0xa0>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    %rax,(%rcx)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    (%rdi),%r13
test   %r13,%r13
je     1187 <str_append+0x37>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
add    %rbp,%rbx
jmp    1189 <str_append+0x39>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %r13,%r13
je     11bc <str_append+0x6c>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <strcat@plt>
mov    %rbp,%rdi
add    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,(%r14)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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