// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, 0x4(%rdi)
jl 0x7c261
movq %rsi, %rbx
xorl %r15d, %r15d
xorl %edx, %edx
movq %rdi, -0x38(%rbp)
callq 0x7c2ae
movq %rbx, %rdi
callq 0x76ee2
movq -0x38(%rbp), %r8
cmpl $0x0, 0x4(%r8)
jle 0x7bec1
movslq (%rbx), %rax
testq %rax, %rax
jle 0x7bec1
movq 0x8(%rbx), %rcx
imulq $0x38, %rax, %rax
leaq (%rcx,%rax), %r14
addq $-0x38, %r14
jmp 0x7bec4
xorl %r14d, %r14d
movq %rbx, -0x40(%rbp)
testq %r14, %r14
je 0x7bed5
movl 0x20(%r14), %r15d
addl 0x1c(%r14), %r15d
xorl %r9d, %r9d
cmpl $0x2, 0x4(%r8)
movl $0x0, %r13d
jl 0x7bfee
xorl %r13d, %r13d
movl $0x1, %r12d
xorl %r9d, %r9d
movslq 0x8(%r8), %rax
cmpq %rax, %r12
jge 0x7c270
movq 0x10(%r8), %rax
movq %r12, %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rbx
movslq (%rax,%rcx), %rax
testq %rax, %rax
jle 0x7bf2d
movq 0x8(%rbx), %rcx
imulq $0x38, %rax, %rdx
cmpl $0x0, -0x18(%rcx,%rdx)
jne 0x7bf2d
decl %eax
movl %eax, (%rbx)
movslq (%rbx), %rax
testq %rax, %rax
jle 0x7bf9e
testq %r14, %r14
je 0x7bf9e
movq 0x8(%rbx), %rdi
movdqu (%r14), %xmm0
movdqu 0xc(%r14), %xmm1
movdqu (%rdi), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0xc(%rdi), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %ecx
cmpl $0xffff, %ecx           # imm = 0xFFFF
jne 0x7bf9e
movq 0x28(%rdi), %rcx
orq 0x28(%r14), %rcx
jne 0x7bf9e
movl 0x20(%rdi), %ecx
addl %ecx, 0x20(%r14)
addl 0x20(%rdi), %r15d
leaq 0x38(%rdi), %rsi
imulq $0x38, %rax, %rdx
addq $-0x38, %rdx
movl %r9d, -0x30(%rbp)
callq 0xc130
movl -0x30(%rbp), %r9d
movq -0x38(%rbp), %r8
decl (%rbx)
movslq (%rbx), %rax
testq %rax, %rax
jle 0x7bfb6
movq 0x8(%rbx), %rcx
imulq $0x38, %rax, %rdx
leaq (%rcx,%rdx), %r14
addq $-0x38, %r14
addl 0x10(%rbx), %r9d
testl %eax, %eax
jle 0x7bfdb
movq 0x8(%rbx), %rcx
imulq $0x38, %rax, %rdx
xorl %esi, %esi
movl %r15d, 0x1c(%rcx,%rsi)
addl 0x20(%rcx,%rsi), %r15d
addq $0x38, %rsi
cmpq %rsi, %rdx
jne 0x7bfc8
addl %eax, %r13d
incq %r12
movslq 0x4(%r8), %rax
cmpq %rax, %r12
jl 0x7bef5
movq -0x40(%rbp), %r12
movl (%r12), %ebx
addl %r13d, %ebx
movl 0x4(%r12), %eax
cmpl %ebx, %eax
jge 0x7c073
testl %eax, %eax
je 0x7c018
movl %eax, %r14d
shrl $0x1f, %r14d
addl %eax, %r14d
sarl %r14d
addl %eax, %r14d
jmp 0x7c01e
movl $0x8, %r14d
cmpl %ebx, %r14d
cmovlel %ebx, %r14d
cmpl %r14d, %eax
jge 0x7c073
movl %r9d, -0x30(%rbp)
movslq %r14d, %rax
imulq $0x38, %rax, %rdi
callq 0x424fb
movq %rax, %r15
movq 0x8(%r12), %rsi
testq %rsi, %rsi
je 0x7c061
movslq (%r12), %rax
imulq $0x38, %rax, %rdx
movq %r15, %rdi
callq 0xc080
movq 0x8(%r12), %rdi
callq 0x425d4
movq %r15, 0x8(%r12)
movl %r14d, 0x4(%r12)
movq -0x38(%rbp), %r8
movl -0x30(%rbp), %r9d
movl %ebx, (%r12)
movslq 0x10(%r12), %r15
movslq %r9d, %rbx
addq %rbx, %r15
movl 0x14(%r12), %eax
cmpl %r15d, %eax
jge 0x7c0fd
testl %eax, %eax
je 0x7c0a2
movl %eax, %r14d
shrl $0x1f, %r14d
addl %eax, %r14d
sarl %r14d
addl %eax, %r14d
jmp 0x7c0a8
movl $0x8, %r14d
cmpl %r15d, %r14d
cmovlel %r15d, %r14d
cmpl %r14d, %eax
jge 0x7c0fd
movq %r15, -0x30(%rbp)
movslq %r14d, %rdi
addq %rdi, %rdi
callq 0x424fb
movq %r12, %r15
movq %rax, %r12
movq 0x18(%r15), %rsi
testq %rsi, %rsi
je 0x7c0ea
movslq 0x10(%r15), %rdx
addq %rdx, %rdx
movq %r12, %rdi
callq 0xc080
movq 0x18(%r15), %rdi
callq 0x425d4
movq %r12, 0x18(%r15)
movl %r14d, 0x14(%r15)
movq -0x38(%rbp), %r8
movq %r15, %r12
movq -0x30(%rbp), %r15
movl %r15d, 0x10(%r12)
addq %r15, %r15
addq 0x18(%r12), %r15
addq %rbx, %rbx
subq %rbx, %r15
cmpl $0x2, 0x4(%r8)
jl 0x7c1be
movslq (%r12), %rax
imulq $0x38, %rax, %rax
addq 0x8(%r12), %rax
movslq %r13d, %rcx
negq %rcx
imulq $0x38, %rcx, %rcx
addq %rax, %rcx
movq %rcx, -0x30(%rbp)
movl $0x1, %ebx
movl $0x38, %r14d
movslq 0x8(%r8), %rax
cmpq %rax, %rbx
jge 0x7c270
movq %r15, %r12
movq 0x10(%r8), %r15
movslq -0x18(%r15,%r14), %rax
testq %rax, %rax
je 0x7c183
movq -0x10(%r15,%r14), %rsi
imulq $0x38, %rax, %rdx
movq %rdx, -0x48(%rbp)
movq -0x30(%rbp), %r13
movq %r13, %rdi
callq 0xc080
addq -0x48(%rbp), %r13
movq %r13, -0x30(%rbp)
movslq -0x8(%r15,%r14), %r13
testq %r13, %r13
je 0x7c1a7
movq (%r15,%r14), %rsi
leaq (,%r13,2), %rdx
movq %r12, %rdi
callq 0xc080
leaq (%r12,%r13,2), %r15
jmp 0x7c1aa
movq %r12, %r15
incq %rbx
movq -0x38(%rbp), %r8
movslq 0x4(%r8), %rax
addq $0x20, %r14
cmpq %rax, %rbx
jl 0x7c144
movq -0x40(%rbp), %rbx
movq %r15, 0x50(%rbx)
movl (%rbx), %eax
testq %rax, %rax
je 0x7c1e5
testl %eax, %eax
jle 0x7c28f
movq 0x8(%rbx), %rcx
imulq $0x38, %rax, %rax
cmpq $0x0, -0x10(%rcx,%rax)
je 0x7c1f1
movq %rbx, %rdi
callq 0x76d7e
movq -0x38(%rbp), %r8
movq 0x8(%rbx), %rax
movslq (%rbx), %rcx
imulq $0x38, %rcx, %rcx
addq %rax, %rcx
addq $-0x38, %rcx
leaq 0x88(%rbx), %rax
cmpl $0x0, 0x20(%rcx)
je 0x7c247
movdqu (%rcx), %xmm0
movdqu 0xc(%rcx), %xmm1
movdqu (%rax), %xmm2
pcmpeqb %xmm0, %xmm2
movdqu 0xc(%rax), %xmm0
pcmpeqb %xmm1, %xmm0
pand %xmm2, %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x7c259
movq %rbx, %rdi
callq 0x76d7e
movq -0x38(%rbp), %r8
jmp 0x7c259
movdqu (%rax), %xmm0
movdqu 0xc(%rax), %xmm1
movdqu %xmm1, 0xc(%rcx)
movdqu %xmm0, (%rcx)
movl $0x1, 0x4(%r8)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3f401(%rip), %rdi     # 0xbb678
leaq 0x3f40d(%rip), %rsi     # 0xbb68b
leaq 0x548fa(%rip), %rcx     # 0xd0b7f
movl $0x65c, %edx            # imm = 0x65C
callq 0xc140
leaq 0x53744(%rip), %rdi     # 0xcf9da
leaq 0x3f3ee(%rip), %rsi     # 0xbb68b
leaq 0x40958(%rip), %rcx     # 0xbcbfc
movl $0x666, %edx            # imm = 0x666
callq 0xc140