#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__ realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
void __append_str(char **pstr1, char *str2, char *sep, size_t n) {
  if (!*pstr1 || !str2)
    return;
  size_t len = strlen(str2);
  if (sep) {
    *pstr1 = (char *)realloc(*pstr1, strlen(*pstr1) + len + 2);
    strcat(*pstr1, sep);
  } else {
    *pstr1 = (char *)realloc(*pstr1, strlen(*pstr1) + len + 1);
  }
  strncat(*pstr1, str2, ((n == 0) ? len : n));
}


x86 assembly (clang, -O0):
<__append_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     1171 <__append_str+0x31>
cmpq   $0x0,-0x10(%rbp)
jne    1176 <__append_str+0x36>
jmp    1251 <__append_str+0x111>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d8 <__append_str+0x98>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
add    -0x28(%rbp),%rsi
add    $0x2,%rsi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
jmp    120d <__append_str+0xcd>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1238 <__append_str+0xf8>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1240 <__append_str+0x100>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rdx
call   1040 <strncat@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__append_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     119f <__append_str+0x5f>
mov    %rdi,%r15
mov    (%rdi),%rbp
test   %rbp,%rbp
je     119f <__append_str+0x5f>
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1030 <strlen@plt>
test   %r13,%r13
je     11ae <__append_str+0x6e>
lea    (%r12,%rax,1),%rsi
add    $0x2,%rsi
mov    %rbp,%rdi
call   1050 <realloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    %r13,%rsi
call   1060 <strcat@plt>
mov    (%r15),%rdi
jmp    11c4 <__append_str+0x84>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbp,%rdi
call   1050 <realloc@plt>
mov    %rax,%rdi
mov    %rax,(%r15)
test   %r14,%r14
cmovne %r14,%r12
mov    %rbx,%rsi
mov    %r12,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strncat@plt>

LLVM IR (-O0):
define void @__append_str(i8** %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load i8**, i8*** %5
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %13, %4
br label %63
17: ; preds = %13
%18 = load i8*, i8** %6
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %9
%20 = load i8*, i8** %7
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %38
22: ; preds = %17
%23 = load i8**, i8*** %5
%24 = load i8*, i8** %23
%25 = load i8**, i8*** %5
%26 = load i8*, i8** %25
%27 = call i64 @strlen(i8* %26)
%28 = load i64, i64* %9
%29 = add i64 %27, %28
%30 = add i64 %29, 2
%31 = call i64 @realloc(i8* %24, i64 %30)
%32 = inttoptr i64 %31 to i8*
%33 = load i8**, i8*** %5
store i8* %32, i8** %33
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = load i8*, i8** %7
%37 = call i32 @strcat(i8* %35, i8* %36)
br label %50
38: ; preds = %17
%39 = load i8**, i8*** %5
%40 = load i8*, i8** %39
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = call i64 @strlen(i8* %42)
%44 = load i64, i64* %9
%45 = add i64 %43, %44
%46 = add i64 %45, 1
%47 = call i64 @realloc(i8* %40, i64 %46)
%48 = inttoptr i64 %47 to i8*
%49 = load i8**, i8*** %5
store i8* %48, i8** %49
br label %50
50: ; preds = %38, %22
%51 = load i8**, i8*** %5
%52 = load i8*, i8** %51
%53 = load i8*, i8** %6
%54 = load i64, i64* %8
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = load i64, i64* %9
br label %60
58: ; preds = %50
%59 = load i64, i64* %8
br label %60
60: ; preds = %58, %56
%61 = phi i64 [ %57, %56 ], [ %59, %58 ]
%62 = call i32 @strncat(i8* %52, i8* %53, i64 %61)
br label %63
63: ; preds = %60, %16
ret void
}

LLVM IR (-O2):
define void @__append_str(i8** nocapture %0, i8* %1, i8* %2, i64 %3) {
%5 = load i8*, i8** %0
%6 = icmp ne i8* %5, null
%7 = icmp ne i8* %1, null
%8 = and i1 %7, %6
br i1 %8, label %9, label %30
9: ; preds = %4
%10 = tail call i64 @strlen(i8* %1)
%11 = icmp eq i8* %2, null
%12 = tail call i64 @strlen(i8* %5)
br i1 %11, label %20, label %13
13: ; preds = %9
%14 = add i64 %10, 2
%15 = add i64 %14, %12
%16 = tail call i64 @realloc(i8* %5, i64 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %0
%18 = tail call i32 @strcat(i8* %17, i8* %2)
%19 = load i8*, i8** %0
br label %25
20: ; preds = %9
%21 = add i64 %10, 1
%22 = add i64 %21, %12
%23 = tail call i64 @realloc(i8* %5, i64 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %0
br label %25
25: ; preds = %20, %13
%26 = phi i8* [ %24, %20 ], [ %19, %13 ]
%27 = icmp eq i64 %3, 0
%28 = select i1 %27, i64 %10, i64 %3
%29 = tail call i32 @strncat(i8* %26, i8* %1, i64 %28)
br label %30
30: ; preds = %4, %25
ret void
}