// File: Ciaxur[P]yagb-emu/include/imgui/imgui_draw.cpp
// Function: ImFont::BuildLookupTable()
void ImFont::BuildLookupTable()
{
    int max_codepoint = 0;
    for (int i = 0; i != Glyphs.Size; i++)
        max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint);

    // Build lookup table
    IM_ASSERT(Glyphs.Size < 0xFFFF); // -1 is reserved
    IndexAdvanceX.clear();
    IndexLookup.clear();
    DirtyLookupTables = false;
    memset(Used4kPagesMap, 0, sizeof(Used4kPagesMap));
    GrowIndex(max_codepoint + 1);
    for (int i = 0; i < Glyphs.Size; i++)
    {
        int codepoint = (int)Glyphs[i].Codepoint;
        IndexAdvanceX[codepoint] = Glyphs[i].AdvanceX;
        IndexLookup[codepoint] = (ImWchar)i;

        // Mark 4K page as used
        const int page_n = codepoint / 4096;
        Used4kPagesMap[page_n >> 3] |= 1 << (page_n & 7);
    }

    // Create a glyph to handle TAB
    // FIXME: Needs proper TAB handling but it needs to be contextualized (or we could arbitrary say that each string starts at "column 0" ?)
    if (FindGlyph((ImWchar)' '))
    {
        if (Glyphs.back().Codepoint != '\t')   // So we can call this function multiple times (FIXME: Flaky)
            Glyphs.resize(Glyphs.Size + 1);
        ImFontGlyph& tab_glyph = Glyphs.back();
        tab_glyph = *FindGlyph((ImWchar)' ');
        tab_glyph.Codepoint = '\t';
        tab_glyph.AdvanceX *= IM_TABSIZE;
        IndexAdvanceX[(int)tab_glyph.Codepoint] = (float)tab_glyph.AdvanceX;
        IndexLookup[(int)tab_glyph.Codepoint] = (ImWchar)(Glyphs.Size - 1);
    }

    // Mark special glyphs as not visible (note that AddGlyph already mark as non-visible glyphs with zero-size polygons)
    SetGlyphVisible((ImWchar)' ', false);
    SetGlyphVisible((ImWchar)'\t', false);

    // Setup fall-backs
    FallbackGlyph = FindGlyphNoFallback(FallbackChar);
    FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f;
    for (int i = 0; i < max_codepoint + 1; i++)
        if (IndexAdvanceX[i] < 0.0f)
            IndexAdvanceX[i] = FallbackAdvanceX;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movq -0x38(%rbp), %rcx
movl -0x10(%rbp), %eax
cmpl 0x28(%rcx), %eax
je 0x11b632
movq -0x38(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x3c(%rbp)
addq $0x28, %rdi
movl -0x10(%rbp), %esi
callq 0x1285e0
movl -0x3c(%rbp), %edi
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x118ff0
movl %eax, -0xc(%rbp)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x11b5f2
movq -0x38(%rbp), %rax
cmpl $0xffff, 0x28(%rax)     # imm = 0xFFFF
jge 0x11b641
jmp 0x11b660
leaq 0x6f751(%rip), %rdi     # 0x18ad99
leaq 0x6eb27(%rip), %rsi     # 0x18a176
movl $0xb57, %edx            # imm = 0xB57
leaq 0x6f753(%rip), %rcx     # 0x18adae
callq 0xd180
movq -0x38(%rbp), %rdi
callq 0xe92d0
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
callq 0xedfe0
movq -0x38(%rbp), %rdi
movb $0x0, 0x56(%rdi)
addq $0x68, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xd2f0
movq -0x38(%rbp), %rdi
movl -0xc(%rbp), %esi
addl $0x1, %esi
callq 0x11bec0
movl $0x0, -0x14(%rbp)
movq -0x38(%rbp), %rcx
movl -0x14(%rbp), %eax
cmpl 0x28(%rcx), %eax
jge 0x11b75e
movq -0x38(%rbp), %rdi
addq $0x28, %rdi
movl -0x14(%rbp), %esi
callq 0x1285e0
movq -0x38(%rbp), %rdi
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x18(%rbp)
addq $0x28, %rdi
movl -0x14(%rbp), %esi
callq 0x1285e0
movq -0x38(%rbp), %rdi
movss 0x4(%rax), %xmm0
movss %xmm0, -0x44(%rbp)
movl -0x18(%rbp), %esi
callq 0xea950
movss -0x44(%rbp), %xmm0
movq -0x38(%rbp), %rdi
movss %xmm0, (%rax)
movl -0x14(%rbp), %eax
movw %ax, -0x3e(%rbp)
addq $0x18, %rdi
movl -0x18(%rbp), %esi
callq 0x1094a0
movw -0x3e(%rbp), %cx
movw %cx, (%rax)
movl -0x18(%rbp), %eax
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, -0x1c(%rbp)
movl -0x1c(%rbp), %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movl -0x1c(%rbp), %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
movzbl 0x68(%rax,%rcx), %edx
orl %esi, %edx
movb %dl, 0x68(%rax,%rcx)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x11b6a4
movq -0x38(%rbp), %rdi
movl $0x20, %esi
callq 0x11bf50
cmpq $0x0, %rax
je 0x11b852
movq -0x38(%rbp), %rdi
addq $0x28, %rdi
callq 0x128660
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x9, %eax
je 0x11b7a5
movq -0x38(%rbp), %rax
movq %rax, %rdi
addq $0x28, %rdi
movl 0x28(%rax), %esi
addl $0x1, %esi
callq 0x1286c0
movq -0x38(%rbp), %rdi
addq $0x28, %rdi
callq 0x128660
movq -0x38(%rbp), %rdi
movq %rax, -0x28(%rbp)
movl $0x20, %esi
callq 0x11bf50
movq %rax, %rsi
movq -0x28(%rbp), %rdi
movl $0x28, %edx
callq 0xd0a0
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $0x80000000, %ecx       # imm = 0x80000000
orl $0x9, %ecx
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
movss 0x56f3e(%rip), %xmm0    # 0x172734
mulss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%rax)
movq -0x28(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0xea950
movss -0x4c(%rbp), %xmm0
movq -0x38(%rbp), %rdi
movss %xmm0, (%rax)
movl 0x28(%rdi), %eax
subl $0x1, %eax
movw %ax, -0x46(%rbp)
addq $0x18, %rdi
movq -0x28(%rbp), %rax
movl (%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x1094a0
movw -0x46(%rbp), %cx
movw %cx, (%rax)
movq -0x38(%rbp), %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x11bfd0
movq -0x38(%rbp), %rdi
movl $0x9, %esi
xorl %edx, %edx
callq 0x11bfd0
movq -0x38(%rbp), %rax
movq %rax, %rdi
movzwl 0x52(%rax), %esi
callq 0x11b920
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
cmpq $0x0, 0x38(%rax)
je 0x11b8a8
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0x50(%rbp)
jmp 0x11b8b2
xorps %xmm0, %xmm0
movss %xmm0, -0x50(%rbp)
jmp 0x11b8b2
movq -0x38(%rbp), %rax
movss -0x50(%rbp), %xmm0
movss %xmm0, 0x10(%rax)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl -0xc(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x11b914
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0xea950
xorps %xmm0, %xmm0
ucomiss (%rax), %xmm0
jbe 0x11b907
movq -0x38(%rbp), %rdi
movss 0x10(%rdi), %xmm0
movss %xmm0, -0x54(%rbp)
movl -0x2c(%rbp), %esi
callq 0xea950
movss -0x54(%rbp), %xmm0
movss %xmm0, (%rax)
jmp 0x11b909
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x11b8c7
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)