// File: /workspace/github_cpp_makefile_star2_O3/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 $0x84e8, %rsp           # imm = 0x84E8
movl %esi, %ebx
movq %rdi, %r15
movl $0x100, %edi            # imm = 0x100
movl $0x4, %esi
callq 0x3130
movq %rax, %rdi
testl %ebx, %ebx
jle 0x51d3
movq %rbx, -0x48(%rbp)
movl %ebx, %edx
xorl %r12d, %r12d
movl $0x100, %r14d           # imm = 0x100
xorl %ecx, %ecx
movq %rdx, -0x30(%rbp)
movq %r15, -0x38(%rbp)
movq (%r15,%r12,8), %rax
movzwl (%rax), %r13d
cmpl %r13d, %r14d
jle 0x4a76
movl %r14d, %ebx
jmp 0x4abc
movl %ecx, -0x3c(%rbp)
movl %r14d, %eax
movl %eax, %ebx
leal (%rbx,%rbx), %eax
cmpl %r13d, %ebx
jle 0x4a7c
movl %ebx, %esi
shlq $0x2, %rsi
callq 0x32f0
movq %rax, %r15
movl %r14d, %eax
leaq (%r15,%rax,4), %rdi
movl %ebx, %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 -0x30(%rbp), %rdx
movl %r13d, %eax
incl (%rdi,%rax,4)
cmpl %r13d, %ecx
cmovlel %r13d, %ecx
incq %r12
movl %ebx, %r14d
cmpq %rdx, %r12
jne 0x4a64
movq -0x48(%rbp), %rbx
movl %ebx, 0x5ca2(%rip)      # 0xa780
testl %ecx, %ecx
je 0x51d9
movq %rdi, -0xa0(%rbp)
leaq (,%rdx,8), %rax
leaq (%rax,%rax,2), %rdi
addq $0x18, %rdi
movl %ecx, %r14d
callq 0x3260
movq %rax, -0x70(%rbp)
movl $0x42744, %edi          # imm = 0x42744
callq 0x3260
leal -0x1(%rbx), %ecx
addl $0xe, %ebx
testl %ecx, %ecx
cmovnsl %ecx, %ebx
movq %rax, -0x50(%rbp)
andl $-0x10, %ebx
addl $0x10, %ebx
movslq %ebx, %rbx
movq %rbx, %rdi
callq 0x3260
movq %rax, %r12
movq %rbx, %rdi
callq 0x3260
movq %rax, -0x68(%rbp)
movq %rsp, -0xc8(%rbp)
movq %rsp, %rdi
movl %r14d, -0x3c(%rbp)
movl %r14d, %eax
leaq 0x17(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdi
movq %rdi, %rsp
leaq 0x8(,%rax,8), %rdx
movq %rdi, -0xa8(%rbp)
xorl %esi, %esi
callq 0x30f0
movl $0x400, %eax            # imm = 0x400
movl $0x109d1, -0x8510(%rbp,%rax) # imm = 0x109D1
addq $0x400, %rax            # imm = 0x400
cmpq $0x8400, %rax           # imm = 0x8400
jne 0x4b80
leaq -0x8510(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x30f0
xorpd %xmm0, %xmm0
movq -0x70(%rbp), %rax
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq -0x30(%rbp), %rax
leaq (%r15,%rax,8), %rax
movq %r12, %rcx
movq %r15, (%r12)
movq %rax, 0x8(%r12)
leaq -0x40(%rbp), %rax
movl $0x0, (%rax)
movl $0x1, %r15d
leaq -0x60(%rbp), %r14
movq -0x68(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq %rcx, %rbx
movq %r14, %rdi
xorl %esi, %esi
callq 0x32d0
xorps %xmm0, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
xorps %xmm0, %xmm0
cvtsi2sdq -0x58(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
subq $0x8, %rsp
movl -0x3c(%rbp), %edi
movq -0xa0(%rbp), %rsi
movl %r15d, %edx
movq %rbx, -0x68(%rbp)
movq %rbx, %rcx
movq -0xa8(%rbp), %r8
leaq -0x8510(%rbp), %r9
leaq -0x94(%rbp), %rax
pushq %rax
leaq -0x98(%rbp), %rax
pushq %rax
leaq -0x40(%rbp), %rax
pushq %rax
callq 0x51ec
addq $0x20, %rsp
movl %eax, %r12d
movq %r14, %rdi
xorl %esi, %esi
callq 0x32d0
xorps %xmm0, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
xorps %xmm0, %xmm0
cvtsi2sdq -0x58(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movl %r12d, %ebx
movq %r12, -0x38(%rbp)
testl %r12d, %r12d
jle 0x4cb1
leaq (,%rbx,4), %rdx
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x30f0
movq %rbx, -0x88(%rbp)
movslq -0x40(%rbp), %r14
movslq -0x94(%rbp), %rax
movq -0xa8(%rbp), %rcx
leaq (%rcx,%r14,8), %rcx
movl %r15d, -0x48(%rbp)
testl %r15d, %r15d
jle 0x4db7
movl %r14d, %edx
negl %edx
movslq %edx, %rdx
movl -0x48(%rbp), %esi
movq %rsi, -0x30(%rbp)
movl $0x1, %edi
xorl %r8d, %r8d
movq %r8, %r9
shlq $0x4, %r9
movq -0x68(%rbp), %rsi
movq (%rsi,%r9), %r10
cmpq 0x8(%rsi,%r9), %r10
je 0x4daa
addq %rsi, %r9
movq (%r10), %r11
movzwl (%r11), %ebx
movl %ebx, %r12d
subl %r14d, %r12d
jle 0x4d8f
cmpl %r12d, %eax
cmovll %eax, %r12d
movq (%rcx), %r15
movzbl 0x2(%r11,%r14), %r13d
movl (%r15,%r13,4), %r15d
cmpl $0x2, %r12d
jl 0x4d61
addq %rdx, %rbx
cmpq %rax, %rbx
cmovgeq %rax, %rbx
incq %rbx
leaq (%r11,%r14), %r12
movq -0x10(%rcx,%rbx,8), %r13
movzbl (%r12,%rbx), %esi
addl (%r13,%rsi,4), %r15d
decq %rbx
cmpq $0x2, %rbx
jg 0x4d49
movslq %edi, %rdi
leaq (%rdi,%rdi,2), %rsi
movslq %r15d, %rbx
movq -0x50(%rbp), %r13
movl (%r13,%rbx,4), %r15d
movq -0x70(%rbp), %r12
movl %r15d, (%r12,%rsi,8)
movq %r9, 0x8(%r12,%rsi,8)
movq %r11, 0x10(%r12,%rsi,8)
movl %edi, (%r13,%rbx,4)
incl %edi
jmp 0x4d9c
movq (%r9), %rbx
leaq 0x8(%rbx), %r15
movq %r15, (%r9)
movq %r11, (%rbx)
addq $0x8, %r10
cmpq 0x8(%r9), %r10
jne 0x4d0f
incq %r8
cmpq -0x30(%rbp), %r8
jne 0x4cf2
testl %eax, %eax
jle 0x4dcf
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl $0x109d1, (%rsi)        # imm = 0x109D1
incq %rdx
cmpq %rdx, %rax
jne 0x4dbd
movl $0x0, -0x8510(%rbp)
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl $0x0, %r11d
cmpl $0x0, -0x38(%rbp)
movq -0x50(%rbp), %rsi
movq -0xb0(%rbp), %r12
movq -0x88(%rbp), %r15
jle 0x4e20
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
xorl %ecx, %ecx
xorl %r11d, %r11d
xorl %edx, %edx
cmpl $0x0, (%rsi,%rcx,4)
cmovnel %ecx, %r10d
setne %dl
addl %edx, %r11d
incq %rcx
cmpq %rcx, %r15
jne 0x4e08
movq 0x51b9(%rip), %rcx      # 0x9fe0
movq (%rcx), %rdi
movl 0x5950(%rip), %edx      # 0xa780
leal (%r14,%rax), %r9d
decl %r9d
movl -0x98(%rbp), %r13d
cmpl %r13d, %eax
leaq 0x22ce(%rip), %rbx      # 0x7116
leaq 0x2a97(%rip), %rax      # 0x78e6
cmoveq %rax, %rbx
leaq 0x228a(%rip), %rsi      # 0x70e4
movl -0x48(%rbp), %ecx
movl %r14d, %r8d
xorl %eax, %eax
pushq %r10
pushq -0x38(%rbp)
pushq %r11
pushq %rbx
callq 0x32a0
addq $0x20, %rsp
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x32d0
xorps %xmm0, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
xorps %xmm0, %xmm0
cvtsi2sdq -0x58(%rbp), %xmm0
addl %r14d, %r13d
movl %r13d, -0x30(%rbp)
mulsd 0x2219(%rip), %xmm0     # 0x70c0
movsd %xmm0, -0x78(%rbp)
movl $0x0, 0x58ca(%rip)      # 0xa780
cmpl $0x0, -0x38(%rbp)
movq -0x70(%rbp), %rbx
movl $0x0, %edx
jle 0x50cd
movslq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
xorl %edx, %edx
xorl %esi, %esi
movq -0x50(%rbp), %rax
movq %r15, -0x88(%rbp)
movl -0x4(%rax,%r15,4), %edi
testl %edi, %edi
je 0x50b8
movslq %edi, %rax
leaq (%rax,%rax,2), %rax
movq 0x8(%rbx,%rax,8), %r8
movq 0x8(%r8), %r15
movslq %edi, %rax
leaq (%rax,%rax,2), %rax
movq 0x10(%rbx,%rax,8), %rcx
movq %rcx, -0x8(%r15)
addq $-0x8, %r15
movslq (%rbx,%rax,8), %rdi
leaq (%rdi,%rdi,2), %rax
cmpq %r8, 0x8(%rbx,%rax,8)
je 0x4efd
movq 0x8(%r8), %r9
movq %r15, %r13
movq (%r13), %rax
movzwl (%rax), %eax
cmpl %eax, -0x30(%rbp)
jl 0x4f3c
addq $0x8, %r13
cmpq %r9, %r13
jb 0x4f27
movq %r9, %rax
subq %r13, %rax
shrq $0x3, %rax
cmpl $0x10, %eax
jl 0x4f6a
movslq %edx, %rcx
incl %edx
shlq $0x4, %rcx
movq %r13, (%r12,%rcx)
movq %r9, 0x8(%r12,%rcx)
addl %eax, %esi
movl %esi, 0x581b(%rip)      # 0xa780
jmp 0x50af
cmpl $0x2, %eax
jl 0x50af
movq (%r13), %rax
movzwl (%rax), %eax
cmpl %eax, -0x30(%rbp)
jge 0x50af
leaq 0x8(%r13), %rax
cmpq %r9, %rax
je 0x50af
movq %r8, -0xf0(%rbp)
movq %rdi, -0xf8(%rbp)
movl %esi, -0x8c(%rbp)
movq %rdx, -0x100(%rbp)
movq %r9, -0x108(%rbp)
movq (%rax), %rcx
movq %rcx, -0xb8(%rbp)
leaq -0x8(%rax), %rcx
movq %rax, -0xc0(%rbp)
cmpq %r13, %rcx
jb 0x5064
movq -0xb8(%rbp), %rcx
movzwl (%rcx), %eax
movl %eax, -0x48(%rbp)
subl -0x30(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x38(%rbp), %rax
addq %rcx, %rax
addq $0x2, %rax
movq %rax, -0x110(%rbp)
movq -0xc0(%rbp), %r14
movq -0x8(%r14), %rbx
movzwl (%rbx), %r12d
movzwl %r12w, %eax
subl -0x30(%rbp), %eax
movl -0x90(%rbp), %edx
movl %edx, %ecx
cmpl %eax, %edx
jl 0x501c
movl %eax, %ecx
movq -0x38(%rbp), %rax
leaq (%rbx,%rax), %rdi
addq $0x2, %rdi
movslq %ecx, %rdx
movq -0x110(%rbp), %rsi
callq 0x30c0
testl %eax, %eax
setne %cl
cmpw -0x48(%rbp), %r12w
seta %dl
testl %eax, %eax
js 0x5061
orb %dl, %cl
je 0x5061
movq %rbx, (%r14)
leaq -0x8(%r14), %rax
addq $-0x10, %r14
cmpq %r13, %r14
movq %rax, %r14
jae 0x4fff
jmp 0x5064
movq %r14, %rax
movq -0xb8(%rbp), %rcx
movq %rcx, (%rax)
movq -0xc0(%rbp), %rax
addq $0x8, %rax
movq -0x108(%rbp), %r9
cmpq %r9, %rax
jne 0x4fb2
movq -0x70(%rbp), %rbx
movq -0xb0(%rbp), %r12
movq -0x100(%rbp), %rdx
movl -0x8c(%rbp), %esi
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %r8
movq %r15, 0x8(%r8)
jmp 0x4ee5
movq -0x88(%rbp), %rcx
leaq -0x1(%rcx), %r15
cmpq $0x2, %rcx
jge 0x4ed5
movsd 0x1feb(%rip), %xmm2     # 0x70c0
movsd -0xe8(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd -0xe0(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
mulsd %xmm2, %xmm1
addsd -0xd8(%rbp), %xmm1
movsd -0x78(%rbp), %xmm2
addsd -0xd0(%rbp), %xmm2
movsd %xmm2, -0x78(%rbp)
movq 0x4ecd(%rip), %rax      # 0x9fe0
movq %rdx, %r13
movq (%rax), %r14
subsd %xmm1, %xmm2
movsd %xmm2, -0x38(%rbp)
subsd %xmm0, %xmm1
movsd %xmm1, -0x80(%rbp)
leaq -0x60(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x32d0
xorps %xmm0, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm0
xorps %xmm2, %xmm2
cvtsi2sdq -0x58(%rbp), %xmm2
mulsd 0x1f6d(%rip), %xmm2     # 0x70c0
addsd %xmm0, %xmm2
subsd -0x78(%rbp), %xmm2
movq %r14, %rdi
movq %r15, %r14
leaq 0x1faf(%rip), %rsi      # 0x7118
movsd -0x80(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movb $0x3, %al
callq 0x32a0
movl -0x30(%rbp), %eax
movl %eax, -0x40(%rbp)
movl %r13d, %r15d
movq %r12, %rcx
testl %r13d, %r13d
jne 0x4be8
movq -0x68(%rbp), %rdi
callq 0x3230
movq %r12, %rdi
callq 0x3230
movq -0x50(%rbp), %rdi
callq 0x3230
movq %rbx, %rdi
callq 0x3230
movq -0xa0(%rbp), %rdi
callq 0x3230
movq -0xc8(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, 0x55a7(%rip)      # 0xa780
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3230