// File: /workspace/github_cpp_makefile_star2_O2/Ciaxur[P]yagb-emu/include/imgui/imgui.cpp
// Function: ImGui::NavScoreItem(ImGuiNavMoveResult*, ImRect)
static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand)
{
    ImGuiContext& g = *GImGui;
    ImGuiWindow* window = g.CurrentWindow;
    if (g.NavLayer != window->DC.NavLayerCurrent)
        return false;

    const ImRect& curr = g.NavScoringRect; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width)
    g.NavScoringCount++;

    // When entering through a NavFlattened border, we consider child window items as fully clipped for scoring
    if (window->ParentWindow == g.NavWindow)
    {
        IM_ASSERT((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened);
        if (!window->ClipRect.Overlaps(cand))
            return false;
        cand.ClipWithFull(window->ClipRect); // This allows the scored item to not overlap other candidates in the parent window
    }

    // We perform scoring on items bounding box clipped by the current clipping rectangle on the other axis (clipping on our movement axis would give us equal scores for all clipped items)
    // For example, this ensure that items in one column are not reached when moving vertically from items in another column.
    NavClampRectToVisibleAreaForMoveDir(g.NavMoveClipDir, cand, window->ClipRect);

    // Compute distance between boxes
    // FIXME-NAV: Introducing biases for vertical navigation, needs to be removed.
    float dbx = NavScoreItemDistInterval(cand.Min.x, cand.Max.x, curr.Min.x, curr.Max.x);
    float dby = NavScoreItemDistInterval(ImLerp(cand.Min.y, cand.Max.y, 0.2f), ImLerp(cand.Min.y, cand.Max.y, 0.8f), ImLerp(curr.Min.y, curr.Max.y, 0.2f), ImLerp(curr.Min.y, curr.Max.y, 0.8f)); // Scale down on Y to keep using box-distance for vertically touching items
    if (dby != 0.0f && dbx != 0.0f)
        dbx = (dbx / 1000.0f) + ((dbx > 0.0f) ? +1.0f : -1.0f);
    float dist_box = ImFabs(dbx) + ImFabs(dby);

    // Compute distance between centers (this is off by a factor of 2, but we only compare center distances with each other so it doesn't matter)
    float dcx = (cand.Min.x + cand.Max.x) - (curr.Min.x + curr.Max.x);
    float dcy = (cand.Min.y + cand.Max.y) - (curr.Min.y + curr.Max.y);
    float dist_center = ImFabs(dcx) + ImFabs(dcy); // L1 metric (need this for our connectedness guarantee)

    // Determine which quadrant of 'curr' our candidate item 'cand' lies in based on distance
    ImGuiDir quadrant;
    float dax = 0.0f, day = 0.0f, dist_axial = 0.0f;
    if (dbx != 0.0f || dby != 0.0f)
    {
        // For non-overlapping boxes, use distance between boxes
        dax = dbx;
        day = dby;
        dist_axial = dist_box;
        quadrant = ImGetDirQuadrantFromDelta(dbx, dby);
    }
    else if (dcx != 0.0f || dcy != 0.0f)
    {
        // For overlapping boxes with different centers, use distance between centers
        dax = dcx;
        day = dcy;
        dist_axial = dist_center;
        quadrant = ImGetDirQuadrantFromDelta(dcx, dcy);
    }
    else
    {
        // Degenerate case: two overlapping buttons with same center, break ties arbitrarily (note that LastItemId here is really the _previous_ item order, but it doesn't matter)
        quadrant = (window->DC.LastItemId < g.NavId) ? ImGuiDir_Left : ImGuiDir_Right;
    }

#if IMGUI_DEBUG_NAV_SCORING
    char buf[128];
    if (IsMouseHoveringRect(cand.Min, cand.Max))
    {
        ImFormatString(buf, IM_ARRAYSIZE(buf), "dbox (%.2f,%.2f->%.4f)\ndcen (%.2f,%.2f->%.4f)\nd (%.2f,%.2f->%.4f)\nnav %c, quadrant %c", dbx, dby, dist_box, dcx, dcy, dist_center, dax, day, dist_axial, "WENS"[g.NavMoveDir], "WENS"[quadrant]);
        ImDrawList* draw_list = GetForegroundDrawList(window);
        draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255,200,0,100));
        draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255,255,0,200));
        draw_list->AddRectFilled(cand.Max - ImVec2(4, 4), cand.Max + CalcTextSize(buf) + ImVec2(4, 4), IM_COL32(40,0,0,150));
        draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Max, ~0U, buf);
    }
    else if (g.IO.KeyCtrl) // Hold to preview score in matching quadrant. Press C to rotate.
    {
        if (IsKeyPressedMap(ImGuiKey_C)) { g.NavMoveDirLast = (ImGuiDir)((g.NavMoveDirLast + 1) & 3); g.IO.KeysDownDuration[g.IO.KeyMap[ImGuiKey_C]] = 0.01f; }
        if (quadrant == g.NavMoveDir)
        {
            ImFormatString(buf, IM_ARRAYSIZE(buf), "%.0f/%.0f", dist_box, dist_center);
            ImDrawList* draw_list = GetForegroundDrawList(window);
            draw_list->AddRectFilled(cand.Min, cand.Max, IM_COL32(255, 0, 0, 200));
            draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Min, IM_COL32(255, 255, 255, 255), buf);
        }
    }
#endif

    // Is it in the quadrant we're interesting in moving to?
    bool new_best = false;
    if (quadrant == g.NavMoveDir)
    {
        // Does it beat the current best candidate?
        if (dist_box < result->DistBox)
        {
            result->DistBox = dist_box;
            result->DistCenter = dist_center;
            return true;
        }
        if (dist_box == result->DistBox)
        {
            // Try using distance between center points to break ties
            if (dist_center < result->DistCenter)
            {
                result->DistCenter = dist_center;
                new_best = true;
            }
            else if (dist_center == result->DistCenter)
            {
                // Still tied! we need to be extra-careful to make sure everything gets linked properly. We consistently break ties by symbolically moving "later" items
                // (with higher index) to the right/downwards by an infinitesimal amount since we the current "best" button already (so it must have a lower index),
                // this is fairly easy. This rule ensures that all buttons with dx==dy==0 will end up being linked in order of appearance along the x axis.
                if (((g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? dby : dbx) < 0.0f) // moving bj to the right/down decreases distance
                    new_best = true;
            }
        }
    }

    // Axial check: if 'curr' has no link at all in some direction and 'cand' lies roughly in that direction, add a tentative link. This will only be kept if no "real" matches
    // are found, so it only augments the graph produced by the above method using extra links. (important, since it doesn't guarantee strong connectedness)
    // This is just to avoid buttons having no links in a particular direction when there's a suitable neighbor. you get good graphs without this too.
    // 2017/09/29: FIXME: This now currently only enabled inside menu bars, ideally we'd disable it everywhere. Menus in particular need to catch failure. For general navigation it feels awkward.
    // Disabling it may lead to disconnected graphs when nodes are very spaced out on different axis. Perhaps consider offering this as an option?
    if (result->DistBox == FLT_MAX && dist_axial < result->DistAxial)  // Check axial match
        if (g.NavLayer == ImGuiNavLayer_Menu && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu))
            if ((g.NavMoveDir == ImGuiDir_Left && dax < 0.0f) || (g.NavMoveDir == ImGuiDir_Right && dax > 0.0f) || (g.NavMoveDir == ImGuiDir_Up && day < 0.0f) || (g.NavMoveDir == ImGuiDir_Down && day > 0.0f))
            {
                result->DistAxial = dist_axial;
                new_best = true;
            }

    return new_best;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, -0x30(%rbp)
movq 0x9a878(%rip), %r15     # 0xe3ce0
movq 0x1ae0(%r15), %r12
movl 0x1cdc(%r15), %eax
cmpl 0x14c(%r12), %eax
jne 0x494f4
movq %rdi, %rbx
incl 0x1cd8(%r15)
movq 0x1c90(%r15), %rax
cmpq %rax, 0x388(%r12)
jne 0x494d1
movl 0xc(%rax), %eax
orl 0xc(%r12), %eax
btl $0x17, %eax
jae 0x4980b
leaq 0x258(%r12), %r14
leaq -0x30(%rbp), %rsi
movq %r14, %rdi
callq 0x4a2f6
testb %al, %al
je 0x494f4
leaq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x4b582
leaq 0x258(%r12), %rdx
cmpl $0x1, 0x1d18(%r15)
ja 0x49503
leaq -0x2c(%rbp), %rcx
leaq 0x25c(%r12), %rsi
pushq $0xc
popq %rax
jmp 0x4950d
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq -0x30(%rbp), %rcx
pushq $0x8
popq %rax
movq %rdx, %rsi
movss (%rcx), %xmm0
movss (%rsi), %xmm1
movss (%rdx,%rax), %xmm2
movaps %xmm2, %xmm3
minss %xmm0, %xmm3
cmpltss %xmm1, %xmm0
movaps %xmm0, %xmm4
andnps %xmm3, %xmm4
andps %xmm1, %xmm0
orps %xmm4, %xmm0
movss %xmm0, (%rcx)
movss -0x30(%rbp,%rax), %xmm0
minss %xmm0, %xmm2
cmpltss %xmm1, %xmm0
movaps %xmm0, %xmm3
andnps %xmm2, %xmm3
andps %xmm1, %xmm0
orps %xmm3, %xmm0
movss %xmm0, -0x30(%rbp,%rax)
movss -0x30(%rbp), %xmm0
movss -0x28(%rbp), %xmm5
movss 0x1cc8(%r15), %xmm4
movss 0x1cd0(%r15), %xmm6
ucomiss %xmm5, %xmm4
jbe 0x49581
movaps %xmm5, %xmm2
subss %xmm4, %xmm2
jmp 0x49590
xorps %xmm2, %xmm2
ucomiss %xmm6, %xmm0
jbe 0x49590
movaps %xmm0, %xmm2
subss %xmm6, %xmm2
movss -0x2c(%rbp), %xmm1
movss -0x24(%rbp), %xmm8
movaps %xmm8, %xmm7
subss %xmm1, %xmm7
movss 0x52a68(%rip), %xmm13   # 0x9c014
movss 0x53373(%rip), %xmm11   # 0x9c928
movaps %xmm7, %xmm3
mulss %xmm11, %xmm3
addss %xmm1, %xmm3
movss 0x1ccc(%r15), %xmm9
movss 0x1cd4(%r15), %xmm10
movaps %xmm10, %xmm12
subss %xmm9, %xmm12
movaps %xmm12, %xmm14
mulss %xmm13, %xmm14
addss %xmm9, %xmm14
ucomiss %xmm3, %xmm14
jbe 0x495f7
subss %xmm14, %xmm3
jmp 0x49618
mulss %xmm13, %xmm7
addss %xmm1, %xmm7
mulss %xmm11, %xmm12
addss %xmm9, %xmm12
ucomiss %xmm12, %xmm7
jbe 0x4965b
subss %xmm12, %xmm7
movaps %xmm7, %xmm3
xorps %xmm7, %xmm7
movaps %xmm3, %xmm11
cmpneqss %xmm7, %xmm11
movd %xmm11, %eax
andl $0x1, %eax
ucomiss %xmm7, %xmm2
jne 0x49634
jnp 0x49660
ucomiss %xmm7, %xmm3
jne 0x4963b
jnp 0x49660
xorl %eax, %eax
ucomiss %xmm7, %xmm2
seta %al
divss 0x529a9(%rip), %xmm2    # 0x9bff4
leaq 0x53b12(%rip), %rcx     # 0x9d164
addss (%rcx,%rax,4), %xmm2
movb $0x1, %al
jmp 0x49660
xorps %xmm3, %xmm3
xorl %eax, %eax
movaps 0x53188(%rip), %xmm11   # 0x9c7f0
movaps %xmm2, %xmm12
andps %xmm11, %xmm12
movaps %xmm3, %xmm13
andps %xmm11, %xmm13
movaps %xmm13, %xmm7
addss %xmm12, %xmm7
addss %xmm5, %xmm0
addss %xmm6, %xmm4
subss %xmm4, %xmm0
addss %xmm8, %xmm1
addss %xmm10, %xmm9
subss %xmm9, %xmm1
movaps %xmm0, %xmm6
andps %xmm11, %xmm6
andps %xmm1, %xmm11
movaps %xmm6, %xmm5
addss %xmm11, %xmm5
testb %al, %al
jne 0x496e7
xorps %xmm4, %xmm4
ucomiss %xmm4, %xmm2
jne 0x496e7
jp 0x496e7
ucomiss %xmm4, %xmm0
jne 0x4970f
jp 0x4970f
ucomiss %xmm4, %xmm1
jne 0x4970f
jp 0x4970f
movl 0x124(%r12), %ecx
xorl %eax, %eax
cmpl 0x1c98(%r15), %ecx
setae %al
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
jmp 0x4972c
xorps %xmm0, %xmm0
xorl %eax, %eax
ucomiss %xmm0, %xmm2
seta %al
xorl %ecx, %ecx
ucomiss %xmm0, %xmm3
seta %cl
orl $0x2, %ecx
ucomiss %xmm13, %xmm12
cmovbel %ecx, %eax
movaps %xmm2, %xmm0
movaps %xmm3, %xmm1
movaps %xmm7, %xmm4
jmp 0x4972c
xorl %eax, %eax
ucomiss %xmm4, %xmm0
seta %al
xorl %ecx, %ecx
ucomiss %xmm4, %xmm1
seta %cl
orl $0x2, %ecx
ucomiss %xmm11, %xmm6
cmovbel %ecx, %eax
movaps %xmm5, %xmm4
movl 0x1d10(%r15), %ecx
movss 0x10(%rbx), %xmm6
cmpl %ecx, %eax
jne 0x49784
ucomiss %xmm7, %xmm6
jbe 0x49752
movss %xmm7, 0x10(%rbx)
movss %xmm5, 0x14(%rbx)
movb $0x1, %al
jmp 0x494f6
ucomiss %xmm6, %xmm7
jne 0x49784
jp 0x49784
movss 0x14(%rbx), %xmm7
ucomiss %xmm5, %xmm7
jbe 0x4976a
movss %xmm5, 0x14(%rbx)
jmp 0x49780
ucomiss %xmm7, %xmm5
jne 0x49784
jp 0x49784
testb $0x2, %al
jne 0x49778
movaps %xmm2, %xmm3
xorps %xmm2, %xmm2
ucomiss %xmm3, %xmm2
jbe 0x49784
movb $0x1, %al
jmp 0x49786
xorl %eax, %eax
ucomiss 0x530e3(%rip), %xmm6    # 0x9c870
jne 0x494f6
jp 0x494f6
movss 0x18(%rbx), %xmm2
ucomiss %xmm4, %xmm2
jbe 0x494f6
cmpl $0x1, 0x1cdc(%r15)
jne 0x494f6
movq 0x1c90(%r15), %rdx
testb $0x10, 0xf(%rdx)
jne 0x494f6
testl %ecx, %ecx
jne 0x497d2
xorps %xmm2, %xmm2
ucomiss %xmm0, %xmm2
ja 0x49801
cmpl $0x1, %ecx
jne 0x497df
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm0
ja 0x49801
cmpl $0x2, %ecx
jne 0x497ec
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
ja 0x49801
cmpl $0x3, %ecx
jne 0x494f6
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x494f6
movss %xmm4, 0x18(%rbx)
jmp 0x4974b
leaq 0x579d0(%rip), %rdi     # 0xa11e2
leaq 0x5397c(%rip), %rsi     # 0x9d195
leaq 0x57a07(%rip), %rcx     # 0xa1227
movl $0x20fb, %edx           # imm = 0x20FB
callq 0xc140