// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r14
pushq %rbx
movq 0xb5783(%rip), %rdx     # 0xfdcd0
movl 0x1c70(%rdx), %ecx
testq %rcx, %rcx
je 0x48616
testq %rdi, %rdi
sete %r8b
testl %ecx, %ecx
setle %r9b
xorl %eax, %eax
orb %r8b, %r9b
jne 0x48603
movq 0x1c78(%rdx), %rdx
leaq 0x8(%rdx), %r8
movl $0x1, %r9d
xorl %eax, %eax
leaq (%rax,%rax,2), %r10
shlq $0x4, %r10
movq 0x8(%rdx,%r10), %r10
testq %r10, %r10
je 0x485f2
movl 0xc(%r10), %r10d
btl $0x1a, %r10d
jae 0x4861b
btl $0x18, %r10d
jb 0x485f2
xorl %r10d, %r10d
movq %r9, %r11
movq %r8, %rbx
movq (%rbx), %r14
testq %r14, %r14
je 0x485cc
movq 0x390(%r14), %r14
cmpq 0x390(%rdi), %r14
je 0x485d1
movb $0x1, %r14b
jmp 0x485d7
movb $0x1, %r10b
xorl %r14d, %r14d
testb %r14b, %r14b
je 0x485ec
addq $0x30, %rbx
leaq 0x1(%r11), %r14
cmpq %rcx, %r11
movq %r14, %r11
jb 0x485b4
testb $0x1, %r10b
je 0x48603
incq %rax
addq $0x30, %r8
incq %r9
cmpq %rcx, %rax
jne 0x48587
movl %ecx, %eax
cmpl %ecx, %eax
jge 0x48616
movzbl %sil, %esi
movl %eax, %edi
popq %rbx
popq %r14
popq %rbp
jmp 0x58144
popq %rbx
popq %r14
popq %rbp
retq
leaq 0x71a98(%rip), %rdi     # 0xba0ba
leaq 0x6fb3c(%rip), %rsi     # 0xb8165
leaq 0x71abe(%rip), %rcx     # 0xba0ee
movl $0x1f3a, %edx           # imm = 0x1F3A
callq 0xc140