// File: /workspace/github_cpp_makefile_star2_O3/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 0xca8c
cmpl $0x4, (%rsi)
jne 0xca8c
cmpl $0x4, (%rdx)
jne 0xca8c
movq %rdi, %r15
movq %rcx, -0x90(%rbp)
movq %rsi, -0x98(%rbp)
movq %rdx, -0x48(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
callq 0x2080
movq %rax, %r13
movslq %r13d, %r14
incq %r14
movq %r14, %rdi
callq 0x15705
movq %r13, -0x88(%rbp)
incq %r13
cmpq %r14, %r13
cmovaeq %r14, %r13
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2130
movq %r14, %rbx
movb $0x0, -0x1(%r14,%r13)
leaq -0xa0(%rbp), %rdi
movq %r15, (%rdi)
leaq -0x68(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x58(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0xa8fd
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testb %al, %al
je 0xc7fa
xorl %r12d, %r12d
movq -0x60(%rbp), %rax
subq %r15, %rax
movb $0x0, (%rbx,%rax)
movq -0x68(%rbp), %rax
subq %r15, %rax
addq %rbx, %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %r14
movq %r14, -0x78(%rbp)
movq -0x50(%rbp), %r13
movq %r13, -0x70(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x12dc6
subl %r14d, %r13d
addl %r13d, %r12d
leaq -0xa0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x58(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0xa8fd
testb %al, %al
jne 0xc79b
decl %r12d
movq -0x98(%rbp), %r13
movq 0x20(%r13), %rax
leaq -0xd0(%rbp), %rcx
movq %rax, (%rcx)
movl 0x10(%r13), %eax
movl %eax, 0x8(%rcx)
movq 0x28(%r13), %rax
movq %rax, 0x10(%rcx)
movq %rcx, 0x28(%r13)
movl 0x12e80(%rip), %eax     # 0x1f6a8
movl %eax, 0x10(%r13)
movq 0x12e6d(%rip), %rcx     # 0x1f6a0
movq %rcx, 0x20(%r13)
movq 0x18(%r13), %rdx
testq %rdx, %rdx
movq -0x48(%rbp), %r15
je 0xc870
addl $0xffffff00, (%rdx)     # imm = 0xFFFFFF00
movq 0x18(%r13), %rdi
cmpl $0xff, (%rdi)
jg 0xc85b
callq 0x1572b
movq $0x0, 0x18(%r13)
movl 0x12e3f(%rip), %eax     # 0x1f6a8
movq 0x12e30(%rip), %rcx     # 0x1f6a0
andb $-0x21, 0x38(%r13)
movq 0x20(%r15), %rdx
leaq -0xb8(%rbp), %rsi
movq %rdx, (%rsi)
movl 0x10(%r15), %edx
movl %edx, 0x8(%rsi)
movq 0x28(%r15), %rdx
movq %rdx, 0x10(%rsi)
movq %rsi, 0x28(%r15)
movl %eax, 0x10(%r15)
movq %rcx, 0x20(%r15)
movq 0x18(%r15), %rax
testq %rax, %rax
je 0xc8c6
addl $0xffffff00, (%rax)     # imm = 0xFFFFFF00
movq 0x18(%r15), %rdi
cmpl $0xff, (%rdi)
jg 0xc8be
callq 0x1572b
movq $0x0, 0x18(%r15)
andb $-0x21, 0x38(%r15)
movq %r13, -0x80(%rbp)
movq %r15, -0x78(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rdi
movslq -0x34(%rbp), %rax
leaq (%rax,%rax,2), %rax
leaq (%rdi,%rax,8), %rsi
movq -0x70(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x80(%rbp), %xmm0
movups %xmm0, (%rsp)
callq 0x126c9
movq 0x28(%r13), %r14
testq %r14, %r14
je 0xc961
cmpl $0x3, 0x10(%r13)
jne 0xc922
movq 0x20(%r13), %rdi
testq %rdi, %rdi
je 0xc922
callq 0x1572b
movl 0x8(%r14), %eax
movl %eax, 0x10(%r13)
movq (%r14), %rax
movq %rax, 0x20(%r13)
movq 0x18(%r13), %rax
testq %rax, %rax
je 0xc959
addl $0xffffff00, (%rax)     # imm = 0xFFFFFF00
movq 0x18(%r13), %rdi
cmpl $0xff, (%rdi)
jg 0xc951
callq 0x1572b
movq $0x0, 0x18(%r13)
movq 0x10(%r14), %rax
movq %rax, 0x28(%r13)
movq 0x28(%r15), %r14
testq %r14, %r14
je 0xc9be
cmpl $0x3, 0x10(%r15)
jne 0xc97f
movq 0x20(%r15), %rdi
testq %rdi, %rdi
je 0xc97f
callq 0x1572b
movl 0x8(%r14), %eax
movl %eax, 0x10(%r15)
movq (%r14), %rax
movq %rax, 0x20(%r15)
movq 0x18(%r15), %rax
testq %rax, %rax
je 0xc9b6
addl $0xffffff00, (%rax)     # imm = 0xFFFFFF00
movq 0x18(%r15), %rdi
cmpl $0xff, (%rdi)
jg 0xc9ae
callq 0x1572b
movq $0x0, 0x18(%r15)
movq 0x10(%r14), %rax
movq %rax, 0x28(%r15)
movl -0x34(%rbp), %edx
cmpl $0x2, %edx
movl $0x1, %ecx
cmovgel %edx, %ecx
addl %ecx, %r12d
cmpl -0x88(%rbp), %r12d
jle 0xc9f3
movq %rbx, %rdi
callq 0x1572b
movslq %r12d, %rdi
incq %rdi
callq 0x15705
movq %rax, %r12
movl -0x34(%rbp), %edx
jmp 0xc9f6
movq %rbx, %r12
xorl %r14d, %r14d
testl %edx, %edx
jle 0xca5b
movq -0x40(%rbp), %r15
movl %edx, %ecx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, -0x48(%rbp)
xorl %r13d, %r13d
movq 0x8(%r15,%r13), %rcx
movq 0x10(%r15,%r13), %r8
testq %r13, %r13
je 0xca33
movslq %r14d, %rdx
incl %r14d
movb $0x20, (%r12,%rdx)
movq 0x8(%r15,%r13), %rsi
jmp 0xca36
movq %rcx, %rsi
subq %rcx, %r8
movslq %r14d, %rbx
leaq (%r12,%rbx), %rdi
movslq %r8d, %r14
movq %r14, %rdx
callq 0x2130
addl %ebx, %r14d
addq $0x18, %r13
cmpq %r13, -0x48(%rbp)
jne 0xca12
movslq %r14d, %r14
movb $0x0, (%r12,%r14)
movq 0x127a1(%rip), %rcx     # 0x1f208
movl $0x3, 0x8(%rcx)
movq %r12, (%rcx)
cmpl $0x0, -0x34(%rbp)
jle 0xca7e
movl $0x0, -0x34(%rbp)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xca8c
callq 0x1572b
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xcaa7
jmp 0xcaa7
movq %rax, %rbx
jmp 0xcab7
movq %rax, %rbx
cmpl $0x0, -0x34(%rbp)
jle 0xcab7
movl $0x0, -0x34(%rbp)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xcac5
callq 0x1572b
movq %rbx, %rdi
callq 0x23a0
nop