// File: wichtounet[P]articles/plf_colony_alpha/plf_timsort.h
// Function: plf::TimSort<Trivial<8>**, plf::colony<Trivial<8>, std::allocator<Trivial<8>>, unsigned short>::sort_dereferencer<plf::colony<Trivial<8>, std::allocator<Trivial<8>>, unsigned short>::less>>::sort(Trivial<8>**, Trivial<8>**, plf::Compare<Trivial<8>* const&, plf::colony<Trivial<8>, std::allocator<Trivial<8>>, unsigned short>::sort_dereferencer<plf::colony<Trivial<8>, std::allocator<Trivial<8>>, unsigned short>::less>>)
static void sort(iter_t const lo, iter_t const hi, compare_t c)
	{
		diff_t nRemaining = (hi - lo);

		if (nRemaining < 2)
		{
			return; // nothing to do
		}

		if (nRemaining < MIN_MERGE)
		{
			diff_t const initRunLen = countRunAndMakeAscending(lo, hi, c);
			binarySort(lo, hi, lo + initRunLen, c);
			return;
		}

		TimSort ts(c);
		diff_t const minRun = minRunLength(nRemaining);
		iter_t cur = lo;

		do
		{
			diff_t runLen = countRunAndMakeAscending(cur, hi, c);

			if (runLen < minRun)
			{
				diff_t const force = std::min(nRemaining, minRun);
				binarySort(cur, cur + force, cur + runLen, c);
				runLen = force;
			}

			ts.pushRun(cur, runLen);
			ts.mergeCollapse();

			cur += runLen;
			nRemaining -= runLen;
		} while (nRemaining != 0);

		assert(cur == hi);
		ts.mergeForceCollapse();
		assert(ts.pending_.size() == 1);

	}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdx, 0x70(%rsp)
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq %rdx, 0x110(%rsp)
movq 0x118(%rsp), %rax
movq 0x120(%rsp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movq %rax, 0x108(%rsp)
cmpq $0x2, 0x108(%rsp)
jge 0xe5523
jmp 0xe57f2
cmpq $0x20, 0x108(%rsp)
jge 0xe55e6
movq 0x70(%rsp), %rsi
movq 0x120(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0xff(%rsp), %rdi
callq 0xe5960
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rsi
leaq 0xff(%rsp), %rdx
callq 0xe5830
movq 0x70(%rsp), %rsi
movq %rax, 0x100(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x120(%rsp), %rax
movq 0x100(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
leaq 0xfe(%rsp), %rdi
callq 0xe5960
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq 0xfe(%rsp), %rcx
callq 0xe5980
jmp 0xe57f2
movq 0x70(%rsp), %rsi
leaq 0xbf(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0xe5960
movq 0x38(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xe5a70
movq 0x108(%rsp), %rdi
callq 0xe5ac0
movq %rax, 0x40(%rsp)
jmp 0xe5623
movq 0x40(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x70(%rsp), %rsi
movq 0x98(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x8f(%rsp), %rdi
callq 0xe5960
jmp 0xe566e
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdi
leaq 0x8f(%rsp), %rdx
callq 0xe5830
movq %rax, 0x20(%rsp)
jmp 0xe568c
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
cmpq 0xb0(%rsp), %rax
jge 0xe5762
leaq 0x108(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x91f40
movq 0x70(%rsp), %rsi
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x80(%rsp), %rcx
leaq (%rax,%rcx,8), %rcx
movq %rcx, 0x10(%rsp)
movq 0x90(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
leaq 0x7f(%rsp), %rdi
callq 0xe5960
jmp 0xe570f
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
leaq 0x7f(%rsp), %rcx
callq 0xe5980
jmp 0xe572a
movq 0x80(%rsp), %rax
movq %rax, 0x90(%rsp)
jmp 0xe5762
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xe5e10
jmp 0xe57fa
movq 0x98(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
callq 0xe5b10
jmp 0xe5781
leaq 0xc0(%rsp), %rdi
callq 0xe5b60
jmp 0xe5790
movq 0x90(%rsp), %rax
shlq $0x3, %rax
addq 0x98(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x90(%rsp), %rcx
movq 0x108(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x108(%rsp)
cmpq $0x0, 0x108(%rsp)
jne 0xe5640
leaq 0xc0(%rsp), %rdi
callq 0xe5d50
jmp 0xe57e5
leaq 0xc0(%rsp), %rdi
callq 0xe5e10
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xa8(%rsp), %rdi
callq 0x5360
nopw (%rax,%rax)