// File: donfreiday[P]gb/./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 (ImCharIsSpace(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)
                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 < IndexXAdvance.Size ? IndexXAdvance[(int)c] : FallbackXAdvance) * 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 0x45f58
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rdi
callq 0x7330
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 (%rax), %xmm0
movss %xmm0, -0x40(%rbp)
leaq -0x8(%rbp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x2d280
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 0x461bc
testb $0x1, -0x45(%rbp)
je 0x460a1
cmpq $0x0, -0x50(%rbp)
jne 0x46009
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 0x45c30
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x46007
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x46009
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x4609f
movss -0x8(%rbp), %xmm1
movss -0x44(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x46030
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 0x4609a
movq -0x58(%rbp), %rax
movb (%rax), %al
movb %al, -0x59(%rbp)
movsbl -0x59(%rbp), %edi
callq 0x45ec0
testb $0x1, %al
jne 0x46071
jmp 0x4607f
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x46098
movsbl -0x59(%rbp), %eax
cmpl $0xa, %eax
jne 0x46096
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x4609a
jmp 0x4609a
jmp 0x4604f
jmp 0x45fae
jmp 0x460a1
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x80, -0x6c(%rbp)
jae 0x460ca
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x460f8
movq -0x58(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x6c(%rbp), %rdi
callq 0x9850
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x6c(%rbp)
jne 0x460f6
jmp 0x461bc
jmp 0x460f8
cmpl $0x20, -0x6c(%rbp)
jae 0x46141
cmpl $0xa, -0x6c(%rbp)
jne 0x46134
movss -0x8(%rbp), %xmm0
movss -0x44(%rbp), %xmm1
callq 0x40d30
movss %xmm0, -0x8(%rbp)
movss -0x3c(%rbp), %xmm0
addss -0x4(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x44(%rbp)
jmp 0x45fae
cmpl $0xd, -0x6c(%rbp)
jne 0x4613f
jmp 0x45fae
jmp 0x46141
movq -0x78(%rbp), %rcx
movl -0x6c(%rbp), %eax
cmpl 0x58(%rcx), %eax
jge 0x4616b
movq -0x78(%rbp), %rdi
addq $0x58, %rdi
movl -0x6c(%rbp), %esi
callq 0x31610
movss (%rax), %xmm0
movss %xmm0, -0x84(%rbp)
jmp 0x4617c
movq -0x78(%rbp), %rax
movss 0x50(%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 0x461a8
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x461bc
movss -0x70(%rbp), %xmm0
addss -0x44(%rbp), %xmm0
movss %xmm0, -0x44(%rbp)
jmp 0x45fae
movss -0x8(%rbp), %xmm1
movss -0x44(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x461d5
movss -0x44(%rbp), %xmm0
movss %xmm0, -0x8(%rbp)
movss -0x44(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
ja 0x461f1
movss -0x4(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x46200
jp 0x46200
movss -0x3c(%rbp), %xmm0
addss -0x4(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x46212
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movsd -0x8(%rbp), %xmm0
addq $0x90, %rsp
popq %rbp
retq