#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 */
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
scalar_t__ vktrace_malloc(int);
char *vktrace_copy_and_append(const char *pBaseString, const char *pSeparator,
                              const char *pAppendString) {
  size_t baseSize = (pBaseString != NULL) ? strlen(pBaseString) : 0;
  size_t separatorSize =
      ((pAppendString != NULL) && strlen(pAppendString) && (pSeparator != NULL))
          ? strlen(pSeparator)
          : 0;
  size_t appendSize = (pAppendString != NULL) ? strlen(pAppendString) : 0;
  size_t bufferSize = baseSize + separatorSize + appendSize + 1;
  char *retVal = (char *)vktrace_malloc(sizeof(char) * bufferSize);
  if (retVal != NULL) {
    strncpy(retVal, pBaseString, baseSize);
    strncpy(&retVal[baseSize], pSeparator, separatorSize);
    strncpy(&retVal[baseSize + separatorSize], pAppendString, appendSize);
  }
  retVal[bufferSize - 1] = '\0';
  return retVal;
}


x86 assembly (clang, -O0):
<vktrace_copy_and_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1161 <vktrace_copy_and_append+0x31>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x48(%rbp)
jmp    116c <vktrace_copy_and_append+0x3c>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    116c <vktrace_copy_and_append+0x3c>
mov    -0x48(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11af <vktrace_copy_and_append+0x7f>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
je     11af <vktrace_copy_and_append+0x7f>
cmpq   $0x0,-0x10(%rbp)
je     11af <vktrace_copy_and_append+0x7f>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x50(%rbp)
jmp    11ba <vktrace_copy_and_append+0x8a>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    11ba <vktrace_copy_and_append+0x8a>
mov    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11df <vktrace_copy_and_append+0xaf>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x58(%rbp)
jmp    11ea <vktrace_copy_and_append+0xba>
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
jmp    11ea <vktrace_copy_and_append+0xba>
mov    -0x58(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <vktrace_malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1266 <vktrace_copy_and_append+0x136>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vktrace_copy_and_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r14
test   %rdi,%rdi
mov    %rdi,(%rsp)
je     1157 <vktrace_copy_and_append+0x27>
call   1050 <strlen@plt>
mov    %rax,%rbx
jmp    1159 <vktrace_copy_and_append+0x29>
xor    %ebx,%ebx
test   %rbp,%rbp
mov    %rbp,0x10(%rsp)
mov    %r14,0x8(%rsp)
je     118e <vktrace_copy_and_append+0x5e>
xor    %r13d,%r13d
test   %r14,%r14
je     1181 <vktrace_copy_and_append+0x51>
cmpb   $0x0,0x0(%rbp)
je     1181 <vktrace_copy_and_append+0x51>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
jmp    1193 <vktrace_copy_and_append+0x63>
xor    %r13d,%r13d
xor    %ebp,%ebp
lea    (%rbx,%r13,1),%r14
lea    (%r14,%rbp,1),%r12
lea    0x1(%r12),%edi
call   1030 <vktrace_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11e2 <vktrace_copy_and_append+0xb2>
mov    %r15,%rdi
mov    (%rsp),%rsi
mov    %rbx,%rdx
call   1040 <strncpy@plt>
add    %r15,%rbx
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
mov    %r13,%rdx
call   1040 <strncpy@plt>
add    %r15,%r14
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
movb   $0x0,(%r15,%r12,1)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @vktrace_copy_and_append(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i64 @strlen(i8* %15)
br label %18
17: ; preds = %3
br label %18
18: ; preds = %17, %14
%19 = phi i64 [ %16, %14 ], [ 0, %17 ]
store i64 %19, i64* %7
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %32
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = call i64 @strlen(i8* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = call i64 @strlen(i8* %30)
br label %33
32: ; preds = %26, %22, %18
br label %33
33: ; preds = %32, %29
%34 = phi i64 [ %31, %29 ], [ 0, %32 ]
store i64 %34, i64* %8
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i8*, i8** %6
%39 = call i64 @strlen(i8* %38)
br label %41
40: ; preds = %33
br label %41
41: ; preds = %40, %37
%42 = phi i64 [ %39, %37 ], [ 0, %40 ]
store i64 %42, i64* %9
%43 = load i64, i64* %7
%44 = load i64, i64* %8
%45 = add i64 %43, %44
%46 = load i64, i64* %9
%47 = add i64 %45, %46
%48 = add i64 %47, 1
store i64 %48, i64* %10
%49 = load i64, i64* %10
%50 = mul i64 1, %49
%51 = trunc i64 %50 to i32
%52 = call i64 @vktrace_malloc(i32 %51)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %11
%54 = load i8*, i8** %11
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %75
56: ; preds = %41
%57 = load i8*, i8** %11
%58 = load i8*, i8** %4
%59 = load i64, i64* %7
%60 = call i32 @strncpy(i8* %57, i8* %58, i64 %59)
%61 = load i8*, i8** %11
%62 = load i64, i64* %7
%63 = getelementptr inbounds i8, i8* %61, i64 %62
%64 = load i8*, i8** %5
%65 = load i64, i64* %8
%66 = call i32 @strncpy(i8* %63, i8* %64, i64 %65)
%67 = load i8*, i8** %11
%68 = load i64, i64* %7
%69 = load i64, i64* %8
%70 = add i64 %68, %69
%71 = getelementptr inbounds i8, i8* %67, i64 %70
%72 = load i8*, i8** %6
%73 = load i64, i64* %9
%74 = call i32 @strncpy(i8* %71, i8* %72, i64 %73)
br label %75
75: ; preds = %56, %41
%76 = load i8*, i8** %11
%77 = load i64, i64* %10
%78 = sub i64 %77, 1
%79 = getelementptr inbounds i8, i8* %76, i64 %78
store i8 0, i8* %79
%80 = load i8*, i8** %11
ret i8* %80
}

LLVM IR (-O2):
define i8* @vktrace_copy_and_append(i8* %0, i8* %1, i8* %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i64 @strlen(i8* %0)
br label %7
7: ; preds = %3, %5
%8 = phi i64 [ %6, %5 ], [ 0, %3 ]
%9 = icmp eq i8* %2, null
br i1 %9, label %20, label %10
10: ; preds = %7
%11 = load i8, i8* %2
%12 = icmp ne i8 %11, 0
%13 = icmp ne i8* %1, null
%14 = and i1 %13, %12
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i64 @strlen(i8* %1)
br label %17
17: ; preds = %10, %15
%18 = phi i64 [ 0, %10 ], [ %16, %15 ]
%19 = tail call i64 @strlen(i8* %2)
br label %20
20: ; preds = %7, %17
%21 = phi i64 [ %18, %17 ], [ 0, %7 ]
%22 = phi i64 [ %19, %17 ], [ 0, %7 ]
%23 = add i64 %21, %8
%24 = add i64 %23, %22
%25 = trunc i64 %24 to i32
%26 = add i32 %25, 1
%27 = tail call i64 @vktrace_malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
%29 = icmp eq i64 %27, 0
br i1 %29, label %36, label %30
30: ; preds = %20
%31 = tail call i32 @strncpy(i8* %28, i8* %0, i64 %8)
%32 = getelementptr inbounds i8, i8* %28, i64 %8
%33 = tail call i32 @strncpy(i8* %32, i8* %1, i64 %21)
%34 = getelementptr inbounds i8, i8* %28, i64 %23
%35 = tail call i32 @strncpy(i8* %34, i8* %2, i64 %22)
br label %36
36: ; preds = %30, %20
%37 = getelementptr inbounds i8, i8* %28, i64 %24
store i8 0, i8* %37
ret i8* %28
}