// File: /workspace/github_cpp_makefile_star2_O2/splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: qf_multi_merge
void qf_multi_merge(QF *qf_arr[], int nqf, QF *qfr)
{
	int i;
	QFi qfi_arr[nqf];
	int flag = 0;
	int smallest_i = 0;
	uint64_t smallest_key = UINT64_MAX;
	for (i=0; i<nqf; i++) {
		qf_iterator(qf_arr[i], &qfi_arr[i], 0);
	}

	while (!flag) {
		uint64_t keys[nqf];
		uint64_t values[nqf];
		uint64_t counts[nqf];
		for (i=0; i<nqf; i++)
			qfi_get(&qfi_arr[i], &keys[i], &values[i], &counts[i]);
		
		do {
			smallest_key = UINT64_MAX;
			for (i=0; i<nqf; i++) {
				if (keys[i] < smallest_key) {
					smallest_key = keys[i]; smallest_i = i;
				}
			}
			qf_insert(qfr, keys[smallest_i], values[smallest_i], counts[smallest_i],
								true, true);
			qfi_next(&qfi_arr[smallest_i]);
			qfi_get(&qfi_arr[smallest_i], &keys[smallest_i], &values[smallest_i],
							&counts[smallest_i]);
		} while(!qfi_end(&qfi_arr[smallest_i]));

		/* remove the qf that is exhausted from the array */
		if (smallest_i < nqf-1)
			memmove(&qfi_arr[smallest_i], &qfi_arr[smallest_i+1],
							(nqf-smallest_i-1)*sizeof(qfi_arr[0]));
		nqf--;
		if (nqf == 1)
			flag = 1;
	}
	if (!qfi_end(&qfi_arr[0])) {
		do {
			uint64_t key, value, count;
			qfi_get(&qfi_arr[0], &key, &value, &count);
			qf_insert(qfr, key, value, count, true, true);
		} while(!qfi_next(&qfi_arr[0]));
	}

	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, -0x50(%rbp)
movl %esi, %ebx
movq %rdi, %r14
movl %esi, %eax
movq %rsp, %r15
imulq $0x30, %rax, %rax
subq %rax, %r15
movq %r15, %rsp
xorl %r12d, %r12d
testl %esi, %esi
movl $0x0, %r13d
cmovgl %esi, %r13d
movq %r15, -0x40(%rbp)
cmpq %r12, %r13
je 0xf540
movq (%r14,%r12,8), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xef99
incq %r12
addq $0x30, %r15
jmp 0xf524
movslq %ebx, %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
xorl %eax, %eax
movq -0x40(%rbp), %rbx
testl %eax, %eax
jne 0xf6b4
movq %rsp, -0x60(%rbp)
movq -0x30(%rbp), %rax
movl %eax, -0x44(%rbp)
movl %eax, %eax
movq %rsp, %r14
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %r14, -0x58(%rbp)
movq %r12, -0x78(%rbp)
movq %r13, -0x70(%rbp)
xorl %r15d, %r15d
cmpq -0x30(%rbp), %r15
jge 0xf5cc
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0xf156
incq %r15
addq $0x8, %r13
addq $0x8, %r12
addq $0x8, %r14
addq $0x30, %rbx
jmp 0xf5a0
xorl %eax, %eax
pushq $-0x1
popq %rcx
movq -0x50(%rbp), %r15
movq -0x38(%rbp), %rdx
movq -0x58(%rbp), %rsi
movq %rdx, -0x38(%rbp)
movslq %edx, %rdi
leaq (%rsi,%rdi,8), %r13
movq -0x78(%rbp), %rdx
leaq (%rdx,%rdi,8), %r14
movq -0x70(%rbp), %rdx
leaq (%rdx,%rdi,8), %rbx
movq %rdi, -0x68(%rbp)
imulq $0x30, %rdi, %r12
addq -0x40(%rbp), %r12
cmpq -0x30(%rbp), %rax
jl 0xf656
movq (%r13), %rsi
movq (%rbx), %rcx
movq %r15, %rdi
pushq $0x1
popq %r8
movl %r8d, %r9d
callq 0xdb69
movq %r12, %rdi
callq 0xf1de
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0xf156
movq (%r12), %rax
movq 0x10(%r12), %rdx
movq 0x8(%rax), %rsi
movl $0x0, %eax
pushq $-0x1
popq %rcx
cmpq 0x18(%rsi), %rdx
jb 0xf604
jmp 0xf674
movq -0x58(%rbp), %rsi
movq (%rsi,%rax,8), %rdx
cmpq %rcx, %rdx
cmovbq %rdx, %rcx
movq -0x38(%rbp), %rdx
cmovbl %eax, %edx
incq %rax
jmp 0xf5dd
movq -0x30(%rbp), %rax
decq %rax
movq %rax, -0x30(%rbp)
cmpq -0x68(%rbp), %rax
jle 0xf6a1
leaq 0x30(%r12), %rsi
movq -0x38(%rbp), %rax
notl %eax
addl -0x44(%rbp), %eax
cltq
imulq $0x30, %rax, %rdx
movq %r12, %rdi
callq 0x2380
xorl %eax, %eax
cmpq $0x1, -0x30(%rbp)
sete %al
movq -0x60(%rbp), %rsp
jmp 0xf551
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x8(%rax), %rax
cmpq 0x18(%rax), %rcx
movq -0x50(%rbp), %r13
jae 0xf712
leaq -0x90(%rbp), %r14
leaq -0x80(%rbp), %r15
pushq $0x1
popq %r12
movq %rbx, %rdi
leaq -0x88(%rbp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0xf156
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rcx
movq %r13, %rdi
movl %r12d, %r8d
movl %r12d, %r9d
callq 0xdb69
movq %rbx, %rdi
callq 0xf1de
testl %eax, %eax
je 0xf6d8
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq