// File: /workspace/github_c_makefile_star2_O1/atomlang[P]atomlang/src/pk_var.c
// Function: stringFormat
String* stringFormat(PKVM* vm, const char* fmt, ...) {
  va_list arg_list;

  // Calculate the total length of the resulting string. This is required to
  // determine the final string size to allocate.
  va_start(arg_list, fmt);
  size_t total_length = 0;
  for (const char* c = fmt; *c != '\0'; c++) {
    switch (*c) {
      case '$':
        total_length += strlen(va_arg(arg_list, const char*));
        break;

      case '@':
        total_length += va_arg(arg_list, String*)->length;
        break;

      default:
        total_length++;
    }
  }
  va_end(arg_list);

  // Now build the new string.
  String* result = _allocateString(vm, total_length);
  va_start(arg_list, fmt);
  char* buff = result->data;
  for (const char* c = fmt; *c != '\0'; c++) {
    switch (*c) {
      case '$':
      {
        const char* string = va_arg(arg_list, const char*);
        size_t length = strlen(string);
        memcpy(buff, string, length);
        buff += length;
      } break;

      case '@':
      {
        String* string = va_arg(arg_list, String*);
        memcpy(buff, string->data, string->length);
        buff += string->length;
      } break;

      default:
      {
        *buff++ = *c;
      } break;
    }
  }
  va_end(arg_list);

  result->hash = utilHashString(result->data);
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
leaq 0x20(%rsp), %r12
movq %rdx, 0x10(%r12)
movq %rcx, 0x18(%r12)
movq %r8, 0x20(%r12)
movq %r9, 0x28(%r12)
testb %al, %al
je 0x963d
movaps %xmm0, 0x50(%rsp)
movaps %xmm1, 0x60(%rsp)
movaps %xmm2, 0x70(%rsp)
movaps %xmm3, 0x80(%rsp)
movaps %xmm4, 0x90(%rsp)
movaps %xmm5, 0xa0(%rsp)
movaps %xmm6, 0xb0(%rsp)
movaps %xmm7, 0xc0(%rsp)
movq %r12, 0x10(%rsp)
leaq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movabsq $0x3000000010, %rcx     # imm = 0x3000000010
movq %rcx, (%rsp)
movl $0x10, %r15d
xorl %ebp, %ebp
movq %rsi, %rbx
movzbl (%rbx), %ecx
cmpl $0x24, %ecx
je 0x9697
cmpl $0x40, %ecx
je 0x967e
testl %ecx, %ecx
je 0x96e3
incq %rbp
jmp 0x96de
cmpl $0x28, %r15d
ja 0x96b0
movq %rax, %rcx
movl %r15d, %eax
addq %r12, %rax
addl $0x8, %r15d
movl %r15d, (%rsp)
jmp 0x96b9
cmpl $0x28, %r15d
ja 0x96c7
movq %rax, %r13
movl %r15d, %eax
addq %r12, %rax
addl $0x8, %r15d
movl %r15d, (%rsp)
jmp 0x96d0
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq (%rax), %rax
movl 0x14(%rax), %eax
addq %rax, %rbp
movq %rcx, %rax
jmp 0x96de
leaq 0x8(%rax), %r13
movq %r13, 0x8(%rsp)
movq (%rax), %rdi
callq 0x2140
addq %rax, %rbp
movq %r13, %rax
incq %rbx
jmp 0x9668
leaq 0x21(%rbp), %rcx
xorl %r13d, %r13d
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xb668
movq %rax, %r15
movl $0x0, (%rax)
movb %r13b, 0x4(%rax)
movq (%rbx), %rax
movq %rax, 0x8(%r15)
movq %r15, (%rbx)
movl %ebp, 0x14(%r15)
leaq 0x1c(%r15), %rbx
movb %r13b, 0x1c(%r15,%rbp)
leal 0x1(%rbp), %eax
movl %eax, 0x18(%r15)
movabsq $0x3000000010, %rax     # imm = 0x3000000010
movq %rax, (%rsp)
leaq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %rbx, %r12
movzbl (%r14), %eax
cmpl $0x24, %eax
je 0x9784
cmpl $0x40, %eax
je 0x976b
testl %eax, %eax
je 0x97fc
movb %al, (%r12)
incq %r12
jmp 0x97f4
movl (%rsp), %ecx
cmpq $0x28, %rcx
ja 0x979d
movq %rcx, %rax
addq 0x10(%rsp), %rax
addl $0x8, %ecx
movl %ecx, (%rsp)
jmp 0x97ab
movl (%rsp), %ecx
cmpq $0x28, %rcx
ja 0x97c7
movq %rcx, %rax
addq 0x10(%rsp), %rax
addl $0x8, %ecx
movl %ecx, (%rsp)
jmp 0x97d5
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq (%rax), %r13
leaq 0x1c(%r13), %rsi
movl 0x14(%r13), %edx
movq %r12, %rdi
callq 0x2280
movl 0x14(%r13), %eax
addq %rax, %r12
jmp 0x97f4
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq (%rax), %r13
movq %r13, %rdi
callq 0x2140
movq %rax, %rbp
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x2280
addq %rbp, %r12
incq %r14
jmp 0x9749
movq %rbx, %rdi
callq 0xf426
movl %eax, 0x10(%r15)
movq %r15, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq