// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
testq %rdi, %rdi
jne 0x322f
movq 0x1a049(%rip), %rdi     # 0x1d278
testq %rdi, %rdi
je 0x34cb
movq 0x8(%rdi), %r15
cmpq (%rdi), %r15
jne 0x34ea
cmpq $0x2, %r15
jae 0x325a
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, 0x8(%rsp)
leaq (,%r15,8), %r12
movq %r12, %rdi
callq 0x22c0
testq %rax, %rax
je 0x3509
movq %rax, %r14
xorl %eax, %eax
cmpq %rax, %r15
je 0x328b
movq %rax, (%r14,%rax,8)
incq %rax
jmp 0x327d
movq %r15, %rdi
shlq $0x7, %rdi
callq 0x22c0
testq %rax, %rax
je 0x3512
movq %rax, %r13
pushq $0x10
popq %rdi
pushq $0x8
popq %rsi
callq 0x2110
testq %rax, %rax
je 0x351b
movq %rax, %rbp
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
pushq $0xf
popq %rdx
xorl %esi, %esi
cmpq $0x10, %rsi
je 0x3360
leaq (,%rsi,4), %rcx
xorl %edi, %edi
cmpq %rdi, %r15
je 0x3317
movq (%r14,%rdi,8), %r8
movq (%rax,%r8,8), %r9
andq %rdx, %r9
shrq %cl, %r9
movq %r9, %r10
imulq %r15, %r10
movq (%rbp,%r9,8), %r11
leaq (,%r10,8), %r10
addq %r13, %r10
movq %r8, (%r10,%r11,8)
incq %r11
movq %r11, (%rbp,%r9,8)
incq %rdi
jmp 0x32dc
movq %r13, %rcx
xorl %edi, %edi
xorl %r8d, %r8d
cmpq $0x10, %r8
je 0x3354
movq (%rbp,%r8,8), %r10
leaq (%r14,%rdi,8), %r11
xorl %r9d, %r9d
cmpq %r9, %r10
je 0x3343
movq (%rcx,%r9,8), %rbx
movq %rbx, (%r11,%r9,8)
incq %r9
jmp 0x3331
andq $0x0, (%rbp,%r8,8)
incq %r8
addq %r9, %rdi
addq %r12, %rcx
jmp 0x331f
shlq $0x4, %rdx
incq %rsi
jmp 0x32c8
movq %r13, %rdi
callq 0x2250
movq %rbp, %rdi
callq 0x2250
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x3524
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2110
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x352d
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x3536
movq %rax, %rbp
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x353f
movq %rax, %r12
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2110
testq %rax, %rax
je 0x3548
movq %rax, %r13
movq 0x8(%rsp), %r10
movq 0x10(%r10), %rax
movq 0x18(%r10), %rcx
movq 0x20(%r10), %rdx
movq 0x28(%r10), %rsi
movq 0x30(%r10), %rdi
xorl %r8d, %r8d
movq 0x10(%rsp), %r11
cmpq %r8, %r15
je 0x343a
movq (%rax,%r8,8), %r9
movq %r9, (%rbx,%r8,8)
movq (%rcx,%r8,8), %r9
movq %r9, (%r11,%r8,8)
movq (%rdx,%r8,8), %r9
movq %r9, (%rbp,%r8,8)
movl (%rsi,%r8,4), %r9d
movl %r9d, (%r12,%r8,4)
movq (%rdi,%r8,8), %r9
movq %r9, (%r13,%r8,8)
incq %r8
jmp 0x3406
xorl %eax, %eax
cmpq %r15, %rax
jae 0x348a
movq (%r14,%rax,8), %rcx
movq (%rbx,%rcx,8), %rdx
movq 0x10(%r10), %rsi
movq %rdx, (%rsi,%rax,8)
movq (%r11,%rcx,8), %rdx
movq 0x18(%r10), %rsi
movq %rdx, (%rsi,%rax,8)
movq (%rbp,%rcx,8), %rdx
movq 0x20(%r10), %rsi
movq %rdx, (%rsi,%rax,8)
movl (%r12,%rcx,4), %edx
movq 0x28(%r10), %rsi
movl %edx, (%rsi,%rax,4)
movq (%r13,%rcx,8), %rcx
movq 0x30(%r10), %rdx
movq %rcx, (%rdx,%rax,8)
incq %rax
movq (%r10), %r15
jmp 0x343c
movq %rbx, %rdi
movq %r11, %rbx
callq 0x2250
movq %rbx, %rdi
callq 0x2250
movq %rbp, %rdi
callq 0x2250
movq %r12, %rdi
callq 0x2250
movq %r13, %rdi
callq 0x2250
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2250
leaq 0xce0e(%rip), %rdi      # 0x102e0
leaq 0xce0b(%rip), %rsi      # 0x102e4
leaq 0xd2ba(%rip), %rcx      # 0x1079a
movl $0x88, %edx
callq 0x2100
leaq 0xd2c3(%rip), %rdi      # 0x107b4
leaq 0xcdec(%rip), %rsi      # 0x102e4
leaq 0xd29b(%rip), %rcx      # 0x1079a
movl $0x89, %edx
callq 0x2100
leaq 0xd2be(%rip), %rdi      # 0x107ce
jmp 0x354f
leaq 0xd30e(%rip), %rdi      # 0x10827
jmp 0x354f
leaq 0xd368(%rip), %rdi      # 0x1088a
jmp 0x354f
leaq 0xd3ac(%rip), %rdi      # 0x108d7
jmp 0x354f
leaq 0xd3fa(%rip), %rdi      # 0x1092e
jmp 0x354f
leaq 0xd444(%rip), %rdi      # 0x10981
jmp 0x354f
leaq 0xd498(%rip), %rdi      # 0x109de
jmp 0x354f
leaq 0xd4f0(%rip), %rdi      # 0x10a3f
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270