// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
movl 0x4(%rsi), %eax
cmpl %edx, %eax
jne 0xa6bd9
testl %ecx, %ecx
je 0xa6c82
movq 0x18(%r15), %rdi
movslq %r14d, %rax
leaq (%rdi,%rax,2), %rsi
leaq -0x38(%rbp), %rdx
movl $0x1, %ecx
callq 0x9cb91
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
xorl %eax, %eax
movl %eax, 0x4(%rbx)
movl %eax, 0xc(%rbx)
movl %r14d, 0x10(%rbx)
movss %xmm1, 0x8(%rbx)
movss %xmm0, (%rbx)
jmp 0xa6cf5
movl %r14d, -0x2c(%rbp)
xorl %ecx, %ecx
xorps %xmm0, %xmm0
xorl %r14d, %r14d
movl %ecx, %r12d
movss %xmm0, 0x4(%rbx)
movq 0x18(%r15), %rcx
movq $0x0, -0x38(%rbp)
movslq %r12d, %rdx
leaq (%rcx,%rdx,2), %r13
cltq
leaq (%rcx,%rax,2), %rsi
movq %r13, %rdi
leaq -0x38(%rbp), %rdx
movl $0x1, %ecx
callq 0x9cb91
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movq -0x38(%rbp), %rax
subq %r13, %rax
shrq %rax
leal (%r12,%rax), %ecx
cmpl -0x2c(%rbp), %ecx
jg 0xa6c3c
addss 0x4(%rbx), %xmm0
movl 0x4(%r15), %eax
movl %r12d, %r14d
jmp 0xa6be5
movl %r12d, 0xc(%rbx)
movl %eax, 0x10(%rbx)
movss %xmm0, 0x8(%rbx)
movl %r14d, 0x14(%rbx)
movl $0x0, (%rbx)
movl -0x2c(%rbp), %r14d
subl %r12d, %r14d
jle 0xa6cf5
xorl %r13d, %r13d
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0xa6d04
addss (%rbx), %xmm0
movss %xmm0, (%rbx)
incl %r13d
cmpl %r13d, %r14d
jne 0xa6c62
jmp 0xa6cf5
movq $0x0, (%rbx)
movl $0x3f800000, 0x8(%rbx)  # imm = 0x3F800000
testl %r14d, %r14d
jle 0xa6ce0
movl %r14d, -0x2c(%rbp)
xorl %r14d, %r14d
movl %r14d, %r12d
movslq 0x4(%r15), %rax
movq 0x18(%r15), %rcx
movq $0x0, -0x38(%rbp)
movslq %r14d, %r14
leaq (%rcx,%r14,2), %r13
leaq (%rcx,%rax,2), %rsi
movq %r13, %rdi
leaq -0x38(%rbp), %rdx
movl $0x1, %ecx
callq 0x9cb91
movq -0x38(%rbp), %rax
subq %r13, %rax
shrq %rax
addl %eax, %r14d
cmpl -0x2c(%rbp), %r14d
jl 0xa6c9c
jmp 0xa6ce6
xorl %r14d, %r14d
xorl %r12d, %r12d
movl %r14d, 0xc(%rbx)
movl $0x0, 0x10(%rbx)
movl %r12d, 0x14(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq