// File: /workspace/github_c_makefile_star2_O0/crcx[P]retroforth/vm/nga-c/retro.c
// Function: string_inject
CELL string_inject(NgaState *vm, char *str, CELL buffer) {
  if (!str) {
    vm->memory[buffer] = 0;
    return 0;
  }

  CELL pos = 0;
  int i = 0;

  while (str[i] != '\0') {
    unsigned char *utf8_str = (unsigned char *)&str[i];
    int utf32_char = 0;
    int bytes_consumed = 0;

    // Convert UTF-8 to UTF-32
    if ((utf8_str[0] & 0x80) == 0x00) {
      // 1-byte UTF-8 sequence
      utf32_char = utf8_str[0];
      bytes_consumed = 1;
    } else if ((utf8_str[0] & 0xE0) == 0xC0) {
      // 2-byte UTF-8 sequence
      utf32_char = ((uint32_t)(utf8_str[0] & 0x1F) << 6) |
                              (utf8_str[1] & 0x3F);
      bytes_consumed = 2;
    } else if ((utf8_str[0] & 0xF0) == 0xE0) {
      // 3-byte UTF-8 sequence
      utf32_char = ((uint32_t)(utf8_str[0] & 0x0F) << 12) |
                   ((uint32_t)(utf8_str[1] & 0x3F) << 6) |
                              (utf8_str[2] & 0x3F);
      bytes_consumed = 3;
    } else if ((utf8_str[0] & 0xF8) == 0xF0) {
      // 4-byte UTF-8 sequence
      utf32_char = ((uint32_t)(utf8_str[0] & 0x07) << 18) |
                   ((uint32_t)(utf8_str[1] & 0x3F) << 12) |
                   ((uint32_t)(utf8_str[2] & 0x3F) << 6) |
                              (utf8_str[3] & 0x3F);
      bytes_consumed = 4;
    } else {
      // Invalid UTF-8, skip byte
      utf32_char = utf8_str[0];
      bytes_consumed = 1;
    }

    vm->memory[buffer + pos] = (CELL)utf32_char;
    vm->memory[buffer + pos + 1] = 0;
    pos++;
    i += bytes_consumed;
  }

  return buffer;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5671
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl $0x0, -0x4(%rbp)
jmp 0x5814
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x580e
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x56d8
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x34(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x57cd
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
andl $0xe0, %eax
cmpl $0xc0, %eax
jne 0x5714
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
andl $0x1f, %eax
shll $0x6, %eax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
andl $0x3f, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl $0x2, -0x38(%rbp)
jmp 0x57cb
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
andl $0xf0, %eax
cmpl $0xe0, %eax
jne 0x575d
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
shll $0xc, %eax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
andl $0x3f, %ecx
shll $0x6, %ecx
orl %ecx, %eax
movq -0x30(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
andl $0x3f, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl $0x3, -0x38(%rbp)
jmp 0x57c9
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
andl $0xf8, %eax
cmpl $0xf0, %eax
jne 0x57b6
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
andl $0x7, %eax
shll $0x12, %eax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
andl $0x3f, %ecx
shll $0xc, %ecx
orl %ecx, %eax
movq -0x30(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
andl $0x3f, %ecx
shll $0x6, %ecx
orl %ecx, %eax
movq -0x30(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
andl $0x3f, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
movl $0x4, -0x38(%rbp)
jmp 0x57c7
movq -0x30(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x34(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0x57c9
jmp 0x57cb
jmp 0x57cd
movl -0x34(%rbp), %edx
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x20(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl -0x20(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x38(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x567f
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopl (%rax)