// File: /workspace/github_cpp_makefile_star2_O2/Ciaxur[P]yagb-emu/include/imgui/imgui_draw.cpp
// Function: ImFont::CalcTextSizeA(float, float, float, char const*, char const*, char const**) const
ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const
{
    if (!text_end)
        text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this.

    const float line_height = size;
    const float scale = size / FontSize;

    ImVec2 text_size = ImVec2(0, 0);
    float line_width = 0.0f;

    const bool word_wrap_enabled = (wrap_width > 0.0f);
    const char* word_wrap_eol = NULL;

    const char* s = text_begin;
    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 - line_width);
                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)
            {
                if (text_size.x < line_width)
                    text_size.x = line_width;
                text_size.y += line_height;
                line_width = 0.0f;
                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
        const char* prev_s = s;
        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')
            {
                text_size.x = ImMax(text_size.x, line_width);
                text_size.y += line_height;
                line_width = 0.0f;
                continue;
            }
            if (c == '\r')
                continue;
        }

        const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX) * scale;
        if (line_width + char_width >= max_width)
        {
            s = prev_s;
            break;
        }

        line_width += char_width;
    }

    if (text_size.x < line_width)
        text_size.x = line_width;

    if (line_width > 0 || text_size.y == 0.0f)
        text_size.y += line_height;

    if (remaining)
        *remaining = s;

    return text_size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, -0x68(%rbp)
movq %rdx, %r15
movq %rsi, %rbx
movss %xmm1, -0x3c(%rbp)
movq %rdi, %r14
testq %rdx, %rdx
movss %xmm2, -0x30(%rbp)
movss %xmm0, -0x2c(%rbp)
jne 0x6b5b4
movq %rbx, %rdi
callq 0xc380
movss -0x30(%rbp), %xmm2
movss -0x2c(%rbp), %xmm0
movq %rax, %r15
addq %rbx, %r15
divss 0x14(%r14), %xmm0
leaq 0x10(%r14), %rax
movq %rax, -0x70(%rbp)
xorps %xmm3, %xmm3
xorl %r12d, %r12d
xorps %xmm1, %xmm1
movss %xmm0, -0x34(%rbp)
movaps %xmm3, -0x60(%rbp)
movaps %xmm1, %xmm5
movq %rbx, %r13
cmpq %r15, %rbx
jae 0x6b73e
ucomiss 0x312da(%rip), %xmm2    # 0x9c8c4
jbe 0x6b632
testq %r12, %r12
jne 0x6b629
movaps %xmm2, %xmm1
subss %xmm5, %xmm1
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
movaps %xmm5, -0x50(%rbp)
callq 0x6b3e0
movaps -0x50(%rbp), %xmm5
movss -0x34(%rbp), %xmm0
movss -0x30(%rbp), %xmm2
movaps -0x60(%rbp), %xmm3
xorl %r12d, %r12d
cmpq %r13, %rax
sete %r12b
addq %rax, %r12
cmpq %r12, %r13
jae 0x6b6b6
movsbl (%r13), %eax
movl %eax, -0x38(%rbp)
testl %eax, %eax
js 0x6b644
leaq 0x1(%r13), %rbx
jmp 0x6b67a
movaps %xmm5, -0x50(%rbp)
leaq -0x38(%rbp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x33a81
movslq %eax, %rbx
addq %r13, %rbx
movl -0x38(%rbp), %eax
testl %eax, %eax
je 0x6b733
movaps -0x60(%rbp), %xmm3
movss -0x30(%rbp), %xmm2
movss -0x34(%rbp), %xmm0
movaps -0x50(%rbp), %xmm5
cmpl $0x1f, %eax
ja 0x6b689
cmpl $0xd, %eax
je 0x6b700
cmpl $0xa, %eax
je 0x6b708
movl %eax, %ecx
shlq $0x2, %rcx
addq 0x8(%r14), %rcx
cmpl (%r14), %eax
cmovgeq -0x70(%rbp), %rcx
movss (%rcx), %xmm1
mulss %xmm0, %xmm1
addss %xmm5, %xmm1
ucomiss -0x3c(%rbp), %xmm1
jb 0x6b5d4
jmp 0x6b73e
ucomiss %xmm3, %xmm5
jbe 0x6b6bf
movss %xmm5, %xmm3            # xmm3 = xmm5[0],xmm3[1,2,3]
movaps %xmm3, %xmm1
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
addss -0x2c(%rbp), %xmm3
movlhps %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
shufps $0xe2, %xmm1, %xmm3     # xmm3 = xmm3[2,0],xmm1[2,3]
movq %r13, %rbx
cmpq %r15, %r13
jae 0x6b6f5
movzbl (%rbx), %eax
leaq 0x1(%rbx), %r13
cmpl $0x9, %eax
je 0x6b6d2
cmpl $0x20, %eax
je 0x6b6d2
leaq 0x1(%rbx), %rcx
cmpb $0xa, %al
cmoveq %rcx, %rbx
xorl %r12d, %r12d
xorps %xmm1, %xmm1
jmp 0x6b5d0
movaps %xmm5, %xmm1
jmp 0x6b5d0
movaps %xmm5, %xmm4
cmpless %xmm3, %xmm4
movaps %xmm4, %xmm1
andps %xmm3, %xmm1
andnps %xmm5, %xmm4
orps %xmm1, %xmm4
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
addss -0x2c(%rbp), %xmm3
unpcklps %xmm3, %xmm4            # xmm4 = xmm4[0],xmm3[0],xmm4[1],xmm3[1]
movaps %xmm4, %xmm3
xorps %xmm1, %xmm1
jmp 0x6b5d0
movq %rbx, %r13
movaps -0x60(%rbp), %xmm3
movaps -0x50(%rbp), %xmm5
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x6b74a
movq %r13, (%rax)
ucomiss %xmm3, %xmm5
jbe 0x6b753
movss %xmm5, %xmm3            # xmm3 = xmm5[0],xmm3[1,2,3]
movaps %xmm3, %xmm0
shufps $0x55, %xmm3, %xmm0     # xmm0 = xmm0[1,1],xmm3[1,1]
xorps %xmm1, %xmm1
movaps %xmm0, %xmm2
cmpeqps %xmm1, %xmm2
cmpltps %xmm5, %xmm1
orps %xmm2, %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x6b778
movaps %xmm3, %xmm0
jmp 0x6b784
addss -0x2c(%rbp), %xmm0
movlhps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
shufps $0xe2, %xmm3, %xmm0     # xmm0 = xmm0[2,0],xmm3[2,3]
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop