// File: 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
movq %rsp, %rbp
subq $0x4140, %rsp           # imm = 0x4140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jl 0x34b5
cmpl $0x3, -0x8(%rbp)
jle 0x34d7
movq 0x7b24(%rip), %rax      # 0xafe0
movq (%rax), %rsi
leaq 0x4b3e(%rip), %rdi      # 0x8004
callq 0x3200
movl $0x0, -0x4(%rbp)
jmp 0x3af1
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5810
movq %rax, -0x30(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x4d40
movq %rax, -0x20(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3b00
cmpl $0x0, %eax
je 0x3529
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x4b0c(%rip), %rdi      # 0x802e
movb $0x0, %al
callq 0x4dd0
xorl %edi, %edi
leaq -0xc0(%rbp), %rsi
callq 0x3100
movq -0xa0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x20(%rbp), %rdi
movl $0xa, %esi
leaq -0xcc(%rbp), %rdx
callq 0x55c0
movq %rax, -0xd8(%rbp)
movslq -0xcc(%rbp), %rdi
shlq $0x3, %rdi
callq 0x3260
movq %rax, -0xe0(%rbp)
movslq -0xcc(%rbp), %rdi
shlq $0x3, %rdi
callq 0x3260
movq %rax, -0xe8(%rbp)
movq -0x18(%rbp), %rdi
movslq -0xcc(%rbp), %rax
shlq %rax
addq %rax, %rdi
callq 0x3260
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jge 0x36b6
movq -0xf8(%rbp), %rdx
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xd8(%rbp), %rax
movslq -0xfc(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movw %ax, %cx
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movw %cx, (%rax)
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x2, %rdi
movq -0xd8(%rbp), %rax
movslq -0xfc(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0xd8(%rbp), %rax
movslq -0xfc(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
callq 0x3150
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x2, %rax
movq -0xd8(%rbp), %rcx
movslq -0xfc(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
addq 0x8(%rcx), %rax
movq %rax, -0xf8(%rbp)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x35c4
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movslq -0xcc(%rbp), %rdx
shlq $0x3, %rdx
callq 0x3150
callq 0x5c20
movsd %xmm0, -0x108(%rbp)
movq -0xe0(%rbp), %rdi
movl -0xcc(%rbp), %esi
callq 0x5cd0
callq 0x5c20
subsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
xorl %edi, %edi
leaq -0xc0(%rbp), %rsi
callq 0x3100
movq -0xa0(%rbp), %rax
subq -0xc8(%rbp), %rax
movq %rax, -0xc8(%rbp)
callq 0x5c20
movsd %xmm0, -0x108(%rbp)
movq -0xe8(%rbp), %rdi
movslq -0xcc(%rbp), %rsi
movl $0x8, %edx
leaq 0x3ce(%rip), %rcx       # 0x3b20
callq 0x3350
callq 0x5c20
subsd -0x108(%rbp), %xmm0
movsd %xmm0, -0x118(%rbp)
cmpl $0x2, -0x8(%rbp)
jle 0x386f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x48bb(%rip), %rsi      # 0x8040
callq 0x3280
cmpl $0x0, %eax
je 0x37ac
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x48a4(%rip), %rsi      # 0x8042
callq 0x3210
movq %rax, -0x4130(%rbp)
jmp 0x37bd
movq 0x7815(%rip), %rax      # 0xafc8
movq (%rax), %rax
movq %rax, -0x4130(%rbp)
movq -0x4130(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
leaq -0x4120(%rbp), %rsi
xorl %edx, %edx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x3030
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jge 0x384e
movq -0x120(%rbp), %rdi
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzwl (%rax), %edx
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
addq $0x2, %rcx
leaq 0x481e(%rip), %rsi      # 0x8054
movb $0x0, %al
callq 0x32a0
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x37ef
movq -0x120(%rbp), %rax
movq 0x776c(%rip), %rcx      # 0xafc8
cmpq (%rcx), %rax
je 0x386d
movq -0x120(%rbp), %rdi
callq 0x31b0
jmp 0x386f
movl $0x0, -0x4124(%rbp)
movl $0x1, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jge 0x3982
movq -0xe0(%rbp), %rdi
movl -0xfc(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x3, %rax
addq %rax, %rdi
movq -0xe0(%rbp), %rsi
movslq -0xfc(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x3b20
movl %eax, -0x4128(%rbp)
cmpl $0x0, -0x4128(%rbp)
jle 0x38dc
jmp 0x3982
cmpl $0x0, -0x4128(%rbp)
jne 0x396c
movq -0xe0(%rbp), %rax
movl -0xfc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq -0xe0(%rbp), %rcx
movslq -0xfc(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jbe 0x396c
movl -0x4124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4124(%rbp)
movq 0x76b6(%rip), %rax      # 0xafe0
movq (%rax), %rdi
movl -0xfc(%rbp), %edx
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzwl (%rax), %ecx
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rsi
movq (%rax,%rsi,8), %r8
addq $0x2, %r8
leaq 0x46df(%rip), %rsi      # 0x8044
movb $0x0, %al
callq 0x32a0
jmp 0x396e
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x3883
movq 0x7657(%rip), %rax      # 0xafe0
movq (%rax), %rdi
movl -0xcc(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0xfc(%rbp), %esi
movl -0xcc(%rbp), %r9d
leaq 0x4706(%rip), %r8       # 0x80b0
leaq 0x4f3d(%rip), %rax      # 0x88ee
cmpl %r9d, %esi
cmoveq %rax, %r8
movl -0x4124(%rbp), %esi
leaq 0x4f29(%rip), %r9       # 0x88ee
leaq 0x46e7(%rip), %rax      # 0x80b3
cmpl $0x0, %esi
cmovneq %rax, %r9
movsd -0x110(%rbp), %xmm0
movsd -0x118(%rbp), %xmm1
movq -0xc8(%rbp), %rax
leaq 0x4669(%rip), %rsi      # 0x805a
movq %rax, (%rsp)
movb $0x2, %al
callq 0x32a0
movl -0xfc(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jge 0x3aad
movq 0x75cb(%rip), %rax      # 0xafe0
movq (%rax), %rdi
movl -0xfc(%rbp), %edx
subl $0x1, %edx
movq -0xe0(%rbp), %rax
movl -0xfc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movzwl (%rax), %ecx
movq -0xe0(%rbp), %rax
movl -0xfc(%rbp), %esi
subl $0x1, %esi
movslq %esi, %rsi
movq (%rax,%rsi,8), %r8
addq $0x2, %r8
leaq 0x4659(%rip), %rsi      # 0x80b6
movb $0x0, %al
callq 0x32a0
movq 0x7575(%rip), %rax      # 0xafe0
movq (%rax), %rdi
movl -0xfc(%rbp), %edx
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movzwl (%rax), %ecx
movq -0xe0(%rbp), %rax
movslq -0xfc(%rbp), %rsi
movq (%rax,%rsi,8), %r8
addq $0x2, %r8
leaq 0x4610(%rip), %rsi      # 0x80b6
movb $0x0, %al
callq 0x32a0
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3ba0
movq -0xe0(%rbp), %rdi
callq 0x3230
movq -0xf0(%rbp), %rdi
callq 0x3230
movq -0xe8(%rbp), %rdi
callq 0x3230
movq -0xd8(%rbp), %rdi
callq 0x3230
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x4140, %rsp           # imm = 0x4140
popq %rbp
retq
nopl (%rax)