// File: /workspace/github_c_makefile_star2_O1/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 0x3ed9
movq 0x1b39f(%rip), %rbx     # 0x1f278
testq %rbx, %rbx
je 0x41f3
movq (%rbx), %r15
cmpq %r15, 0x8(%rbx)
jne 0x4212
cmpq $0x2, %r15
jae 0x3f04
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 0x4231
xorl %ecx, %ecx
movq %rcx, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r15
jne 0x3f21
movq %rax, 0x20(%rsp)
movq %r15, %rdi
shlq $0x7, %rdi
callq 0x22b0
testq %rax, %rax
je 0x4236
movq %rax, %r13
movl $0x10, %edi
movl $0x8, %esi
callq 0x2110
testq %rax, %rax
je 0x423b
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 0x3f99
xorl %r15d, %r15d
xorl %ebx, %ebx
movq (%rbp,%r15,8), %r12
testq %r12, %r12
je 0x3ffd
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 0x3fd7
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 0x3f8a
movq %r13, %rdi
callq 0x2240
movq %rbp, %rdi
callq 0x2240
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x4240
movq %rax, %r12
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
movq %rax, 0x8(%rsp)
testq %rax, %rax
movq 0x48(%rsp), %r14
je 0x4245
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x424a
movq %rax, %rbx
movl $0x4, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x424f
movq %rax, %rbp
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x4254
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
cmpq $0x0, (%r14)
movq 0x20(%rsp), %rbx
je 0x41b2
xorl %eax, %eax
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 0x416a
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 0xd0e6(%rip), %rdi      # 0x112e0
leaq 0xd0e3(%rip), %rsi      # 0x112e4
leaq 0xd592(%rip), %rcx      # 0x1179a
movl $0x88, %edx
callq 0x2100
leaq 0xd59b(%rip), %rdi      # 0x117b4
leaq 0xd0c4(%rip), %rsi      # 0x112e4
leaq 0xd573(%rip), %rcx      # 0x1179a
movl $0x89, %edx
callq 0x2100
callq 0x2654
callq 0x263f
callq 0x262a
callq 0x2615
callq 0x2600
callq 0x25eb
callq 0x25d6
callq 0x25c1