// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rbx, -0x48(%rbp)
jle 0x4af6
movl %ebx, %edx
xorl %r12d, %r12d
movl $0x100, %ebx            # imm = 0x100
xorl %ecx, %ecx
movq %r15, -0x38(%rbp)
movq %rdx, -0x30(%rbp)
movq (%r15,%r12,8), %rax
movzwl (%rax), %r13d
cmpl %r13d, %ebx
jle 0x4a95
movl %ebx, %r14d
jmp 0x4adc
movl %ecx, -0x40(%rbp)
movl %ebx, %eax
movl %eax, %r14d
leal (%r14,%r14), %eax
cmpl %r13d, %r14d
jle 0x4a9a
movl %r14d, %esi
shlq $0x2, %rsi
callq 0x32f0
movq %rax, %r15
movl %ebx, %eax
leaq (%r15,%rax,4), %rdi
movl %r14d, %eax
subl %ebx, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x30f0
movq %r15, %rdi
movl -0x40(%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 %r14d, %ebx
cmpq %rdx, %r12
jne 0x4a83
jmp 0x4af8
xorl %ecx, %ecx
movq -0x48(%rbp), %rbx
movl %ebx, 0x5c7e(%rip)      # 0xa780
testl %ecx, %ecx
je 0x5210
movq %r15, %r12
movq %rdi, -0xa8(%rbp)
movl $0x0, -0x3c(%rbp)
movslq %ebx, %r15
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rdi
addq $0x18, %rdi
movl %ecx, %r14d
callq 0x3260
movq %rax, -0x78(%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, %r13
movq %rbx, %rdi
callq 0x3260
movq %rax, -0x68(%rbp)
movq %rsp, -0xc8(%rbp)
movq %rsp, %rdi
movl %r14d, -0x40(%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, -0x70(%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 0x4bae
leaq -0x8510(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x30f0
xorpd %xmm0, %xmm0
movq -0x78(%rbp), %rax
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq (%r12,%r15,8), %rax
movq %r13, %rcx
movq %r12, (%r13)
movq %rax, 0x8(%r13)
leaq -0x3c(%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 -0x40(%rbp), %edi
movq -0xa8(%rbp), %rsi
movl %r15d, %edx
movq %rbx, -0x68(%rbp)
movq %rbx, %rcx
movq -0x70(%rbp), %r8
leaq -0x8510(%rbp), %r9
leaq -0x9c(%rbp), %rax
pushq %rax
leaq -0xa0(%rbp), %rax
pushq %rax
leaq -0x3c(%rbp), %rax
pushq %rax
callq 0x5223
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, -0x88(%rbp)
movl %r12d, %ebx
movq %r12, -0x30(%rbp)
testl %r12d, %r12d
jle 0x4cda
leaq (,%rbx,4), %rdx
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x30f0
movq %rbx, -0x90(%rbp)
movslq -0x3c(%rbp), %r14
movslq -0x9c(%rbp), %rax
movl %r15d, -0x48(%rbp)
testl %r15d, %r15d
jle 0x4de2
movl %r14d, %ecx
negl %ecx
movslq %ecx, %rcx
movl -0x48(%rbp), %edx
movq %rdx, -0x38(%rbp)
movq -0x70(%rbp), %rdx
leaq (%rdx,%r14,8), %rsi
movl $0x1, %edi
xorl %r8d, %r8d
movq %r8, %r9
shlq $0x4, %r9
movq -0x68(%rbp), %rdx
movq (%rdx,%r9), %r10
cmpq 0x8(%rdx,%r9), %r10
je 0x4dd5
addq %rdx, %r9
movq (%r10), %r11
movzwl (%r11), %ebx
movl %ebx, %r12d
subl %r14d, %r12d
jle 0x4dba
cmpl %r12d, %eax
cmovll %eax, %r12d
movq -0x70(%rbp), %rdx
movq (%rdx,%r14,8), %r15
movzbl 0x2(%r11,%r14), %r13d
movl (%r15,%r13,4), %r15d
cmpl $0x2, %r12d
jl 0x4d8c
addq %rcx, %rbx
cmpq %rax, %rbx
cmovgeq %rax, %rbx
incq %rbx
leaq (%r11,%r14), %r12
movq -0x10(%rsi,%rbx,8), %r13
movzbl (%r12,%rbx), %edx
addl (%r13,%rdx,4), %r15d
decq %rbx
cmpq $0x2, %rbx
jg 0x4d74
movslq %edi, %rdi
leaq (%rdi,%rdi,2), %rdx
movslq %r15d, %rbx
movq -0x50(%rbp), %r13
movl (%r13,%rbx,4), %r15d
movq -0x78(%rbp), %r12
movl %r15d, (%r12,%rdx,8)
movq %r9, 0x8(%r12,%rdx,8)
movq %r11, 0x10(%r12,%rdx,8)
movl %edi, (%r13,%rbx,4)
incl %edi
jmp 0x4dc7
movq (%r9), %rbx
leaq 0x8(%rbx), %r15
movq %r15, (%r9)
movq %r11, (%rbx)
addq $0x8, %r10
cmpq 0x8(%r9), %r10
jne 0x4d35
incq %r8
cmpq -0x38(%rbp), %r8
jne 0x4d18
testl %eax, %eax
jle 0x4e02
movq -0x70(%rbp), %rcx
leaq (%rcx,%r14,8), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl $0x109d1, (%rsi)        # imm = 0x109D1
incq %rdx
cmpq %rdx, %rax
jne 0x4df0
movl $0x0, -0x8510(%rbp)
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl $0x0, %r11d
cmpl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %rsi
movq -0xb0(%rbp), %r12
movq -0x90(%rbp), %r15
jle 0x4e53
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 0x4e3b
movq 0x5186(%rip), %rcx      # 0x9fe0
movq (%rcx), %rdi
movl 0x591d(%rip), %edx      # 0xa780
leal (%r14,%rax), %r9d
decl %r9d
movl -0xa0(%rbp), %r13d
cmpl %r13d, %eax
leaq 0x22a3(%rip), %rbx      # 0x711e
leaq 0x2a6c(%rip), %rax      # 0x78ee
cmoveq %rax, %rbx
leaq 0x225f(%rip), %rsi      # 0x70ec
movl -0x48(%rbp), %ecx
movl %r14d, %r8d
xorl %eax, %eax
pushq %r10
pushq -0x30(%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
mulsd 0x21f5(%rip), %xmm0     # 0x70c8
movsd %xmm0, -0x80(%rbp)
movl $0x0, 0x589e(%rip)      # 0xa780
addl %r14d, %r13d
movl %r13d, -0x38(%rbp)
cmpl $0x0, -0x30(%rbp)
movq -0x78(%rbp), %rbx
movl $0x0, %edx
jle 0x5101
movslq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
xorl %edx, %edx
xorl %esi, %esi
movq -0x50(%rbp), %rax
movq %r15, -0x90(%rbp)
movl -0x4(%rax,%r15,4), %edi
testl %edi, %edi
je 0x50ec
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 0x4f30
movq %r15, %r13
movq (%r13), %rax
movzwl (%rax), %eax
cmpl %eax, -0x38(%rbp)
jl 0x4f6c
addq $0x8, %r13
cmpq 0x8(%r8), %r13
jb 0x4f56
movq 0x8(%r8), %r9
movq %r9, %rax
subq %r13, %rax
shrq $0x3, %rax
cmpl $0x10, %eax
jl 0x4f9e
movslq %edx, %rcx
incl %edx
shlq $0x4, %rcx
movq %r13, (%r12,%rcx)
movq %r9, 0x8(%r12,%rcx)
addl %eax, %esi
movl %esi, 0x57e7(%rip)      # 0xa780
jmp 0x50e3
cmpl $0x2, %eax
jl 0x50e3
movq (%r13), %rax
movzwl (%rax), %eax
cmpl %eax, -0x38(%rbp)
jge 0x50e3
leaq 0x8(%r13), %rax
cmpq %r9, %rax
je 0x50e3
movq %r8, -0xf0(%rbp)
movq %rdi, -0xf8(%rbp)
movl %esi, -0x94(%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 0x5098
movq -0xb8(%rbp), %rcx
movzwl (%rcx), %eax
movl %eax, -0x48(%rbp)
subl -0x38(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0x30(%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 -0x38(%rbp), %eax
movl -0x98(%rbp), %edx
movl %edx, %ecx
cmpl %eax, %edx
jl 0x5050
movl %eax, %ecx
movq -0x30(%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 0x5095
orb %dl, %cl
je 0x5095
movq %rbx, (%r14)
leaq -0x8(%r14), %rax
addq $-0x10, %r14
cmpq %r13, %r14
movq %rax, %r14
jae 0x5033
jmp 0x5098
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 0x4fe6
movq -0x78(%rbp), %rbx
movq -0xb0(%rbp), %r12
movq -0x100(%rbp), %rdx
movl -0x94(%rbp), %esi
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %r8
movq %r15, 0x8(%r8)
jmp 0x4f18
movq -0x90(%rbp), %rcx
leaq -0x1(%rcx), %r15
cmpq $0x2, %rcx
jge 0x4f08
movsd 0x1fbf(%rip), %xmm2     # 0x70c8
movsd -0xe8(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd -0xe0(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
mulsd %xmm2, %xmm1
addsd -0xd8(%rbp), %xmm1
movsd -0x80(%rbp), %xmm2
addsd -0xd0(%rbp), %xmm2
movsd %xmm2, -0x80(%rbp)
movq 0x4e96(%rip), %rax      # 0x9fe0
movq %rdx, %r13
movq (%rax), %r14
subsd %xmm1, %xmm2
movsd %xmm2, -0x30(%rbp)
subsd %xmm0, %xmm1
movsd %xmm1, -0x88(%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 0x1f3b(%rip), %xmm2     # 0x70c8
addsd %xmm0, %xmm2
subsd -0x80(%rbp), %xmm2
movq %r14, %rdi
movq %r15, %r14
leaq 0x1f7d(%rip), %rsi      # 0x7120
movsd -0x88(%rbp), %xmm0
movsd -0x30(%rbp), %xmm1
movb $0x3, %al
callq 0x32a0
movl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl %r13d, %r15d
movq %r12, %rcx
testl %r13d, %r13d
jne 0x4c11
movq -0x68(%rbp), %rdi
callq 0x3230
movq %r12, %rdi
callq 0x3230
movq -0x50(%rbp), %rdi
callq 0x3230
movq %rbx, %rdi
callq 0x3230
movq -0xa8(%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
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3230