// File: Ciaxur[P]yagb-emu/include/imgui/imgui_demo.cpp
// Function: ShowExampleAppDocuments(bool*)
void ShowExampleAppDocuments(bool* p_open)
{
    static ExampleAppDocuments app;

    // Options
    static bool opt_reorderable = true;
    static ImGuiTabBarFlags opt_fitting_flags = ImGuiTabBarFlags_FittingPolicyDefault_;

    bool window_contents_visible = ImGui::Begin("Example: Documents", p_open, ImGuiWindowFlags_MenuBar);
    if (!window_contents_visible)
    {
        ImGui::End();
        return;
    }

    // Menu
    if (ImGui::BeginMenuBar())
    {
        if (ImGui::BeginMenu("File"))
        {
            int open_count = 0;
            for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++)
                open_count += app.Documents[doc_n].Open ? 1 : 0;

            if (ImGui::BeginMenu("Open", open_count < app.Documents.Size))
            {
                for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++)
                {
                    MyDocument* doc = &app.Documents[doc_n];
                    if (!doc->Open)
                        if (ImGui::MenuItem(doc->Name))
                            doc->DoOpen();
                }
                ImGui::EndMenu();
            }
            if (ImGui::MenuItem("Close All Documents", NULL, false, open_count > 0))
                for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++)
                    app.Documents[doc_n].DoQueueClose();
            if (ImGui::MenuItem("Exit", "Alt+F4")) {}
            ImGui::EndMenu();
        }
        ImGui::EndMenuBar();
    }

    // [Debug] List documents with one checkbox for each
    for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++)
    {
        MyDocument* doc = &app.Documents[doc_n];
        if (doc_n > 0)
            ImGui::SameLine();
        ImGui::PushID(doc);
        if (ImGui::Checkbox(doc->Name, &doc->Open))
            if (!doc->Open)
                doc->DoForceClose();
        ImGui::PopID();
    }

    ImGui::Separator();

    // Submit Tab Bar and Tabs
    {
        ImGuiTabBarFlags tab_bar_flags = (opt_fitting_flags) | (opt_reorderable ? ImGuiTabBarFlags_Reorderable : 0);
        if (ImGui::BeginTabBar("##tabs", tab_bar_flags))
        {
            if (opt_reorderable)
                NotifyOfDocumentsClosedElsewhere(app);

            // [DEBUG] Stress tests
            //if ((ImGui::GetFrameCount() % 30) == 0) docs[1].Open ^= 1;            // [DEBUG] Automatically show/hide a tab. Test various interactions e.g. dragging with this on.
            //if (ImGui::GetIO().KeyCtrl) ImGui::SetTabItemSelected(docs[1].Name);  // [DEBUG] Test SetTabItemSelected(), probably not very useful as-is anyway..

            // Submit Tabs
            for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++)
            {
                MyDocument* doc = &app.Documents[doc_n];
                if (!doc->Open)
                    continue;

                ImGuiTabItemFlags tab_flags = (doc->Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0);
                bool visible = ImGui::BeginTabItem(doc->Name, &doc->Open, tab_flags);

                // Cancel attempt to close when unsaved add to save queue so we can display a popup.
                if (!doc->Open && doc->Dirty)
                {
                    doc->Open = true;
                    doc->DoQueueClose();
                }

                MyDocument::DisplayContextMenu(doc);
                if (visible)
                {
                    MyDocument::DisplayContents(doc);
                    ImGui::EndTabItem();
                }
            }

            ImGui::EndTabBar();
        }
    }

    // Update closing queue
    static ImVector<MyDocument*> close_queue;
    if (close_queue.empty())
    {
        // Close queue is locked once we started a popup
        for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++)
        {
            MyDocument* doc = &app.Documents[doc_n];
            if (doc->WantClose)
            {
                doc->WantClose = false;
                close_queue.push_back(doc);
            }
        }
    }

    // Display closing confirmation UI
    if (!close_queue.empty())
    {
        int close_queue_unsaved_documents = 0;
        for (int n = 0; n < close_queue.Size; n++)
            if (close_queue[n]->Dirty)
                close_queue_unsaved_documents++;

        if (close_queue_unsaved_documents == 0)
        {
            // Close documents when all are unsaved
            for (int n = 0; n < close_queue.Size; n++)
                close_queue[n]->DoForceClose();
            close_queue.clear();
        }
        else
        {
            if (!ImGui::IsPopupOpen("Save?"))
                ImGui::OpenPopup("Save?");
            if (ImGui::BeginPopupModal("Save?"))
            {
                ImGui::Text("Save change to the following items?");
                ImGui::SetNextItemWidth(-1.0f);
                if (ImGui::ListBoxHeader("##", close_queue_unsaved_documents, 6))
                {
                    for (int n = 0; n < close_queue.Size; n++)
                        if (close_queue[n]->Dirty)
                            ImGui::Text("%s", close_queue[n]->Name);
                    ImGui::ListBoxFooter();
                }

                if (ImGui::Button("Yes", ImVec2(80, 0)))
                {
                    for (int n = 0; n < close_queue.Size; n++)
                    {
                        if (close_queue[n]->Dirty)
                            close_queue[n]->DoSave();
                        close_queue[n]->DoForceClose();
                    }
                    close_queue.clear();
                    ImGui::CloseCurrentPopup();
                }
                ImGui::SameLine();
                if (ImGui::Button("No", ImVec2(80, 0)))
                {
                    for (int n = 0; n < close_queue.Size; n++)
                        close_queue[n]->DoForceClose();
                    close_queue.clear();
                    ImGui::CloseCurrentPopup();
                }
                ImGui::SameLine();
                if (ImGui::Button("Cancel", ImVec2(80, 0)))
                {
                    close_queue.clear();
                    ImGui::CloseCurrentPopup();
                }
                ImGui::EndPopup();
            }
        }
    }

    ImGui::End();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
cmpb $0x0, 0x123a0a(%rip)    # 0x2144b0
jne 0xf0aed
leaq 0x123a01(%rip), %rdi    # 0x2144b0
callq 0xd380
cmpl $0x0, %eax
je 0xf0aed
leaq 0x1239e0(%rip), %rdi    # 0x2144a0
callq 0x10b960
jmp 0xf0ac7
leaq 0x1b142(%rip), %rdi     # 0x10bc10
leaq 0x1239cb(%rip), %rsi    # 0x2144a0
leaq 0x11dadc(%rip), %rdx    # 0x20e5b8
callq 0xd820
leaq 0x1239c8(%rip), %rdi    # 0x2144b0
callq 0xd580
movq -0x8(%rbp), %rsi
leaq 0x92c83(%rip), %rdi     # 0x18377b
movl $0x400, %edx            # imm = 0x400
callq 0xd0170
andb $0x1, %al
movb %al, -0x15(%rbp)
testb $0x1, -0x15(%rbp)
jne 0xf0b34
callq 0xd3b60
jmp 0xf1242
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq 0x123986(%rip), %rdi    # 0x2144b0
callq 0xd460
jmp 0xf124b
callq 0x1540f0
testb $0x1, %al
jne 0xf0b42
jmp 0xf0cbb
leaq 0x84d6f(%rip), %rdi     # 0x1758b8
movl $0x1, %esi
callq 0x154790
testb $0x1, %al
jne 0xf0b5c
jmp 0xf0cb6
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl 0x12392d(%rip), %eax    # 0x2144a0
jge 0xf0ba5
movl -0x20(%rbp), %esi
leaq 0x123921(%rip), %rdi    # 0x2144a0
callq 0x10bc30
movb 0x8(%rax), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xf0b6a
movl -0x1c(%rbp), %eax
cmpl 0x1238f2(%rip), %eax    # 0x2144a0
setl %al
movzbl %al, %esi
andl $0x1, %esi
leaq 0x91ccf(%rip), %rdi     # 0x18288d
callq 0x154790
testb $0x1, %al
jne 0xf0bc9
jmp 0xf0c32
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl 0x1238c7(%rip), %eax    # 0x2144a0
jge 0xf0c2d
movl -0x24(%rbp), %esi
leaq 0x1238bb(%rip), %rdi    # 0x2144a0
callq 0x10bc30
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0xf0c20
movq -0x30(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x155360
testb $0x1, %al
jne 0xf0c15
jmp 0xf0c1e
movq -0x30(%rbp), %rdi
callq 0x10bcb0
jmp 0xf0c20
jmp 0xf0c22
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xf0bd0
callq 0x1552c0
cmpl $0x0, -0x1c(%rbp)
setg %al
xorl %ecx, %ecx
xorl %ecx, %ecx
movzbl %al, %ecx
andl $0x1, %ecx
leaq 0x92b44(%rip), %rdi     # 0x18378e
xorl %edx, %edx
movl %edx, %esi
callq 0x155360
testb $0x1, %al
jne 0xf0c59
jmp 0xf0c8f
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl 0x123837(%rip), %eax    # 0x2144a0
jge 0xf0c8d
movl -0x34(%rbp), %esi
leaq 0x12382b(%rip), %rdi    # 0x2144a0
callq 0x10bc30
movq %rax, %rdi
callq 0x10bcd0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf0c60
jmp 0xf0c8f
leaq 0x92b0c(%rip), %rdi     # 0x1837a2
leaq 0x91c92(%rip), %rsi     # 0x18292f
xorl %edx, %edx
movl $0x1, %ecx
callq 0x155360
testb $0x1, %al
jne 0xf0caf
jmp 0xf0cb1
jmp 0xf0cb1
callq 0x1552c0
callq 0x154310
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x1237d5(%rip), %eax    # 0x2144a0
jge 0xf0d3e
movl -0x38(%rbp), %esi
leaq 0x1237c9(%rip), %rdi    # 0x2144a0
callq 0x10bc30
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x38(%rbp)
jle 0xf0cf6
xorps %xmm0, %xmm0
movss 0x8128f(%rip), %xmm1    # 0x171f80
callq 0xdc1c0
movq -0x40(%rbp), %rdi
callq 0xdb500
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
addq $0x8, %rsi
callq 0x13c7c0
testb $0x1, %al
jne 0xf0d19
jmp 0xf0d2e
movq -0x40(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0xf0d2c
movq -0x40(%rbp), %rdi
callq 0x10bcf0
jmp 0xf0d2e
callq 0xdb640
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xf0cc2
callq 0x13dec0
movl 0x121f3b(%rip), %eax    # 0x212c84
movb 0x121f30(%rip), %sil    # 0x212c80
xorl %ecx, %ecx
movl $0x1, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
orl %ecx, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %esi
leaq 0x8912c(%rip), %rdi     # 0x179e99
callq 0x1558c0
testb $0x1, %al
jne 0xf0d7b
jmp 0xf0e4f
testb $0x1, 0x121efe(%rip)    # 0x212c80
je 0xf0d90
leaq 0x123715(%rip), %rdi    # 0x2144a0
callq 0x108f10
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl 0x123700(%rip), %eax    # 0x2144a0
jge 0xf0e4a
movl -0x48(%rbp), %esi
leaq 0x1236f0(%rip), %rdi    # 0x2144a0
callq 0x10bc30
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0xf0dc5
jmp 0xf0e3c
movq -0x50(%rbp), %rax
movb 0xa(%rax), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
addq $0x8, %rsi
movl -0x54(%rbp), %edx
callq 0x1573a0
andb $0x1, %al
movb %al, -0x55(%rbp)
movq -0x50(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0xf0e1d
movq -0x50(%rbp), %rax
testb $0x1, 0xa(%rax)
je 0xf0e1d
movq -0x50(%rbp), %rax
movb $0x1, 0x8(%rax)
movq -0x50(%rbp), %rdi
callq 0x10bcd0
movq -0x50(%rbp), %rdi
callq 0x10bd10
testb $0x1, -0x55(%rbp)
je 0xf0e3a
movq -0x50(%rbp), %rdi
callq 0x10bdc0
callq 0x158140
jmp 0xf0e3c
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xf0d97
callq 0x155dd0
cmpb $0x0, 0x123672(%rip)    # 0x2144c8
jne 0xf0e9d
leaq 0x123669(%rip), %rdi    # 0x2144c8
callq 0xd380
cmpl $0x0, %eax
je 0xf0e9d
leaq 0x123648(%rip), %rdi    # 0x2144b8
callq 0x10beb0
jmp 0xf0e77
leaq 0x1b062(%rip), %rdi     # 0x10bee0
leaq 0x123633(%rip), %rsi    # 0x2144b8
leaq 0x11d72c(%rip), %rdx    # 0x20e5b8
callq 0xd820
leaq 0x123630(%rip), %rdi    # 0x2144c8
callq 0xd580
leaq 0x123614(%rip), %rdi    # 0x2144b8
callq 0x10bf20
testb $0x1, %al
jne 0xf0eaf
jmp 0xf0f24
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl 0x1235e1(%rip), %eax    # 0x2144a0
jge 0xf0f22
movl -0x5c(%rbp), %esi
leaq 0x1235d5(%rip), %rdi    # 0x2144a0
callq 0x10bc30
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
testb $0x1, 0xb(%rax)
je 0xf0f15
movq -0x68(%rbp), %rax
movb $0x0, 0xb(%rax)
leaq 0x1235cb(%rip), %rdi    # 0x2144b8
leaq -0x68(%rbp), %rsi
callq 0x10bf40
jmp 0xf0f15
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq 0x1235bd(%rip), %rdi    # 0x2144c8
callq 0xd460
jmp 0xf124b
jmp 0xf0f17
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xf0eb6
jmp 0xf0f24
leaq 0x12358d(%rip), %rdi    # 0x2144b8
callq 0x10bf20
testb $0x1, %al
jne 0xf123d
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl 0x123569(%rip), %eax    # 0x2144b8
jge 0xf0f7f
movl -0x70(%rbp), %esi
leaq 0x12355d(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rax
testb $0x1, 0xa(%rax)
je 0xf0f72
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xf0f74
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xf0f46
cmpl $0x0, -0x6c(%rbp)
jne 0xf0fca
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl 0x123523(%rip), %eax    # 0x2144b8
jge 0xf0fb9
movl -0x74(%rbp), %esi
leaq 0x123517(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rdi
callq 0x10bcf0
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xf0f8c
leaq 0x1234f8(%rip), %rdi    # 0x2144b8
callq 0x10c020
jmp 0xf123b
leaq 0x927d6(%rip), %rdi     # 0x1837a7
xorl %esi, %esi
callq 0xddd60
testb $0x1, %al
jne 0xf0fea
leaq 0x927c4(%rip), %rdi     # 0x1837a7
xorl %esi, %esi
callq 0xdde00
leaq 0x927b6(%rip), %rdi     # 0x1837a7
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0xde680
testb $0x1, %al
jne 0xf1005
jmp 0xf1239
leaq 0x927a1(%rip), %rdi     # 0x1837ad
movb $0x0, %al
callq 0x139160
movss 0x80f65(%rip), %xmm0    # 0x171f80
callq 0xc6860
movl -0x6c(%rbp), %esi
leaq 0x9ed86(%rip), %rdi     # 0x18fdb0
movl $0x6, %edx
callq 0x152b00
testb $0x1, %al
jne 0xf103a
jmp 0xf1099
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl 0x12346e(%rip), %eax    # 0x2144b8
jge 0xf1094
movl -0x78(%rbp), %esi
leaq 0x123462(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rax
testb $0x1, 0xa(%rax)
je 0xf1087
movl -0x78(%rbp), %esi
leaq 0x12344a(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rax
movq (%rax), %rsi
leaq 0x84b47(%rip), %rdi     # 0x175bc7
movb $0x0, %al
callq 0x139160
jmp 0xf1089
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0xf1041
callq 0x152bc0
leaq -0x80(%rbp), %rdi
movss 0x8172b(%rip), %xmm0    # 0x1727d0
xorps %xmm1, %xmm1
callq 0xbf4c0
leaq 0x9271d(%rip), %rdi     # 0x1837d1
leaq -0x80(%rbp), %rsi
callq 0x13a930
testb $0x1, %al
jne 0xf10c6
jmp 0xf114f
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl 0x1233dc(%rip), %eax    # 0x2144b8
jge 0xf113e
movl -0x84(%rbp), %esi
leaq 0x1233cd(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rax
testb $0x1, 0xa(%rax)
je 0xf1113
movl -0x84(%rbp), %esi
leaq 0x1233b2(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rdi
callq 0x10c070
movl -0x84(%rbp), %esi
leaq 0x123398(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rdi
callq 0x10bcf0
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xf10d0
leaq 0x123373(%rip), %rdi    # 0x2144b8
callq 0x10c020
callq 0xde2d0
xorps %xmm0, %xmm0
movss 0x80e26(%rip), %xmm1    # 0x171f80
callq 0xdc1c0
leaq -0x8c(%rbp), %rdi
movss 0x81662(%rip), %xmm0    # 0x1727d0
xorps %xmm1, %xmm1
callq 0xbf4c0
leaq 0x92658(%rip), %rdi     # 0x1837d5
leaq -0x8c(%rbp), %rsi
callq 0x13a930
testb $0x1, %al
jne 0xf118f
jmp 0xf11e3
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl 0x123313(%rip), %eax    # 0x2144b8
jge 0xf11d2
movl -0x90(%rbp), %esi
leaq 0x123304(%rip), %rdi    # 0x2144b8
callq 0x10bfa0
movq (%rax), %rdi
callq 0x10bcf0
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0xf1199
leaq 0x1232df(%rip), %rdi    # 0x2144b8
callq 0x10c020
callq 0xde2d0
xorps %xmm0, %xmm0
movss 0x80d92(%rip), %xmm1    # 0x171f80
callq 0xdc1c0
leaq -0x98(%rbp), %rdi
movss 0x815ce(%rip), %xmm0    # 0x1727d0
xorps %xmm1, %xmm1
callq 0xbf4c0
leaq 0x8dccf(%rip), %rdi     # 0x17eee0
leaq -0x98(%rbp), %rsi
callq 0x13a930
testb $0x1, %al
jne 0xf1223
jmp 0xf1234
leaq 0x12328e(%rip), %rdi    # 0x2144b8
callq 0x10c020
callq 0xde2d0
callq 0xde4e0
jmp 0xf123b
jmp 0xf123d
callq 0xd3b60
addq $0xa0, %rsp
popq %rbp
retq
movq -0x10(%rbp), %rdi
callq 0xd620
nopw %cs:(%rax,%rax)