// File: Ciaxur[P]yagb-emu/include/imgui/imgui.cpp
// Function: ImGui::ClosePopupsOverWindow(ImGuiWindow*, bool)
void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup)
{
    ImGuiContext& g = *GImGui;
    if (g.OpenPopupStack.Size == 0)
        return;

    // Don't close our own child popup windows.
    int popup_count_to_keep = 0;
    if (ref_window)
    {
        // Find the highest popup which is a descendant of the reference window (generally reference window = NavWindow)
        for (; popup_count_to_keep < g.OpenPopupStack.Size; popup_count_to_keep++)
        {
            ImGuiPopupData& popup = g.OpenPopupStack[popup_count_to_keep];
            if (!popup.Window)
                continue;
            IM_ASSERT((popup.Window->Flags & ImGuiWindowFlags_Popup) != 0);
            if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow)
                continue;

            // Trim the stack unless the popup is a direct parent of the reference window (the reference window is often the NavWindow)
            // - With this stack of window, clicking/focusing Popup1 will close Popup2 and Popup3:
            //     Window -> Popup1 -> Popup2 -> Popup3
            // - Each popups may contain child windows, which is why we compare ->RootWindow!
            //     Window -> Popup1 -> Popup1_Child -> Popup2 -> Popup2_Child
            bool ref_window_is_descendent_of_popup = false;
            for (int n = popup_count_to_keep; n < g.OpenPopupStack.Size; n++)
                if (ImGuiWindow* popup_window = g.OpenPopupStack[n].Window)
                    if (popup_window->RootWindow == ref_window->RootWindow)
                    {
                        ref_window_is_descendent_of_popup = true;
                        break;
                    }
            if (!ref_window_is_descendent_of_popup)
                break;
        }
    }
    if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below
    {
        IMGUI_DEBUG_LOG_POPUP("ClosePopupsOverWindow(\"%s\") -> ClosePopupToLevel(%d)\n", ref_window->Name, popup_count_to_keep);
        ClosePopupToLevel(popup_count_to_keep, restore_focus_to_window_under_popup);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movq 0x147165(%rip), %rax    # 0x212f40
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x1c70(%rax)
jne 0xcbdf1
jmp 0xcbf2d
movl $0x0, -0x1c(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0xcbf0e
jmp 0xcbe05
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x1c70(%rcx), %eax
jge 0xcbf0c
movq -0x18(%rbp), %rdi
addq $0x1c70, %rdi           # imm = 0x1C70
movl -0x1c(%rbp), %esi
callq 0xeb4c0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0xcbe3f
jmp 0xcbefe
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl 0xc(%rax), %eax
andl $0x4000000, %eax        # imm = 0x4000000
cmpl $0x0, %eax
je 0xcbe56
jmp 0xcbe75
leaq 0xa927b(%rip), %rdi     # 0x1750d8
leaq 0xa70a5(%rip), %rsi     # 0x172f09
movl $0x1f3a, %edx           # imm = 0x1F3A
leaq 0xa929c(%rip), %rcx     # 0x17510c
callq 0xd180
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl 0xc(%rax), %eax
andl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x0, %eax
je 0xcbe8c
jmp 0xcbefe
movb $0x0, -0x29(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x1c70(%rcx), %eax
jge 0xcbef4
movq -0x18(%rbp), %rdi
addq $0x1c70, %rdi           # imm = 0x1C70
movl -0x30(%rbp), %esi
callq 0xeb4c0
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xcbee7
movq -0x38(%rbp), %rax
movq 0x390(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x390(%rcx), %rax
jne 0xcbee5
movb $0x1, -0x29(%rbp)
jmp 0xcbef4
jmp 0xcbee7
jmp 0xcbee9
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xcbe96
testb $0x1, -0x29(%rbp)
jne 0xcbefc
jmp 0xcbf0c
jmp 0xcbefe
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xcbe05
jmp 0xcbf0e
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x1c70(%rcx), %eax
jge 0xcbf2d
movl -0x1c(%rbp), %edi
movb -0x9(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0xde1b0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)