// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rdi, %r14
movl 0x28(%rdi), %ebp
movl (%rdi), %edi
imull %ebp, %edi
callq 0x1120
testq %rax, %rax
je 0x2f01
movq %rax, %r15
addl %ebp, 0x4(%rbx)
pushq $0x1
popq %rax
movl %ebp, %esi
movq %rbx, 0x8(%rsp)
movq %r15, 0x18(%rsp)
cmpl %esi, %eax
jae 0x2ef5
leal -0x1(%rax), %ecx
movq %rcx, 0x20(%rsp)
xorl %edi, %edi
movq %rax, 0x28(%rsp)
leal (%rdi,%rax), %r8d
movq 0x20(%rsp), %rax
addl %r8d, %eax
decl %esi
cmpl %esi, %eax
cmovbl %eax, %esi
movq 0x20(%r14), %rcx
movl (%r14), %eax
movl %eax, %edx
imull %edi, %edx
movq %r15, %rbp
movslq %edx, %r12
addq %rcx, %r12
movl %eax, %edx
imull %r8d, %edx
movslq %edx, %rdx
addq %rcx, %rdx
movq %rdx, (%rsp)
movl %edi, %ebx
movq %rdi, 0x30(%rsp)
movl %edi, %r13d
movq %r8, 0x38(%rsp)
movl %r8d, %r15d
movl %esi, 0x14(%rsp)
cmpl %esi, %ebx
jg 0x2ea6
cmpl %esi, %r15d
jle 0x2e40
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq *0x18(%r14)
incl %r13d
movl (%r14), %eax
movslq %eax, %rcx
addq %rcx, %r12
jmp 0x2e91
cmpl 0x38(%rsp), %r13d
jge 0x2e60
movq %r14, %rdi
movq %r12, %rsi
movq (%rsp), %rdx
callq *0x8(%r14)
movq 0x8(%rsp), %rcx
incl (%rcx)
testl %eax, %eax
jle 0x2e25
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, 0x40(%rsp)
movl %r13d, %r12d
movq (%rsp), %r13
movq %r13, %rdx
callq *0x18(%r14)
incl %r15d
movl (%r14), %eax
movslq %eax, %rcx
addq %rcx, %r13
movq %r13, (%rsp)
movl %r12d, %r13d
movq 0x40(%rsp), %r12
incl %ebx
addq %rcx, %rbp
movq 0x8(%rsp), %rcx
incl (%rcx)
movl 0x14(%rsp), %esi
jmp 0x2e18
movl %eax, %ecx
movq 0x30(%rsp), %rdx
imull %edx, %ecx
movslq %ecx, %rdi
addq 0x20(%r14), %rdi
movl %esi, %ebx
subl %edx, %ebx
incl %ebx
imull %ebx, %eax
movslq %eax, %rdx
movl %esi, %ebp
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x1100
movq 0x8(%rsp), %rax
addl %ebx, (%rax)
movl %ebp, %edi
incl %edi
movl 0x28(%r14), %esi
cmpl %esi, %edi
movq 0x28(%rsp), %rax
jb 0x2dca
addl %eax, %eax
jmp 0x2db3
movq %r15, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x2f04
pushq $-0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq