// File: /workspace/github_c_makefile_star2_O0/cyb70289[P]tt/tests/heap.c
// Function: kway_merge
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:
subq $0x238, %rsp            # imm = 0x238
leaq 0x210(%rsp), %rdi
leaq 0x4d9a(%rip), %rsi      # 0x6010
movl $0x24, %edx
callq 0x10f0
leaq 0x1e0(%rsp), %rdi
leaq 0x4db1(%rip), %rsi      # 0x6040
movl $0x24, %edx
callq 0x10f0
leaq 0x1b0(%rsp), %rdi
leaq 0x4dc8(%rip), %rsi      # 0x6070
movl $0x24, %edx
callq 0x10f0
leaq 0x170(%rsp), %rdi
leaq 0x4ddf(%rip), %rsi      # 0x60a0
movl $0x34, %edx
callq 0x10f0
leaq 0x210(%rsp), %rax
movq %rax, 0x150(%rsp)
leaq 0x1e0(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x1b0(%rsp), %rax
movq %rax, 0x160(%rsp)
leaq 0x170(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x4dce(%rip), %rax      # 0x60e0
movq %rax, 0x140(%rsp)
movq 0x4dc7(%rip), %rax      # 0x60e8
movq %rax, 0x148(%rsp)
movl $0x8, 0x38(%rsp)
leaq 0x248(%rip), %rax       # 0x1580
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x58(%rsp)
movl $0x4, 0x60(%rsp)
movl $0x1, 0x64(%rsp)
movl $0x0, 0x68(%rsp)
movq $0x0, 0x70(%rsp)
movq $0x0, 0x78(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x4120
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10b0
movl $0x0, 0x1c(%rsp)
cmpl $0x4, 0x1c(%rsp)
jge 0x13f2
movl 0x1c(%rsp), %eax
movl %eax, 0x30(%rsp)
movslq 0x1c(%rsp), %rax
movq 0x150(%rsp,%rax,8), %rax
movslq 0x1c(%rsp), %rcx
movslq 0x20(%rsp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x34(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x15a0
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x13a9
movl $0x0, 0x18(%rsp)
movslq 0x18(%rsp), %rax
cmpq $0x24, %rax
jae 0x14f1
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4040
movl 0x34(%rsp), %ecx
movslq 0x18(%rsp), %rax
movl %ecx, 0xa0(%rsp,%rax,4)
movl 0x30(%rsp), %eax
movl %eax, 0x14(%rsp)
movslq 0x14(%rsp), %rax
movl 0x20(%rsp,%rax,4), %eax
movslq 0x14(%rsp), %rcx
movl 0x140(%rsp,%rcx,4), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x149c
movl $0x0, 0x10(%rsp)
cmpl $0x4, 0x10(%rsp)
jge 0x149a
movl 0x10(%rsp), %eax
cmpl 0x14(%rsp), %eax
je 0x148b
movslq 0x10(%rsp), %rax
movl 0x20(%rsp,%rax,4), %eax
movslq 0x10(%rsp), %rcx
movl 0x140(%rsp,%rcx,4), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0x148b
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x149a
jmp 0x148d
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x1454
jmp 0x149c
movslq 0x14(%rsp), %rax
movl 0x20(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rsp,%rax,4)
movl 0x14(%rsp), %eax
movl %eax, 0x30(%rsp)
movslq 0x14(%rsp), %rax
movq 0x150(%rsp,%rax,8), %rax
movslq 0x14(%rsp), %rcx
movslq 0x20(%rsp,%rcx,4), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x34(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x15a0
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x13fa
movl $0x24, 0xc(%rsp)
movslq 0xc(%rsp), %rax
cmpq $0x28, %rax
jae 0x1530
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4040
movl 0x34(%rsp), %ecx
movslq 0xc(%rsp), %rax
movl %ecx, 0xa0(%rsp,%rax,4)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x14f9
movl $0x0, 0x8(%rsp)
movslq 0x8(%rsp), %rax
cmpq $0x28, %rax
jae 0x156a
movslq 0x8(%rsp), %rax
movl 0xa0(%rsp,%rax,4), %esi
leaq 0x4b9a(%rip), %rdi      # 0x60f0
movb $0x0, %al
callq 0x1090
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x1538
leaq 0x4b83(%rip), %rdi      # 0x60f4
movb $0x0, %al
callq 0x1090
addq $0x238, %rsp            # imm = 0x238
retq