// File: /workspace/github_cpp_makefile_star2_O2/ctSkennerton[P]fxtract/util/rsort_x.c
// Function: main
int
main(int argc, char **argv)
{
    if (argc < 2 || argc > 3) {
	fputs("usage: rsort_x <inputfile> [outfile | -]\n", stderr);
	return 0;
    }
    MEMBUF  inp = chomp(slurp(argv[1]));
    if (nilbuf(inp)) die(": cannot read %s\n", argv[1]);
#ifdef TAP
    plan_tests(1);
#endif
    struct rusage ru;
    getrusage(0, &ru);
    long    rss = ru.ru_maxrss;     

    int	    nrecs;
    MEMREF  *inpv = refsplit(inp.ptr, '\n', &nrecs);
    RSREC   **actv = malloc(nrecs * sizeof(*actv));
    RSREC   **expv = malloc(nrecs * sizeof(*expv));
    uint8_t    *data = malloc(inp.len + nrecs * sizeof(RSREC));
    uint8_t    *dp = data;
    int	    i;

    for (i = 0; i < nrecs; ++i) {
	actv[i] = (RSREC*) dp;
	actv[i]->leng = inpv[i].len;
	memcpy(actv[i]->data, inpv[i].ptr, inpv[i].len);
	dp = &actv[i]->data[inpv[i].len];
    }

    memcpy(expv, actv, nrecs * sizeof(*expv));
    double  t0 = tick();
    rsort(actv, nrecs);
    double  t_rsort = tick() - t0;

    getrusage(0, &ru);
    rss = ru.ru_maxrss - rss;

    t0 = tick();
    qsort(expv, nrecs, sizeof(*expv), (qsort_cmp)rsreccmp);
    double  t_qsort = tick() - t0;

    if (argc > 2) {
	FILE	*fp = strcmp(argv[2], "-") ? fopen(argv[2], "w") : stdout;
        char    buf[16384];
        setvbuf(fp, buf, _IOFBF, 16384);
	for (i = 0; i < nrecs; ++i)
	    fprintf(fp, "%.*s\n", actv[i]->leng, actv[i]->data);
	if (fp != stdout)
	    fclose(fp);
    }

    int	    unstable = 0;
    for (i = 1; i < nrecs; ++i) {
	int	rc = rsreccmp(&actv[i-1], &actv[i]);
	if (rc > 0)
	    break;
	if (rc == 0 && actv[i-1] > actv[i]) {
	    ++unstable;
	    fprintf(stderr, "unstable at %d: %.*s\n", i, actv[i]->leng, actv[i]->data);
	}
    }
#ifdef TAP
    ok(i == nrecs, "%d recs (%"FSIZE"d bytes) sorted correctly (%sstably) in %.2f/%.2f secs; rsort.rss=%ldKB",
	nrecs, inp.len, unstable ? "un" : "", t_rsort, t_qsort, rss);
#else
    fprintf(stderr, "%d recs (%"FSIZE"d bytes) sorted %scorrectly (%sstably) in %.2f/%.2f secs; rsort.rss=%ldKB\n",
	nrecs, inp.len, i == nrecs ? "" : "in", unstable ? "un" : "", t_rsort, t_qsort, rss);
    if (i < nrecs) {
	fprintf(stderr, "[%d] %.*s\n", i-1, actv[i-1]->leng, actv[i-1]->data);
	fprintf(stderr, "[%d] %.*s\n", i, actv[i]->leng, actv[i]->data);
    }
#endif

    buffree(inp);
    free(actv);
    free(data);
    free(expv);
    free(inpv);
#ifdef TAP
    return exit_status();
#else
    return  0;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4108, %rsp           # imm = 0x4108
leal -0x4(%rdi), %eax
cmpl $-0x3, %eax
ja 0x34b0
movq 0x5b44(%rip), %rax      # 0x8fe0
movq (%rax), %rsi
leaq 0x2b5e(%rip), %rdi      # 0x6004
callq 0x3200
jmp 0x37c8
movq %rdi, 0x18(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x8(%rsi), %rdi
callq 0x458c
movq %rax, %rdi
movq %rdx, %rsi
callq 0x3f37
testq %rax, %rax
je 0x3821
movq %rax, %r14
movq %rdx, %r15
xorl %ebp, %ebp
leaq 0x70(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3100
movq 0x20(%rbx), %rax
movq %rax, 0x20(%rsp)
pushq $0xa
popq %rsi
leaq 0xc(%rsp), %rbx
movq %r14, 0x68(%rsp)
movq %r14, %rdi
movq %rbx, %rdx
callq 0x4490
movq %rax, %r13
movslq (%rbx), %r14
leaq (,%r14,8), %rbx
movq %rbx, %rdi
callq 0x3260
movq %rax, %r12
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3260
movq %rax, 0x40(%rsp)
movq %r15, 0x30(%rsp)
leaq (%r15,%r14,2), %rdi
callq 0x3260
movq %rax, %rbx
movq %r13, 0x60(%rsp)
addq $0x8, %r13
movq %rax, 0x58(%rsp)
cmpq %r14, %rbp
jge 0x358a
movq %rbx, (%r12,%rbp,8)
movq (%r13), %r15
movw %r15w, (%rbx)
leaq 0x2(%rbx), %rdi
movq -0x8(%r13), %rsi
movq %r15, %rdx
callq 0x3150
addq %r15, %rbx
addq $0x2, %rbx
incq %rbp
addq $0x10, %r13
jmp 0x3559
movq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
callq 0x3150
callq 0x47f5
movsd %xmm0, 0x50(%rsp)
movl 0xc(%rsp), %esi
movq %r12, %rdi
callq 0x4824
callq 0x47f5
movsd %xmm0, 0x10(%rsp)
leaq 0x70(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3100
movq 0x20(%rbx), %r13
callq 0x47f5
movsd %xmm0, 0x48(%rsp)
movslq 0xc(%rsp), %rsi
leaq 0x24d(%rip), %rcx       # 0x3838
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x3340
callq 0x47f5
movsd %xmm0, 0x28(%rsp)
cmpl $0x3, 0x18(%rsp)
jl 0x3697
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rdi
cmpb $0x2d, (%rdi)
jne 0x362c
cmpb $0x0, 0x1(%rdi)
jne 0x362c
movq 0x59a1(%rip), %rax      # 0x8fc8
movq (%rax), %rbx
jmp 0x363b
leaq 0x2a0d(%rip), %rsi      # 0x6040
callq 0x3210
movq %rax, %rbx
leaq 0x100(%rsp), %rsi
movl $0x4000, %ecx           # imm = 0x4000
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3030
leaq 0x29f9(%rip), %r14      # 0x6052
xorl %r15d, %r15d
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jge 0x3683
movq (%r12,%r15,8), %rcx
movzwl (%rcx), %edx
addq $0x2, %rcx
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x32a0
incq %r15
jmp 0x365c
movq 0x593e(%rip), %rax      # 0x8fc8
cmpq (%rax), %rbx
je 0x3697
movq %rbx, %rdi
callq 0x31b0
movsd 0x10(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
subq 0x20(%rsp), %r13
movq %r13, 0x20(%rsp)
movsd 0x28(%rsp), %xmm0
subsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r12, %r13
addq $0x8, %r13
pushq $0x1
popq %r14
movq 0x5909(%rip), %rbx      # 0x8fe0
xorl %ebp, %ebp
movl $0x0, 0x18(%rsp)
movq 0x30(%rsp), %rcx
movslq 0xc(%rsp), %r15
cmpq %r15, %r14
jge 0x3743
leaq -0x8(%r13), %rdi
movq %r13, %rsi
callq 0x3838
movq 0x30(%rsp), %rcx
testl %eax, %eax
jg 0x3743
jne 0x3711
movq (%r13), %r8
cmpq %r8, -0x8(%r13)
ja 0x371c
incq %r14
addq $0x8, %r13
decl %ebp
jmp 0x36e6
incl 0x18(%rsp)
movq (%rbx), %rdi
movzwl (%r8), %ecx
addq $0x2, %r8
leaq 0x2910(%rip), %rsi      # 0x6042
movl %r14d, %edx
xorl %eax, %eax
callq 0x32a0
movq 0x30(%rsp), %rcx
jmp 0x3711
movq (%rbx), %rdi
cmpl %r15d, %r14d
leaq 0x3196(%rip), %rax      # 0x68e6
leaq 0x2957(%rip), %r8       # 0x60ae
cmoveq %rax, %r8
cmpl $0x0, 0x18(%rsp)
leaq 0x294a(%rip), %r9       # 0x60b1
cmoveq %rax, %r9
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
leaq 0x28dd(%rip), %rsi      # 0x6058
movl %r15d, %edx
movsd 0x10(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movb $0x2, %al
callq 0x32a0
cmpl %r14d, 0xc(%rsp)
jg 0x37dc
movq 0x68(%rsp), %rdi
callq 0x3230
movq %r12, %rdi
callq 0x3230
movq 0x58(%rsp), %rdi
callq 0x3230
movq 0x40(%rsp), %rdi
callq 0x3230
movq 0x60(%rsp), %rdi
callq 0x3230
xorl %eax, %eax
addq $0x4108, %rsp           # imm = 0x4108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rdi
negl %ebp
movq -0x8(%r13), %r8
movzwl (%r8), %ecx
addq $0x2, %r8
leaq 0x28c0(%rip), %r15      # 0x60b4
movq %r15, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x32a0
movq (%rbx), %rdi
movq (%r13), %r8
movzwl (%r8), %ecx
addq $0x2, %r8
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x32a0
jmp 0x3798
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x27fd(%rip), %rdi      # 0x602e
xorl %eax, %eax
callq 0x3f85