// File: /workspace/github_c_makefile_star2_O3/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
leal 0x1(%rax), %ecx
movl %ecx, 0x8(%rsi)
cmpl 0x4(%rsi), %ecx
jbe 0x3313
movl %ecx, 0x4(%rsi)
movl (%r14), %ecx
movq %rsp, %rdx
addq $0xf, %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x50(%rbp)
movq %rdx, %rsp
movl 0xc(%rsi), %r8d
movl 0x10(%rsi), %r12d
cmpl %r12d, %r8d
jge 0x35f3
movq %rsi, -0x30(%rbp)
movq 0x20(%r14), %rax
movl (%r14), %ecx
movl %ecx, %edx
leal (%r8,%r12), %esi
movl %esi, %edi
shrl $0x1f, %edi
addl %esi, %edi
sarl %edi
imull %ecx, %edi
imull %r8d, %ecx
movslq %ecx, %rbx
addq %rax, %rbx
imull %r12d, %edx
movslq %edx, %rcx
addq %rax, %rcx
movq %rcx, -0x38(%rbp)
movslq %edi, %r15
addq %rax, %r15
movl %r12d, %eax
movq %r8, -0x68(%rbp)
subl %r8d, %eax
cmpl $0xf, %eax
movq %rbx, -0x40(%rbp)
movq %r12, -0x70(%rbp)
jl 0x33b4
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq *0x8(%r14)
movq 0x8(%r14), %rcx
movq %r14, %rdi
testl %eax, %eax
js 0x33ba
movq %r15, %rsi
movq -0x38(%rbp), %r12
movq %r12, %rdx
callq *%rcx
jmp 0x33cf
movq -0x38(%rbp), %r12
jmp 0x33e9
movq %rbx, %rsi
movq %rbx, %r13
movq -0x38(%rbp), %r12
movq %r12, %rdx
callq *%rcx
movq %r15, %rbx
movq %r13, %r15
testl %eax, %eax
jns 0x33e9
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 -0x50(%rbp), %rsi
movq %r15, %rdx
callq *0x18(%r14)
movq %r12, %r13
movq -0x40(%rbp), %r15
movq %r15, %rbx
cmpq %r12, %rbx
ja 0x345d
movq %r14, %rdi
movq %rbx, %rsi
movq -0x50(%rbp), %rdx
callq *0x8(%r14)
testl %eax, %eax
jg 0x345d
movq -0x30(%rbp), %rdx
je 0x3495
movslq (%r14), %rax
movl (%rdx), %ecx
jmp 0x34b0
je 0x3438
movslq (%r14), %rax
movq -0x30(%rbp), %rdx
movl (%rdx), %ecx
negq %rax
jmp 0x3456
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq *0x10(%r14)
movslq (%r14), %rax
subq %rax, %r13
negq %rax
movq -0x30(%rbp), %rdx
movl (%rdx), %ecx
incl %ecx
addq %rax, %r12
incl %ecx
movl %ecx, (%rdx)
cmpq %rbx, %r12
jb 0x34bc
movq %r14, %rdi
movq %r12, %rsi
movq -0x50(%rbp), %rdx
callq *0x8(%r14)
testl %eax, %eax
jns 0x3428
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq *0x10(%r14)
movslq (%r14), %rax
addq %rax, %rbx
subq %rax, %r12
movq -0x30(%rbp), %rdx
incl (%rdx)
jmp 0x3401
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq *0x10(%r14)
movq -0x30(%rbp), %rdx
movslq (%r14), %rax
addq %rax, %r15
movl (%rdx), %ecx
incl %ecx
addq %rax, %rbx
incl %ecx
movl %ecx, (%rdx)
jmp 0x3401
movq %r13, -0x48(%rbp)
movq %rbx, %rax
subq %r15, %rax
subq -0x40(%rbp), %r15
cmpq %rax, %r15
movq %rax, -0x60(%rbp)
cmovgeq %rax, %r15
testl %r15d, %r15d
movq %rbx, -0x58(%rbp)
je 0x3513
movslq %r15d, %rax
movq %rbx, %r13
subq %rax, %r13
movq %r14, %rbx
movq %r15, %r14
movq -0x40(%rbp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq *0x10(%rbx)
movslq (%rbx), %rax
addq %rax, %r15
addq %rax, %r13
movq -0x30(%rbp), %rcx
incl (%rcx)
subl %eax, %r14d
jne 0x34f1
jmp 0x3519
movl (%r14), %eax
movq %r14, %rbx
movq -0x38(%rbp), %rdx
movq %rdx, %r15
movq -0x48(%rbp), %rcx
subq %rcx, %r15
subq %r12, %rcx
cmpq %r15, %rcx
movq %rcx, -0x48(%rbp)
cmovlq %rcx, %r15
testl %r15d, %r15d
je 0x357a
movl %r15d, %ecx
subl %eax, %ecx
movslq %ecx, %rax
subq %rax, %rdx
movq %rdx, %r12
movq -0x30(%rbp), %r9
movq %rbx, %r14
movq -0x58(%rbp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r9, %r13
callq *0x10(%r14)
movq %r13, %r9
movslq (%r14), %rsi
addq %rsi, %rbx
addq %rsi, %r12
incl (%r13)
subl %esi, %r15d
jne 0x3553
jmp 0x3584
movslq %eax, %rsi
movq -0x30(%rbp), %r9
movq %rbx, %r14
movq -0x60(%rbp), %rax
cqto
idivq %rsi
movq %rax, %rcx
movq -0x68(%rbp), %r8
leal (%rax,%r8), %edi
decl %edi
movq -0x48(%rbp), %rax
cqto
idivq %rsi
movq -0x70(%rbp), %r12
movl %r12d, %ebx
subl %eax, %ebx
incl %ebx
decl %ecx
movl %r12d, %eax
subl %ebx, %eax
cmpl %eax, %ecx
movl %r8d, %eax
cmovgl %ebx, %eax
movl %edi, %ecx
cmovgl %r12d, %ecx
cmovgl %edi, %r12d
cmovgl %r8d, %ebx
movl %eax, 0xc(%r9)
movl %ecx, 0x10(%r9)
movq %r14, %rdi
movq %r9, %rsi
callq 0x32ee
movl %ebx, %r8d
cmpl %r12d, %ebx
jl 0x3340
movq -0x30(%rbp), %rsi
movl 0x8(%rsi), %eax
decl %eax
movl %eax, 0x8(%rsi)
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop