// File: /workspace/github_cpp_makefile_star2_O2/ctSkennerton[P]fxtract/util/rsort.c
// Function: rsort
void
rsort(RSREC ** recv, int nrecs)
{
    int i, k = 256, maxleng = 0;
    int *lengv = calloc(k, sizeof(*lengv));

    for (i = 0; i < nrecs; ++i) {
        int leng = KEYLENG(recv[i]);
        if (leng >= k) {
            int kk = k;
            while (kk <= leng)
                kk <<= 1;
            lengv = realloc(lengv, kk * sizeof(int));
            memset(lengv + k, 0, (kk - k) * sizeof(int));
            k = kk;
        }

        ++lengv[leng];
        if (maxleng < leng)
            maxleng = leng;
    }

    DIAG_NRECS(nrecs);

    if (maxleng == 0) {         // edge case: empty input 
        free(lengv);
        return;
    }

    int pos = 0, nsrcs = 1, skip;
    PART *pdata = malloc((nrecs + 1) * sizeof(PART));
    PARTNO *partv = malloc(MAXPARTS * sizeof(*partv));
    int rsize = ((nrecs - 1) / MINRANGE + 1) * sizeof(RANGE);
    RANGE *srcv = malloc(rsize);
    RANGE *dstv = malloc(rsize);
    PARTNO *mapv[maxleng + 1];
    PARTNO mdata[MAXMAPS][256];

    for (i = 0; i <= maxleng; ++i)
        mapv[i] = 0;
    for (i = 1; i < MAXMAPS; ++i)
        *mdata[i] = UNUSED;

    // mdata[0] is permanently all 0. It is used to "map"
    //        bytes in the middle of the range (pos;len) that
    //        have exactly one value.
    memset(mdata[0], 0, sizeof(mdata[0]));

    // pdata[0]: a dummy list terminator; simplifies a loop 
    pdata[0] = (PART) { 0, NULL, NULL};

    // Initial srcv: a single range of the entire input 
    srcv[0] = (RANGE) {recv, recv + nrecs};
    for (pos = 0; nsrcs; pos += skip) {
        int len, nparts;

        DIAG_TICK(t1);
        nparts = analyze(maxleng, lengv, nsrcs, srcv,
                         mapv, mdata, &pos, &skip, &len);
        DIAG_TICK(t2);

        // Initialize partv[] to the index of dummy pdata[0]. 
        for (i = 0; i < nparts; ++i)
            partv[i] = 0;

        scatter(nsrcs, srcv, pos, len, mapv, partv, pdata);

        for (i = 0; i < len; ++i)
            *mapv[pos + i] = UNUSED;
        mdata[0][0] = 0;        // Undo "freeing" of mdata[0] 
#if DIAG
        int maxpart = -1;
        for (i = k = 0; i < nparts; ++i) {
            k += !!partv[i];
            if (partv[i]) maxpart = i;
        }
        fprintf(stderr, "# %9d recs %5d rngs on"
                " %d:%d%s %d/%d parts max=%d",
                _nrecs, nsrcs, pos, pos + len - 1,
                len == skip ? "" : "+", k, nparts, maxpart);
        DIAG_TICK(t3);
        DIAG_NRECS(0);          // updated by gather() 
#endif
        nsrcs = gather(pos + skip, nparts, partv, pdata, dstv);
#if DIAG
        fprintf(stderr, "\t%.3f %.3f %.3f secs\n",
                t2 - t1, t3 - t2, tick() - t3);
#endif
        RANGE *tmp = srcv;
        srcv = dstv;
        dstv = tmp;
    }

    free(dstv);
    free(srcv);
    free(partv);
    free(pdata);
    free(lengv);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x84d8, %rsp           # imm = 0x84D8
movl %esi, %r14d
movq %rdi, %r15
pushq $0x4
popq %rsi
movl $0x100, %edi            # imm = 0x100
callq 0x3130
movq %rax, %rdi
xorl %ebx, %ebx
testl %r14d, %r14d
movl $0x0, %edx
movq %r14, -0x50(%rbp)
cmovgl %r14d, %edx
movl $0x100, %r14d           # imm = 0x100
xorl %ecx, %ecx
movq %r15, -0x38(%rbp)
movq %rdx, -0x60(%rbp)
cmpq %rdx, %rbx
je 0x48ea
movq (%r15,%rbx,8), %rax
movzwl (%rax), %r12d
cmpl %r12d, %r14d
jle 0x4887
movl %r14d, %r13d
jmp 0x48d5
movl %ecx, -0x3c(%rbp)
movl %r14d, %eax
movl %eax, %r13d
leal (,%r13,2), %eax
cmpl %r12d, %r13d
jle 0x488d
movl %r13d, %esi
shlq $0x2, %rsi
callq 0x32f0
movq %rax, %r15
movl %r14d, %eax
leaq (%r15,%rax,4), %rdi
movl %r13d, %eax
subl %r14d, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x30f0
movq %r15, %rdi
movl -0x3c(%rbp), %ecx
movq -0x38(%rbp), %r15
movq -0x60(%rbp), %rdx
movl %r12d, %eax
incl (%rdi,%rax,4)
cmpl %r12d, %ecx
cmovlel %r12d, %ecx
incq %rbx
movl %r13d, %r14d
jmp 0x4870
movq -0x50(%rbp), %rbx
movl %ebx, 0x4e8c(%rip)      # 0x9780
testl %ecx, %ecx
je 0x4f10
movq %r15, %r13
movq %rdi, -0x98(%rbp)
leal 0x1(%rbx), %eax
cltq
imulq $0x18, %rax, %rdi
movl %ecx, %r14d
callq 0x3260
movq %rax, %r15
movl $0x42744, %edi          # imm = 0x42744
callq 0x3260
movq %rax, -0x58(%rbp)
leal -0x1(%rbx), %eax
pushq $0x10
popq %rcx
cltd
idivl %ecx
shll $0x4, %eax
addl $0x10, %eax
movslq %eax, %rbx
movq %rbx, %rdi
callq 0x3260
movq %rax, -0x48(%rbp)
movq %rbx, %rdi
callq 0x3260
movq %rax, %rbx
movq %rsp, -0xb0(%rbp)
movl %r14d, -0x3c(%rbp)
movl %r14d, %eax
leaq 0x17(,%rax,8), %rcx
incq %rax
movabsq $0xffffffff0, %rdx      # imm = 0xFFFFFFFF0
andq %rcx, %rdx
movq %rsp, %r8
subq %rdx, %r8
movq %r8, %rsp
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x4991
andq $0x0, (%r8,%rcx,8)
incq %rcx
jmp 0x4982
movl $0x400, %eax            # imm = 0x400
cmpq $0x8400, %rax           # imm = 0x8400
je 0x49b1
movl $0x109d1, -0x8500(%rbp,%rax) # imm = 0x109D1
addq $0x400, %rax            # imm = 0x400
jmp 0x4996
leaq -0x8500(%rbp), %rdi
xorl %r12d, %r12d
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
movq %r8, -0x70(%rbp)
callq 0x30f0
movq -0x70(%rbp), %rdx
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movslq -0x50(%rbp), %rax
leaq (,%rax,8), %rax
addq %r13, %rax
movq -0x48(%rbp), %rcx
movq %r13, (%rcx)
movq %rax, 0x8(%rcx)
leaq -0x8(%rdx), %rax
movq %rax, -0xb8(%rbp)
pushq $0x1
popq %r14
movq %r15, -0x80(%rbp)
movq %rbx, %r13
movl %r12d, -0x68(%rbp)
testl %r14d, %r14d
je 0x4ecc
movq %rdx, %rbx
callq 0x4f23
movsd %xmm0, -0xc0(%rbp)
subq $0x8, %rsp
movl -0x3c(%rbp), %edi
movq -0x98(%rbp), %rsi
movl %r14d, -0x2c(%rbp)
movl %r14d, %edx
movq -0x48(%rbp), %rcx
movq %rbx, %r8
leaq -0x8500(%rbp), %r9
leaq -0x8c(%rbp), %rax
pushq %rax
leaq -0x90(%rbp), %rax
pushq %rax
leaq -0x68(%rbp), %rax
pushq %rax
callq 0x4f52
addq $0x20, %rsp
movl %eax, %ebx
callq 0x4f23
movsd %xmm0, -0x78(%rbp)
testl %ebx, %ebx
movl $0x0, %edx
movq %rbx, -0x50(%rbp)
cmovgl %ebx, %edx
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x4a97
movq -0x58(%rbp), %rax
andl $0x0, (%rax,%rcx,4)
incq %rcx
jmp 0x4a85
movq %rdx, -0x88(%rbp)
movslq -0x68(%rbp), %rbx
movslq -0x8c(%rbp), %rcx
movq -0x70(%rbp), %rax
leaq (%rax,%rbx,8), %rdx
movl %ebx, %edi
negl %edi
movl -0x2c(%rbp), %eax
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
movq %rsi, -0x38(%rbp)
movslq %edi, %rax
movq %rax, -0x60(%rbp)
movq -0xb8(%rbp), %rax
leaq (%rax,%rbx,8), %r8
xorl %r9d, %r9d
pushq $0x1
popq %rax
movl %eax, %r10d
movq %r13, -0xa8(%rbp)
cmpq -0x38(%rbp), %r9
je 0x4bb0
movq %r9, %rdi
shlq $0x4, %rdi
movq -0x48(%rbp), %rax
leaq (%rax,%rdi), %r11
movq (%rax,%rdi), %r14
cmpq 0x8(%r11), %r14
je 0x4ba8
movq (%r14), %r15
movzwl (%r15), %r12d
cmpl %r12d, %ebx
jl 0x4b32
addq $0x8, %r14
movq (%r11), %rdi
leaq 0x8(%rdi), %r12
movq %r12, (%r11)
movq %r15, (%rdi)
movq -0x80(%rbp), %r15
jmp 0x4b05
addq -0x60(%rbp), %r12
movq (%rdx), %r13
leaq (%r15,%rbx), %rdi
incq %rdi
movzbl 0x1(%rdi), %esi
movl (%r13,%rsi,4), %r13d
cmpq %rcx, %r12
cmovgeq %rcx, %r12
addq $0x8, %r14
cmpq $0x2, %r12
jl 0x4b6c
movq (%r8,%r12,8), %rsi
movzbl (%rdi,%r12), %eax
decq %r12
addl (%rsi,%rax,4), %r13d
jmp 0x4b54
movslq %r10d, %r10
imulq $0x18, %r10, %rax
movslq %r13d, %rsi
movq -0x58(%rbp), %r13
movl (%r13,%rsi,4), %edi
movq -0x80(%rbp), %r12
movl %edi, (%r12,%rax)
movq %r11, 0x8(%r12,%rax)
movq %r15, 0x10(%r12,%rax)
movq %r12, %r15
movl %r10d, (%r13,%rsi,4)
incl %r10d
movq -0xa8(%rbp), %r13
jmp 0x4b05
incq %r9
jmp 0x4ae8
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0x4bd0
movq (%rdx,%rdi,8), %rax
movl $0x109d1, (%rax)        # imm = 0x109D1
incq %rdi
jmp 0x4bbc
andl $0x0, -0x8500(%rbp)
xorl %edx, %edx
pushq $-0x1
popq %rax
movl %eax, %r11d
xorl %r10d, %r10d
movq -0x88(%rbp), %rdi
cmpq %rdx, %rdi
je 0x4c07
xorl %eax, %eax
movq -0x58(%rbp), %rsi
cmpl $0x0, (%rsi,%rdx,4)
setne %al
cmovnel %edx, %r11d
addl %eax, %r10d
incq %rdx
jmp 0x4be9
movq 0x43d2(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movl 0x4b69(%rip), %edx      # 0x9780
leal (%rbx,%rcx), %r9d
decl %r9d
movl -0x90(%rbp), %eax
movq %rax, -0x38(%rbp)
cmpl %eax, %ecx
leaq 0x14e5(%rip), %r14      # 0x6116
leaq 0x1cae(%rip), %rax      # 0x68e6
cmoveq %rax, %r14
movl $0x0, -0x64(%rbp)
leaq 0x149a(%rip), %rsi      # 0x60e4
movl -0x2c(%rbp), %ecx
movl %ebx, %r8d
xorl %eax, %eax
pushq %r11
movq -0x50(%rbp), %r12
pushq %r12
pushq %r10
pushq %r14
callq 0x32a0
addq $0x20, %rsp
callq 0x4f23
movq -0x38(%rbp), %rsi
movsd %xmm0, -0xa0(%rbp)
andl $0x0, 0x4b01(%rip)      # 0x9780
addl %ebx, %esi
movslq %esi, %rax
movq %rax, -0x38(%rbp)
movl %r12d, %edi
movl $0x0, -0x2c(%rbp)
testl %edi, %edi
jle 0x4e5b
decq %rdi
movq -0x58(%rbp), %rax
movl (%rax,%rdi,4), %r8d
movq %rdi, -0xc8(%rbp)
testl %r8d, %r8d
je 0x4c92
movslq %r8d, %rax
imulq $0x18, %rax, %rax
movq 0x8(%r15,%rax), %r9
movq 0x8(%r9), %r10
movslq %r8d, %rax
imulq $0x18, %rax, %rax
movq 0x10(%r15,%rax), %rcx
movq %rcx, -0x8(%r10)
addq $-0x8, %r10
movslq (%r15,%rax), %r8
imulq $0x18, %r8, %rax
cmpq %r9, 0x8(%r15,%rax)
je 0x4cc1
movq 0x8(%r9), %r11
movq %r10, %r14
movq (%r14), %rax
movzwl (%rax), %eax
cmpl %eax, %esi
jl 0x4cfe
addq $0x8, %r14
cmpq %r11, %r14
jb 0x4ceb
movq %r11, %rax
subq %r14, %rax
shrq $0x3, %rax
cmpl $0x10, %eax
jl 0x4d39
movl -0x2c(%rbp), %edx
movslq %edx, %rcx
incl %edx
movl %edx, -0x2c(%rbp)
shlq $0x4, %rcx
movq %r14, (%r13,%rcx)
movq %r11, 0x8(%r13,%rcx)
movl -0x64(%rbp), %ecx
addl %eax, %ecx
movl %ecx, -0x64(%rbp)
movl %ecx, 0x4a4c(%rip)      # 0x9780
jmp 0x4e52
cmpl $0x2, %eax
jl 0x4e52
movq (%r14), %rax
movzwl (%rax), %eax
cmpl %eax, %esi
jge 0x4e52
movq %r14, %r15
movq %r8, -0xe8(%rbp)
movq %r9, -0xe0(%rbp)
movq %r10, -0xd8(%rbp)
movq %r11, -0xd0(%rbp)
leaq 0x8(%r15), %rax
cmpq %r11, %rax
je 0x4e47
movq %rax, -0xf8(%rbp)
movq (%rax), %rcx
movzwl (%rcx), %eax
movl %eax, -0x60(%rbp)
subl %esi, %eax
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq %rcx, -0xf0(%rbp)
addq %rcx, %rax
addq $0x2, %rax
movq %rax, -0x88(%rbp)
cmpq %r14, %r15
jb 0x4e0d
movq %r14, %r13
movq (%r15), %rbx
movzwl (%rbx), %r14d
movzwl %r14w, %eax
movq %rsi, %r12
subl %esi, %eax
movl -0x50(%rbp), %edx
movl %edx, %ecx
cmpl %eax, %edx
jl 0x4dcd
movl %eax, %ecx
movq -0x38(%rbp), %rax
leaq (%rbx,%rax), %rdi
addq $0x2, %rdi
movslq %ecx, %rdx
movq -0x88(%rbp), %rsi
callq 0x30c0
testl %eax, %eax
setne %cl
cmpw -0x60(%rbp), %r14w
seta %dl
testl %eax, %eax
movq %r12, %rsi
movq %r13, %r14
js 0x4e0d
orb %dl, %cl
je 0x4e0d
movq %rbx, 0x8(%r15)
addq $-0x8, %r15
jmp 0x4daa
movq -0xf0(%rbp), %rax
movq %rax, 0x8(%r15)
movq -0xf8(%rbp), %r15
movq -0xc8(%rbp), %rdi
movq -0xe8(%rbp), %r8
movq -0xe0(%rbp), %r9
movq -0xd8(%rbp), %r10
movq -0xd0(%rbp), %r11
jmp 0x4d6f
movq -0x80(%rbp), %r15
movq -0xa8(%rbp), %r13
movq %r10, 0x8(%r9)
jmp 0x4cac
movq 0x417e(%rip), %rax      # 0x8fe0
movq (%rax), %rbx
movsd -0xa0(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
subsd -0xc0(%rbp), %xmm1
movsd %xmm1, -0x78(%rbp)
movq %rsi, %r12
callq 0x4f23
movapd %xmm0, %xmm2
subsd -0xa0(%rbp), %xmm2
movq %rbx, %rdi
leaq 0x1272(%rip), %rsi      # 0x6118
movsd -0x78(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movb $0x3, %al
callq 0x32a0
movq -0x48(%rbp), %rbx
movq %r13, -0x48(%rbp)
movq -0x70(%rbp), %rdx
movl -0x2c(%rbp), %r14d
jmp 0x4a08
movq %r13, %rdi
callq 0x3230
movq -0x48(%rbp), %rdi
callq 0x3230
movq -0x58(%rbp), %rdi
callq 0x3230
movq %r15, %rdi
callq 0x3230
movq -0x98(%rbp), %rdi
callq 0x3230
movq -0xb0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3230