// File: Ciaxur[P]yagb-emu/include/imgui/imstb_textedit.h
// Function: ImStb::stb_textedit_find_charpos(ImStb::StbFindState*, ImGuiInputTextState*, int, int)
static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line)
{
   StbTexteditRow r;
   int prev_start = 0;
   int z = STB_TEXTEDIT_STRINGLEN(str);
   int i=0, first;

   if (n == z) {
      // if it's at the end, then find the last line -- simpler than trying to
      // explicitly handle this case in the regular code
      if (single_line) {
         STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
         find->y = 0;
         find->first_char = 0;
         find->length = z;
         find->height = r.ymax - r.ymin;
         find->x = r.x1;
      } else {
         find->y = 0;
         find->x = 0;
         find->height = 1;
         while (i < z) {
            STB_TEXTEDIT_LAYOUTROW(&r, str, i);
            prev_start = i;
            i += r.num_chars;
         }
         find->first_char = i;
         find->length = 0;
         find->prev_first = prev_start;
      }
      return;
   }

   // search rows to find the one that straddles character n
   find->y = 0;

   for(;;) {
      STB_TEXTEDIT_LAYOUTROW(&r, str, i);
      if (n < i + r.num_chars)
         break;
      prev_start = i;
      i += r.num_chars;
      find->y += r.baseline_y_delta;
   }

   find->first_char = first = i;
   find->length = r.num_chars;
   find->height = r.ymax - r.ymin;
   find->prev_first = prev_start;

   // now scan to find xpos
   find->x = r.x0;
   for (i=0; first+i < n; ++i)
      find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x158d60
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x38(%rbp), %eax
jne 0x159959
cmpl $0x0, -0x18(%rbp)
je 0x1598e4
movq -0x10(%rbp), %rsi
leaq -0x30(%rbp), %rdi
xorl %edx, %edx
callq 0x159a60
movq -0x8(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0xc(%rax)
movl -0x38(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x10(%rax)
movss -0x20(%rbp), %xmm0
subss -0x24(%rbp), %xmm0
movq -0x8(%rbp), %rax
movss %xmm0, 0x8(%rax)
movss -0x2c(%rbp), %xmm0
movq -0x8(%rbp), %rax
movss %xmm0, (%rax)
jmp 0x159954
movq -0x8(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
movq -0x8(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movq -0x8(%rbp), %rax
movss 0x17335(%rip), %xmm0    # 0x170c3c
movss %xmm0, 0x8(%rax)
movl -0x3c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x159935
movq -0x10(%rbp), %rsi
movl -0x3c(%rbp), %edx
leaq -0x30(%rbp), %rdi
callq 0x159a60
movl -0x3c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x1c(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x15990c
movl -0x3c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x10(%rax)
movl -0x34(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x159a21
movq -0x8(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
movq -0x10(%rbp), %rsi
movl -0x3c(%rbp), %edx
leaq -0x30(%rbp), %rdi
callq 0x159a60
movl -0x14(%rbp), %eax
movl -0x3c(%rbp), %ecx
addl -0x1c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x159984
jmp 0x1599a8
movl -0x3c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x1c(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movss -0x28(%rbp), %xmm0
movq -0x8(%rbp), %rax
addss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%rax)
jmp 0x159965
movl -0x3c(%rbp), %ecx
movl %ecx, -0x40(%rbp)
movq -0x8(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x1c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x10(%rax)
movss -0x20(%rbp), %xmm0
subss -0x24(%rbp), %xmm0
movq -0x8(%rbp), %rax
movss %xmm0, 0x8(%rax)
movl -0x34(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x14(%rax)
movss -0x30(%rbp), %xmm0
movq -0x8(%rbp), %rax
movss %xmm0, (%rax)
movl $0x0, -0x3c(%rbp)
movl -0x40(%rbp), %eax
addl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x159a21
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x159b20
movq -0x8(%rbp), %rax
addss (%rax), %xmm0
movss %xmm0, (%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1599f0
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)