// File: /workspace/github_cpp_makefile_star2_O3/Ciaxur[P]yagb-emu/include/imgui/imgui.cpp
// Function: ImGui::FindBestWindowPosForPopupEx(ImVec2 const&, ImVec2 const&, int*, ImRect const&, ImRect const&, ImGuiPopupPositionPolicy)
ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy)
{
    ImVec2 base_pos_clamped = ImClamp(ref_pos, r_outer.Min, r_outer.Max - size);
    //GetForegroundDrawList()->AddRect(r_avoid.Min, r_avoid.Max, IM_COL32(255,0,0,255));
    //GetForegroundDrawList()->AddRect(r_outer.Min, r_outer.Max, IM_COL32(0,255,0,255));

    // Combo Box policy (we want a connecting edge)
    if (policy == ImGuiPopupPositionPolicy_ComboBox)
    {
        const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Down, ImGuiDir_Right, ImGuiDir_Left, ImGuiDir_Up };
        for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++)
        {
            const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n];
            if (n != -1 && dir == *last_dir) // Already tried this direction?
                continue;
            ImVec2 pos;
            if (dir == ImGuiDir_Down)  pos = ImVec2(r_avoid.Min.x, r_avoid.Max.y);          // Below, Toward Right (default)
            if (dir == ImGuiDir_Right) pos = ImVec2(r_avoid.Min.x, r_avoid.Min.y - size.y); // Above, Toward Right
            if (dir == ImGuiDir_Left)  pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Max.y); // Below, Toward Left
            if (dir == ImGuiDir_Up)    pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Min.y - size.y); // Above, Toward Left
            if (!r_outer.Contains(ImRect(pos, pos + size)))
                continue;
            *last_dir = dir;
            return pos;
        }
    }

    // Tooltip and Default popup policy
    // (Always first try the direction we used on the last frame, if any)
    if (policy == ImGuiPopupPositionPolicy_Tooltip || policy == ImGuiPopupPositionPolicy_Default)
    {
        const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left };
        for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++)
        {
            const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n];
            if (n != -1 && dir == *last_dir) // Already tried this direction?
                continue;

            const float avail_w = (dir == ImGuiDir_Left ? r_avoid.Min.x : r_outer.Max.x) - (dir == ImGuiDir_Right ? r_avoid.Max.x : r_outer.Min.x);
            const float avail_h = (dir == ImGuiDir_Up ? r_avoid.Min.y : r_outer.Max.y) - (dir == ImGuiDir_Down ? r_avoid.Max.y : r_outer.Min.y);

            // If there not enough room on one axis, there's no point in positioning on a side on this axis (e.g. when not enough width, use a top/bottom position to maximize available width)
            if (avail_w < size.x && (dir == ImGuiDir_Left || dir == ImGuiDir_Right))
                continue;
            if (avail_h < size.y && (dir == ImGuiDir_Up || dir == ImGuiDir_Down))
                continue;

            ImVec2 pos;
            pos.x = (dir == ImGuiDir_Left) ? r_avoid.Min.x - size.x : (dir == ImGuiDir_Right) ? r_avoid.Max.x : base_pos_clamped.x;
            pos.y = (dir == ImGuiDir_Up) ? r_avoid.Min.y - size.y : (dir == ImGuiDir_Down) ? r_avoid.Max.y : base_pos_clamped.y;

            // Clamp top-left corner of popup
            pos.x = ImMax(pos.x, r_outer.Min.x);
            pos.y = ImMax(pos.y, r_outer.Min.y);

            *last_dir = dir;
            return pos;
        }
    }

    // Fallback when not enough room:
    *last_dir = ImGuiDir_None;

    // For tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible.
    if (policy == ImGuiPopupPositionPolicy_Tooltip)
        return ref_pos + ImVec2(2, 2);

    // Otherwise try to keep within display
    ImVec2 pos = ref_pos;
    pos.x = ImMax(ImMin(pos.x + size.x, r_outer.Max.x) - size.x, r_outer.Min.x);
    pos.y = ImMax(ImMin(pos.y + size.y, r_outer.Max.y) - size.y, r_outer.Min.y);
    return pos;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
movss 0x8(%rcx), %xmm6
movss 0xc(%rcx), %xmm4
movss (%rsi), %xmm5
movss 0x4(%rsi), %xmm3
movss (%rdi), %xmm7
movss 0x4(%rdi), %xmm8
movss (%rcx), %xmm2
movss 0x4(%rcx), %xmm1
cmpl $0x1, %r9d
jne 0x57941
movl (%rdx), %eax
xorl %r10d, %r10d
xorl %ecx, %ecx
cmpl $-0x1, %eax
setne %r10b
sete %cl
movss 0x8(%r8), %xmm9
subss %xmm5, %xmm9
movss 0x4(%r8), %xmm10
subss %xmm3, %xmm10
movaps %xmm9, %xmm11
unpcklps %xmm10, %xmm11          # xmm11 = xmm11[0],xmm10[0],xmm11[1],xmm10[1]
movss 0xc(%r8), %xmm12
unpcklps %xmm12, %xmm9           # xmm9 = xmm9[0],xmm12[0],xmm9[1],xmm12[1]
movups (%r8), %xmm0
movlhps %xmm0, %xmm10           # xmm10 = xmm10[0],xmm0[0]
shufps $0xe2, %xmm0, %xmm10    # xmm10 = xmm10[2,0],xmm0[2,3]
movlhps %xmm0, %xmm12           # xmm12 = xmm12[0],xmm0[0]
shufps $0xe2, %xmm0, %xmm12    # xmm12 = xmm12[2,0],xmm0[2,3]
shll $0x2, %r10d
leaq 0x5df79(%rip), %rsi     # 0xb5830
subq %r10, %rsi
leaq 0x5e097(%rip), %r10     # 0xb5958
movq %rdx, %r11
testq %rcx, %rcx
cmovneq %rsi, %r11
movl (%r11), %r11d
je 0x578d5
cmpl %eax, %r11d
je 0x57934
cmpl $0x3, %r11d
ja 0x578ff
movl %r11d, %ebx
movslq (%r10,%rbx,4), %rbx
addq %r10, %rbx
jmpq *%rbx
movaps %xmm9, %xmm0
jmp 0x57902
movaps %xmm11, %xmm0
jmp 0x57902
movaps %xmm12, %xmm0
jmp 0x57902
movaps %xmm10, %xmm0
jmp 0x57902
xorps %xmm0, %xmm0
ucomiss %xmm2, %xmm0
jb 0x57934
movaps %xmm0, %xmm13
shufps $0x55, %xmm0, %xmm13    # xmm13 = xmm13[1,1],xmm0[1,1]
ucomiss %xmm1, %xmm13
jb 0x57934
movaps %xmm5, %xmm14
addss %xmm0, %xmm14
ucomiss %xmm14, %xmm6
jb 0x57934
addss %xmm3, %xmm13
ucomiss %xmm13, %xmm4
jae 0x57ad8
incq %rcx
addq $0x4, %rsi
cmpq $0x5, %rcx
jne 0x578c1
testl $0xfffffffd, %r9d       # imm = 0xFFFFFFFD
jne 0x57a5e
movaps %xmm6, %xmm0
subss %xmm5, %xmm0
movaps %xmm4, %xmm10
subss %xmm3, %xmm10
minss %xmm8, %xmm10
movaps %xmm8, %xmm9
cmpltss %xmm1, %xmm9
movaps %xmm9, %xmm11
andnps %xmm10, %xmm11
andps %xmm1, %xmm9
orps %xmm11, %xmm9
minss %xmm7, %xmm0
movaps %xmm7, %xmm10
cmpltss %xmm2, %xmm10
movaps %xmm10, %xmm11
andnps %xmm0, %xmm11
andps %xmm2, %xmm10
orps %xmm11, %xmm10
movl (%rdx), %ecx
xorl %eax, %eax
xorl %esi, %esi
cmpl $-0x1, %ecx
setne %al
sete %sil
movss (%r8), %xmm12
movss 0x4(%r8), %xmm0
movss 0x8(%r8), %xmm13
movss 0xc(%r8), %xmm11
shll $0x2, %eax
leaq 0x634c4(%rip), %r8      # 0xbae90
subq %rax, %r8
movq %rdx, %rax
testq %rsi, %rsi
cmovneq %r8, %rax
movl (%rax), %eax
je 0x579e1
cmpl %ecx, %eax
je 0x57a4d
movaps %xmm12, %xmm14
testl %eax, %eax
je 0x579ed
movaps %xmm6, %xmm14
movaps %xmm13, %xmm15
cmpl $0x1, %eax
je 0x579fa
movaps %xmm2, %xmm15
subss %xmm15, %xmm14
ucomiss %xmm14, %xmm5
seta %r10b
cmpl $0x2, %eax
setb %r11b
testb %r11b, %r10b
jne 0x57a4d
movaps %xmm0, %xmm14
cmpl $0x2, %eax
je 0x57a20
movaps %xmm4, %xmm14
movaps %xmm11, %xmm15
cmpl $0x3, %eax
je 0x57a2d
movaps %xmm1, %xmm15
subss %xmm15, %xmm14
ucomiss %xmm14, %xmm3
jbe 0x57add
movl %eax, %r10d
andl $-0x2, %r10d
cmpl $0x2, %r10d
jne 0x57add
incq %rsi
addq $0x4, %r8
cmpq $0x5, %rsi
jne 0x579cf
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
cmpl $0x2, %r9d
jne 0x57a87
movss 0x5ddf6(%rip), %xmm0    # 0xb5868
addss %xmm0, %xmm7
addss %xmm0, %xmm8
unpcklps %xmm8, %xmm7            # xmm7 = xmm7[0],xmm8[0],xmm7[1],xmm8[1]
movaps %xmm7, %xmm0
jmp 0x57b3c
movsd (%rdi), %xmm7
movaps %xmm5, %xmm8
addss %xmm7, %xmm8
minss %xmm6, %xmm8
subss %xmm5, %xmm8
movaps %xmm2, %xmm0
cmpless %xmm8, %xmm0
andps %xmm0, %xmm8
andnps %xmm2, %xmm0
orps %xmm8, %xmm0
shufps $0x55, %xmm7, %xmm7     # xmm7 = xmm7[1,1,1,1]
addss %xmm3, %xmm7
minss %xmm4, %xmm7
subss %xmm3, %xmm7
movaps %xmm1, %xmm2
cmpless %xmm7, %xmm2
andps %xmm2, %xmm7
andnps %xmm1, %xmm2
orps %xmm7, %xmm2
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
jmp 0x57b3c
movl %r11d, (%rdx)
jmp 0x57b3c
testl %eax, %eax
je 0x57aec
cmpl $0x1, %eax
jne 0x57af7
movaps %xmm13, %xmm10
jmp 0x57b0f
subss %xmm5, %xmm12
movaps %xmm12, %xmm10
jmp 0x57b0f
cmpl $0x2, %eax
jne 0x57b06
subss %xmm3, %xmm0
movaps %xmm0, %xmm9
jmp 0x57b0f
cmpl $0x3, %eax
jne 0x57b0f
movaps %xmm11, %xmm9
movaps %xmm2, %xmm0
cmpless %xmm10, %xmm0
andps %xmm0, %xmm10
andnps %xmm2, %xmm0
orps %xmm10, %xmm0
movaps %xmm1, %xmm2
cmpless %xmm9, %xmm2
andps %xmm2, %xmm9
andnps %xmm1, %xmm2
orps %xmm9, %xmm2
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movl %eax, (%rdx)
popq %rbx
popq %rbp
retq