// File: 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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
je 0xdbe7
movq -0x10(%rbp), %rax
cmpl $0x4, (%rax)
jne 0xdbe7
movq -0x18(%rbp), %rax
cmpl $0x4, (%rax)
je 0xdbec
jmp 0xdf76
leaq -0x30(%rbp), %rdi
callq 0x16390
movq -0x8(%rbp), %rdi
callq 0x20b0
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x34(%rbp), %rsi
callq 0x16150
movq %rax, -0x120(%rbp)
jmp 0xdc1e
movq -0x120(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x68(%rbp), %rdx
leaq -0x70(%rbp), %rcx
leaq -0x78(%rbp), %r8
callq 0xc210
movb %al, -0x121(%rbp)
jmp 0xdc52
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0xdc61
jmp 0xdce2
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x8(%rbp), %rdx
subq %rdx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
movq -0x8(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x163c0
jmp 0xdcb2
movq -0x78(%rbp), %rax
movq -0x70(%rbp), %rcx
subq %rcx, %rax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0xdc31
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x16540
jmp 0xdf7f
movq -0x10(%rbp), %rdi
leaq 0x199d3(%rip), %rsi     # 0x276c0
leaq -0xa8(%rbp), %rdx
callq 0x4d10
jmp 0xdcfb
movq -0x10(%rbp), %rax
movl 0x38(%rax), %ecx
andl $-0x21, %ecx
movl %ecx, 0x38(%rax)
movq -0x18(%rbp), %rdi
leaq 0x199ad(%rip), %rsi     # 0x276c0
leaq -0xc0(%rbp), %rdx
callq 0x4d10
jmp 0xdd21
movq -0x18(%rbp), %rax
movl 0x38(%rax), %ecx
andl $-0x21, %ecx
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movups -0xd8(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
movq -0xe0(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movaps -0xf0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x16440
jmp 0xdd95
movq -0x10(%rbp), %rdi
callq 0x4d80
jmp 0xdda0
movq -0x18(%rbp), %rdi
callq 0x4d80
jmp 0xddab
movq -0x40(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x12c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x164e0
movl %eax, -0x128(%rbp)
jmp 0xddd0
movl -0x128(%rbp), %edi
decl %edi
xorl %esi, %esi
callq 0xca20
movl %eax, -0x130(%rbp)
jmp 0xdde7
movl -0x130(%rbp), %ecx
movl -0x12c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xfc(%rbp)
movl -0x34(%rbp), %eax
cmpl -0xfc(%rbp), %eax
jge 0xde46
movq -0x40(%rbp), %rax
movq %rax, -0x138(%rbp)
cmpq $0x0, %rax
je 0xde23
movq -0x138(%rbp), %rdi
callq 0x1b110
movslq -0xfc(%rbp), %rdi
callq 0x16090
movq %rax, -0x140(%rbp)
jmp 0xde38
movq -0x140(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0x100(%rbp)
movl $0x0, -0x104(%rbp)
movl -0x104(%rbp), %eax
movl %eax, -0x148(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x164e0
movl %eax, -0x144(%rbp)
jmp 0xde77
movl -0x148(%rbp), %eax
movl -0x144(%rbp), %ecx
cmpl %ecx, %eax
jge 0xdf46
movl -0x104(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x16500
movq %rax, -0x150(%rbp)
jmp 0xdea3
movq -0x150(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x110(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x114(%rbp)
cmpl $0x0, -0x104(%rbp)
je 0xdef8
movq -0xf8(%rbp), %rax
movl -0x100(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x100(%rbp)
movslq %ecx, %rcx
movb $0x20, (%rax,%rcx)
movq -0xf8(%rbp), %rdi
movslq -0x100(%rbp), %rax
addq %rax, %rdi
movq -0x110(%rbp), %rax
movq 0x8(%rax), %rsi
movslq -0x114(%rbp), %rdx
callq 0x2170
movl -0x114(%rbp), %eax
addl -0x100(%rbp), %eax
movl %eax, -0x100(%rbp)
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0xde5a
movq -0xf8(%rbp), %rax
movslq -0x100(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x192d1(%rip), %rdi     # 0x27230
movq -0xf8(%rbp), %rsi
callq 0x15b80
jmp 0xdf6d
leaq -0x30(%rbp), %rdi
callq 0x16540
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x23e0
nopl (%rax,%rax)