// File: /workspace/github_cpp_makefile_star2_O2/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:
cmpl $0x2, 0x4(%rdi)
jl 0x656c0
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorl %edx, %edx
callq 0x656c2
movq %r14, %rdi
callq 0x61f94
cmpl $0x0, 0x4(%rbx)
movq %r14, -0x48(%rbp)
jle 0x65465
cmpl $0x0, (%r14)
jle 0x65465
movq $0x0, -0x38(%rbp)
movq %r14, %rdi
callq 0x4b5ee
movq %rax, %r8
movl 0x20(%rax), %r15d
addl 0x1c(%rax), %r15d
jmp 0x65473
movq $0x0, -0x38(%rbp)
xorl %r8d, %r8d
xorl %r15d, %r15d
leaq 0x8(%rbx), %rax
movq %rax, -0x50(%rbp)
pushq $0x1
popq %r13
xorl %r12d, %r12d
movq %rbx, -0x30(%rbp)
cmpl 0x4(%rbx), %r13d
jge 0x65593
movq %r8, -0x58(%rbp)
movl %r12d, -0x3c(%rbp)
movq -0x50(%rbp), %rdi
movl %r13d, %esi
callq 0x6f49a
movq %rax, %r14
cmpl $0x0, (%rax)
jle 0x654c2
movq %r14, %rdi
callq 0x4b5ee
cmpl $0x0, 0x20(%rax)
jne 0x654c2
movq %r14, %rdi
callq 0x6f2b4
movl (%r14), %eax
testl %eax, %eax
setg %cl
movq -0x58(%rbp), %r8
testq %r8, %r8
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x6553b
movl %r13d, %r12d
movq %r14, %rdi
xorl %esi, %esi
movq %r8, %r13
callq 0x4b5b8
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
pushq $0x1c
popq %rdx
callq 0xc5f0
testl %eax, %eax
je 0x65509
movq -0x30(%rbp), %rbx
movq %r13, %r8
jmp 0x65535
movq 0x28(%rbx), %rax
orq 0x28(%r13), %rax
movq %r13, %r8
jne 0x65531
movl 0x20(%rbx), %eax
addl %eax, 0x20(%r8)
addl 0x20(%rbx), %r15d
movq 0x8(%r14), %rsi
movq %r14, %rdi
callq 0x6f566
movq -0x58(%rbp), %r8
movq -0x30(%rbp), %rbx
movl %r12d, %r13d
movl (%r14), %eax
testl %eax, %eax
jle 0x6554d
movq %r14, %rdi
callq 0x4b5ee
movq %rax, %r8
movl (%r14), %eax
movl 0x10(%r14), %ecx
movq 0x8(%r14), %rdx
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
imulq $0x38, %rsi, %rsi
xorl %edi, %edi
movl -0x3c(%rbp), %r12d
cmpq %rdi, %rsi
je 0x6557e
movl %r15d, 0x1c(%rdx,%rdi)
addl 0x20(%rdx,%rdi), %r15d
addq $0x38, %rdi
jmp 0x65569
addl %eax, %r12d
movq -0x38(%rbp), %rax
addl %ecx, %eax
movq %rax, -0x38(%rbp)
incl %r13d
jmp 0x65486
movq -0x48(%rbp), %r15
movl (%r15), %esi
addl %r12d, %esi
movq %r15, %rdi
callq 0x6f004
leaq 0x10(%r15), %rdi
movl 0x10(%r15), %esi
movq -0x38(%rbp), %r13
addl %r13d, %esi
callq 0x49a10
movslq (%r15), %rax
imulq $0x38, %rax, %rax
addq 0x8(%r15), %rax
movslq %r12d, %rcx
negq %rcx
imulq $0x38, %rcx, %r14
addq %rax, %r14
movslq 0x10(%r15), %r12
addq %r12, %r12
addq 0x18(%r15), %r12
movslq %r13d, %rax
addq %rax, %rax
subq %rax, %r12
pushq $0x1
popq %r13
cmpl 0x4(%rbx), %r13d
jge 0x65642
movq -0x50(%rbp), %rdi
movl %r13d, %esi
callq 0x6f49a
movq %rax, %rbx
movslq (%rax), %rax
testq %rax, %rax
je 0x6561c
movq 0x8(%rbx), %rsi
imulq $0x38, %rax, %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0xc080
addq %r15, %r14
movslq 0x10(%rbx), %r15
testq %r15, %r15
je 0x65639
movq 0x18(%rbx), %rsi
leaq (%r15,%r15), %rdx
movq %r12, %rdi
callq 0xc080
leaq (%r12,%r15,2), %r12
movq -0x30(%rbp), %rbx
incl %r13d
jmp 0x655e9
movq -0x48(%rbp), %r14
movq %r12, 0x50(%r14)
cmpl $0x0, (%r14)
je 0x6565f
movq %r14, %rdi
callq 0x4b5ee
cmpq $0x0, 0x28(%rax)
je 0x65667
movq %r14, %rdi
callq 0x61f16
movq 0x8(%r14), %rax
movslq (%r14), %rcx
imulq $0x38, %rcx, %rcx
leaq (%rax,%rcx), %rdi
addq $-0x38, %rdi
leaq 0x88(%r14), %rsi
cmpl $0x0, 0x20(%rdi)
je 0x6569d
pushq $0x1c
popq %rdx
callq 0xc5f0
testl %eax, %eax
je 0x656ab
movq %r14, %rdi
callq 0x61f16
jmp 0x656ab
movups (%rsi), %xmm0
movups 0xc(%rsi), %xmm1
movups %xmm1, 0xc(%rdi)
movups %xmm0, (%rdi)
movl $0x1, 0x4(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop