// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, -0x50(%rbp)
movl %esi, %r14d
movq %rsp, %r13
movq %r14, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
subq %rax, %r13
movq %r13, %rsp
movl %esi, -0x30(%rbp)
testl %esi, %esi
jle 0x13223
movq %rdi, %r12
xorl %r15d, %r15d
movq %r13, %rbx
movq (%r12,%r15,8), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x12995
incq %r15
addq $0x30, %rbx
cmpq %r15, %r14
jne 0x13209
movq %r13, -0x40(%rbp)
xorl %ebx, %ebx
xorl %r14d, %r14d
movl -0x30(%rbp), %ecx
movq %rbx, -0x60(%rbp)
movq %rsp, -0x58(%rbp)
movl %ecx, %r13d
movq %rsp, %r12
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movq %r13, %rax
movq -0x40(%rbp), %rdx
movq %r12, %rsi
movq %r15, -0x70(%rbp)
movq %rbx, -0x68(%rbp)
testl %ecx, %ecx
jle 0x132b7
movq %rsi, -0x38(%rbp)
movq %rdx, -0x30(%rbp)
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x12be6
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x48(%rbp), %rax
addq $0x8, %rbx
addq $0x8, %r15
addq $0x8, %rsi
addq $0x30, %rdx
decq %rax
jne 0x13277
testl %r13d, %r13d
jle 0x132dc
movq $-0x1, %rax
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
cmpq %rax, %rdx
cmovbq %rdx, %rax
cmovbl %ecx, %r14d
incq %rcx
cmpq %rcx, %r13
jne 0x132c5
movslq %r14d, %rbx
leaq (%r12,%rbx,8), %rax
movq %rax, -0x38(%rbp)
movq (%rax), %rsi
movq -0x70(%rbp), %rax
leaq (%rax,%rbx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rax
leaq (%rax,%rbx,8), %rax
movq %rax, -0x48(%rbp)
movq (%rax), %rcx
movq -0x50(%rbp), %rdi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0x10351
leaq (%rbx,%rbx,2), %r15
shlq $0x4, %r15
movq -0x40(%rbp), %rax
leaq (%rax,%r15), %rbx
movq %rbx, %rdi
callq 0x12c5d
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x12be6
movq -0x40(%rbp), %rcx
movq (%rcx,%r15), %rax
movq 0x10(%rcx,%r15), %rcx
movq 0x8(%rax), %rax
cmpq 0x18(%rax), %rcx
jb 0x132b7
movq %rbx, %rdi
leal -0x1(%r13), %ecx
cmpl %ecx, %r14d
movq -0x60(%rbp), %rbx
jge 0x13392
movl %ecx, %r15d
leaq 0x30(%rdi), %rsi
movl %r14d, %eax
notl %eax
addl %r13d, %eax
cltq
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
callq 0x2390
movl %r15d, %ecx
cmpl $0x1, %ecx
cmovel %ecx, %ebx
movq -0x58(%rbp), %rsp
testl %ebx, %ebx
je 0x1322f
movq -0x40(%rbp), %r13
movq (%r13), %rax
movq 0x10(%r13), %rcx
movq 0x8(%rax), %rax
cmpq 0x18(%rax), %rcx
movq -0x50(%rbp), %r14
jae 0x1341a
leaq -0x80(%rbp), %r15
leaq -0x78(%rbp), %r12
movq (%r13), %rbx
movq 0x10(%r13), %rsi
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x126cd
movq 0x8(%r13), %rsi
movq 0x8(%rbx), %rax
movb 0x38(%rax), %cl
shlq %cl, %rsi
orq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rcx
incq 0x60(%rax)
addq %rcx, 0x58(%rax)
movq %r14, %rdi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0x10351
movq %r13, %rdi
callq 0x12c5d
testl %eax, %eax
je 0x133c6
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq