// File: 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
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x28(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0xc3c00
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movlpd %xmm0, -0x44(%rbp)
movsd -0x44(%rbp), %xmm0
callq 0xdf120
movlpd %xmm0, -0x3c(%rbp)
cmpl $0x1, -0x34(%rbp)
jne 0xdecfe
movq 0x93cc7(%rip), %rax     # 0x172810
movq %rax, -0x60(%rbp)
movq 0x93cc4(%rip), %rax     # 0x172818
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %edx
cmovnel %ecx, %eax
movl %eax, -0x64(%rbp)
cmpl $0x4, -0x64(%rbp)
jge 0xdecfc
cmpl $-0x1, -0x64(%rbp)
jne 0xdeb8c
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
jmp 0xdeb9a
movslq -0x64(%rbp), %rax
movl -0x60(%rbp,%rax,4), %eax
movl %eax, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $-0x1, -0x64(%rbp)
je 0xdebb9
movl -0x68(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xdebb9
jmp 0xdecee
leaq -0x8(%rbp), %rdi
callq 0xe7de0
cmpl $0x3, -0x68(%rbp)
jne 0xdebea
movq -0x30(%rbp), %rax
movss (%rax), %xmm0
movq -0x30(%rbp), %rax
movss 0xc(%rax), %xmm1
leaq -0x70(%rbp), %rdi
callq 0xbf4c0
movq -0x70(%rbp), %rax
movq %rax, -0x8(%rbp)
cmpl $0x1, -0x68(%rbp)
jne 0xdec1b
movq -0x30(%rbp), %rax
movss (%rax), %xmm0
movq -0x30(%rbp), %rax
movss 0x4(%rax), %xmm1
movq -0x18(%rbp), %rax
subss 0x4(%rax), %xmm1
leaq -0x78(%rbp), %rdi
callq 0xbf4c0
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
cmpl $0x0, -0x68(%rbp)
jne 0xdec4c
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm0
movq -0x18(%rbp), %rax
subss (%rax), %xmm0
movq -0x30(%rbp), %rax
movss 0xc(%rax), %xmm1
leaq -0x80(%rbp), %rdi
callq 0xbf4c0
movq -0x80(%rbp), %rax
movq %rax, -0x8(%rbp)
cmpl $0x2, -0x68(%rbp)
jne 0xdec8c
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm0
movq -0x18(%rbp), %rax
subss (%rax), %xmm0
movq -0x30(%rbp), %rax
movss 0x4(%rax), %xmm1
movq -0x18(%rbp), %rax
subss 0x4(%rax), %xmm1
leaq -0x88(%rbp), %rdi
callq 0xbf4c0
movq -0x88(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x8(%rbp), %rdi
callq 0xc41b0
movlpd %xmm0, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0x8(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0xe8660
movq -0xe8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xe8a40
xorb $-0x1, %al
testb $0x1, %al
jne 0xdecde
jmp 0xdece0
jmp 0xdecee
movl -0x68(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0xdf104
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xdeb6e
jmp 0xdecfe
cmpl $0x2, -0x34(%rbp)
je 0xded0e
cmpl $0x0, -0x34(%rbp)
jne 0xdf039
movq 0x9983b(%rip), %rax     # 0x178550
movq %rax, -0xb0(%rbp)
movq 0x99835(%rip), %rax     # 0x178558
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %edx
cmovnel %ecx, %eax
movl %eax, -0xb4(%rbp)
cmpl $0x4, -0xb4(%rbp)
jge 0xdf037
cmpl $-0x1, -0xb4(%rbp)
jne 0xded67
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xec(%rbp)
jmp 0xded7b
movslq -0xb4(%rbp), %rax
movl -0xb0(%rbp,%rax,4), %eax
movl %eax, -0xec(%rbp)
movl -0xec(%rbp), %eax
movl %eax, -0xb8(%rbp)
cmpl $-0x1, -0xb4(%rbp)
je 0xdeda3
movl -0xb8(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jne 0xdeda3
jmp 0xdf023
cmpl $0x0, -0xb8(%rbp)
jne 0xdedbe
movq -0x30(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0xf0(%rbp)
jmp 0xdedcf
movq -0x28(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0xf0(%rbp)
movss -0xf0(%rbp), %xmm0
movss %xmm0, -0xf4(%rbp)
cmpl $0x1, -0xb8(%rbp)
jne 0xdedfb
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0xf8(%rbp)
jmp 0xdee0b
movq -0x28(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0xf8(%rbp)
movss -0xf4(%rbp), %xmm0
movss -0xf8(%rbp), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0xbc(%rbp)
cmpl $0x2, -0xb8(%rbp)
jne 0xdee43
movq -0x30(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0xfc(%rbp)
jmp 0xdee54
movq -0x28(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, -0xfc(%rbp)
movss -0xfc(%rbp), %xmm0
movss %xmm0, -0x100(%rbp)
cmpl $0x3, -0xb8(%rbp)
jne 0xdee80
movq -0x30(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, -0x104(%rbp)
jmp 0xdee91
movq -0x28(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0x104(%rbp)
movss -0x100(%rbp), %xmm0
movss -0x104(%rbp), %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0xc0(%rbp)
movss -0xbc(%rbp), %xmm1
movq -0x18(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xdeed9
cmpl $0x0, -0xb8(%rbp)
je 0xdeed4
cmpl $0x1, -0xb8(%rbp)
jne 0xdeed9
jmp 0xdf023
movss -0xc0(%rbp), %xmm1
movq -0x18(%rbp), %rax
movss 0x4(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xdef06
cmpl $0x2, -0xb8(%rbp)
je 0xdef01
cmpl $0x3, -0xb8(%rbp)
jne 0xdef06
jmp 0xdf023
leaq -0x8(%rbp), %rdi
callq 0xe7de0
cmpl $0x0, -0xb8(%rbp)
jne 0xdef32
movq -0x30(%rbp), %rax
movss (%rax), %xmm0
movq -0x18(%rbp), %rax
subss (%rax), %xmm0
movss %xmm0, -0x108(%rbp)
jmp 0xdef6b
cmpl $0x1, -0xb8(%rbp)
jne 0xdef4e
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0x10c(%rbp)
jmp 0xdef5b
movss -0x3c(%rbp), %xmm0
movss %xmm0, -0x10c(%rbp)
movss -0x10c(%rbp), %xmm0
movss %xmm0, -0x108(%rbp)
movss -0x108(%rbp), %xmm0
movss %xmm0, -0x8(%rbp)
cmpl $0x2, -0xb8(%rbp)
jne 0xdef9d
movq -0x30(%rbp), %rax
movss 0x4(%rax), %xmm0
movq -0x18(%rbp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, -0x110(%rbp)
jmp 0xdefd6
cmpl $0x3, -0xb8(%rbp)
jne 0xdefb9
movq -0x30(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, -0x114(%rbp)
jmp 0xdefc6
movss -0x38(%rbp), %xmm0
movss %xmm0, -0x114(%rbp)
movss -0x114(%rbp), %xmm0
movss %xmm0, -0x110(%rbp)
movss -0x110(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
movss -0x8(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss (%rax), %xmm1
callq 0xc88c0
movss %xmm0, -0x8(%rbp)
movss -0x4(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss 0x4(%rax), %xmm1
callq 0xc88c0
movss %xmm0, -0x4(%rbp)
movl -0xb8(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0xdf104
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xded43
jmp 0xdf039
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpl $0x2, -0x34(%rbp)
jne 0xdf08c
movq -0x10(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0xc8(%rbp), %rdi
movq %rdi, -0x120(%rbp)
movss 0x92f62(%rip), %xmm1    # 0x171fcc
movaps %xmm1, %xmm0
callq 0xbf4c0
movq -0x128(%rbp), %rdi
movq -0x120(%rbp), %rsi
callq 0xc41b0
movlpd %xmm0, -0x8(%rbp)
jmp 0xdf104
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movss -0x8(%rbp), %xmm0
movq -0x18(%rbp), %rax
addss (%rax), %xmm0
movq -0x28(%rbp), %rax
movss 0x8(%rax), %xmm1
callq 0xc4620
movq -0x18(%rbp), %rax
subss (%rax), %xmm0
movq -0x28(%rbp), %rax
movss (%rax), %xmm1
callq 0xc88c0
movss %xmm0, -0x8(%rbp)
movss -0x4(%rbp), %xmm0
movq -0x18(%rbp), %rax
addss 0x4(%rax), %xmm0
movq -0x28(%rbp), %rax
movss 0xc(%rax), %xmm1
callq 0xc4620
movq -0x18(%rbp), %rax
subss 0x4(%rax), %xmm0
movq -0x28(%rbp), %rax
movss 0x4(%rax), %xmm1
callq 0xc88c0
movss %xmm0, -0x4(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)