// File: /workspace/github_c_makefile_star2_O1/cyb70289[P]tt/src/common/sort.c
// Function: tt_sort_merge
static int tt_sort_merge(struct tt_sort_input *in, struct tt_sort_stat *stat)
{
	/* Allocate copy buffer */
	void *tmpbuf = malloc(in->count * in->num.size);
	if (!tmpbuf)
		return TT_ENOMEM;
	stat->space += in->count;

	int l = 1;	/* subarray length */
	while (l < in->count) {
		/* Merge [0]~[l-1] with [l]~[2l-1],
		 *       [2l]~[3l-1] with [3l]~[4l-1], ...
		 */
		int head = 0, mid, end;
		do {
			mid = head + l - 1;
			end = _tt_min(mid + l, in->count - 1);

			/* Merge [head]~[mid] with [mid+1]~[end] */
			int p = head, p1 = head, p2 = mid + 1;
			void *tmptr = tmpbuf;
			void *ptr1 = sort_ptr(in, p1);
			void *ptr2 = sort_ptr(in, p2);
			while (p <= end) {
				bool la = p2 > end;	/* Pick left array */
				if (!la) {
					if (p1 > mid) {
						la = false;
					} else {
						la = in->num.cmp(&in->num,
							ptr1, ptr2) <= 0;
						stat->time++;	/* Compare */
					}
				}

				if (la) {
					in->num._set(&in->num, tmptr, ptr1);
					p1++;
					ptr1 += in->num.size;
				} else {
					in->num._set(&in->num, tmptr, ptr2);
					p2++;
					ptr2 += in->num.size;
				}
				p++;
				tmptr += in->num.size;
				stat->time++;	/* Set */
			}
			memcpy(sort_ptr(in, head), tmpbuf,
					(end - head + 1) * in->num.size);
			stat->time += (end - head + 1);	/* Set */

			head = end + 1;
		} while (head < in->count);

		l <<= 1;
	}

	free(tmpbuf);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r15
movq %rdi, %r14
movl 0x28(%rdi), %ebx
movl (%rdi), %edi
imull %ebx, %edi
callq 0x1120
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x3265
addl %ebx, 0x4(%r15)
cmpl $0x2, 0x28(%r14)
jb 0x3257
movl $0x1, %eax
movq %r15, 0x18(%rsp)
leal -0x1(%rax), %ecx
movq %rcx, 0x38(%rsp)
xorl %edx, %edx
movq %rax, 0x40(%rsp)
leal (%rdx,%rax), %ecx
movq 0x38(%rsp), %rax
movq %rcx, 0x30(%rsp)
addl %ecx, %eax
movl 0x28(%r14), %ecx
decl %ecx
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0xc(%rsp)
movl %ecx, %eax
movq %rdx, 0x28(%rsp)
subl %edx, %eax
movq %rax, 0x20(%rsp)
jl 0x3203
movq 0x20(%r14), %rax
movl (%r14), %ecx
movl %ecx, %edx
movq 0x30(%rsp), %rsi
imull %esi, %edx
movslq %edx, %r12
addq %rax, %r12
movq 0x28(%rsp), %rdx
imull %edx, %ecx
movslq %ecx, %r13
addq %rax, %r13
movq 0x20(%rsp), %rax
leal 0x1(%rax), %ebx
movq 0x10(%rsp), %rbp
movl %esi, %r15d
movl %edx, 0x8(%rsp)
cmpl 0xc(%rsp), %r15d
setg %al
jg 0x31bf
movl 0x8(%rsp), %eax
cmpl 0x30(%rsp), %eax
jge 0x31dc
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq *0x8(%r14)
testl %eax, %eax
setle %al
movq 0x18(%rsp), %rcx
incl (%rcx)
testb %al, %al
je 0x31dc
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq *0x18(%r14)
incl 0x8(%rsp)
movslq (%r14), %rax
addq %rax, %r13
jmp 0x31f2
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq *0x18(%r14)
incl %r15d
movslq (%r14), %rax
addq %rax, %r12
movslq (%r14), %rax
addq %rax, %rbp
movq 0x18(%rsp), %rax
incl (%rax)
decl %ebx
jne 0x3192
movl (%r14), %eax
movq 0x28(%rsp), %rcx
imull %eax, %ecx
movslq %ecx, %rdi
addq 0x20(%r14), %rdi
movq 0x20(%rsp), %rbx
incl %ebx
imull %ebx, %eax
movslq %eax, %rdx
movq 0x10(%rsp), %rsi
callq 0x1100
movq 0x18(%rsp), %r15
addl %ebx, (%r15)
movl 0xc(%rsp), %eax
incl %eax
movl %eax, %edx
cmpl 0x28(%r14), %eax
movq 0x40(%rsp), %rax
jb 0x3127
addl %eax, %eax
cmpl 0x28(%r14), %eax
jb 0x3118
movq 0x10(%rsp), %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x326a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq