// File: /workspace/github_c_makefile_star2_O2/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 0x3afa
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)
pushq $0x10
popq %rbp
xorl %r15d, %r15d
movq %rsi, %rbx
movzbl (%rbx), %edx
movl %ebp, %ecx
cmpl $0x24, %edx
je 0x3b51
cmpl $0x40, %edx
je 0x3b3b
testl %edx, %edx
je 0x3b9a
incq %r15
jmp 0x3b95
cmpl $0x28, %ebp
ja 0x3b67
addq %r12, %rcx
addl $0x8, %ebp
movl %ebp, (%rsp)
movq %rax, %rdx
movq %rcx, %rax
jmp 0x3b70
cmpl $0x28, %ebp
ja 0x3b7e
addq %r12, %rcx
addl $0x8, %ebp
movl %ebp, (%rsp)
movq %rax, %r13
movq %rcx, %rax
jmp 0x3b87
leaq 0x8(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq (%rax), %rax
movl 0x14(%rax), %eax
addq %rax, %r15
movq %rdx, %rax
jmp 0x3b95
leaq 0x8(%rax), %r13
movq %r13, 0x8(%rsp)
movq (%rax), %rdi
callq 0x2140
addq %rax, %r15
movq %r13, %rax
incq %rbx
jmp 0x3b23
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x34d8
movq %rax, %rbx
movabsq $0x3000000010, %rax     # imm = 0x3000000010
movq %rax, (%rsp)
leaq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %rbx, %r15
addq $0x1c, %r15
movq %r15, %r12
movzbl (%r14), %eax
cmpl $0x24, %eax
je 0x3c0f
cmpl $0x40, %eax
je 0x3bf6
testl %eax, %eax
je 0x3c87
movb %al, (%r12)
incq %r12
jmp 0x3c7f
movl (%rsp), %ecx
cmpq $0x28, %rcx
ja 0x3c28
movq %rcx, %rax
addq 0x10(%rsp), %rax
addl $0x8, %ecx
movl %ecx, (%rsp)
jmp 0x3c36
movl (%rsp), %ecx
cmpq $0x28, %rcx
ja 0x3c52
movq %rcx, %rax
addq 0x10(%rsp), %rax
addl $0x8, %ecx
movl %ecx, (%rsp)
jmp 0x3c60
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 0x3c7f
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 0x3bd4
movq %r15, %rdi
callq 0xabd7
movl %eax, 0x10(%rbx)
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq