// File: /workspace/github_cpp_makefile_star2_O3/donfreiday[P]gb/./imgui/imgui.cpp
// Function: FindBestPopupWindowPos(ImVec2 const&, ImVec2 const&, int, int*, ImRect const&)
static ImVec2 FindBestPopupWindowPos(const ImVec2& base_pos, const ImVec2& size, ImGuiWindowFlags flags, int* last_dir, const ImRect& r_inner)
{
    const ImGuiStyle& style = GImGui->Style;

    // Clamp into visible area while not overlapping the cursor
    ImRect r_outer(GetVisibleRect());
    r_outer.Reduce(style.DisplaySafeAreaPadding);
    ImVec2 base_pos_clamped = ImClamp(base_pos, r_outer.Min, r_outer.Max - size);

    for (int n = (*last_dir != -1) ? -1 : 0; n < 4; n++)   // Right, down, up, left. Favor last used direction.
    {
        const int dir = (n == -1) ? *last_dir : n;
        ImRect rect(dir == 0 ? r_inner.Max.x : r_outer.Min.x, dir == 1 ? r_inner.Max.y : r_outer.Min.y, dir == 3 ? r_inner.Min.x : r_outer.Max.x, dir == 2 ? r_inner.Min.y : r_outer.Max.y);
        if (rect.GetWidth() < size.x || rect.GetHeight() < size.y)
            continue;
        *last_dir = dir;
        return ImVec2(dir == 0 ? r_inner.Max.x : dir == 3 ? r_inner.Min.x - size.x : base_pos_clamped.x, dir == 1 ? r_inner.Max.y : dir == 2 ? r_inner.Min.y - size.y : base_pos_clamped.y);
    }

    // Fallback
    *last_dir = -1;
    if (flags & ImGuiWindowFlags_Tooltip) // For tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible.
        return base_pos + ImVec2(2,2);

    // Otherwise try to keep within display
    ImVec2 pos = base_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:
movaps %xmm0, %xmm2
movq 0x41c3b(%rip), %rax     # 0x55480
movss 0xa8(%rax), %xmm0
ucomiss 0xb0(%rax), %xmm0
jne 0x13858
jnp 0x1387d
movss 0xac(%rax), %xmm0
ucomiss 0xb4(%rax), %xmm0
jne 0x1386b
jnp 0x1387d
movsd 0xa8(%rax), %xmm3
movsd 0xb0(%rax), %xmm4
jmp 0x13885
movsd 0x8(%rax), %xmm4
xorps %xmm3, %xmm3
pushq %rbp
movq %rsp, %rbp
movss 0x1450(%rax), %xmm5
movss 0x1454(%rax), %xmm0
movaps %xmm4, %xmm6
subss %xmm5, %xmm6
addss %xmm3, %xmm5
shufps $0x55, %xmm3, %xmm3     # xmm3 = xmm3[1,1,1,1]
addss %xmm0, %xmm3
shufps $0x55, %xmm4, %xmm4     # xmm4 = xmm4[1,1,1,1]
subss %xmm0, %xmm4
movaps %xmm6, %xmm0
subss %xmm2, %xmm0
movaps %xmm4, %xmm7
subss %xmm1, %xmm7
movss (%rdi), %xmm8
movss 0x4(%rdi), %xmm10
minss %xmm8, %xmm0
movaps %xmm8, -0x20(%rbp)
cmpltss %xmm5, %xmm8
movaps %xmm8, %xmm9
andps %xmm5, %xmm9
andnps %xmm0, %xmm8
orps %xmm9, %xmm8
minss %xmm10, %xmm7
movaps %xmm10, -0x10(%rbp)
movaps %xmm10, %xmm9
cmpltss %xmm3, %xmm9
movaps %xmm9, %xmm0
andps %xmm3, %xmm0
andnps %xmm7, %xmm9
orps %xmm0, %xmm9
movl (%rdx), %eax
xorl %r8d, %r8d
cmpl $-0x1, %eax
setne %r8b
negl %r8d
movss 0x8(%rcx), %xmm13
movss 0xc(%rcx), %xmm11
movss (%rcx), %xmm0
movss 0x4(%rcx), %xmm7
cmpl $-0x1, %r8d
movl %r8d, %ecx
cmovel %eax, %ecx
movaps %xmm13, %xmm14
testl %ecx, %ecx
je 0x1394a
movaps %xmm5, %xmm14
movaps %xmm11, %xmm15
cmpl $0x1, %ecx
je 0x13957
movaps %xmm3, %xmm15
movaps %xmm0, %xmm12
cmpl $0x3, %ecx
je 0x13964
movaps %xmm6, %xmm12
movaps %xmm7, %xmm10
cmpl $0x2, %ecx
je 0x13971
movaps %xmm4, %xmm10
subss %xmm14, %xmm12
ucomiss %xmm12, %xmm2
ja 0x13987
subss %xmm15, %xmm10
ucomiss %xmm10, %xmm1
jbe 0x139e8
incl %r8d
cmpl $0x4, %r8d
jne 0x13934
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
btl $0x18, %esi
jb 0x139fd
movsd (%rdi), %xmm0
movaps %xmm0, %xmm7
addss %xmm2, %xmm7
minss %xmm6, %xmm7
subss %xmm2, %xmm7
movaps %xmm5, %xmm8
cmpless %xmm7, %xmm8
andps %xmm8, %xmm7
andnps %xmm5, %xmm8
orps %xmm7, %xmm8
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
addss %xmm1, %xmm0
minss %xmm4, %xmm0
subss %xmm1, %xmm0
movaps %xmm3, %xmm1
cmpless %xmm0, %xmm1
andps %xmm1, %xmm0
andnps %xmm3, %xmm1
orps %xmm0, %xmm1
jmp 0x13a17
movl %ecx, (%rdx)
testl %ecx, %ecx
je 0x13a1d
cmpl $0x3, %ecx
jne 0x13a23
subss %xmm2, %xmm0
movaps %xmm0, %xmm8
jmp 0x13a3f
movss 0x299cf(%rip), %xmm0    # 0x3d3d4
movaps -0x20(%rbp), %xmm8
addss %xmm0, %xmm8
movaps -0x10(%rbp), %xmm1
addss %xmm0, %xmm1
unpcklps %xmm1, %xmm8            # xmm8 = xmm8[0],xmm1[0],xmm8[1],xmm1[1]
jmp 0x13a43
movaps %xmm13, %xmm8
jmp 0x13a3f
cmpl $0x1, %ecx
je 0x13a2c
movaps %xmm9, %xmm11
cmpl $0x2, %ecx
jne 0x13a3b
subss %xmm1, %xmm7
movaps %xmm7, %xmm9
jmp 0x13a3f
movaps %xmm11, %xmm9
unpcklps %xmm9, %xmm8            # xmm8 = xmm8[0],xmm9[0],xmm8[1],xmm9[1]
movaps %xmm8, %xmm0
popq %rbp
retq