// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/src/common/sort.c
// Function: tt_sort_quick
static int tt_sort_quick(struct tt_sort_input *in, struct tt_sort_stat *stat)
{
	stat->depth++;	/* Max stack depth */
	if (stat->depth > stat->space)
		stat->space = stat->depth;

	/* Buffer to hold pivot value */
	char pivot[in->num.size] _tt_align(8);

	int p = stat->head;
	int r = stat->tail;
	while (p < r) {
		/* Select pivot */
		void *vp = sort_ptr(in, p);
		void *vr = sort_ptr(in, r);
		/* Pick middle element as pivot if short array */
		void *vq = sort_ptr(in, (p + r) / 2);
		void *vm = vq;
		if (r - p >= 15) {
			/* Pick medium-3 as pivot if long array */
			if (in->num.cmp(&in->num, vp, vq) < 0) {
				vm = vp;
				if (in->num.cmp(&in->num, vp, vr) < 0) {
					vm = vr;
					if (in->num.cmp(&in->num, vq, vr) < 0)
						vm = vq;
				}
			} else {
				vm = vq;
				if (in->num.cmp(&in->num, vq, vr) < 0) {
					vm = vr;
					if (in->num.cmp(&in->num, vp, vr) < 0)
						vm = vp;
				}
			}
		}
		/* Save pivot value */
		in->num._set(&in->num, pivot, vm);

		/* Split-end partition */
		void *pa = vp, *pb = vp;
		void *pc = vr, *pd = vr;
		while (1) {
			int r;
			while (pb <= pc &&
				(r = in->num.cmp(&in->num, pb, pivot)) <= 0) {
				if (r == 0) {
					in->num.swap(&in->num, pa, pb);
					pa += in->num.size;
					stat->time++;
				}
				pb += in->num.size;
				stat->time++;
			}
			while (pc >= pb &&
				(r = in->num.cmp(&in->num, pc, pivot)) >= 0) {
				if (r == 0) {
					in->num.swap(&in->num, pc, pd);
					pd -= in->num.size;
					stat->time++;
				}
				pc -= in->num.size;
				stat->time++;
			}
			if (pb > pc)
				break;
			in->num.swap(&in->num, pb, pc);
			pb += in->num.size;
			pc -= in->num.size;
			stat->time++;
		}
		/* Current layout
		 *  +-----+---------+---------+-----+
		 *  |  =  |    <    |    >    |  =  |
		 *  +-----+---------+---------+-----+
		 *  vp    pa      pc pb       pd   rp
		 */

		int l = _tt_min(pa - vp, pb - pa);
		void *vt = pb - l;
		while (l) {
			in->num.swap(&in->num, vp, vt);
			vp += in->num.size;
			vt += in->num.size;
			l -= in->num.size;
			stat->time++;
		}
		l = _tt_min(pd - pc, vr - pd);
		vt = pb;
		vr -= (l - in->num.size);
		while (l) {
			in->num.swap(&in->num, vt, vr);
			vt += in->num.size;
			vr += in->num.size;
			l -= in->num.size;
			stat->time++;
		}
		int q1 = p + (pb - pa) / in->num.size - 1;
		int q2 = r - (pd - pc) / in->num.size + 1;
		/* Current layout
		 *  +---------+---------+----------+
		 *  |    <    |    =    |    >     |
		 *  +---------+---------+----------+
		 *  p        q1         q2         r
		 */

		/* Call upon short list recursively, leave long one iterated */
		if (q1 - p <= r - q2) {
			stat->head = p;
			stat->tail = q1;
			tt_sort_quick(in, stat);
			p = q2;
		} else {
			stat->head = q2;
			stat->tail = r;
			tt_sort_quick(in, stat);
			r = q1;
		}
	}

	stat->depth--;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
movl 0x8(%rsi), %eax
incl %eax
movl %eax, 0x8(%rsi)
cmpl 0x4(%rsi), %eax
jbe 0x2feb
movl %eax, 0x4(%rsi)
movl (%r14), %eax
movq %rsp, %rcx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movl 0xc(%rsi), %ebx
movq %rsi, -0x38(%rbp)
movl 0x10(%rsi), %r15d
movl %r15d, %ecx
subl %ebx, %ecx
jle 0x329e
movq 0x20(%r14), %r10
movl (%r14), %edi
movl %edi, %r8d
leal (%rbx,%r15), %eax
cltd
pushq $0x2
popq %r9
idivl %r9d
imull %edi, %eax
imull %ebx, %edi
movslq %edi, %r13
addq %r10, %r13
movq %r15, -0x60(%rbp)
imull %r15d, %r8d
movslq %r8d, %r12
addq %r10, %r12
movslq %eax, %r15
addq %r10, %r15
cmpl $0xf, %ecx
movq %r13, -0x30(%rbp)
movq %rbx, -0x58(%rbp)
jl 0x30a9
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq *0x8(%r14)
movq 0x8(%r14), %rcx
movq %r14, %rdi
testl %eax, %eax
js 0x3081
movq %r15, %rsi
movq %r12, %rdx
callq *%rcx
movq %r13, %rbx
jmp 0x308f
movq %r13, %rsi
movq %r12, %rdx
callq *%rcx
movq %r15, %rbx
movq %r13, %r15
testl %eax, %eax
jns 0x30a9
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq *0x8(%r14)
testl %eax, %eax
cmovnsq %r12, %rbx
movq %rbx, %r15
movq %r14, %rdi
movq -0x40(%rbp), %rsi
movq %r15, %rdx
callq *0x18(%r14)
movq %r12, %r13
movq %r12, -0x50(%rbp)
movq -0x30(%rbp), %rsi
movq %rsi, %rbx
movq %rsi, %r15
jmp 0x3137
movq %r14, %rdi
movq %r12, %rsi
movq -0x40(%rbp), %rdx
callq *0x8(%r14)
testl %eax, %eax
js 0x3117
je 0x30ec
movslq (%r14), %rax
movq -0x38(%rbp), %rdx
movl (%rdx), %ecx
negq %rax
jmp 0x310a
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq *0x10(%r14)
movslq (%r14), %rax
subq %rax, %r13
negq %rax
movq -0x38(%rbp), %rdx
movl (%rdx), %ecx
incl %ecx
addq %rax, %r12
incl %ecx
movl %ecx, (%rdx)
movq -0x30(%rbp), %rsi
jmp 0x3183
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq *0x10(%r14)
movslq (%r14), %rax
addq %rax, %rbx
subq %rax, %r12
movq -0x38(%rbp), %rdx
incl (%rdx)
movq -0x30(%rbp), %rsi
cmpq %r12, %rbx
ja 0x3183
movq %r14, %rdi
movq %rbx, %rsi
movq -0x40(%rbp), %rdx
callq *0x8(%r14)
testl %eax, %eax
jg 0x3111
movq -0x38(%rbp), %rdx
je 0x315b
movslq (%r14), %rax
movl (%rdx), %ecx
jmp 0x3176
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq *0x10(%r14)
movq -0x38(%rbp), %rdx
movslq (%r14), %rax
addq %rax, %r15
movl (%rdx), %ecx
incl %ecx
movq -0x30(%rbp), %rsi
addq %rax, %rbx
incl %ecx
movl %ecx, (%rdx)
jmp 0x3137
cmpq %rbx, %r12
jae 0x30ca
movq %r13, -0x30(%rbp)
movq %rbx, %rax
subq %r15, %rax
subq %rsi, %r15
cmpq %rax, %r15
movq %rax, -0x48(%rbp)
cmovgeq %rax, %r15
movslq %r15d, %rax
movq %rbx, %r13
subq %rax, %r13
movq %rbx, -0x68(%rbp)
testl %r15d, %r15d
je 0x31ea
movq %r14, %rdi
movq %r12, %rbx
movq %r15, %r12
movq %rsi, %r15
movq %r13, %rdx
callq *0x10(%r14)
movq %r15, %rsi
movq %r12, %r15
movq %rbx, %r12
movq -0x68(%rbp), %rbx
movslq (%r14), %rax
addq %rax, %rsi
addq %rax, %r13
subl %eax, %r15d
movq -0x38(%rbp), %rax
incl (%rax)
jmp 0x31b1
movq -0x50(%rbp), %rdx
movq %rdx, %r15
movq -0x30(%rbp), %rax
subq %rax, %r15
subq %r12, %rax
cmpq %r15, %rax
movq %rax, -0x30(%rbp)
cmovlq %rax, %r15
movl (%r14), %eax
movl %r15d, %ecx
subl %eax, %ecx
movslq %ecx, %rcx
subq %rcx, %rdx
movq %rdx, %r12
movq -0x38(%rbp), %rsi
movq -0x58(%rbp), %r13
testl %r15d, %r15d
je 0x3245
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq *0x10(%r14)
movq -0x38(%rbp), %rsi
movslq (%r14), %rax
addq %rax, %rbx
addq %rax, %r12
subl %eax, %r15d
incl (%rsi)
jmp 0x321f
movslq %eax, %r8
movq -0x48(%rbp), %rax
cqto
idivq %r8
movq %rax, %rcx
leal (%rax,%r13), %edi
decl %edi
movq -0x30(%rbp), %rax
cqto
idivq %r8
movq -0x60(%rbp), %r15
movl %r15d, %ebx
subl %eax, %ebx
incl %ebx
decl %ecx
movl %r15d, %eax
subl %ebx, %eax
cmpl %eax, %ecx
movl %r13d, %eax
cmovgl %ebx, %eax
movl %edi, %ecx
cmovgl %r15d, %ecx
cmovgl %edi, %r15d
cmovgl %r13d, %ebx
movl %eax, 0xc(%rsi)
movl %ecx, 0x10(%rsi)
movq %r14, %rdi
callq 0x2fc7
jmp 0x300e
movq -0x38(%rbp), %rax
decl 0x8(%rax)
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop