// File: ctSkennerton[P]fxtract/util/sort_x.c
// Function: main
int
main(int argc, char **argv)
{
    if (argc != 2) die("usage: sort_x <inpfile>");

    MEMBUF  inp = chomp(slurp(argv[1]));
    int	    nrecs;
    MEMREF  *inpv = refsplit(inp.ptr, '\n', &nrecs);
    MEMREF  *v1 = malloc(nrecs * sizeof(MEMREF));
    MEMREF  *v2 = malloc(nrecs * sizeof(MEMREF));
    MEMREF  *v3 = malloc(nrecs * sizeof(MEMREF));
    int     wid, i;

    double  t1[MAXWID] = {}, t2[MAXWID] = {}, t3[MAXWID] = {};
    
    for (wid = 2; wid < MAXWID; wid++) {
        memcpy(v1, inpv, nrecs * sizeof(MEMREF));
        double  start = tick();
        for (i = 0; i + wid < nrecs; i+= wid) {
            switch (wid) {
            case 2:
                if (refcmp(v1[0], v1[1]) > 0)
                    SWAP(v1[0], v1[1]);
                break;
            case 3:
                simpl3(v1);
                break;
            default:
                simple(v1, nrecs);
            }
        }
        t1[wid] += tick() - start;

        memcpy(v2, inpv, nrecs * sizeof(MEMREF));
        start = tick();
        for (i = 0; i + wid < nrecs; i+= wid) quick(v2, nrecs);
        t2[wid] += tick() - start;
        assert(!memcmp(v1, v2, nrecs * sizeof(MEMREF)));

        memcpy(v3, inpv, nrecs * sizeof(MEMREF));
        start = tick();
        for (i = 0; i + wid < nrecs; i+= wid) owsort(v3, nrecs);
        t3[wid] += tick() - start;
        assert(!memcmp(v1, v3, nrecs * sizeof(MEMREF)));
    }

    for (wid = 2; wid < MAXWID; wid++)
        printf("%4d %.6f %.6f %.6f ms\n", wid, 
                1000 * t1[wid] * wid / nrecs,
                1000 * t2[wid] * wid / nrecs,
                1000 * t3[wid] * wid / nrecs);

    return  0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x680, %rsp            # imm = 0x680
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x34bd
leaq 0x3b5a(%rip), %rdi      # 0x7010
movb $0x0, %al
callq 0x4e80
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x58c0
movq %rax, -0x30(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x4df0
movq %rax, -0x20(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x20(%rbp), %rdi
movl $0xa, %esi
leaq -0x34(%rbp), %rdx
callq 0x5670
movq %rax, -0x40(%rbp)
movslq -0x34(%rbp), %rdi
shlq $0x4, %rdi
callq 0x3260
movq %rax, -0x48(%rbp)
movslq -0x34(%rbp), %rdi
shlq $0x4, %rdi
callq 0x3260
movq %rax, -0x50(%rbp)
movslq -0x34(%rbp), %rdi
shlq $0x4, %rdi
callq 0x3260
movq %rax, -0x58(%rbp)
leaq -0x260(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x30f0
leaq -0x460(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x30f0
leaq -0x660(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x30f0
movl $0x2, -0x5c(%rbp)
cmpl $0x40, -0x5c(%rbp)
jge 0x37ec
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
callq 0x3150
callq 0x5cd0
movsd %xmm0, -0x668(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl -0x5c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x3667
movl -0x5c(%rbp), %eax
movl %eax, -0x67c(%rbp)
subl $0x2, %eax
je 0x35cf
jmp 0x35c2
movl -0x67c(%rbp), %eax
subl $0x3, %eax
je 0x3640
jmp 0x364b
movq -0x48(%rbp), %rcx
movq -0x48(%rbp), %rax
movq (%rcx), %rdi
movq 0x8(%rcx), %rsi
movq 0x10(%rax), %rdx
movq 0x18(%rax), %rcx
callq 0x54a0
cmpl $0x0, %eax
jle 0x363e
jmp 0x35f2
movq -0x48(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x678(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x670(%rbp)
movq -0x48(%rbp), %rax
movq -0x48(%rbp), %rcx
movq 0x10(%rcx), %rdx
movq %rdx, (%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
movq -0x678(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x670(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0x363e
jmp 0x3657
movq -0x48(%rbp), %rdi
callq 0x38a0
jmp 0x3657
movq -0x48(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x3a90
jmp 0x3659
movl -0x5c(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x35a3
callq 0x5cd0
subsd -0x668(%rbp), %xmm0
movslq -0x5c(%rbp), %rax
addsd -0x260(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x260(%rbp,%rax,8)
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
callq 0x3150
callq 0x5cd0
movsd %xmm0, -0x668(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl -0x5c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x36d5
movq -0x50(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x3b70
movl -0x5c(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x36b3
callq 0x5cd0
subsd -0x668(%rbp), %xmm0
movslq -0x5c(%rbp), %rax
addsd -0x460(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x460(%rbp,%rax,8)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
callq 0x30c0
cmpl $0x0, %eax
jne 0x3714
jmp 0x3733
leaq 0x390d(%rip), %rdi      # 0x7028
leaq 0x392e(%rip), %rsi      # 0x7050
movl $0x47, %edx
leaq 0x392b(%rip), %rcx      # 0x7059
callq 0x3120
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
callq 0x3150
callq 0x5cd0
movsd %xmm0, -0x668(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
addl -0x5c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x377e
movq -0x58(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x3ba0
movl -0x5c(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x375c
callq 0x5cd0
subsd -0x668(%rbp), %xmm0
movslq -0x5c(%rbp), %rax
addsd -0x660(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x660(%rbp,%rax,8)
movq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rsi
movslq -0x34(%rbp), %rdx
shlq $0x4, %rdx
callq 0x30c0
cmpl $0x0, %eax
jne 0x37bd
jmp 0x37dc
leaq 0x38ac(%rip), %rdi      # 0x7070
leaq 0x3885(%rip), %rsi      # 0x7050
movl $0x4d, %edx
leaq 0x3882(%rip), %rcx      # 0x7059
callq 0x3120
jmp 0x37de
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x3570
movl $0x2, -0x5c(%rbp)
cmpl $0x40, -0x5c(%rbp)
jge 0x3891
movl -0x5c(%rbp), %esi
movslq -0x5c(%rbp), %rax
movsd 0x37fc(%rip), %xmm0     # 0x7008
mulsd -0x260(%rbp,%rax,8), %xmm0
cvtsi2sdl -0x5c(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvtsi2sdl -0x34(%rbp), %xmm1
divsd %xmm1, %xmm0
movslq -0x5c(%rbp), %rax
movsd 0x37d5(%rip), %xmm1     # 0x7008
mulsd -0x460(%rbp,%rax,8), %xmm1
cvtsi2sdl -0x5c(%rbp), %xmm2
mulsd %xmm2, %xmm1
cvtsi2sdl -0x34(%rbp), %xmm2
divsd %xmm2, %xmm1
movslq -0x5c(%rbp), %rax
movsd 0x37ae(%rip), %xmm2     # 0x7008
mulsd -0x660(%rbp,%rax,8), %xmm2
cvtsi2sdl -0x5c(%rbp), %xmm3
mulsd %xmm3, %xmm2
cvtsi2sdl -0x34(%rbp), %xmm3
divsd %xmm3, %xmm2
leaq 0x381c(%rip), %rdi      # 0x7098
movb $0x3, %al
callq 0x3040
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x37f3
xorl %eax, %eax
addq $0x680, %rsp            # imm = 0x680
popq %rbp
retq
nopl (%rax)