// File: Ciaxur[P]yagb-emu/include/imgui/imgui_draw.cpp
// Function: ImDrawListSplitter::Merge(ImDrawList*)
void ImDrawListSplitter::Merge(ImDrawList* draw_list)
{
    // Note that we never use or rely on _Channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use.
    if (_Count <= 1)
        return;

    SetCurrentChannel(draw_list, 0);
    draw_list->_PopUnusedDrawCmd();

    // Calculate our final buffer sizes. Also fix the incorrect IdxOffset values in each command.
    int new_cmd_buffer_count = 0;
    int new_idx_buffer_count = 0;
    ImDrawCmd* last_cmd = (_Count > 0 && draw_list->CmdBuffer.Size > 0) ? &draw_list->CmdBuffer.back() : NULL;
    int idx_offset = last_cmd ? last_cmd->IdxOffset + last_cmd->ElemCount : 0;
    for (int i = 1; i < _Count; i++)
    {
        ImDrawChannel& ch = _Channels[i];

        // Equivalent of PopUnusedDrawCmd() for this channel's cmdbuffer and except we don't need to test for UserCallback.
        if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0)
            ch._CmdBuffer.pop_back();

        if (ch._CmdBuffer.Size > 0 && last_cmd != NULL)
        {
            ImDrawCmd* next_cmd = &ch._CmdBuffer[0];
            if (ImDrawCmd_HeaderCompare(last_cmd, next_cmd) == 0 && last_cmd->UserCallback == NULL && next_cmd->UserCallback == NULL)
            {
                // Merge previous channel last draw command with current channel first draw command if matching.
                last_cmd->ElemCount += next_cmd->ElemCount;
                idx_offset += next_cmd->ElemCount;
                ch._CmdBuffer.erase(ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges.
            }
        }
        if (ch._CmdBuffer.Size > 0)
            last_cmd = &ch._CmdBuffer.back();
        new_cmd_buffer_count += ch._CmdBuffer.Size;
        new_idx_buffer_count += ch._IdxBuffer.Size;
        for (int cmd_n = 0; cmd_n < ch._CmdBuffer.Size; cmd_n++)
        {
            ch._CmdBuffer.Data[cmd_n].IdxOffset = idx_offset;
            idx_offset += ch._CmdBuffer.Data[cmd_n].ElemCount;
        }
    }
    draw_list->CmdBuffer.resize(draw_list->CmdBuffer.Size + new_cmd_buffer_count);
    draw_list->IdxBuffer.resize(draw_list->IdxBuffer.Size + new_idx_buffer_count);

    // Write commands and indices in order (they are fairly small structures, we don't copy vertices only indices)
    ImDrawCmd* cmd_write = draw_list->CmdBuffer.Data + draw_list->CmdBuffer.Size - new_cmd_buffer_count;
    ImDrawIdx* idx_write = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size - new_idx_buffer_count;
    for (int i = 1; i < _Count; i++)
    {
        ImDrawChannel& ch = _Channels[i];
        if (int sz = ch._CmdBuffer.Size) { memcpy(cmd_write, ch._CmdBuffer.Data, sz * sizeof(ImDrawCmd)); cmd_write += sz; }
        if (int sz = ch._IdxBuffer.Size) { memcpy(idx_write, ch._IdxBuffer.Data, sz * sizeof(ImDrawIdx)); idx_write += sz; }
    }
    draw_list->_IdxWritePtr = idx_write;

    // Ensure there's always a non-callback draw command trailing the command-buffer
    if (draw_list->CmdBuffer.Size == 0 || draw_list->CmdBuffer.back().UserCallback != NULL)
        draw_list->AddDrawCmd();

    // If current command is used with different settings we need to add a new command
    ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1];
    if (curr_cmd->ElemCount == 0)
        ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset
    else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0)
        draw_list->AddDrawCmd();

    _Count = 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpl $0x1, 0x4(%rax)
jg 0x114e46
jmp 0x115216
movq -0x78(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %edx, %edx
callq 0x115220
movq -0x10(%rbp), %rdi
callq 0x10f130
movq -0x78(%rbp), %rax
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x0, 0x4(%rax)
jle 0x114e8e
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x114e8e
movq -0x10(%rbp), %rdi
callq 0xeba20
movq %rax, -0x80(%rbp)
jmp 0x114e96
xorl %eax, %eax
movq %rax, -0x80(%rbp)
jmp 0x114e96
movq -0x80(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x114ebb
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x20(%rbp), %rcx
addl 0x20(%rcx), %eax
movl %eax, -0x84(%rbp)
jmp 0x114ec5
xorl %eax, %eax
movl %eax, -0x84(%rbp)
jmp 0x114ec5
movl -0x84(%rbp), %eax
movl %eax, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
movq -0x78(%rbp), %rcx
movl -0x28(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x115028
movq -0x78(%rbp), %rdi
addq $0x8, %rdi
movl -0x28(%rbp), %esi
callq 0x127430
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x114f1a
movq -0x30(%rbp), %rdi
callq 0xeba20
cmpl $0x0, 0x20(%rax)
jne 0x114f1a
movq -0x30(%rbp), %rdi
callq 0x1270c0
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x114f97
cmpq $0x0, -0x20(%rbp)
je 0x114f97
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0xeb940
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x1c, %edx
callq 0xd9f0
cmpl $0x0, %eax
jne 0x114f95
movq -0x20(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x114f95
movq -0x38(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x114f95
movq -0x38(%rbp), %rax
movl 0x20(%rax), %ecx
movq -0x20(%rbp), %rax
addl 0x20(%rax), %ecx
movl %ecx, 0x20(%rax)
movq -0x38(%rbp), %rax
movl 0x20(%rax), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x1275d0
jmp 0x114f97
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x114fad
movq -0x30(%rbp), %rdi
callq 0xeba20
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x30(%rbp), %rax
movl 0x10(%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x115018
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rdx
imulq $0x38, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x3c(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x114fcd
jmp 0x11501a
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x114ed5
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
addl -0x14(%rbp), %esi
callq 0x126c60
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %esi
addl -0x18(%rbp), %esi
callq 0xe7f00
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movslq (%rcx), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movslq -0x14(%rbp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x10(%rcx), %rcx
shlq %rcx
addq %rcx, %rax
movslq -0x18(%rbp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl $0x1, -0x54(%rbp)
movq -0x78(%rbp), %rcx
movl -0x54(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x115158
movq -0x78(%rbp), %rdi
addq $0x8, %rdi
movl -0x54(%rbp), %esi
callq 0x127430
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
je 0x11510c
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rsi
movslq -0x64(%rbp), %rax
imulq $0x38, %rax, %rdx
callq 0xd0a0
movl -0x64(%rbp), %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x60(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x115148
movq -0x50(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rsi
movslq -0x68(%rbp), %rdx
shlq %rdx
callq 0xd0a0
movl -0x68(%rbp), %ecx
movq -0x50(%rbp), %rax
movslq %ecx, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
jmp 0x11514a
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x1150ab
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
je 0x11517d
movq -0x10(%rbp), %rdi
callq 0xeba20
cmpq $0x0, 0x28(%rax)
je 0x115186
movq -0x10(%rbp), %rdi
callq 0x10f070
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x1151e2
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x88(%rcx), %rdx
movq %rdx, (%rax)
movq 0x90(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x98(%rcx), %rdx
movq %rdx, 0x10(%rax)
movl 0xa0(%rcx), %ecx
movl %ecx, 0x18(%rax)
jmp 0x11520b
movq -0x70(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x88, %rsi
movl $0x1c, %edx
callq 0xd9f0
cmpl $0x0, %eax
je 0x115209
movq -0x10(%rbp), %rdi
callq 0x10f070
jmp 0x11520b
movq -0x78(%rbp), %rax
movl $0x1, 0x4(%rax)
addq $0x90, %rsp
popq %rbp
retq
nop