// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r12
movq %rcx, %r14
movl %edx, -0x2c(%rbp)
movq %rsi, %r13
movq %rdi, %rbx
movq 0x3fef7(%rip), %r15     # 0x50480
callq 0x10489
movss 0x1450(%r15), %xmm2
movaps %xmm1, %xmm3
subss %xmm2, %xmm3
addss %xmm0, %xmm2
movaps %xmm2, -0x80(%rbp)
leaq -0x90(%rbp), %rsi
movss %xmm2, (%rsi)
movss 0x1454(%r15), %xmm2
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
addss %xmm2, %xmm0
movaps %xmm0, -0x70(%rbp)
movss %xmm0, 0x4(%rsi)
movss %xmm3, 0x8(%rsi)
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
subss %xmm2, %xmm1
movss %xmm1, 0xc(%rsi)
movaps %xmm1, %xmm2
movaps %xmm1, -0x60(%rbp)
movss (%r13), %xmm1
movss %xmm1, -0x34(%rbp)
movss 0x4(%r13), %xmm4
movss %xmm4, -0x30(%rbp)
movaps %xmm3, -0x50(%rbp)
movaps %xmm3, %xmm0
subss %xmm1, %xmm0
movaps %xmm2, %xmm1
subss %xmm4, %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movq %rbx, %rdi
callq 0x104d6
movss -0x34(%rbp), %xmm13
movaps -0x80(%rbp), %xmm12
movss -0x30(%rbp), %xmm11
movaps -0x60(%rbp), %xmm10
movaps -0x70(%rbp), %xmm9
movaps %xmm0, %xmm1
movl (%r14), %eax
xorl %esi, %esi
cmpl $-0x1, %eax
setne %sil
negl %esi
movss 0x8(%r12), %xmm0
movss 0xc(%r12), %xmm2
movss (%r12), %xmm4
movss 0x4(%r12), %xmm3
cmpl $0x4, %esi
je 0x106d9
movl %esi, %edx
incl %edx
movl %esi, %ecx
cmovel %eax, %ecx
movaps %xmm0, %xmm5
testl %ecx, %ecx
je 0x10677
movaps %xmm12, %xmm5
movaps %xmm2, %xmm6
cmpl $0x1, %ecx
je 0x10683
movaps %xmm9, %xmm6
movaps %xmm4, %xmm7
cmpl $0x3, %ecx
je 0x1068f
movaps -0x50(%rbp), %xmm7
movaps %xmm3, %xmm8
cmpl $0x2, %ecx
je 0x1069c
movaps %xmm10, %xmm8
subss %xmm5, %xmm7
ucomiss %xmm7, %xmm13
movl %edx, %esi
ja 0x1065e
subss %xmm6, %xmm8
ucomiss %xmm8, %xmm11
movl %edx, %esi
ja 0x1065e
movl %ecx, (%r14)
testl %ecx, %ecx
je 0x106cd
cmpl $0x3, %ecx
jne 0x1074f
subss %xmm13, %xmm4
movaps %xmm4, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movaps %xmm1, %xmm2
jmp 0x10768
orl $-0x1, (%r14)
btl $0x18, -0x2c(%rbp)
jb 0x10735
movsd (%rbx), %xmm1
movaps %xmm13, %xmm2
addss %xmm1, %xmm2
minss -0x50(%rbp), %xmm2
subss %xmm13, %xmm2
movaps %xmm12, %xmm0
cmpless %xmm2, %xmm0
andps %xmm0, %xmm2
andnps %xmm12, %xmm0
orps %xmm2, %xmm0
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
addss %xmm11, %xmm1
minss %xmm10, %xmm1
subss %xmm11, %xmm1
movaps %xmm9, %xmm2
cmpless %xmm1, %xmm2
andps %xmm2, %xmm1
andnps %xmm9, %xmm2
orps %xmm1, %xmm2
jmp 0x10768
movss 0x25c9b(%rip), %xmm1    # 0x363d8
movss (%rbx), %xmm0
addss %xmm1, %xmm0
addss 0x4(%rbx), %xmm1
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
jmp 0x1076b
movaps %xmm1, %xmm0
cmpl $0x1, %ecx
je 0x10768
cmpl $0x2, %ecx
jne 0x106cd
subss %xmm11, %xmm3
movaps %xmm3, %xmm2
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq