// File: /workspace/github_c_makefile_star2_O2/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 0x2ddf(%rip), %xmm0     # 0x4040
leaq 0x110(%rsp), %rax
movaps %xmm0, 0x10(%rax)
movaps 0x2dbc(%rip), %xmm0     # 0x4030
movaps %xmm0, (%rax)
movl $0x2767, 0x20(%rax)     # imm = 0x2767
movaps 0x2deb(%rip), %xmm0     # 0x4070
leaq 0xe0(%rsp), %rcx
movaps %xmm0, 0x10(%rcx)
movaps 0x2dc8(%rip), %xmm0     # 0x4060
movaps %xmm0, (%rcx)
movl $0x12fd1, 0x20(%rcx)    # imm = 0x12FD1
movaps 0x2df7(%rip), %xmm0     # 0x40a0
leaq 0xb0(%rsp), %rdx
movaps %xmm0, 0x10(%rdx)
movaps 0x2dd4(%rip), %xmm0     # 0x4090
movaps %xmm0, (%rdx)
movl $0x1e63, 0x20(%rdx)     # imm = 0x1E63
movaps 0x2e13(%rip), %xmm0     # 0x40e0
leaq 0x140(%rsp), %rsi
movaps %xmm0, 0x20(%rsi)
movaps 0x2df0(%rip), %xmm0     # 0x40d0
movaps %xmm0, 0x10(%rsi)
movaps 0x2dd5(%rip), %xmm0     # 0x40c0
movaps %xmm0, (%rsi)
movl $0xd, 0x30(%rsi)
movq %rax, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
leaq 0x48(%rsp), %rbx
movl $0x8, (%rbx)
leaq 0x18c(%rip), %rax       # 0x14a7
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)
andl $0x0, 0x30(%rbx)
movups %xmm0, 0x38(%rbx)
movq %rbx, %rdi
callq 0x287d
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
cmpq $0x4, %r15
je 0x138c
movl %r15d, 0x8(%rsp)
movq 0x20(%rsp,%r15,8), %rax
movslq 0x10(%rsp,%r15,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14ae
incq %r15
jmp 0x1360
xorl %r15d, %r15d
leaq 0x48(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0x2c80(%rip), %r12      # 0x4020
cmpq $0x24, %r15
je 0x1433
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2834
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 0x1405
movq %r12, %rdi
xorl %esi, %esi
cmpq $0x4, %rsi
je 0x1405
cmpq %rsi, %rdx
je 0x13f3
movl (%rdi), %r8d
decl %r8d
cmpl %r8d, 0x10(%rsp,%rsi,4)
jne 0x13fc
incq %rsi
addq $0x4, %rdi
jmp 0x13db
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 0x20(%rsp,%rax,8), %rax
movslq %ecx, %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x14ae
incq %r15
jmp 0x13a0
pushq $0x24
popq %r15
leaq 0x48(%rsp), %rbx
leaq 0x8(%rsp), %r14
cmpq $0x28, %r15
je 0x1463
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2834
movl 0xc(%rsp), %eax
movl %eax, 0x180(%rsp,%r15,4)
incq %r15
jmp 0x1441
leaq 0x2c8a(%rip), %rbx      # 0x40f4
xorl %r14d, %r14d
cmpq $0x28, %r14
je 0x148a
movl 0x180(%rsp,%r14,4), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
incq %r14
jmp 0x146d
leaq 0x2d8e(%rip), %rdi      # 0x421f
callq 0x1050
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r14
popq %r15
retq