// File: 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
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq 0x628bb(%rip), %rax     # 0x794c0
addq $0x13e8, %rax           # imm = 0x13E8
movq %rax, -0x38(%rbp)
callq 0x16a20
movlpd %xmm0, -0x48(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movlpd %xmm1, -0x40(%rbp)
movq -0x38(%rbp), %rsi
addq $0x68, %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x31430
movq -0x90(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rsi
callq 0xdf20
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rsi
movlpd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
callq 0x16b10
movlpd %xmm0, -0x50(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %edx
cmovnel %ecx, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x4, -0x5c(%rbp)
jge 0x16ed9
cmpl $-0x1, -0x5c(%rbp)
jne 0x16cac
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x94(%rbp)
jmp 0x16cb5
movl -0x5c(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x60(%rbp)
cmpl $0x0, -0x60(%rbp)
jne 0x16cd7
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0x98(%rbp)
jmp 0x16ce4
movss -0x48(%rbp), %xmm0
movss %xmm0, -0x98(%rbp)
movss -0x98(%rbp), %xmm0
movss %xmm0, -0x9c(%rbp)
cmpl $0x1, -0x60(%rbp)
jne 0x16d0d
movq -0x30(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, -0xa0(%rbp)
jmp 0x16d1a
movss -0x44(%rbp), %xmm0
movss %xmm0, -0xa0(%rbp)
movss -0xa0(%rbp), %xmm0
movss %xmm0, -0xa4(%rbp)
cmpl $0x3, -0x60(%rbp)
jne 0x16d42
movq -0x30(%rbp), %rax
movss (%rax), %xmm0
movss %xmm0, -0xa8(%rbp)
jmp 0x16d4f
movss -0x40(%rbp), %xmm0
movss %xmm0, -0xa8(%rbp)
movss -0xa8(%rbp), %xmm0
movss %xmm0, -0xac(%rbp)
cmpl $0x2, -0x60(%rbp)
jne 0x16d78
movq -0x30(%rbp), %rax
movss 0x4(%rax), %xmm0
movss %xmm0, -0xb0(%rbp)
jmp 0x16d85
movss -0x3c(%rbp), %xmm0
movss %xmm0, -0xb0(%rbp)
movss -0xac(%rbp), %xmm2
movss -0xa4(%rbp), %xmm1
movss -0x9c(%rbp), %xmm0
movss -0xb0(%rbp), %xmm3
leaq -0x70(%rbp), %rdi
callq 0x2f230
leaq -0x70(%rbp), %rdi
callq 0x2fe50
movaps %xmm0, %xmm1
movq -0x18(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
ja 0x16de1
leaq -0x70(%rbp), %rdi
callq 0x2f520
movaps %xmm0, %xmm1
movq -0x18(%rbp), %rax
movss 0x4(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x16de6
jmp 0x16ecb
movl -0x60(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x60(%rbp)
jne 0x16e08
movq -0x30(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0xb4(%rbp)
jmp 0x16e45
cmpl $0x3, -0x60(%rbp)
jne 0x16e28
movq -0x30(%rbp), %rax
movss (%rax), %xmm0
movq -0x18(%rbp), %rax
subss (%rax), %xmm0
movss %xmm0, -0xb8(%rbp)
jmp 0x16e35
movss -0x50(%rbp), %xmm0
movss %xmm0, -0xb8(%rbp)
movss -0xb8(%rbp), %xmm0
movss %xmm0, -0xb4(%rbp)
movss -0xb4(%rbp), %xmm0
movss %xmm0, -0xbc(%rbp)
cmpl $0x1, -0x60(%rbp)
jne 0x16e6e
movq -0x30(%rbp), %rax
movss 0xc(%rax), %xmm0
movss %xmm0, -0xc0(%rbp)
jmp 0x16ead
cmpl $0x2, -0x60(%rbp)
jne 0x16e90
movq -0x30(%rbp), %rax
movss 0x4(%rax), %xmm0
movq -0x18(%rbp), %rax
subss 0x4(%rax), %xmm0
movss %xmm0, -0xc4(%rbp)
jmp 0x16e9d
movss -0x4c(%rbp), %xmm0
movss %xmm0, -0xc4(%rbp)
movss -0xc4(%rbp), %xmm0
movss %xmm0, -0xc0(%rbp)
movss -0xbc(%rbp), %xmm0
movss -0xc0(%rbp), %xmm1
leaq -0x8(%rbp), %rdi
callq 0x2d280
jmp 0x16f99
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x16c8e
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl -0x1c(%rbp), %eax
andl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x0, %eax
je 0x16f30
movq -0x10(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0x78(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
movss 0x441aa(%rip), %xmm1    # 0x5b0b8
movaps %xmm1, %xmm0
callq 0x2d280
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0xc7e0
movlpd %xmm0, -0x8(%rbp)
jmp 0x16f99
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movss -0x8(%rbp), %xmm0
movq -0x18(%rbp), %rax
addss (%rax), %xmm0
movss -0x40(%rbp), %xmm1
callq 0xe370
movq -0x18(%rbp), %rax
subss (%rax), %xmm0
movss -0x48(%rbp), %xmm1
callq 0xb7c0
movss %xmm0, -0x8(%rbp)
movss -0x4(%rbp), %xmm0
movq -0x18(%rbp), %rax
addss 0x4(%rax), %xmm0
movss -0x3c(%rbp), %xmm1
callq 0xe370
movq -0x18(%rbp), %rax
subss 0x4(%rax), %xmm0
movss -0x44(%rbp), %xmm1
callq 0xb7c0
movss %xmm0, -0x4(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)