// File: /workspace/github_c_makefile_star2_O3/Ziddykins[P]Dawn/src/cmdsys.c
// Function: sort_cmd_sys
void sort_cmd_sys(CmdSys cs) {
    size_t const SHIFT = 4;
    uint64_t const BITMASK = (1<<SHIFT)-1;
    unsigned short const HASH_BITLEN = 64;

    struct cmd_sys * ccs = cs ? cs : commands;
    assert(ccs);
    assert(ccs->capacity == ccs->len);
    if(ccs->len <= 1) {
        return;
    }

    size_t * positions;
    CALLEXIT(!(positions = malloc(ccs->len * sizeof *positions)))
    for(size_t i = 0; i < ccs->len; i++) {
        positions[i] = i;
    }

    size_t * buckets;
    CALLEXIT(!(buckets = malloc(ccs->len * (BITMASK+1) * sizeof *buckets))) //actually a 2d array

    size_t * idx;
    CALLEXIT(!(idx = calloc(BITMASK+1, sizeof *idx)))

    size_t bitmask = BITMASK;
    for(size_t i = 0; i < HASH_BITLEN / SHIFT; i++) {

        for(size_t j = 0; j < ccs->len; j++) {
            size_t pos = positions[j];
            size_t bucket = (ccs->hashes[pos] & bitmask) >> (i*SHIFT);

            buckets[ccs->len * bucket + idx[bucket]] = pos;
            idx[bucket]++;
        }

        size_t c = 0;
        for(size_t j = 0; j <= BITMASK; j++) {
            for(size_t k = 0; k < idx[j]; k++) {
                positions[c] = buckets[ccs->len * j + k];
                c++;
            }
            idx[j] = 0;
        }
        bitmask <<= SHIFT;
    }

    free(buckets);
    free(idx);

    uint64_t * hashes;
    CALLEXIT(!(hashes = calloc(ccs->len, sizeof *ccs->hashes)))
    char ** cmds;
    CALLEXIT(!(cmds = calloc(ccs->len, sizeof *ccs->cmds)))
    char ** helptexts;
    CALLEXIT(!(helptexts = calloc(ccs->len, sizeof *ccs->helptexts)))
    int * auth_levels;
    CALLEXIT(!(auth_levels = calloc(ccs->len, sizeof *ccs->auth_levels)))
    cmd_func *fn;
    CALLEXIT(!(fn = calloc(ccs->len, sizeof *ccs->fn)))

    for(size_t i = 0; i < ccs->len; i++) {
        hashes[i] = ccs->hashes[i];
        cmds[i] = ccs->cmds[i];
        helptexts[i] = ccs->helptexts[i];
        auth_levels[i] = ccs->auth_levels[i];
        fn[i] = ccs->fn[i];
    }

    for(size_t i = 0; i < ccs->len; i++) {
        ccs->hashes[i] = hashes[positions[i]];
        ccs->cmds[i] = cmds[positions[i]];
        ccs->helptexts[i] = helptexts[positions[i]];
        ccs->auth_levels[i] = auth_levels[positions[i]];
        ccs->fn[i] = fn[positions[i]];
    }

    free(hashes);
    free(cmds);
    free(helptexts);
    free(auth_levels);
    free(fn);
    free(positions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
testq %rdi, %rdi
jne 0x3eef
movq 0x1b389(%rip), %rbx     # 0x1f278
testq %rbx, %rbx
je 0x4286
movq 0x8(%rbx), %r15
cmpq (%rbx), %r15
jne 0x42a5
cmpq $0x2, %r15
jae 0x3f1a
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (,%r15,8), %rdi
movq %rdi, 0x28(%rsp)
callq 0x22b0
testq %rax, %rax
je 0x42c4
leaq 0x1(%r15), %rsi
andq $-0x2, %rsi
leaq -0x1(%r15), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0xd34d(%rip), %xmm1     # 0x112a0
xorl %ecx, %ecx
movdqa 0xd353(%rip), %xmm2     # 0x112b0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0xd353(%rip), %xmm4     # 0x112c0
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x3fa2
movq %rcx, (%rax,%rcx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x3fb9
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rsi
jne 0x3f6d
movq %rax, 0x20(%rsp)
movq %r15, %rdi
shlq $0x7, %rdi
callq 0x22b0
testq %rax, %rax
je 0x42c9
movq %rax, %r13
movl $0x10, %edi
movl $0x8, %esi
callq 0x2110
testq %rax, %rax
je 0x42ce
movq %rax, %rbp
movq %rbx, 0x48(%rsp)
movq 0x10(%rbx), %r10
movl $0xf, %r14d
xorl %eax, %eax
movq 0x20(%rsp), %r9
movq %r15, 0x50(%rsp)
movq %r13, 0x18(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq (,%rax,4), %rcx
xorl %eax, %eax
movq (%r9,%rax,8), %rdx
movq (%r10,%rdx,8), %rsi
andq %r14, %rsi
shrq %cl, %rsi
movq %rsi, %rdi
imulq %r15, %rdi
movq (%rbp,%rsi,8), %r8
leaq (,%rdi,8), %rdi
addq %r13, %rdi
movq %rdx, (%rdi,%r8,8)
incq %r8
movq %r8, (%rbp,%rsi,8)
incq %rax
cmpq %rax, %r15
jne 0x4032
xorl %r15d, %r15d
xorl %ebx, %ebx
movq (%rbp,%r15,8), %r12
testq %r12, %r12
je 0x4096
leaq (%r9,%rbx,8), %rdi
leaq (,%r12,8), %rdx
movq %r13, %rsi
callq 0x2170
movq 0x20(%rsp), %r9
addq %r12, %rbx
movq $0x0, (%rbp,%r15,8)
incq %r15
addq 0x28(%rsp), %r13
cmpq $0x10, %r15
jne 0x4070
shlq $0x4, %r14
movq 0x8(%rsp), %rax
incq %rax
cmpq $0x10, %rax
movq 0x50(%rsp), %r15
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %r10
jne 0x4023
movq %r13, %rdi
callq 0x2240
movq %rbp, %rdi
callq 0x2240
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x42d3
movq %rax, %r12
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
movq %rax, 0x8(%rsp)
testq %rax, %rax
movq 0x48(%rsp), %r14
je 0x42d8
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x42dd
movq %rax, %rbx
movl $0x4, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x42e2
movq %rax, %rbp
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x42e7
movq %rax, %r13
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rax
movq %rax, 0x30(%rsp)
movq 0x20(%r14), %rax
movq %rax, 0x38(%rsp)
movq 0x28(%r14), %rax
movq %rax, 0x40(%rsp)
movq 0x30(%r14), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq %r12, 0x18(%rsp)
movq 0x28(%rsp), %r12
movq %r12, %rdx
callq 0x2170
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %r12, %rdx
callq 0x2170
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x2170
shlq $0x2, %r15
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rdx
callq 0x2170
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
movq 0x8(%rsp), %r12
movq 0x18(%rsp), %r15
callq 0x2170
movq %rbx, %rdi
xorl %eax, %eax
movq 0x20(%rsp), %rbx
movq (%rbx,%rax,8), %rcx
movq (%r15,%rcx,8), %rdx
movq 0x10(%r14), %rsi
movq %rdx, (%rsi,%rax,8)
movq (%r12,%rcx,8), %rdx
movq 0x18(%r14), %rsi
movq %rdx, (%rsi,%rax,8)
movq (%rdi,%rcx,8), %rdx
movq 0x20(%r14), %rsi
movq %rdx, (%rsi,%rax,8)
movl (%rbp,%rcx,4), %edx
movq 0x28(%r14), %rsi
movl %edx, (%rsi,%rax,4)
movq (%r13,%rcx,8), %rcx
movq 0x30(%r14), %rdx
movq %rcx, (%rdx,%rax,8)
incq %rax
cmpq (%r14), %rax
jb 0x41fd
movq %rdi, %r14
movq %r15, %rdi
callq 0x2240
movq %r12, %rdi
callq 0x2240
movq %r14, %rdi
callq 0x2240
movq %rbp, %rdi
callq 0x2240
movq %r13, %rdi
callq 0x2240
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2240
leaq 0xd087(%rip), %rdi      # 0x11314
leaq 0xd084(%rip), %rsi      # 0x11318
leaq 0xd533(%rip), %rcx      # 0x117ce
movl $0x88, %edx
callq 0x2100
leaq 0xd53c(%rip), %rdi      # 0x117e8
leaq 0xd065(%rip), %rsi      # 0x11318
leaq 0xd514(%rip), %rcx      # 0x117ce
movl $0x89, %edx
callq 0x2100
callq 0x2654
callq 0x263f
callq 0x262a
callq 0x2615
callq 0x2600
callq 0x25eb
callq 0x25d6
callq 0x25c1