// File: Ciaxur[P]yagb-emu/include/imgui/imgui.cpp
// Function: ImGui::OpenPopupEx(unsigned int, int)
void ImGui::OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags)
{
    ImGuiContext& g = *GImGui;
    ImGuiWindow* parent_window = g.CurrentWindow;
    const int current_stack_size = g.BeginPopupStack.Size;

    if (popup_flags & ImGuiPopupFlags_NoOpenOverExistingPopup)
        if (IsPopupOpen(0u, ImGuiPopupFlags_AnyPopupId))
            return;

    ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack.
    popup_ref.PopupId = id;
    popup_ref.Window = NULL;
    popup_ref.SourceWindow = g.NavWindow;
    popup_ref.OpenFrameCount = g.FrameCount;
    popup_ref.OpenParentId = parent_window->IDStack.back();
    popup_ref.OpenPopupPos = NavCalcPreferredRefPos();
    popup_ref.OpenMousePos = IsMousePosValid(&g.IO.MousePos) ? g.IO.MousePos : popup_ref.OpenPopupPos;

    IMGUI_DEBUG_LOG_POPUP("OpenPopupEx(0x%08X)\n", id);
    if (g.OpenPopupStack.Size < current_stack_size + 1)
    {
        g.OpenPopupStack.push_back(popup_ref);
    }
    else
    {
        // Gently handle the user mistakenly calling OpenPopup() every frame. It is a programming mistake! However, if we were to run the regular code path, the ui
        // would become completely unusable because the popup will always be in hidden-while-calculating-size state _while_ claiming focus. Which would be a very confusing
        // situation for the programmer. Instead, we silently allow the popup to proceed, it will keep reappearing and the programming error will be more obvious to understand.
        if (g.OpenPopupStack[current_stack_size].PopupId == id && g.OpenPopupStack[current_stack_size].OpenFrameCount == g.FrameCount - 1)
        {
            g.OpenPopupStack[current_stack_size].OpenFrameCount = popup_ref.OpenFrameCount;
        }
        else
        {
            // Close child popups if any, then flag popup for open/reopen
            ClosePopupToLevel(current_stack_size, false);
            g.OpenPopupStack.push_back(popup_ref);
        }

        // When reopening a popup we first refocus its parent, otherwise if its parent is itself a popup it would get closed by ClosePopupsOverWindow().
        // This is equivalent to what ClosePopupToLevel() does.
        //if (g.OpenPopupStack[current_stack_size].PopupId == id)
        //    FocusWindow(parent_window);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq 0x1350db(%rip), %rax    # 0x212f40
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x1ae0(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c80(%rax), %eax
movl %eax, -0x1c(%rbp)
movl -0x8(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xddea9
xorl %edi, %edi
movl $0x80, %esi
callq 0xcbb60
testb $0x1, %al
jne 0xddea2
jmp 0xddea7
jmp 0xddff7
jmp 0xddea9
leaq -0x50(%rbp), %rdi
callq 0xec040
movl -0x4(%rbp), %eax
movl %eax, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x1c90(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x1a68(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
addq $0xc8, %rdi
callq 0xe9130
movl (%rax), %eax
movl %eax, -0x34(%rbp)
callq 0xde000
movlpd %xmm0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0xcb710
testb $0x1, %al
jne 0xddf1d
jmp 0xddf31
movq -0x10(%rbp), %rax
addq $0x8, %rax
addq $0x120, %rax            # imm = 0x120
movq %rax, -0x60(%rbp)
jmp 0xddf3d
leaq -0x50(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c70(%rax), %eax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0xddf75
movq -0x10(%rbp), %rdi
addq $0x1c70, %rdi           # imm = 0x1C70
leaq -0x50(%rbp), %rsi
callq 0xeb680
jmp 0xddff7
movq -0x10(%rbp), %rdi
addq $0x1c70, %rdi           # imm = 0x1C70
movl -0x1c(%rbp), %esi
callq 0xeb4c0
movl (%rax), %eax
cmpl -0x4(%rbp), %eax
jne 0xddfd7
movq -0x10(%rbp), %rdi
addq $0x1c70, %rdi           # imm = 0x1C70
movl -0x1c(%rbp), %esi
callq 0xeb4c0
movl 0x18(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x1a68(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xddfd7
movl -0x38(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rdi
addq $0x1c70, %rdi           # imm = 0x1C70
movl -0x1c(%rbp), %esi
callq 0xeb4c0
movl -0x64(%rbp), %ecx
movl %ecx, 0x18(%rax)
jmp 0xddff5
movl -0x1c(%rbp), %edi
xorl %esi, %esi
callq 0xde1b0
movq -0x10(%rbp), %rdi
addq $0x1c70, %rdi           # imm = 0x1C70
leaq -0x50(%rbp), %rsi
callq 0xeb680
jmp 0xddff7
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)