// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x28(%rdi), %ebp
movl (%rdi), %edi
imull %ebp, %edi
callq 0x1120
testq %rax, %rax
je 0x321e
movq %rax, %r13
addl %ebp, 0x4(%rbx)
cmpl $0x2, %ebp
jb 0x3212
movl %ebp, %r9d
movl $0x1, %esi
movq %rbx, 0x10(%rsp)
movq %r13, 0x20(%rsp)
leal -0x1(%rsi), %r10d
xorl %edi, %edi
movq %rsi, 0x40(%rsp)
movq %r10, 0x38(%rsp)
leal (%rdi,%rsi), %r8d
leal (%r10,%r8), %eax
decl %r9d
cmpl %r9d, %eax
cmovbl %eax, %r9d
movq 0x20(%r14), %rcx
movl (%r14), %eax
movl %eax, %edx
imull %edi, %edx
movslq %edx, %r12
movl %r9d, %r15d
subl %edi, %r15d
jge 0x30f4
movq %r13, %rsi
movq %rbx, %rbp
jmp 0x31c9
imull %r8d, %eax
cltq
addq %rcx, %rax
movq %rax, 0x8(%rsp)
addq %rcx, %r12
movq %r15, 0x28(%rsp)
leal 0x1(%r15), %ebx
movq %r13, %rbp
movq %r8, 0x48(%rsp)
movl %r8d, %r15d
movq %rdi, 0x30(%rsp)
movl %edi, %r13d
movl %r9d, 0x1c(%rsp)
cmpl %r9d, %r15d
jle 0x3146
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq *0x18(%r14)
incl %r13d
movl (%r14), %eax
movslq %eax, %rcx
addq %rcx, %r12
jmp 0x3194
cmpl 0x48(%rsp), %r13d
jge 0x3167
movq %r14, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %rdx
callq *0x8(%r14)
movq 0x10(%rsp), %rcx
incl (%rcx)
testl %eax, %eax
jle 0x312b
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, 0x50(%rsp)
movq 0x8(%rsp), %r12
movq %r12, %rdx
callq *0x18(%r14)
incl %r15d
movl (%r14), %eax
movslq %eax, %rcx
addq %rcx, %r12
movq %r12, 0x8(%rsp)
movq 0x50(%rsp), %r12
addq %rcx, %rbp
movq 0x10(%rsp), %rcx
incl (%rcx)
decl %ebx
movl 0x1c(%rsp), %r9d
jne 0x3126
movq 0x20(%r14), %rcx
movq 0x30(%rsp), %rdx
imull %eax, %edx
movslq %edx, %r12
movq 0x10(%rsp), %rbp
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %r15
addq %rcx, %r12
movl %r9d, %ebx
incl %ebx
incl %r15d
imull %r15d, %eax
movslq %eax, %rdx
movq %r12, %rdi
movq %rsi, %r13
callq 0x1100
movq %rbx, %rdi
addl %r15d, (%rbp)
movl 0x28(%r14), %r9d
cmpl %r9d, %edi
movq %rbp, %rbx
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %r10
jb 0x30c0
addl %esi, %esi
cmpl %r9d, %esi
jb 0x30b0
movq %r13, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x3223
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq