// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, %r14d
movq %r8, -0x30(%rbp)
movq %rdx, %rbx
movss 0x8(%rcx), %xmm0
subss (%rsi), %xmm0
movss 0xc(%rcx), %xmm1
movq %rsi, -0x38(%rbp)
subss 0x4(%rsi), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movq %rdi, -0x50(%rbp)
movq %rcx, -0x40(%rbp)
movq %rcx, %rsi
callq 0x45358
movaps %xmm0, -0x60(%rbp)
pushq $-0x4
popq %r13
movl %r14d, -0x44(%rbp)
cmpl $0x1, %r14d
jne 0x45159
xorl %eax, %eax
cmpl $-0x1, (%rbx)
setne %al
movq %r13, %r12
subq %rax, %r12
shll $0x2, %eax
leaq 0x577ef(%rip), %r14     # 0x9c860
subq %rax, %r14
testq %r12, %r12
je 0x45159
movq %r14, %rax
cmpq $-0x5, %r12
cmoveq %rbx, %rax
movl (%rax), %r15d
je 0x45096
cmpl (%rbx), %r15d
je 0x4514d
cmpl $0x3, %r15d
ja 0x4510c
movl %r15d, %eax
leaq 0x578e2(%rip), %rcx     # 0x9c988
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq -0x38(%rbp), %rcx
jmpq *%rax
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm2
subss (%rcx), %xmm2
movss 0xc(%rax), %xmm0
jmp 0x450de
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm2
subss (%rcx), %xmm2
movss 0x4(%rax), %xmm0
subss 0x4(%rcx), %xmm0
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
jmp 0x45113
movq -0x30(%rbp), %rax
movups (%rax), %xmm2
shufps $0xec, %xmm2, %xmm2     # xmm2 = xmm2[0,3,2,3]
jmp 0x45113
movq -0x30(%rbp), %rax
movups (%rax), %xmm0
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
subss 0x4(%rcx), %xmm2
movlhps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
shufps $0xe2, %xmm0, %xmm2     # xmm2 = xmm2[2,0],xmm0[2,3]
jmp 0x45113
xorps %xmm2, %xmm2
movq -0x38(%rbp), %rcx
movaps %xmm2, -0x70(%rbp)
movss (%rcx), %xmm0
addss %xmm2, %xmm0
movaps %xmm2, %xmm1
shufps $0x55, %xmm2, %xmm1     # xmm1 = xmm1[1,1],xmm2[1,1]
addss 0x4(%rcx), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movaps %xmm2, %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movups %xmm1, -0x80(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x49f2c
testb %al, %al
jne 0x452e1
incq %r12
addq $0x4, %r14
jmp 0x45074
movl -0x44(%rbp), %r9d
testl $0xfffffffd, %r9d       # imm = 0xFFFFFFFD
movq -0x38(%rbp), %r8
jne 0x45255
movl (%rbx), %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
setne %al
movq -0x30(%rbp), %rsi
movss (%rsi), %xmm7
movss 0x4(%rsi), %xmm4
movq -0x40(%rbp), %rdx
movss 0x8(%rdx), %xmm8
movss 0x8(%rsi), %xmm2
movss (%rdx), %xmm1
movss 0x4(%rdx), %xmm12
movss (%r8), %xmm6
movss 0x4(%r8), %xmm5
movss 0xc(%rdx), %xmm9
movss 0xc(%rsi), %xmm3
subq %rax, %r13
shll $0x2, %eax
leaq 0x5d00f(%rip), %rdx     # 0xa21d0
subq %rax, %rdx
testq %r13, %r13
je 0x45255
movq %rbx, %rax
cmpq $-0x5, %r13
cmovneq %rdx, %rax
movl (%rax), %eax
je 0x451e0
cmpl %ecx, %eax
je 0x45249
movaps %xmm7, %xmm10
testl %eax, %eax
je 0x451ec
movaps %xmm8, %xmm10
movaps %xmm2, %xmm11
cmpl $0x1, %eax
je 0x451f9
movaps %xmm1, %xmm11
subss %xmm11, %xmm10
ucomiss %xmm10, %xmm6
seta %sil
cmpl $0x2, %eax
setb %dil
testb %dil, %sil
jne 0x45249
movaps %xmm4, %xmm10
cmpl $0x2, %eax
je 0x4521f
movaps %xmm9, %xmm10
movaps %xmm3, %xmm11
cmpl $0x3, %eax
je 0x4522c
movaps %xmm12, %xmm11
subss %xmm11, %xmm10
ucomiss %xmm10, %xmm5
jbe 0x452ea
movl %eax, %esi
andl $-0x2, %esi
cmpl $0x2, %esi
jne 0x452ea
incq %r13
addq $0x4, %rdx
jmp 0x451c4
orl $-0x1, (%rbx)
cmpl $0x2, %r9d
jne 0x45279
movss 0x5762e(%rip), %xmm1    # 0x9c894
movq -0x50(%rbp), %rax
movss (%rax), %xmm0
addss %xmm1, %xmm0
addss 0x4(%rax), %xmm1
jmp 0x452dc
movq -0x50(%rbp), %rax
movsd (%rax), %xmm5
movss (%r8), %xmm1
movaps %xmm5, %xmm2
addss %xmm1, %xmm2
movq -0x40(%rbp), %rax
minss 0x8(%rax), %xmm2
movss 0x4(%r8), %xmm3
subss %xmm1, %xmm2
movss (%rax), %xmm1
movss 0x4(%rax), %xmm4
movaps %xmm1, %xmm0
cmpless %xmm2, %xmm0
andps %xmm0, %xmm2
andnps %xmm1, %xmm0
shufps $0x55, %xmm5, %xmm5     # xmm5 = xmm5[1,1,1,1]
addss %xmm3, %xmm5
minss 0xc(%rax), %xmm5
orps %xmm2, %xmm0
subss %xmm3, %xmm5
movaps %xmm4, %xmm1
cmpless %xmm5, %xmm1
andps %xmm1, %xmm5
andnps %xmm4, %xmm1
orps %xmm5, %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
jmp 0x45342
movl %r15d, (%rbx)
movaps -0x70(%rbp), %xmm0
jmp 0x45342
testl %eax, %eax
je 0x45305
cmpl $0x1, %eax
je 0x45311
movaps -0x60(%rbp), %xmm2
cmpl $0x2, %eax
jne 0x45351
subss %xmm5, %xmm4
movaps %xmm4, %xmm3
jmp 0x45319
movq -0x30(%rbp), %rcx
movss (%rcx), %xmm2
subss %xmm6, %xmm2
movaps -0x60(%rbp), %xmm3
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
movaps %xmm1, %xmm0
cmpless %xmm2, %xmm0
andps %xmm0, %xmm2
andnps %xmm1, %xmm0
orps %xmm2, %xmm0
movaps %xmm12, %xmm1
cmpless %xmm3, %xmm1
andps %xmm1, %xmm3
andnps %xmm12, %xmm1
orps %xmm3, %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movl %eax, (%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3, %eax
jne 0x45311
jmp 0x45319