// File: /workspace/github_cpp_makefile_star2_O3/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, %r13d
movl %esi, %r14d
movq %rsp, %rcx
movq %r14, %rax
shlq $0x4, %rax
leaq (%rax,%rax,2), %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
testl %esi, %esi
jle 0x12f5d
movq %rdi, %r12
xorl %r15d, %r15d
movq -0x30(%rbp), %rbx
movq (%r12,%r15,8), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x12766
incq %r15
addq $0x30, %rbx
cmpq %r15, %r14
jne 0x12f43
xorl %r14d, %r14d
movq %rsp, -0x68(%rbp)
movl %r13d, %r15d
movq %rsp, %r12
leaq 0xf(,%r15,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
testl %r13d, %r13d
jle 0x12fed
movq %r15, %rax
movq -0x30(%rbp), %rcx
movq %r12, %rdx
movq -0x60(%rbp), %r13
movq -0x58(%rbp), %rbx
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x129d7
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %rax
addq $0x8, %rbx
addq $0x8, %r13
addq $0x8, %rdx
addq $0x30, %rcx
decq %rax
jne 0x12fad
testl %r15d, %r15d
jle 0x13012
movq $-0x1, %rax
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
cmpq %rax, %rdx
cmovbq %rdx, %rax
cmovbl %ecx, %r14d
incq %rcx
cmpq %rcx, %r15
jne 0x12ffb
movslq %r14d, %rbx
leaq (%r12,%rbx,8), %rax
movq %rax, -0x38(%rbp)
movq (%rax), %rsi
movq -0x60(%rbp), %rax
leaq (%rax,%rbx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x58(%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 0x10199
leaq (%rbx,%rbx,2), %r13
shlq $0x4, %r13
movq -0x30(%rbp), %rax
leaq (%rax,%r13), %rbx
movq %rbx, %rdi
callq 0x12a69
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x129d7
movq -0x30(%rbp), %rcx
movq (%rcx,%r13), %rax
movq 0x10(%rcx,%r13), %rcx
movq 0x8(%rax), %rax
cmpq 0x18(%rax), %rcx
jb 0x12fed
leal -0x1(%r15), %r13d
cmpl %r13d, %r14d
jge 0x130be
leaq 0x30(%rbx), %rsi
movl %r14d, %eax
notl %eax
addl %r15d, %eax
cltq
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
movq %rbx, %rdi
callq 0x2380
movq -0x68(%rbp), %rsp
cmpl $0x1, %r13d
jne 0x12f60
movq -0x30(%rbp), %r13
movq (%r13), %rax
movq 0x10(%r13), %rcx
movq 0x8(%rax), %rax
cmpq 0x18(%rax), %rcx
movq -0x50(%rbp), %r14
jae 0x1312b
leaq -0x78(%rbp), %rbx
leaq -0x80(%rbp), %r15
leaq -0x70(%rbp), %r12
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x129d7
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rcx
movq %r14, %rdi
movl $0x1, %r8d
movl $0x1, %r9d
callq 0x10199
movq %r13, %rdi
callq 0x12a69
testl %eax, %eax
je 0x130f2
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq