// File: /workspace/github_cpp_makefile_star2_O2/Fohlen[P]cubescript-standalone/command.cpp
// Function: sortlist(char*, ident*, ident*, unsigned int*)
void sortlist(char *list, ident *x, ident *y, uint *body)
{
    if(x == y || x->type != ID_ALIAS || y->type != ID_ALIAS) return;

    vector<sortitem> items;
    int macrolen = strlen(list), total = 0;
    char *macros = newstring(list, macrolen);
    const char *curlist = list, *start, *end, *quotestart, *quoteend;
    while(parselist(curlist, start, end, quotestart, quoteend))
    {
        macros[end - list] = '\0';
        sortitem item = { &macros[start - list], quotestart, quoteend };
        items.add(item);
        total += int(quoteend - quotestart);
    } 

    identstack xstack, ystack;
    pusharg(*x, nullval, xstack); x->flags &= ~IDF_UNKNOWN;
    pusharg(*y, nullval, ystack); y->flags &= ~IDF_UNKNOWN;

    sortfun f = { x, y, body };
    items.sort(f);

    poparg(*x);
    poparg(*y);
    
    char *sorted = macros;
    int sortedlen = total + max(items.length() - 1, 0);
    if(macrolen < sortedlen)
    {
        delete[] macros;
        sorted = newstring(sortedlen);
    }

    int offset = 0;
    loopv(items)
    {
        sortitem &item = items[i];
        int len = int(item.quoteend - item.quotestart);
        if(i) sorted[offset++] = ' ';
        memcpy(&sorted[offset], item.quotestart, len);
        offset += len;
    }
    sorted[offset] = '\0';
 
    commandret->setstr(sorted);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
cmpq %rdx, %rsi
je 0x8ed5
cmpl $0x4, (%rsi)
jne 0x8ed5
cmpl $0x4, (%rdx)
jne 0x8ed5
movq %rdi, %r12
movq %rcx, -0x68(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdx, -0x78(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
callq 0x20a0
movq %rax, -0x60(%rbp)
movslq %eax, %rsi
movq %r12, %rdi
callq 0xd339
movq %rax, %rbx
leaq -0xa0(%rbp), %rax
movq %r12, (%rax)
xorl %r13d, %r13d
leaq -0xa0(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0x90(%rbp), %rdx
leaq -0x88(%rbp), %rcx
leaq -0x80(%rbp), %r8
callq 0x7b77
testb %al, %al
je 0x8dab
movq -0x90(%rbp), %rax
subq %r12, %rax
movb $0x0, (%rbx,%rax)
movq -0x98(%rbp), %rax
subq %r12, %rax
addq %rbx, %rax
movq %rax, -0x58(%rbp)
movq -0x88(%rbp), %r15
movq %r15, -0x50(%rbp)
movq -0x80(%rbp), %r14
movq %r14, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xd3c0
subl %r15d, %r14d
addl %r14d, %r13d
jmp 0x8d3b
leaq 0x108fe(%rip), %r14     # 0x196b0
leaq -0xd0(%rbp), %rdx
movq -0x70(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3b34
andb $-0x21, 0x38(%r15)
leaq -0xb8(%rbp), %rdx
movq -0x78(%rbp), %rdi
movq %r14, %rsi
movq %rdi, %r14
callq 0x3b34
andb $-0x21, 0x38(%r14)
movq %r15, -0x58(%rbp)
movq %r14, -0x50(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x34(%rbp), %rax
imulq $0x18, %rax, %rsi
addq %rdi, %rsi
movq -0x48(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x58(%rbp), %xmm0
movups %xmm0, (%rsp)
callq 0xcc96
movq %r15, %rdi
callq 0x3b64
movq %r14, %rdi
callq 0x3b64
movl -0x34(%rbp), %eax
cmpl $0x2, %eax
pushq $0x1
popq %rcx
cmovgel %eax, %ecx
leal (%rcx,%r13), %r14d
decl %r14d
cmpl -0x60(%rbp), %r14d
jle 0x8e5e
testq %rbx, %rbx
je 0x8e53
movq %rbx, %rdi
callq 0xf9a8
movslq %r14d, %rdi
callq 0xd31f
movq %rax, %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r14d, %r14d
movslq -0x34(%rbp), %rax
movslq %r14d, %rdx
cmpq %rax, %r12
jge 0x8eb7
movq -0x40(%rbp), %rdi
movq 0x8(%rdi,%r15), %rcx
movq 0x10(%rdi,%r15), %rax
movq %rcx, %rsi
testq %r15, %r15
je 0x8e96
leal 0x1(%rdx), %r14d
movb $0x20, (%rbx,%rdx)
movq 0x8(%rdi,%r15), %rsi
subq %rcx, %rax
movslq %r14d, %r13
leaq (%rbx,%r13), %rdi
movslq %eax, %r14
movq %r14, %rdx
callq 0x2150
addl %r13d, %r14d
incq %r12
addq $0x18, %r15
jmp 0x8e67
movb $0x0, (%rbx,%rdx)
movq 0x1035e(%rip), %rax     # 0x19220
movl $0x3, 0x8(%rax)
movq %rbx, (%rax)
leaq -0x40(%rbp), %rdi
callq 0xd414
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8eed
jmp 0x8eed
jmp 0x8eed
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0xd414
movq %rbx, %rdi
callq 0x23c0
nop