#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 */
typedef int /*<<< orphan*/ va_list;

/* Variables and functions */
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
scalar_t__ vktrace_malloc(int);
int /*<<< orphan*/ vsnprintf(char *, size_t, char const *, int /*<<< orphan*/);
char *vktrace_copy_and_append_args(const char *pBaseString,
                                   const char *pSeparator,
                                   const char *pAppendFormat, va_list args) {
  size_t baseSize = (pBaseString != NULL) ? strlen(pBaseString) : 0;
  size_t separatorSize = (pSeparator != NULL) ? strlen(pSeparator) : 0;
  size_t appendSize = 0;
  size_t bufferSize = 0;
  char *retVal = NULL;
  bufferSize = baseSize + separatorSize + appendSize + 1;
  retVal = (char *)vktrace_malloc(sizeof(char) * bufferSize);
  if (retVal != NULL) {
    strncpy(retVal, pBaseString, baseSize);
    strncpy(&retVal[baseSize], pSeparator, separatorSize);
    vsnprintf(&retVal[baseSize + separatorSize], appendSize, pAppendFormat,
              args);
  }
  return retVal;
}


x86 assembly (clang, -O0):
<vktrace_copy_and_append_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1174 <vktrace_copy_and_append_args+0x34>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x50(%rbp)
jmp    117f <vktrace_copy_and_append_args+0x3f>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    117f <vktrace_copy_and_append_args+0x3f>
mov    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a4 <vktrace_copy_and_append_args+0x64>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x58(%rbp)
jmp    11af <vktrace_copy_and_append_args+0x6f>
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
jmp    11af <vktrace_copy_and_append_args+0x6f>
mov    -0x58(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <vktrace_malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1246 <vktrace_copy_and_append_args+0x106>
mov    -0x48(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1050 <vsnprintf@plt>
mov    -0x48(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vktrace_copy_and_append_args>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0xc(%rsp)
mov    %rdx,0x10(%rsp)
mov    %rsi,%r12
mov    %rdi,%r13
test   %rdi,%rdi
je     117f <vktrace_copy_and_append_args+0x3f>
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
test   %r12,%r12
je     1186 <vktrace_copy_and_append_args+0x46>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
jmp    1188 <vktrace_copy_and_append_args+0x48>
xor    %ebp,%ebp
test   %r12,%r12
jne    1172 <vktrace_copy_and_append_args+0x32>
xor    %ebx,%ebx
lea    (%rbx,%rbp,1),%r14
lea    0x1(%r14),%edi
call   1030 <vktrace_malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     11d2 <vktrace_copy_and_append_args+0x92>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
add    %r15,%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <strncpy@plt>
add    %r15,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    0x10(%rsp),%rdx
mov    0xc(%rsp),%ecx
call   1050 <vsnprintf@plt>
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_args(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
br label %20
19: ; preds = %4
br label %20
20: ; preds = %19, %16
%21 = phi i64 [ %18, %16 ], [ 0, %19 ]
store i64 %21, i64* %9
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = call i64 @strlen(i8* %25)
br label %28
27: ; preds = %20
br label %28
28: ; preds = %27, %24
%29 = phi i64 [ %26, %24 ], [ 0, %27 ]
store i64 %29, i64* %10
store i64 0, i64* %11
store i64 0, i64* %12
store i8* null, i8** %13
%30 = load i64, i64* %9
%31 = load i64, i64* %10
%32 = add i64 %30, %31
%33 = load i64, i64* %11
%34 = add i64 %32, %33
%35 = add i64 %34, 1
store i64 %35, i64* %12
%36 = load i64, i64* %12
%37 = mul i64 1, %36
%38 = trunc i64 %37 to i32
%39 = call i64 @vktrace_malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %13
%41 = load i8*, i8** %13
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %63
43: ; preds = %28
%44 = load i8*, i8** %13
%45 = load i8*, i8** %5
%46 = load i64, i64* %9
%47 = call i32 @strncpy(i8* %44, i8* %45, i64 %46)
%48 = load i8*, i8** %13
%49 = load i64, i64* %9
%50 = getelementptr inbounds i8, i8* %48, i64 %49
%51 = load i8*, i8** %6
%52 = load i64, i64* %10
%53 = call i32 @strncpy(i8* %50, i8* %51, i64 %52)
%54 = load i8*, i8** %13
%55 = load i64, i64* %9
%56 = load i64, i64* %10
%57 = add i64 %55, %56
%58 = getelementptr inbounds i8, i8* %54, i64 %57
%59 = load i64, i64* %11
%60 = load i8*, i8** %7
%61 = load i32, i32* %8
%62 = call i32 @vsnprintf(i8* %58, i64 %59, i8* %60, i32 %61)
br label %63
63: ; preds = %43, %28
%64 = load i8*, i8** %13
ret i8* %64
}

LLVM IR (-O2):
define i8* @vktrace_copy_and_append_args(i8* %0, i8* %1, i8* nocapture %2, i32 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i64 @strlen(i8* %0)
br label %8
8: ; preds = %4, %6
%9 = phi i64 [ %7, %6 ], [ 0, %4 ]
%10 = icmp eq i8* %1, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i64 @strlen(i8* %1)
br label %13
13: ; preds = %8, %11
%14 = phi i64 [ %12, %11 ], [ 0, %8 ]
%15 = add i64 %14, %9
%16 = trunc i64 %15 to i32
%17 = add i32 %16, 1
%18 = tail call i64 @vktrace_malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %27, label %21
21: ; preds = %13
%22 = tail call i32 @strncpy(i8* %19, i8* %0, i64 %9)
%23 = getelementptr inbounds i8, i8* %19, i64 %9
%24 = tail call i32 @strncpy(i8* %23, i8* %1, i64 %14)
%25 = getelementptr inbounds i8, i8* %19, i64 %15
%26 = tail call i32 @vsnprintf(i8* %25, i64 0, i8* %2, i32 %3)
br label %27
27: ; preds = %21, %13
ret i8* %19
}