// 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>>::mergeHi(Trivial<8>**, long, Trivial<8>**, long)
void mergeHi(iter_t const base1, diff_t len1, iter_t const base2, diff_t len2)
	{
		assert(len1 > 0 && len2 > 0 && base1 + len1 == base2);

		copy_to_tmp(base2, len2);

		iter_t cursor1 = base1 + (len1 - 1);
		tmp_iter_t cursor2 = tmp_.begin() + (len2 - 1);
		iter_t dest = base2 + (len2 - 1);

		*(dest--) = PLF_TIMSORT_MOVE(*(cursor1--));
		if (--len1 == 0)
		{
			PLF_TIMSORT_MOVE_RANGE(tmp_.begin(), tmp_.begin() + len2, dest - (len2 - 1));
			return;
		}
		if (len2 == 1)
		{
			dest -= len1;
			cursor1 -= len1;
			PLF_TIMSORT_MOVE_BACKWARD(cursor1 + 1, cursor1 + (1 + len1), dest + (1 + len1));
			*dest = PLF_TIMSORT_MOVE(*cursor2);
			return;
		}

		int minGallop(minGallop_);

		// outer:
		while (true)
		{
			int count1 = 0;
			int count2 = 0;
			bool break_outer = false;

			do
			{
				assert(len1 > 0 && len2 > 1);

				if (comp_.lt(*cursor2, *cursor1))
				{
					*(dest--) = PLF_TIMSORT_MOVE(*(cursor1--));
					++count1;
					count2 = 0;
					if (--len1 == 0)
					{
						break_outer = true;
						break;
					}
				}
				else
				{
					*(dest--) = PLF_TIMSORT_MOVE(*(cursor2--));
					++count2;
					count1 = 0;
					if (--len2 == 1)
					{
						break_outer = true;
						break;
					}
				}
			} while ((count1 | count2) < minGallop);
			
			
			if (break_outer)
			{
				break;
			}

			do
			{
				assert(len1 > 0 && len2 > 1);

				count1 = len1 - gallopRight(*cursor2, base1, len1, len1 - 1);

				if (count1 != 0)
				{
					dest -= count1;
					cursor1 -= count1;
					len1 -= count1;
					PLF_TIMSORT_MOVE_BACKWARD(cursor1 + 1, cursor1 + (1 + count1), dest + (1 + count1));

					if (len1 == 0)
					{
						break_outer = true;
						break;
					}
				}

				*(dest--) = PLF_TIMSORT_MOVE(*(cursor2--));

				if (--len2 == 1)
				{
					break_outer = true;
					break;
				}

				count2 = len2 - gallopLeft(*cursor1, tmp_.begin(), len2, len2 - 1);

				if (count2 != 0)
				{
					dest -= count2;
					cursor2 -= count2;
					len2 -= count2;
					PLF_TIMSORT_MOVE_RANGE(cursor2 + 1, cursor2 + (1 + count2), dest + 1);
					if (len2 <= 1)
					{
						break_outer = true;
						break;
					}
				}

				*(dest--) = PLF_TIMSORT_MOVE(*(cursor1--));

				if (--len1 == 0)
				{
					break_outer = true;
					break;
				}

				minGallop--;
			} while ((count1 >= MIN_GALLOP) | (count2 >= MIN_GALLOP));
			
			
			if (break_outer)
			{
				break;
			}

			if (minGallop < 0)
			{
				minGallop = 0;
			}
			
			minGallop += 2;
		} 

		minGallop_ = std::min(minGallop, 1);

		if (len2 == 1)
		{
			assert(len1 > 0);
			dest -= len1;
			PLF_TIMSORT_MOVE_BACKWARD(cursor1 + (1 - len1), cursor1 + 1, dest + (1 + len1));
			*dest = PLF_TIMSORT_MOVE(*cursor2);
		}
		else
		{
			assert(len2 != 0 && "Comparison function violates its general contract");
			assert(len1 == 0);
			assert(len2 > 1);
			PLF_TIMSORT_MOVE_RANGE(tmp_.begin(), tmp_.begin() + len2, dest - (len2 - 1));
		}
	}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rcx, 0xb8(%rsp)
movq %r8, 0xb0(%rsp)
movq 0xd0(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movq 0xb8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
callq 0xe83c0
movq 0x18(%rsp), %rdi
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rcx
subq $0x1, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%rsp)
addq $0x8, %rdi
callq 0xe8450
movq %rax, 0x98(%rsp)
movq 0xb0(%rsp), %rsi
subq $0x1, %rsi
leaq 0x98(%rsp), %rdi
callq 0xe84e0
movq %rax, 0xa0(%rsp)
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rcx
subq $0x1, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, %rcx
addq $-0x8, %rcx
movq %rcx, 0xa8(%rsp)
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, (%rax)
movq 0xc0(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, %rax
jne 0xe7ad9
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
callq 0xe8450
movq 0x18(%rsp), %rdi
movq %rax, 0x88(%rsp)
addq $0x8, %rdi
callq 0xe8450
movq %rax, 0x78(%rsp)
movq 0xb0(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0xe84e0
movq %rax, 0x80(%rsp)
movq 0x90(%rsp), %rdx
movq 0xb0(%rsp), %rcx
subq $0x1, %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x3, %rax
addq %rax, %rdx
movq 0x88(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xe8480
jmp 0xe8147
cmpq $0x1, 0xb0(%rsp)
jne 0xe7b97
movq 0xc0(%rsp), %rdx
movq 0x90(%rsp), %rax
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0xc0(%rsp), %rdx
movq 0xa8(%rsp), %rax
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rdi
addq $0x8, %rdi
movq 0xa8(%rsp), %rsi
movq 0xc0(%rsp), %rax
addq $0x1, %rax
shlq $0x3, %rax
addq %rax, %rsi
movq 0x90(%rsp), %rdx
movq 0xc0(%rsp), %rax
addq $0x1, %rax
shlq $0x3, %rax
addq %rax, %rdx
callq 0x16020
leaq 0xa0(%rsp), %rdi
callq 0xe8520
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
jmp 0xe8147
movq 0x18(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movb $0x0, 0x6b(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xe8520
movq 0x18(%rsp), %rdi
movq %rax, %rsi
movq 0xa8(%rsp), %rdx
callq 0xe5e40
testb $0x1, %al
jne 0xe7be0
jmp 0xe7c4d
movq 0xa8(%rsp), %rax
movq %rax, %rcx
addq $-0x8, %rcx
movq %rcx, 0xa8(%rsp)
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, (%rax)
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movq 0xc0(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, %rax
jne 0xe7c4b
movb $0x1, 0x6b(%rsp)
jmp 0xe7cd2
jmp 0xe7cbe
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0xe9920
movq %rax, 0x60(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xe8520
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, (%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
movl $0x0, 0x70(%rsp)
movq 0xb0(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0xb0(%rsp)
cmpq $0x1, %rax
jne 0xe7cbc
movb $0x1, 0x6b(%rsp)
jmp 0xe7cd2
jmp 0xe7cbe
jmp 0xe7cc0
movl 0x70(%rsp), %eax
orl 0x6c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jl 0xe7bb8
testb $0x1, 0x6b(%rsp)
je 0xe7cde
jmp 0xe801f
jmp 0xe7ce0
movq 0xc0(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xe8520
movq 0x18(%rsp), %rdi
movq %rax, %rsi
movq 0xc8(%rsp), %rdx
movq 0xc0(%rsp), %rcx
movq 0xc0(%rsp), %r8
subq $0x1, %r8
callq 0xe6e60
movq %rax, %rcx
movq 0x10(%rsp), %rax
subq %rcx, %rax
movl %eax, 0x70(%rsp)
cmpl $0x0, 0x70(%rsp)
je 0xe7df3
movl 0x70(%rsp), %ecx
movq 0x90(%rsp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movl 0x70(%rsp), %ecx
movq 0xa8(%rsp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0xa8(%rsp)
movslq 0x70(%rsp), %rcx
movq 0xc0(%rsp), %rax
subq %rcx, %rax
movq %rax, 0xc0(%rsp)
movq 0xa8(%rsp), %rdi
addq $0x8, %rdi
movq 0xa8(%rsp), %rsi
movl 0x70(%rsp), %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
addq %rax, %rsi
movq 0x90(%rsp), %rdx
movl 0x70(%rsp), %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
addq %rax, %rdx
callq 0x16020
cmpq $0x0, 0xc0(%rsp)
jne 0xe7df1
movb $0x1, 0x6b(%rsp)
jmp 0xe7ff7
jmp 0xe7df3
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0xe9920
movq %rax, 0x58(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xe8520
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, (%rax)
movq 0xb0(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0xb0(%rsp)
cmpq $0x1, %rax
jne 0xe7e52
movb $0x1, 0x6b(%rsp)
jmp 0xe7ff7
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, (%rsp)
addq $0x8, %rdi
callq 0xe8450
movq 0x18(%rsp), %rdi
movq (%rsp), %rsi
movq %rax, 0x50(%rsp)
movq 0xb0(%rsp), %rcx
movq 0xb0(%rsp), %r8
subq $0x1, %r8
movq 0x50(%rsp), %rdx
callq 0xe9960
movq %rax, %rcx
movq 0x8(%rsp), %rax
subq %rcx, %rax
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
je 0xe7f71
movl 0x6c(%rsp), %ecx
movq 0x90(%rsp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movslq 0x6c(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0xe9cd0
movslq 0x6c(%rsp), %rcx
movq 0xb0(%rsp), %rax
subq %rcx, %rax
movq %rax, 0xb0(%rsp)
leaq 0xa0(%rsp), %rdi
movl $0x1, %esi
callq 0xe84e0
movq %rax, 0x48(%rsp)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0xe84e0
movq %rax, 0x40(%rsp)
movq 0x90(%rsp), %rdx
addq $0x8, %rdx
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0xe8480
cmpq $0x1, 0xb0(%rsp)
jg 0xe7f6f
movb $0x1, 0x6b(%rsp)
jmp 0xe7ff7
jmp 0xe7f71
movq 0xa8(%rsp), %rax
movq %rax, %rcx
addq $-0x8, %rcx
movq %rcx, 0xa8(%rsp)
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, 0x90(%rsp)
movq %rcx, (%rax)
movq 0xc0(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, %rax
jne 0xe7fc6
movb $0x1, 0x6b(%rsp)
jmp 0xe7ff7
movl 0x74(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x74(%rsp)
cmpl $0x7, 0x70(%rsp)
setge %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x7, 0x6c(%rsp)
setge %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
cmpl $0x0, %eax
jne 0xe7ce0
testb $0x1, 0x6b(%rsp)
je 0xe8000
jmp 0xe801f
cmpl $0x0, 0x74(%rsp)
jge 0xe800f
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x74(%rsp)
jmp 0xe7ba3
movl $0x1, 0x3c(%rsp)
leaq 0x74(%rsp), %rdi
leaq 0x3c(%rsp), %rsi
callq 0xe8960
movq %rax, %rcx
movq 0x18(%rsp), %rax
movl (%rcx), %ecx
movl %ecx, 0x4(%rax)
cmpq $0x1, 0xb0(%rsp)
jne 0xe80db
movq 0xc0(%rsp), %rdx
movq 0x90(%rsp), %rax
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0xa8(%rsp), %rdi
movl $0x1, %eax
subq 0xc0(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq 0xa8(%rsp), %rsi
addq $0x8, %rsi
movq 0x90(%rsp), %rdx
movq 0xc0(%rsp), %rax
addq $0x1, %rax
shlq $0x3, %rax
addq %rax, %rdx
callq 0x16020
leaq 0xa0(%rsp), %rdi
callq 0xe8520
movq (%rax), %rcx
movq 0x90(%rsp), %rax
movq %rcx, (%rax)
jmp 0xe8147
movq 0x18(%rsp), %rdi
addq $0x8, %rdi
callq 0xe8450
movq 0x18(%rsp), %rdi
movq %rax, 0x30(%rsp)
addq $0x8, %rdi
callq 0xe8450
movq %rax, 0x20(%rsp)
movq 0xb0(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq 0xe84e0
movq %rax, 0x28(%rsp)
movq 0x90(%rsp), %rdx
movq 0xb0(%rsp), %rcx
subq $0x1, %rcx
xorl %eax, %eax
subq %rcx, %rax
shlq $0x3, %rax
addq %rax, %rdx
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0xe8480
addq $0xd8, %rsp
retq
nop