// File: /workspace/github_cpp_makefile_star2_O2/Ciaxur[P]yagb-emu/include/imgui/imgui_draw.cpp
// Function: ImFont::RenderText(ImDrawList*, float, ImVec2, unsigned int, ImVec4 const&, char const*, char const*, float, bool) const
void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const
{
    if (!text_end)
        text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls.

    // Align to be pixel perfect
    pos.x = IM_FLOOR(pos.x);
    pos.y = IM_FLOOR(pos.y);
    float x = pos.x;
    float y = pos.y;
    if (y > clip_rect.w)
        return;

    const float scale = size / FontSize;
    const float line_height = FontSize * scale;
    const bool word_wrap_enabled = (wrap_width > 0.0f);
    const char* word_wrap_eol = NULL;

    // Fast-forward to first visible line
    const char* s = text_begin;
    if (y + line_height < clip_rect.y && !word_wrap_enabled)
        while (y + line_height < clip_rect.y && s < text_end)
        {
            s = (const char*)memchr(s, '\n', text_end - s);
            s = s ? s + 1 : text_end;
            y += line_height;
        }

    // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve()
    // Note that very large horizontal line will still be affected by the issue (e.g. a one megabyte string buffer without a newline will likely crash atm)
    if (text_end - s > 10000 && !word_wrap_enabled)
    {
        const char* s_end = s;
        float y_end = y;
        while (y_end < clip_rect.w && s_end < text_end)
        {
            s_end = (const char*)memchr(s_end, '\n', text_end - s_end);
            s_end = s_end ? s_end + 1 : text_end;
            y_end += line_height;
        }
        text_end = s_end;
    }
    if (s == text_end)
        return;

    // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized)
    const int vtx_count_max = (int)(text_end - s) * 4;
    const int idx_count_max = (int)(text_end - s) * 6;
    const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max;
    draw_list->PrimReserve(idx_count_max, vtx_count_max);

    ImDrawVert* vtx_write = draw_list->_VtxWritePtr;
    ImDrawIdx* idx_write = draw_list->_IdxWritePtr;
    unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx;

    while (s < text_end)
    {
        if (word_wrap_enabled)
        {
            // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature.
            if (!word_wrap_eol)
            {
                word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - pos.x));
                if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity.
                    word_wrap_eol++;    // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below
            }

            if (s >= word_wrap_eol)
            {
                x = pos.x;
                y += line_height;
                word_wrap_eol = NULL;

                // Wrapping skips upcoming blanks
                while (s < text_end)
                {
                    const char c = *s;
                    if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; }
                }
                continue;
            }
        }

        // Decode and advance source
        unsigned int c = (unsigned int)*s;
        if (c < 0x80)
        {
            s += 1;
        }
        else
        {
            s += ImTextCharFromUtf8(&c, s, text_end);
            if (c == 0) // Malformed UTF-8?
                break;
        }

        if (c < 32)
        {
            if (c == '\n')
            {
                x = pos.x;
                y += line_height;
                if (y > clip_rect.w)
                    break; // break out of main loop
                continue;
            }
            if (c == '\r')
                continue;
        }

        const ImFontGlyph* glyph = FindGlyph((ImWchar)c);
        if (glyph == NULL)
            continue;

        float char_width = glyph->AdvanceX * scale;
        if (glyph->Visible)
        {
            // We don't do a second finer clipping test on the Y axis as we've already skipped anything before clip_rect.y and exit once we pass clip_rect.w
            float x1 = x + glyph->X0 * scale;
            float x2 = x + glyph->X1 * scale;
            float y1 = y + glyph->Y0 * scale;
            float y2 = y + glyph->Y1 * scale;
            if (x1 <= clip_rect.z && x2 >= clip_rect.x)
            {
                // Render a character
                float u1 = glyph->U0;
                float v1 = glyph->V0;
                float u2 = glyph->U1;
                float v2 = glyph->V1;

                // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads.
                if (cpu_fine_clip)
                {
                    if (x1 < clip_rect.x)
                    {
                        u1 = u1 + (1.0f - (x2 - clip_rect.x) / (x2 - x1)) * (u2 - u1);
                        x1 = clip_rect.x;
                    }
                    if (y1 < clip_rect.y)
                    {
                        v1 = v1 + (1.0f - (y2 - clip_rect.y) / (y2 - y1)) * (v2 - v1);
                        y1 = clip_rect.y;
                    }
                    if (x2 > clip_rect.z)
                    {
                        u2 = u1 + ((clip_rect.z - x1) / (x2 - x1)) * (u2 - u1);
                        x2 = clip_rect.z;
                    }
                    if (y2 > clip_rect.w)
                    {
                        v2 = v1 + ((clip_rect.w - y1) / (y2 - y1)) * (v2 - v1);
                        y2 = clip_rect.w;
                    }
                    if (y1 >= y2)
                    {
                        x += char_width;
                        continue;
                    }
                }

                // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here:
                {
                    idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2);
                    idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3);
                    vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1;
                    vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1;
                    vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2;
                    vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2;
                    vtx_write += 4;
                    vtx_current_idx += 4;
                    idx_write += 6;
                }
            }
        }
        x += char_width;
    }

    // Give back unused vertices (clipped ones, blanks) ~ this is essentially a PrimUnreserve() action.
    draw_list->VtxBuffer.Size = (int)(vtx_write - draw_list->VtxBuffer.Data); // Same as calling shrink()
    draw_list->IdxBuffer.Size = (int)(idx_write - draw_list->IdxBuffer.Data);
    draw_list->CmdBuffer[draw_list->CmdBuffer.Size - 1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size);
    draw_list->_VtxWritePtr = vtx_write;
    draw_list->_IdxWritePtr = idx_write;
    draw_list->_VtxCurrentIdx = vtx_current_idx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movss %xmm2, -0x3c(%rbp)
movq %r9, %r13
movq %r8, %r14
movq %rcx, %r15
movl %edx, -0x94(%rbp)
movq %rsi, %rbx
movq %rdi, %r12
testq %r9, %r9
movaps %xmm1, -0x70(%rbp)
jne 0x648e3
movq %r14, %rdi
movss %xmm0, -0x38(%rbp)
callq 0xc380
movaps -0x70(%rbp), %xmm1
movss -0x38(%rbp), %xmm0
movq %rax, %r13
addq %r14, %r13
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
cvttps2dq %xmm1, %xmm1
cvtdq2ps %xmm1, %xmm2
movss 0xc(%r15), %xmm1
movss %xmm1, -0x40(%rbp)
ucomiss %xmm1, %xmm2
ja 0x64ea4
movss 0x14(%r12), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x38(%rbp)
mulss %xmm0, %xmm1
xorps %xmm3, %xmm3
movss -0x3c(%rbp), %xmm0
ucomiss %xmm3, %xmm0
movq %r15, -0xa8(%rbp)
movss %xmm1, -0x30(%rbp)
ja 0x6498a
movaps %xmm2, %xmm0
addss %xmm1, %xmm0
movss 0x4(%r15), %xmm3
movss %xmm3, -0x50(%rbp)
ucomiss %xmm0, %xmm3
jbe 0x6498a
pushq $0xa
popq %r15
addss %xmm2, %xmm1
movss -0x50(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x6498a
cmpq %r13, %r14
jae 0x6498a
movq %r13, %rdx
subq %r14, %rdx
movq %r14, %rdi
movl %r15d, %esi
movss %xmm1, -0x34(%rbp)
callq 0xc400
movss -0x30(%rbp), %xmm1
leaq 0x1(%rax), %r14
testq %rax, %rax
cmoveq %r13, %r14
movss -0x34(%rbp), %xmm2
jmp 0x6494a
movss -0x3c(%rbp), %xmm0
ucomiss 0x37f2e(%rip), %xmm0    # 0x9c8c4
movaps %xmm2, -0x50(%rbp)
ja 0x649ed
movq %r13, %rax
subq %r14, %rax
cmpq $0x2711, %rax           # imm = 0x2711
jl 0x649ed
pushq $0xa
popq %r15
movq %r14, %rdi
movss -0x40(%rbp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x649ea
cmpq %r13, %rdi
jae 0x649ea
movq %r13, %rdx
subq %rdi, %rdx
movl %r15d, %esi
movss %xmm2, -0x34(%rbp)
callq 0xc400
movss -0x34(%rbp), %xmm2
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %r13, %rdi
addss -0x30(%rbp), %xmm2
jmp 0x649b1
movq %rdi, %r13
cmpq %r13, %r14
je 0x64ea4
cvttps2dq -0x70(%rbp), %xmm0
cvtdq2ps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movl %r13d, %edx
subl %r14d, %edx
imull $0x6, %edx, %esi
shll $0x2, %edx
movl 0x10(%rbx), %eax
movl %eax, -0x8c(%rbp)
movq %rbx, %rdi
movl %esi, -0x90(%rbp)
callq 0x623b8
movaps -0x70(%rbp), %xmm2
movaps -0x50(%rbp), %xmm13
movss -0x30(%rbp), %xmm1
movq 0x48(%rbx), %rax
movq %rax, -0x88(%rbp)
movq 0x50(%rbx), %rax
movq %rax, -0x80(%rbp)
movl 0x34(%rbx), %eax
movq %rax, -0x78(%rbp)
movq $0x0, -0xa0(%rbp)
xorps %xmm3, %xmm3
movss %xmm2, -0x2c(%rbp)
movq %r14, %r15
movq -0xa0(%rbp), %rcx
movaps %xmm13, %xmm4
movss -0x2c(%rbp), %xmm5
movaps %xmm13, -0x50(%rbp)
cmpq %r13, %r15
jae 0x64e4b
movss -0x3c(%rbp), %xmm0
ucomiss %xmm3, %xmm0
jbe 0x64b32
testq %rcx, %rcx
jne 0x64ae3
movaps %xmm2, %xmm1
subss %xmm5, %xmm1
addss -0x3c(%rbp), %xmm1
movq %r12, %rdi
movss -0x38(%rbp), %xmm0
movq %r15, %rsi
movq %r13, %rdx
movss %xmm4, -0x34(%rbp)
movss %xmm5, -0x40(%rbp)
callq 0x6b3e0
movss -0x40(%rbp), %xmm5
movss -0x34(%rbp), %xmm4
xorps %xmm3, %xmm3
movaps -0x70(%rbp), %xmm2
movaps -0x50(%rbp), %xmm13
movss -0x30(%rbp), %xmm1
xorl %ecx, %ecx
cmpq %r15, %rax
sete %cl
addq %rax, %rcx
cmpq %rcx, %r15
jb 0x64b1f
addss %xmm1, %xmm4
movq %r15, %rax
cmpq %r13, %r15
jae 0x64b12
movzbl (%rax), %ecx
leaq 0x1(%rax), %r15
cmpl $0x9, %ecx
je 0x64aec
cmpl $0x20, %ecx
je 0x64aec
leaq 0x1(%rax), %r15
cmpb $0xa, %cl
cmovneq %rax, %r15
jmp 0x64b15
movq %rax, %r15
xorl %ecx, %ecx
movaps %xmm2, %xmm5
jmp 0x64a78
movq %r15, %r14
movaps %xmm4, %xmm13
movss %xmm5, -0x2c(%rbp)
movq %rcx, -0xa0(%rbp)
movsbl (%r14), %ecx
movl %ecx, -0x54(%rbp)
testl %ecx, %ecx
movq -0xa8(%rbp), %r15
js 0x64b49
incq %r14
jmp 0x64b7e
movaps %xmm13, -0x50(%rbp)
leaq -0x54(%rbp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x33a81
movl -0x54(%rbp), %ecx
testl %ecx, %ecx
je 0x64e4b
cltq
addq %rax, %r14
movss -0x30(%rbp), %xmm1
movaps -0x50(%rbp), %xmm13
movaps -0x70(%rbp), %xmm2
xorps %xmm3, %xmm3
cmpl $0x1f, %ecx
ja 0x64bab
cmpl $0xa, %ecx
jne 0x64ba2
addss %xmm1, %xmm13
ucomiss 0xc(%r15), %xmm13
movss %xmm2, -0x2c(%rbp)
jbe 0x64a60
jmp 0x64e4b
cmpl $0xd, %ecx
je 0x64a60
movaps %xmm13, -0x50(%rbp)
movzwl %cx, %esi
movq %r12, %rdi
callq 0x6b25a
testq %rax, %rax
je 0x64bed
movss 0x4(%rax), %xmm0
movss -0x38(%rbp), %xmm3
mulss %xmm3, %xmm0
cmpl $0x0, (%rax)
js 0x64c03
movaps -0x50(%rbp), %xmm13
movss -0x2c(%rbp), %xmm2
addss %xmm0, %xmm2
movss %xmm2, -0x2c(%rbp)
movss -0x30(%rbp), %xmm1
jmp 0x64bf7
movss -0x30(%rbp), %xmm1
movaps -0x50(%rbp), %xmm13
movaps -0x70(%rbp), %xmm2
xorps %xmm3, %xmm3
jmp 0x64a60
movss 0x8(%rax), %xmm8
mulss %xmm3, %xmm8
movss -0x2c(%rbp), %xmm2
addss %xmm2, %xmm8
movss 0x8(%r15), %xmm1
ucomiss %xmm8, %xmm1
movaps -0x50(%rbp), %xmm13
jb 0x64bdd
movss 0x10(%rax), %xmm9
mulss %xmm3, %xmm9
addss %xmm2, %xmm9
movss (%r15), %xmm2
ucomiss %xmm2, %xmm9
jb 0x64bd8
movss 0xc(%rax), %xmm10
mulss %xmm3, %xmm10
addss %xmm13, %xmm10
movss 0x14(%rax), %xmm5
mulss %xmm3, %xmm5
addss %xmm13, %xmm5
movss 0x18(%rax), %xmm3
movss 0x1c(%rax), %xmm7
movss 0x20(%rax), %xmm6
movss 0x24(%rax), %xmm4
cmpb $0x0, 0x10(%rbp)
je 0x64cbc
ucomiss %xmm8, %xmm2
jbe 0x64ccd
movaps %xmm9, %xmm11
subss %xmm2, %xmm11
movaps %xmm9, %xmm12
subss %xmm8, %xmm12
divss %xmm12, %xmm11
movss 0x37356(%rip), %xmm8    # 0x9bff8
subss %xmm11, %xmm8
movaps %xmm6, %xmm11
subss %xmm3, %xmm11
mulss %xmm8, %xmm11
addss %xmm11, %xmm3
jmp 0x64cd1
movaps %xmm8, %xmm2
movaps %xmm9, %xmm1
movaps %xmm10, %xmm8
jmp 0x64d87
movaps %xmm8, %xmm2
movss 0x4(%r15), %xmm8
ucomiss %xmm10, %xmm8
jbe 0x64d17
movaps %xmm5, %xmm11
subss %xmm8, %xmm11
movaps %xmm5, %xmm12
subss %xmm10, %xmm12
divss %xmm12, %xmm11
movss 0x372fb(%rip), %xmm10   # 0x9bff8
subss %xmm11, %xmm10
movaps %xmm4, %xmm11
subss %xmm7, %xmm11
mulss %xmm10, %xmm11
addss %xmm11, %xmm7
jmp 0x64d1b
movaps %xmm10, %xmm8
ucomiss %xmm1, %xmm9
jbe 0x64d43
movaps %xmm1, %xmm10
subss %xmm2, %xmm10
subss %xmm2, %xmm9
divss %xmm9, %xmm10
subss %xmm3, %xmm6
mulss %xmm10, %xmm6
addss %xmm3, %xmm6
jmp 0x64d47
movaps %xmm9, %xmm1
movss 0xc(%r15), %xmm9
ucomiss %xmm9, %xmm5
jbe 0x64d79
movaps %xmm9, %xmm10
subss %xmm8, %xmm10
subss %xmm8, %xmm5
divss %xmm5, %xmm10
subss %xmm7, %xmm4
mulss %xmm10, %xmm4
addss %xmm7, %xmm4
movaps %xmm9, %xmm5
jmp 0x64d7d
movaps %xmm5, %xmm9
ucomiss %xmm9, %xmm8
jae 0x64e38
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %rsi
movw %si, (%rdx)
leal 0x1(%rsi), %eax
movw %ax, 0x2(%rdx)
leal 0x2(%rsi), %eax
movw %ax, 0x4(%rdx)
movw %si, 0x6(%rdx)
movw %ax, 0x8(%rdx)
leal 0x3(%rsi), %eax
movw %ax, 0xa(%rdx)
movq -0x88(%rbp), %rcx
movss %xmm2, (%rcx)
movss %xmm8, 0x4(%rcx)
movl -0x94(%rbp), %eax
movl %eax, 0x10(%rcx)
movss %xmm3, 0x8(%rcx)
movss %xmm7, 0xc(%rcx)
movss %xmm1, 0x14(%rcx)
movss %xmm8, 0x18(%rcx)
movl %eax, 0x24(%rcx)
movss %xmm6, 0x1c(%rcx)
movss %xmm7, 0x20(%rcx)
movss %xmm1, 0x28(%rcx)
movss %xmm5, 0x2c(%rcx)
movl %eax, 0x38(%rcx)
movss %xmm6, 0x30(%rcx)
movss %xmm4, 0x34(%rcx)
movss %xmm2, 0x3c(%rcx)
movss %xmm5, 0x40(%rcx)
movl %eax, 0x4c(%rcx)
movss %xmm3, 0x44(%rcx)
movss %xmm4, 0x48(%rcx)
addq $0x50, %rcx
movq %rcx, -0x88(%rbp)
addl $0x4, %esi
movq %rsi, -0x78(%rbp)
addq $0xc, %rdx
movq %rdx, -0x80(%rbp)
jmp 0x64bd8
movss -0x2c(%rbp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, -0x2c(%rbp)
jmp 0x64be6
movq -0x88(%rbp), %r15
movq %r15, %rax
subq 0x28(%rbx), %rax
pushq $0x14
popq %rcx
cqto
idivq %rcx
movl %eax, 0x20(%rbx)
movq -0x80(%rbp), %r12
movq %r12, %r14
subq 0x18(%rbx), %r14
shrq %r14
movl %r14d, 0x10(%rbx)
movl -0x8c(%rbp), %eax
addl -0x90(%rbp), %eax
subl %eax, %r14d
movl (%rbx), %esi
decl %esi
movq %rbx, %rdi
callq 0x4b5b8
addl %r14d, 0x20(%rax)
movq %r15, 0x48(%rbx)
movq %r12, 0x50(%rbx)
movq -0x78(%rbp), %rax
movl %eax, 0x34(%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq