// File: 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
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movss %xmm0, -0x14(%rbp)
movss %xmm1, -0x18(%rbp)
movss %xmm2, -0x1c(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x11c578
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rdi
callq 0xd410
movq %rax, %rcx
movq -0x80(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x78(%rbp), %rax
movss -0x14(%rbp), %xmm0
movss %xmm0, -0x3c(%rbp)
movss -0x14(%rbp), %xmm0
divss 0x14(%rax), %xmm0
movss %xmm0, -0x40(%rbp)
leaq -0x8(%rbp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0xbf4c0
xorps %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x1c(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
seta %al
andb $0x1, %al
movb %al, -0x45(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x11c7d8
testb $0x1, -0x45(%rbp)
je 0x11c6c2
cmpq $0x0, -0x50(%rbp)
jne 0x11c62a
movq -0x78(%rbp), %rdi
movss -0x40(%rbp), %xmm0
movq -0x58(%rbp), %rsi
movq -0x30(%rbp), %rdx
movss -0x1c(%rbp), %xmm1
subss -0x44(%rbp), %xmm1
callq 0x11c240
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x11c628
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x11c62a
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x11c6c0
movss -0x8(%rbp), %xmm1
movss -0x44(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11c651
movss -0x44(%rbp), %xmm0
movss %xmm0, -0x8(%rbp)
movss -0x3c(%rbp), %xmm0
addss -0x4(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x11c6bb
movq -0x58(%rbp), %rax
movb (%rax), %al
movb %al, -0x59(%rbp)
movsbl -0x59(%rbp), %edi
callq 0x11c840
testb $0x1, %al
jne 0x11c692
jmp 0x11c6a0
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x11c6b9
movsbl -0x59(%rbp), %eax
cmpl $0xa, %eax
jne 0x11c6b7
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x11c6bb
jmp 0x11c6bb
jmp 0x11c670
jmp 0x11c5cf
jmp 0x11c6c2
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x80, -0x6c(%rbp)
jae 0x11c6eb
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x11c719
movq -0x58(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x6c(%rbp), %rdi
callq 0xc36d0
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x6c(%rbp)
jne 0x11c717
jmp 0x11c7d8
jmp 0x11c719
cmpl $0x20, -0x6c(%rbp)
jae 0x11c762
cmpl $0xa, -0x6c(%rbp)
jne 0x11c755
movss -0x8(%rbp), %xmm0
movss -0x44(%rbp), %xmm1
callq 0x10f610
movss %xmm0, -0x8(%rbp)
movss -0x3c(%rbp), %xmm0
addss -0x4(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
jmp 0x11c5cf
cmpl $0xd, -0x6c(%rbp)
jne 0x11c760
jmp 0x11c5cf
jmp 0x11c762
movq -0x78(%rbp), %rcx
movl -0x6c(%rbp), %eax
cmpl (%rcx), %eax
jge 0x11c787
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x6c(%rbp), %ecx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x84(%rbp)
jmp 0x11c798
movq -0x78(%rbp), %rax
movss 0x10(%rax), %xmm0
movss %xmm0, -0x84(%rbp)
movss -0x84(%rbp), %xmm0
mulss -0x40(%rbp), %xmm0
movss %xmm0, -0x70(%rbp)
movss -0x44(%rbp), %xmm0
addss -0x70(%rbp), %xmm0
ucomiss -0x18(%rbp), %xmm0
jb 0x11c7c4
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x11c7d8
movss -0x70(%rbp), %xmm0
addss -0x44(%rbp), %xmm0
movss %xmm0, -0x44(%rbp)
jmp 0x11c5cf
movss -0x8(%rbp), %xmm1
movss -0x44(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11c7f1
movss -0x44(%rbp), %xmm0
movss %xmm0, -0x8(%rbp)
movss -0x44(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
ja 0x11c80d
movss -0x4(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x11c81c
jp 0x11c81c
movss -0x3c(%rbp), %xmm0
addss -0x4(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x11c82e
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movsd -0x8(%rbp), %xmm0
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)