// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x178, %rsp            # imm = 0x178
testb %al, %al
je 0x4f1b
movaps %xmm0, 0x90(%rsp)
movaps %xmm1, 0xa0(%rsp)
movaps %xmm2, 0xb0(%rsp)
movaps %xmm3, 0xc0(%rsp)
movaps %xmm4, 0xd0(%rsp)
movaps %xmm5, 0xe0(%rsp)
movaps %xmm6, 0xf0(%rsp)
movaps %xmm7, 0x100(%rsp)
movq %r9, 0x88(%rsp)
movq %r8, 0x80(%rsp)
movq %rcx, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movq %rdi, 0x170(%rsp)
movq %rsi, 0x168(%rsp)
leaq 0x150(%rsp), %rax
leaq 0x60(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x180(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x10, (%rax)
movq $0x0, 0x148(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x50d5
movq 0x140(%rsp), %rax
movsbl (%rax), %eax
movl %eax, 0x5c(%rsp)
subl $0x24, %eax
je 0x4fc3
jmp 0x4fb5
movl 0x5c(%rsp), %eax
subl $0x40, %eax
je 0x5037
jmp 0x50a6
leaq 0x150(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0x150(%rsp), %eax
movl %eax, 0x58(%rsp)
cmpl $0x28, %eax
ja 0x4ffc
movq 0x50(%rsp), %rcx
movl 0x58(%rsp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, 0x48(%rsp)
jmp 0x5015
movq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq (%rax), %rdi
callq 0x2130
addq 0x148(%rsp), %rax
movq %rax, 0x148(%rsp)
jmp 0x50ba
leaq 0x150(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x150(%rsp), %eax
movl %eax, 0x44(%rsp)
cmpl $0x28, %eax
ja 0x5070
movq 0x38(%rsp), %rcx
movl 0x44(%rsp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, 0x30(%rsp)
jmp 0x5089
movq 0x38(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl 0x14(%rax), %eax
addq 0x148(%rsp), %rax
movq %rax, 0x148(%rsp)
jmp 0x50ba
movq 0x148(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x148(%rsp)
jmp 0x50bc
movq 0x140(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x140(%rsp)
jmp 0x4f8b
leaq 0x150(%rsp), %rax
movq 0x170(%rsp), %rdi
movq 0x148(%rsp), %rsi
callq 0x45e0
movq %rax, 0x138(%rsp)
leaq 0x150(%rsp), %rax
leaq 0x60(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x180(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x10, (%rax)
movq 0x138(%rsp), %rax
addq $0x1c, %rax
movq %rax, 0x130(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x531a
movq 0x128(%rsp), %rax
movsbl (%rax), %eax
movl %eax, 0x2c(%rsp)
subl $0x24, %eax
je 0x5184
jmp 0x5172
movl 0x2c(%rsp), %eax
subl $0x40, %eax
je 0x5235
jmp 0x52dc
leaq 0x150(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x150(%rsp), %eax
movl %eax, 0x28(%rsp)
cmpl $0x28, %eax
ja 0x51bd
movq 0x20(%rsp), %rcx
movl 0x28(%rsp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, 0x18(%rsp)
jmp 0x51d6
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rdi
callq 0x2130
movq %rax, 0x118(%rsp)
movq 0x130(%rsp), %rdi
movq 0x120(%rsp), %rsi
movq 0x118(%rsp), %rdx
callq 0x2270
movq 0x118(%rsp), %rax
addq 0x130(%rsp), %rax
movq %rax, 0x130(%rsp)
jmp 0x52ff
leaq 0x150(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x150(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpl $0x28, %eax
ja 0x526d
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, (%rsp)
jmp 0x5285
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, (%rsp)
movq (%rsp), %rax
movq (%rax), %rax
movq %rax, 0x110(%rsp)
movq 0x130(%rsp), %rdi
movq 0x110(%rsp), %rsi
addq $0x1c, %rsi
movq 0x110(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, %edx
callq 0x2270
movq 0x110(%rsp), %rax
movl 0x14(%rax), %ecx
movq 0x130(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x130(%rsp)
jmp 0x52ff
movq 0x128(%rsp), %rax
movb (%rax), %cl
movq 0x130(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x130(%rsp)
movb %cl, (%rax)
jmp 0x5301
movq 0x128(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x128(%rsp)
jmp 0x5148
movq 0x138(%rsp), %rdi
addq $0x1c, %rdi
callq 0x140c0
movl %eax, %ecx
movq 0x138(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x138(%rsp), %rax
addq $0x178, %rsp            # imm = 0x178
retq
nopl (%rax,%rax)