// File: Ciaxur[P]yagb-emu/include/imgui/imgui_demo.cpp
// Function: ExampleAppConsole::TextEditCallback(ImGuiInputTextCallbackData*)
int     TextEditCallback(ImGuiInputTextCallbackData* data)
    {
        //AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd);
        switch (data->EventFlag)
        {
        case ImGuiInputTextFlags_CallbackCompletion:
            {
                // Example of TEXT COMPLETION

                // Locate beginning of current word
                const char* word_end = data->Buf + data->CursorPos;
                const char* word_start = word_end;
                while (word_start > data->Buf)
                {
                    const char c = word_start[-1];
                    if (c == ' ' || c == '\t' || c == ',' || c == ';')
                        break;
                    word_start--;
                }

                // Build a list of candidates
                ImVector<const char*> candidates;
                for (int i = 0; i < Commands.Size; i++)
                    if (Strnicmp(Commands[i], word_start, (int)(word_end - word_start)) == 0)
                        candidates.push_back(Commands[i]);

                if (candidates.Size == 0)
                {
                    // No match
                    AddLog("No match for \"%.*s\"!\n", (int)(word_end - word_start), word_start);
                }
                else if (candidates.Size == 1)
                {
                    // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing.
                    data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
                    data->InsertChars(data->CursorPos, candidates[0]);
                    data->InsertChars(data->CursorPos, " ");
                }
                else
                {
                    // Multiple matches. Complete as much as we can..
                    // So inputing "C"+Tab will complete to "CL" then display "CLEAR" and "CLASSIFY" as matches.
                    int match_len = (int)(word_end - word_start);
                    for (;;)
                    {
                        int c = 0;
                        bool all_candidates_matches = true;
                        for (int i = 0; i < candidates.Size && all_candidates_matches; i++)
                            if (i == 0)
                                c = toupper(candidates[i][match_len]);
                            else if (c == 0 || c != toupper(candidates[i][match_len]))
                                all_candidates_matches = false;
                        if (!all_candidates_matches)
                            break;
                        match_len++;
                    }

                    if (match_len > 0)
                    {
                        data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start));
                        data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len);
                    }

                    // List matches
                    AddLog("Possible matches:\n");
                    for (int i = 0; i < candidates.Size; i++)
                        AddLog("- %s\n", candidates[i]);
                }

                break;
            }
        case ImGuiInputTextFlags_CallbackHistory:
            {
                // Example of HISTORY
                const int prev_history_pos = HistoryPos;
                if (data->EventKey == ImGuiKey_UpArrow)
                {
                    if (HistoryPos == -1)
                        HistoryPos = History.Size - 1;
                    else if (HistoryPos > 0)
                        HistoryPos--;
                }
                else if (data->EventKey == ImGuiKey_DownArrow)
                {
                    if (HistoryPos != -1)
                        if (++HistoryPos >= History.Size)
                            HistoryPos = -1;
                }

                // A better implementation would preserve the data on the current input line along with cursor position.
                if (prev_history_pos != HistoryPos)
                {
                    const char* history_str = (HistoryPos >= 0) ? History[HistoryPos] : "";
                    data->DeleteChars(0, data->BufTextLen);
                    data->InsertChars(0, history_str);
                }
            }
        }
        return 0;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
subl $0x40, %eax
je 0x10a70e
jmp 0x10a6fb
movl -0x74(%rbp), %eax
subl $0x80, %eax
je 0x10aafb
jmp 0x10ac21
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x2c(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jbe 0x10a779
movq -0x20(%rbp), %rax
movb -0x1(%rax), %al
movb %al, -0x21(%rbp)
movsbl -0x21(%rbp), %eax
cmpl $0x20, %eax
je 0x10a769
movsbl -0x21(%rbp), %eax
cmpl $0x9, %eax
je 0x10a769
movsbl -0x21(%rbp), %eax
cmpl $0x2c, %eax
je 0x10a769
movsbl -0x21(%rbp), %eax
cmpl $0x3b, %eax
jne 0x10a76b
jmp 0x10a779
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x10a72d
leaq -0x38(%rbp), %rdi
callq 0x109da0
movl $0x0, -0x3c(%rbp)
movq -0x80(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x110(%rcx), %eax
jge 0x10a83f
movq -0x80(%rbp), %rdi
addq $0x110, %rdi            # imm = 0x110
movl -0x3c(%rbp), %esi
callq 0x10ace0
movq %rax, -0x88(%rbp)
jmp 0x10a7b8
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x18(%rbp), %edx
movl %esi, %eax
subl %eax, %edx
callq 0x10ac40
movl %eax, -0x8c(%rbp)
jmp 0x10a7da
movl -0x8c(%rbp), %eax
cmpl $0x0, %eax
jne 0x10a82f
movq -0x80(%rbp), %rdi
addq $0x110, %rdi            # imm = 0x110
movl -0x3c(%rbp), %esi
callq 0x10ace0
movq %rax, -0x98(%rbp)
jmp 0x10a801
movq -0x98(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x109e40
jmp 0x10a813
jmp 0x10a82f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x109ff0
jmp 0x10ac2c
jmp 0x10a831
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x10a789
cmpl $0x0, -0x38(%rbp)
jne 0x10a869
movq -0x80(%rbp), %rdi
movl -0x18(%rbp), %edx
movq -0x20(%rbp), %rcx
movl %ecx, %eax
subl %eax, %edx
leaq 0x78339(%rip), %rsi     # 0x182b94
xorl %eax, %eax
callq 0x109ea0
jmp 0x10a864
jmp 0x10aaed
cmpl $0x1, -0x38(%rbp)
jne 0x10a8f5
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %eax
movl 0x18(%rdi), %ecx
movl %eax, %esi
subl %ecx, %esi
movl -0x18(%rbp), %edx
subl %eax, %edx
callq 0x14b3d0
jmp 0x10a88d
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl 0x2c(%rax), %eax
movl %eax, -0xa4(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x10ace0
movq %rax, -0xa0(%rbp)
jmp 0x10a8b5
movl -0xa4(%rbp), %esi
movq -0xb0(%rbp), %rdi
movq -0xa0(%rbp), %rax
movq (%rax), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x14b4e0
jmp 0x10a8d7
movq -0x10(%rbp), %rdi
movl 0x2c(%rdi), %esi
leaq 0x6b98b(%rip), %rdx     # 0x176270
xorl %eax, %eax
movl %eax, %ecx
callq 0x14b4e0
jmp 0x10a8f0
jmp 0x10aaeb
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movb $0x1, -0x55(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x38(%rbp), %ecx
movb %al, -0xb1(%rbp)
jge 0x10a92e
movb -0x55(%rbp), %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0x10a93d
jmp 0x10a9d1
cmpl $0x0, -0x5c(%rbp)
jne 0x10a974
movl -0x5c(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x10ace0
movq %rax, -0xc0(%rbp)
jmp 0x10a958
movq -0xc0(%rbp), %rax
movq (%rax), %rax
movslq -0x50(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0xd1a0
movl %eax, -0x54(%rbp)
jmp 0x10a9c1
cmpl $0x0, -0x54(%rbp)
je 0x10a9bb
movl -0x54(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0x5c(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x10ace0
movq %rax, -0xc8(%rbp)
jmp 0x10a998
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movslq -0x50(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0xd1a0
movl %eax, %ecx
movl -0xcc(%rbp), %eax
cmpl %ecx, %eax
je 0x10a9bf
movb $0x0, -0x55(%rbp)
jmp 0x10a9c1
jmp 0x10a9c3
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x10a915
testb $0x1, -0x55(%rbp)
jne 0x10a9d9
jmp 0x10a9e7
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x10a903
cmpl $0x0, -0x50(%rbp)
jle 0x10aa86
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %eax
movl 0x18(%rdi), %ecx
movl %eax, %esi
subl %ecx, %esi
movl -0x18(%rbp), %edx
subl %eax, %edx
callq 0x14b3d0
jmp 0x10aa0b
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl 0x2c(%rax), %eax
movl %eax, -0xdc(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x10ace0
movq %rax, -0xd8(%rbp)
jmp 0x10aa33
movq -0xd8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x10ace0
movq %rax, -0xf0(%rbp)
jmp 0x10aa58
movq -0xf8(%rbp), %rdx
movl -0xdc(%rbp), %esi
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rax
movq (%rax), %rcx
movslq -0x50(%rbp), %rax
addq %rax, %rcx
callq 0x14b4e0
jmp 0x10aa84
jmp 0x10aa86
movq -0x80(%rbp), %rdi
leaq 0x78119(%rip), %rsi     # 0x182baa
xorl %eax, %eax
callq 0x109ea0
jmp 0x10aa9a
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x10aae9
movl -0x60(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x10ace0
movq %rax, -0x100(%rbp)
jmp 0x10aabe
movq -0x80(%rbp), %rdi
movq -0x100(%rbp), %rax
movq (%rax), %rdx
leaq 0x780ea(%rip), %rsi     # 0x182bbd
xorl %eax, %eax
callq 0x109ea0
jmp 0x10aadc
jmp 0x10aade
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x10aaa1
jmp 0x10aaeb
jmp 0x10aaed
leaq -0x38(%rbp), %rdi
callq 0x109ff0
jmp 0x10ac21
movq -0x80(%rbp), %rax
movl 0x130(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, 0x14(%rax)
jne 0x10ab58
movq -0x80(%rbp), %rax
cmpl $-0x1, 0x130(%rax)
jne 0x10ab34
movq -0x80(%rbp), %rax
movl 0x120(%rax), %ecx
subl $0x1, %ecx
movl %ecx, 0x130(%rax)
jmp 0x10ab56
movq -0x80(%rbp), %rax
cmpl $0x0, 0x130(%rax)
jle 0x10ab54
movq -0x80(%rbp), %rax
movl 0x130(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x130(%rax)
jmp 0x10ab56
jmp 0x10ab9e
movq -0x10(%rbp), %rax
cmpl $0x4, 0x14(%rax)
jne 0x10ab9c
movq -0x80(%rbp), %rax
cmpl $-0x1, 0x130(%rax)
je 0x10ab9a
movq -0x80(%rbp), %rcx
movl 0x130(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x130(%rcx)
cmpl 0x120(%rcx), %eax
jl 0x10ab98
movq -0x80(%rbp), %rax
movl $0xffffffff, 0x130(%rax) # imm = 0xFFFFFFFF
jmp 0x10ab9a
jmp 0x10ab9c
jmp 0x10ab9e
movq -0x80(%rbp), %rcx
movl -0x64(%rbp), %eax
cmpl 0x130(%rcx), %eax
je 0x10ac1f
movq -0x80(%rbp), %rax
cmpl $0x0, 0x130(%rax)
jl 0x10abdf
movq -0x80(%rbp), %rax
movq %rax, %rdi
addq $0x120, %rdi            # imm = 0x120
movl 0x130(%rax), %esi
callq 0x10a030
movq (%rax), %rax
movq %rax, -0x108(%rbp)
jmp 0x10abef
leaq 0x67323(%rip), %rax     # 0x171f09
movq %rax, -0x108(%rbp)
jmp 0x10abef
movq -0x108(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x20(%rax), %edx
xorl %esi, %esi
callq 0x14b3d0
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rdx
xorl %esi, %esi
xorl %eax, %eax
movl %eax, %ecx
callq 0x14b4e0
jmp 0x10ac21
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0xd620
nopw %cs:(%rax,%rax)
nop