// File: /workspace/github_c_makefile_star2_O3/cyb70289[P]tt/tests/heap.c
// Function: main
static void kway_merge(void)
{
	int s1[] = { 3, 15, 27, 27, 28, 99, 1001, 5666, 10087 };
	int s2[] = { 10, 11, 98, 99, 100, 200, 344, 566, 77777 };
	int s3[] = { 1, 7, 91, 101, 444, 712, 866, 5666, 7779 };
	int s4[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

	int *s[4] = { s1, s2, s3, s4 };
	int l[4] = { ARRAY_SIZE(s1), ARRAY_SIZE(s2),
		ARRAY_SIZE(s3), ARRAY_SIZE(s4) };

	int r[ARRAY_SIZE(s1) + ARRAY_SIZE(s2) +
		ARRAY_SIZE(s3) + ARRAY_SIZE(s4)];

	struct pair pt[4];
	struct tt_heap heap = {
		.num	= {
			.size	= sizeof(struct pair),
			.cmp	= kway_cmp,
			.swap	= NULL,
		},
		.data	= pt,
		.cap	= ARRAY_SIZE(pt),
		.htype	= TT_HEAP_MIN,
	};

	tt_heap_init(&heap);

	struct pair p;

	int idx[4] = { 0, 0, 0, 0 };
	for (int i = 0; i < 4; i++) {
		p.si = i;
		p.sv = s[i][idx[i]];
		tt_heap_insert(&heap, &p);
	}

	for (int i = 0; i < ARRAY_SIZE(r) - 4; i++) {
		/* Get minimal */
		tt_heap_extract(&heap, &p);
		r[i] = p.sv;
		/* Continue with current list */
		int nexts = p.si;
		if (idx[nexts] == (l[nexts] - 1)) {
			/* Current list is done, pick any available list */
			for (int j = 0; j < 4; j++) {
				if (j != nexts && idx[j] != (l[j] - 1)) {
					nexts = j;
					break;
				}
			}
		}
		/* Insert to heap */
		idx[nexts]++;
		p.si = nexts;
		p.sv = s[nexts][idx[nexts]];
		tt_heap_insert(&heap, &p);
	}

	/* Last 4 data */
	for (int i = ARRAY_SIZE(r) - 4; i < ARRAY_SIZE(r); i++) {
		tt_heap_extract(&heap, &p);
		r[i] = p.sv;
	}

	/* Got it! */
	for (int i = 0; i < ARRAY_SIZE(r); i++)
		printf("%d ", r[i]);
	printf("\n\n");
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movaps 0x2c3f(%rip), %xmm0     # 0x4040
leaq 0x110(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps 0x2c1c(%rip), %xmm0     # 0x4030
movaps %xmm0, (%rax)
movl $0x2767, 0x20(%rax)     # imm = 0x2767
movaps 0x2c4b(%rip), %xmm0     # 0x4070
leaq 0xe0(%rsp), %rcx
movaps %xmm0, 0x10(%rcx)
movaps 0x2c28(%rip), %xmm0     # 0x4060
movaps %xmm0, (%rcx)
movl $0x12fd1, 0x20(%rcx)    # imm = 0x12FD1
movaps 0x2c57(%rip), %xmm0     # 0x40a0
leaq 0xb0(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps 0x2c34(%rip), %xmm0     # 0x4090
movaps %xmm0, (%rdx)
movl $0x1e63, 0x20(%rdx)     # imm = 0x1E63
movaps 0x2c73(%rip), %xmm0     # 0x40e0
leaq 0x140(%rsp), %rsi
movaps %xmm0, 0x20(%rsi)
movaps 0x2c50(%rip), %xmm0     # 0x40d0
movaps %xmm0, 0x10(%rsi)
movaps 0x2c35(%rip), %xmm0     # 0x40c0
movaps %xmm0, (%rsi)
movl $0xd, 0x30(%rsi)
movq %rax, 0x70(%rsp)
movq %rcx, 0x78(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, 0x88(%rsp)
leaq 0x28(%rsp), %rbx
movl $0x8, (%rbx)
leaq 0x1d0(%rip), %rax       # 0x1691
movq %rax, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
leaq 0x90(%rsp), %rax
movq %rax, 0x20(%rbx)
movabsq $0x100000004, %rax      # imm = 0x100000004
movq %rax, 0x28(%rbx)
movl $0x0, 0x30(%rbx)
movups %xmm0, 0x38(%rbx)
movq %rbx, %rdi
callq 0x2a63
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
movl %r15d, 0x8(%rsp)
movq 0x70(%rsp,%r15,8), %rax
movslq 0x10(%rsp,%r15,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movl 0x58(%rsp), %esi
cmpl 0x50(%rsp), %esi
je 0x154f
imull 0x28(%rsp), %esi
addq 0x48(%rsp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq *0x40(%rsp)
movl 0x58(%rsp), %esi
leal 0x1(%rsi), %eax
movl %eax, 0x58(%rsp)
movq %rbx, %rdi
callq *0x68(%rsp)
incq %r15
cmpq $0x4, %r15
jne 0x1509
xorl %r15d, %r15d
leaq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0x2ab4(%rip), %r12      # 0x4020
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a18
movl 0xc(%rsp), %eax
movl %eax, 0x180(%rsp,%r15,4)
movslq 0x8(%rsp), %rax
movl %eax, %edx
movl 0x10(%rsp,%rax,4), %ecx
movl (%r12,%rax,4), %esi
decl %esi
cmpl %esi, %ecx
jne 0x15c7
movq %r12, %rdi
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x15af
movl (%rdi), %r8d
decl %r8d
cmpl %r8d, 0x10(%rsp,%rsi,4)
jne 0x15be
incq %rsi
addq $0x4, %rdi
cmpq $0x4, %rsi
jne 0x159d
jmp 0x15c7
movl 0x10(%rsp,%rsi,4), %ecx
movq %rsi, %rax
movl %esi, %edx
leal 0x1(%rcx), %esi
movl %esi, 0x10(%rsp,%rax,4)
movl %edx, 0x8(%rsp)
movq 0x70(%rsp,%rax,8), %rax
movslq %ecx, %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movl 0x58(%rsp), %esi
cmpl 0x50(%rsp), %esi
je 0x1612
imull 0x28(%rsp), %esi
addq 0x48(%rsp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq *0x40(%rsp)
movl 0x58(%rsp), %esi
leal 0x1(%rsi), %eax
movl %eax, 0x58(%rsp)
movq %rbx, %rdi
callq *0x68(%rsp)
incq %r15
cmpq $0x24, %r15
jne 0x156c
movl $0x24, %r15d
leaq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a18
movl 0xc(%rsp), %eax
movl %eax, 0x180(%rsp,%r15,4)
incq %r15
cmpq $0x28, %r15
jne 0x162f
leaq 0x2a9e(%rip), %rbx      # 0x40f4
xorl %r14d, %r14d
movl 0x180(%rsp,%r14,4), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
incq %r14
cmpq $0x28, %r14
jne 0x1659
leaq 0x2ba4(%rip), %rdi      # 0x421f
callq 0x1050
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r14
popq %r15
retq