// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rdx, %r15
movq %rsi, %r13
movss %xmm1, -0x3c(%rbp)
movss %xmm0, -0x2c(%rbp)
movq %rdi, %r14
testq %rdx, %rdx
movss %xmm2, -0x30(%rbp)
jne 0x835e0
movq %r13, %rdi
callq 0xc350
movss -0x30(%rbp), %xmm2
movq %rax, %r15
addq %r13, %r15
movq %rbx, -0x68(%rbp)
cmpq %r13, %r15
jbe 0x83797
movss -0x2c(%rbp), %xmm0
divss 0x14(%r14), %xmm0
movss %xmm0, -0x38(%rbp)
leaq 0x10(%r14), %r12
xorps %xmm3, %xmm3
xorps %xmm5, %xmm5
xorl %ebx, %ebx
xorps %xmm4, %xmm4
ucomiss %xmm5, %xmm2
jbe 0x8365c
testq %rbx, %rbx
jne 0x83653
movaps %xmm2, %xmm1
subss %xmm4, %xmm1
movq %r14, %rdi
movss -0x38(%rbp), %xmm0
movq %r13, %rsi
movq %r15, %rdx
movaps %xmm3, -0x60(%rbp)
movaps %xmm4, -0x50(%rbp)
callq 0x83404
xorps %xmm5, %xmm5
movaps -0x50(%rbp), %xmm4
movss -0x30(%rbp), %xmm2
movaps -0x60(%rbp), %xmm3
xorl %ebx, %ebx
cmpq %r13, %rax
sete %bl
addq %rax, %rbx
cmpq %rbx, %r13
jae 0x8371a
movsbl (%r13), %eax
movl %eax, -0x34(%rbp)
testl %eax, %eax
js 0x8366e
leaq 0x1(%r13), %rax
jmp 0x836a4
movaps %xmm4, -0x50(%rbp)
movaps %xmm3, -0x60(%rbp)
leaq -0x34(%rbp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x41855
cltq
addq %r13, %rax
cmpl $0x0, -0x34(%rbp)
je 0x83773
movaps -0x60(%rbp), %xmm3
movss -0x30(%rbp), %xmm2
movaps -0x50(%rbp), %xmm4
xorps %xmm5, %xmm5
movl -0x34(%rbp), %ecx
cmpq $0x1f, %rcx
ja 0x836e9
cmpq $0xd, %rcx
je 0x8376f
cmpl $0xa, %ecx
jne 0x836e9
movaps %xmm4, %xmm0
cmpless %xmm3, %xmm0
movaps %xmm0, %xmm1
andps %xmm3, %xmm1
andnps %xmm4, %xmm0
orps %xmm1, %xmm0
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
addss -0x2c(%rbp), %xmm3
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
xorps %xmm4, %xmm4
xorl %ecx, %ecx
movaps %xmm0, %xmm3
jmp 0x83785
movq %rcx, %rdx
shlq $0x2, %rdx
addq 0x8(%r14), %rdx
cmpl (%r14), %ecx
cmovgeq %r12, %rdx
movss (%rdx), %xmm0
mulss -0x38(%rbp), %xmm0
addss %xmm4, %xmm0
ucomiss -0x3c(%rbp), %xmm0
setae %cl
cmovaeq %r13, %rax
jae 0x83785
movaps %xmm0, %xmm4
jmp 0x83785
ucomiss %xmm3, %xmm4
jbe 0x83723
movss %xmm4, %xmm3            # xmm3 = xmm4[0],xmm3[1,2,3]
movaps %xmm3, %xmm0
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
addss -0x2c(%rbp), %xmm3
movlhps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
shufps $0xe2, %xmm0, %xmm3     # xmm3 = xmm3[2,0],xmm0[2,3]
xorps %xmm4, %xmm4
cmpq %r15, %r13
jae 0x8376b
movq %r13, %rcx
movzbl (%r13), %eax
cmpl $0x20, %eax
sete %dl
leal -0x9(%rax), %esi
cmpb $0x2, %sil
setb %sil
orb %dl, %sil
movzbl %sil, %r13d
addq %rcx, %r13
cmpl $0x20, %eax
je 0x83739
cmpl $0x9, %eax
je 0x83739
xorl %ebx, %ebx
jmp 0x8378c
xorl %ecx, %ecx
jmp 0x83785
movb $0x1, %cl
movaps -0x60(%rbp), %xmm3
movss -0x30(%rbp), %xmm2
movaps -0x50(%rbp), %xmm4
xorps %xmm5, %xmm5
movq %rax, %r13
testb %cl, %cl
jne 0x8379d
cmpq %r15, %r13
jb 0x8360c
jmp 0x8379d
xorps %xmm4, %xmm4
xorps %xmm3, %xmm3
movq -0x68(%rbp), %rax
testq %rax, %rax
je 0x837a9
movq %r13, (%rax)
ucomiss %xmm3, %xmm4
jbe 0x837b2
movss %xmm4, %xmm3            # xmm3 = xmm4[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 %xmm4, %xmm1
orps %xmm2, %xmm1
movd %xmm1, %eax
testb $0x1, %al
jne 0x837d7
movaps %xmm3, %xmm0
jmp 0x837e3
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