// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x58(%rsp), %rax
movl 0x28(%rax), %eax
movq 0x58(%rsp), %rcx
imull (%rcx), %eax
movl %eax, %eax
movl %eax, %edi
callq 0x1120
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x4631
movl $0xffffffff, 0x64(%rsp) # imm = 0xFFFFFFFF
jmp 0x48c9
movq 0x58(%rsp), %rax
movl 0x28(%rax), %ecx
movq 0x50(%rsp), %rax
addl 0x4(%rax), %ecx
movl %ecx, 0x4(%rax)
movl $0x1, 0x44(%rsp)
movl 0x44(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x28(%rcx), %eax
jae 0x48b7
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
addl 0x44(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
addl 0x44(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x58(%rsp), %rax
movl 0x28(%rax), %eax
subl $0x1, %eax
movl %eax, 0x30(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x30(%rsp), %eax
jae 0x46a3
movl 0x34(%rsp), %eax
movl %eax, (%rsp)
jmp 0x46aa
movl 0x30(%rsp), %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x24(%rsp), %ecx
movq 0x58(%rsp), %rdx
imull (%rdx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x20(%rsp), %ecx
movq 0x58(%rsp), %rdx
imull (%rdx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x38(%rsp), %eax
jg 0x483d
movl 0x20(%rsp), %eax
cmpl 0x38(%rsp), %eax
setg %al
andb $0x1, %al
movb %al, 0x7(%rsp)
testb $0x1, 0x7(%rsp)
jne 0x4789
movl 0x24(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jle 0x4755
movb $0x0, 0x7(%rsp)
jmp 0x4787
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x58(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq *%rax
cmpl $0x0, %eax
setle %al
andb $0x1, %al
movb %al, 0x7(%rsp)
movq 0x50(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x4789
testb $0x1, 0x7(%rsp)
je 0x47ce
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq *%rax
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movq 0x58(%rsp), %rax
movl (%rax), %ecx
movq 0x10(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
jmp 0x480a
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq *%rax
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
movq 0x58(%rsp), %rax
movl (%rax), %ecx
movq 0x8(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
movq 0x58(%rsp), %rax
movl (%rax), %ecx
movq 0x18(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x50(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x471e
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rdi
movl 0x40(%rsp), %eax
movq 0x58(%rsp), %rcx
imull (%rcx), %eax
cltq
addq %rax, %rdi
movq 0x48(%rsp), %rsi
movl 0x38(%rsp), %eax
subl 0x40(%rsp), %eax
addl $0x1, %eax
movq 0x58(%rsp), %rcx
imull (%rcx), %eax
movslq %eax, %rdx
callq 0x1100
movl 0x38(%rsp), %ecx
subl 0x40(%rsp), %ecx
addl $0x1, %ecx
movq 0x50(%rsp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl 0x28(%rcx), %eax
jb 0x4666
movl 0x44(%rsp), %eax
shll %eax
movl %eax, 0x44(%rsp)
jmp 0x464c
movq 0x48(%rsp), %rdi
callq 0x1030
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)