// File: /workspace/github_cpp_makefile_star2_O3/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 $0x4118, %rsp           # imm = 0x4118
leal -0x4(%rdi), %eax
cmpl $-0x3, %eax
ja 0x35af
callq 0x33f7
jmp 0x3936
movq %rdi, 0x20(%rsp)
movq %rsi, 0x48(%rsp)
movq 0x8(%rsi), %rdi
callq 0x4761
movq %rax, %rdi
movq %rdx, %rsi
callq 0x40ec
testq %rax, %rax
je 0x3957
movq %rax, %r14
movq %rdx, %r15
leaq 0x80(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3100
movq 0x20(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x14(%rsp), %rbx
movq %r14, 0x78(%rsp)
movq %r14, %rdi
movl $0xa, %esi
movq %rbx, %rdx
callq 0x462d
movq %rax, 0x60(%rsp)
movslq (%rbx), %r12
leaq (,%r12,8), %rbx
movq %rbx, %rdi
callq 0x3260
movq %rax, %r13
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x3260
movq %rax, 0x68(%rsp)
movq %r15, 0x50(%rsp)
leaq (%r15,%r12,2), %rdi
callq 0x3260
movq %rax, 0x58(%rsp)
testq %r12, %r12
jle 0x3695
movq 0x60(%rsp), %rax
leaq 0x8(%rax), %r14
xorl %ebp, %ebp
movq 0x58(%rsp), %r15
movq %r15, (%r13,%rbp,8)
movq (%r14), %rbx
movw %bx, (%r15)
leaq 0x2(%r15), %rdi
movq -0x8(%r14), %rsi
movq %rbx, %rdx
callq 0x3150
addq %rbx, %r15
addq $0x2, %r15
incq %rbp
addq $0x10, %r14
cmpq %r12, %rbp
jl 0x3666
movq 0x68(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq 0x18(%rsp), %rdx
callq 0x3150
callq 0x49e9
movsd %xmm0, 0x38(%rsp)
movl 0x14(%rsp), %esi
movq %r13, %rdi
callq 0x4a18
callq 0x49e9
movsd %xmm0, 0x18(%rsp)
leaq 0x80(%rsp), %rbx
xorl %edi, %edi
movq %rbx, %rsi
callq 0x3100
movq 0x20(%rbx), %r12
callq 0x49e9
movsd %xmm0, 0x70(%rsp)
movslq 0x14(%rsp), %rsi
leaq 0x275(%rip), %rcx       # 0x396e
movl $0x8, %edx
movq %r14, %rdi
callq 0x3340
callq 0x49e9
movsd %xmm0, 0x40(%rsp)
cmpl $0x3, 0x20(%rsp)
jl 0x37ad
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rdi
cmpb $0x2d, (%rdi)
jne 0x373c
cmpb $0x0, 0x1(%rdi)
jne 0x373c
movq 0x6891(%rip), %rax      # 0x9fc8
movq (%rax), %r14
jmp 0x374b
leaq 0x38fd(%rip), %rsi      # 0x7040
callq 0x3210
movq %rax, %r14
leaq 0x110(%rsp), %rsi
movl $0x4000, %ecx           # imm = 0x4000
movq %r14, %rdi
xorl %edx, %edx
callq 0x3030
cmpl $0x0, 0x14(%rsp)
jle 0x3799
leaq 0x38e2(%rip), %rbx      # 0x7052
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 0x14(%rsp), %rax
cmpq %rax, %r15
jl 0x3773
movq 0x6828(%rip), %rax      # 0x9fc8
cmpq (%rax), %r14
je 0x37ad
movq %r14, %rdi
callq 0x31b0
movsd 0x18(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
subq 0x28(%rsp), %r12
movsd 0x40(%rsp), %xmm0
subsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x14(%rsp), %eax
leaq 0x4105(%rip), %rsi      # 0x78e6
movl $0x1, %r14d
movl %eax, 0x20(%rsp)
cmpl $0x2, %eax
jl 0x388e
movq %r12, 0x38(%rsp)
movl $0x1, %r14d
movl $0x0, 0x28(%rsp)
movq %r13, 0x30(%rsp)
movq -0x8(%r13,%r14,8), %r15
movq (%r13,%r14,8), %rbp
movzwl (%r15), %edx
movzwl (%rbp), %ebx
movl %edx, %r13d
subl %ebx, %r13d
cmovael %ebx, %edx
leaq 0x2(%r15), %rdi
leaq 0x2(%rbp), %r12
movq %r12, %rsi
callq 0x30c0
testl %eax, %eax
cmovnel %eax, %r13d
testl %r13d, %r13d
jg 0x3898
cmpq %rbp, %r15
jbe 0x384c
testl %r13d, %r13d
je 0x3860
movq 0x30(%rsp), %r13
incq %r14
movslq 0x20(%rsp), %rax
cmpq %rax, %r14
jl 0x380c
jmp 0x389d
incl 0x28(%rsp)
movq 0x6775(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x37cd(%rip), %rsi      # 0x7042
movl %r14d, %edx
movl %ebx, %ecx
movq %r12, %r8
xorl %eax, %eax
callq 0x32a0
movl 0x14(%rsp), %eax
movl %eax, 0x20(%rsp)
jmp 0x384c
movq %rsi, %r9
movq 0x50(%rsp), %rcx
jmp 0x38c5
movq 0x30(%rsp), %r13
cmpl $0x0, 0x28(%rsp)
leaq 0x403d(%rip), %rax      # 0x78e6
leaq 0x3801(%rip), %r9       # 0x70b1
cmoveq %rax, %r9
movq 0x50(%rsp), %rcx
movq 0x38(%rsp), %r12
leaq 0x4021(%rip), %rsi      # 0x78e6
movq 0x6714(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
movl 0x20(%rsp), %edx
cmpl %edx, %r14d
leaq 0x37d1(%rip), %r8       # 0x70ae
cmoveq %rsi, %r8
movq %r12, (%rsp)
leaq 0x376c(%rip), %rsi      # 0x7058
movsd 0x18(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
movb $0x2, %al
callq 0x32a0
cmpl 0x14(%rsp), %r14d
jl 0x394a
movq 0x78(%rsp), %rdi
callq 0x3230
movq %r13, %rdi
callq 0x3230
movq 0x58(%rsp), %rdi
callq 0x3230
movq 0x68(%rsp), %rdi
callq 0x3230
movq 0x60(%rsp), %rdi
callq 0x3230
xorl %eax, %eax
addq $0x4118, %rsp           # imm = 0x4118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r14d, %edi
movq %r13, %rsi
callq 0x3390
jmp 0x3906
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x36c7(%rip), %rdi      # 0x702e
xorl %eax, %eax
callq 0x413a