// File: /workspace/github_cpp_makefile_star2_O1/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
movl %edi, %ebp
leal -0x4(%rbp), %eax
cmpl $-0x3, %eax
ja 0x35b1
callq 0x33f7
jmp 0x3956
movq %rsi, 0x38(%rsp)
movq 0x8(%rsi), %rdi
callq 0x476b
movq %rax, %rdi
movq %rdx, %rsi
callq 0x4109
testq %rax, %rax
je 0x3979
movq %rax, %r15
movq %rdx, %r13
leaq 0x70(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3100
movq 0x20(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0xc(%rsp), %rbx
movq %r15, 0x48(%rsp)
movq %r15, %rdi
movl $0xa, %esi
movq %rbx, %rdx
callq 0x4660
movq %rax, 0x40(%rsp)
movslq (%rbx), %r12
leaq (,%r12,8), %r15
movq %r15, %rdi
callq 0x3260
movq %rax, %r14
movq %r15, %rdi
callq 0x3260
movq %rax, 0x50(%rsp)
leaq (,%r12,2), %rdi
addq %r13, %rdi
movq %r14, %r12
callq 0x3260
movq %rax, %r14
movl (%rbx), %eax
testl %eax, %eax
jle 0x3695
movq 0x40(%rsp), %rax
leaq 0x8(%rax), %rbx
xorl %r15d, %r15d
movq %r14, %rdi
movq %rdi, (%r12,%r15,8)
movq (%rbx), %rdx
movw %dx, (%rdi)
addq $0x2, %rdi
movq -0x8(%rbx), %rsi
callq 0x3150
movq (%r12,%r15,8), %rax
movq (%rbx), %rcx
leaq (%rax,%rcx), %rdi
addq $0x2, %rdi
incq %r15
movslq 0xc(%rsp), %rax
addq $0x10, %rbx
cmpq %rax, %r15
jl 0x365e
movq %r14, 0x60(%rsp)
movq %r13, 0x68(%rsp)
movslq %eax, %rdx
shlq $0x3, %rdx
movq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3150
callq 0x4a06
movsd %xmm0, 0x20(%rsp)
movl 0xc(%rsp), %esi
movq %r12, 0x10(%rsp)
movq %r12, %rdi
callq 0x4a38
callq 0x4a06
movsd %xmm0, 0x30(%rsp)
leaq 0x70(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3100
movq 0x20(%rbx), %rbx
callq 0x4a06
movsd %xmm0, 0x58(%rsp)
movslq 0xc(%rsp), %rsi
leaq 0x289(%rip), %rcx       # 0x3990
movl $0x8, %edx
movq %r15, %rdi
callq 0x3340
callq 0x4a06
movsd %xmm0, 0x28(%rsp)
cmpl $0x3, %ebp
movq 0x48(%rsp), %r12
jl 0x37d4
movq %rbx, %rbp
movq 0x38(%rsp), %rax
movq 0x10(%rax), %rbx
leaq 0x3900(%rip), %rsi      # 0x7040
movq %rbx, %rdi
callq 0x3280
testl %eax, %eax
jne 0x3758
movq 0x6875(%rip), %rax      # 0x9fc8
movq (%rax), %r14
jmp 0x376a
leaq 0x38e3(%rip), %rsi      # 0x7042
movq %rbx, %rdi
callq 0x3210
movq %rax, %r14
leaq 0x100(%rsp), %rsi
movl $0x4000, %ecx           # imm = 0x4000
movq %r14, %rdi
xorl %edx, %edx
callq 0x3030
cmpl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r13
jle 0x37bd
leaq 0x38c0(%rip), %rbx      # 0x7054
xorl %r15d, %r15d
movq (%r13,%r15,8), %rcx
movzwl (%rcx), %edx
addq $0x2, %rcx
movq %r14, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x32a0
incq %r15
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jl 0x3797
movq 0x6804(%rip), %rax      # 0x9fc8
cmpq (%rax), %r14
je 0x37d1
movq %r14, %rdi
callq 0x31b0
movq %rbp, %rbx
movsd 0x30(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
subq 0x18(%rsp), %rbx
movq %rbx, 0x20(%rsp)
movsd 0x28(%rsp), %xmm0
subsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x40e5(%rip), %rcx      # 0x78ee
movl $0x1, %r14d
cmpl $0x2, 0xc(%rsp)
movq %rcx, %r9
jl 0x38db
movl $0x1, %r14d
movl $0x0, 0x18(%rsp)
movq 0x10(%rsp), %r12
movq %r12, %rax
movq -0x8(%r12,%r14,8), %r12
movq (%rax,%r14,8), %r13
movzwl (%r12), %edx
movzwl (%r13), %r15d
movl %edx, %ebp
subl %r15d, %ebp
cmovgel %r15d, %edx
leaq 0x2(%r12), %rdi
leaq 0x2(%r13), %rbx
movq %rbx, %rsi
callq 0x30c0
testl %eax, %eax
cmovnel %eax, %ebp
testl %ebp, %ebp
jg 0x389e
cmpq %r13, %r12
jbe 0x389e
testl %ebp, %ebp
movq 0x10(%rsp), %r12
jne 0x38a3
incl 0x18(%rsp)
movq 0x675e(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x37b8(%rip), %rsi      # 0x7044
movl %r14d, %edx
movl %r15d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x32a0
jmp 0x38a3
movq 0x10(%rsp), %r12
testl %ebp, %ebp
jg 0x38b8
incq %r14
movslq 0xc(%rsp), %rax
cmpq %rax, %r14
jl 0x3830
cmpl $0x0, 0x18(%rsp)
leaq 0x402a(%rip), %rax      # 0x78ee
leaq 0x37e8(%rip), %r9       # 0x70b3
cmoveq %rax, %r9
movq 0x48(%rsp), %r12
leaq 0x4013(%rip), %rcx      # 0x78ee
movq 0x66fe(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
movl 0xc(%rsp), %edx
cmpl %edx, %r14d
leaq 0x37bd(%rip), %r8       # 0x70b0
cmoveq %rcx, %r8
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
leaq 0x3753(%rip), %rsi      # 0x705a
movq 0x68(%rsp), %rcx
movsd 0x30(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movb $0x2, %al
callq 0x32a0
cmpl 0xc(%rsp), %r14d
jl 0x396a
movq %r12, %rdi
callq 0x3230
movq 0x10(%rsp), %rdi
callq 0x3230
movq 0x60(%rsp), %rdi
callq 0x3230
movq 0x50(%rsp), %rdi
callq 0x3230
movq 0x40(%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
movl %r14d, %edi
movq 0x10(%rsp), %rsi
callq 0x3390
jmp 0x3926
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x36a5(%rip), %rdi      # 0x702e
xorl %eax, %eax
callq 0x4157