// File: 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
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl -0xc(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
movq %rax, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x80(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq $-0x1, -0x40(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x27be0
movq -0x80(%rbp), %rsi
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x1c(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x273d0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x27ba9
jmp 0x27be2
cmpl $0x0, -0x34(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x27bf4
jmp 0x27e41
movl -0xc(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x48(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x98(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x88(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x27cc8
movq -0x88(%rbp), %rcx
movq -0x90(%rbp), %rdx
movq -0x98(%rbp), %rsi
movq -0x80(%rbp), %rdi
movslq -0x1c(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rcx
callq 0x275d0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x27c6b
jmp 0x27cca
movq $-0x1, -0x40(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x27d1c
movq -0x98(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x40(%rbp), %rax
jae 0x27d0f
movq -0x98(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x27d11
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x27cd9
movq -0x88(%rbp), %rax
movq -0x90(%rbp), %rcx
movq -0x98(%rbp), %rdx
movq -0x18(%rbp), %rdi
movslq -0x38(%rbp), %rsi
movq (%rdx,%rsi,8), %rsi
movslq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rdx
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x25380
movq -0x80(%rbp), %rdi
movslq -0x38(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
callq 0x27680
movq -0x80(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x88(%rbp), %rcx
movslq -0x38(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movslq -0x38(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movslq -0x38(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movslq -0x38(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rcx
callq 0x275d0
movq -0x80(%rbp), %rdi
movslq -0x38(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
callq 0x27890
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x27cca
movl -0x38(%rbp), %eax
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x27e1f
movq -0x80(%rbp), %rsi
movslq -0x38(%rbp), %rax
imulq $0x30, %rax, %rax
movq %rsi, %rdi
addq %rax, %rdi
movl -0x38(%rbp), %eax
addl $0x1, %eax
cltq
imulq $0x30, %rax, %rax
addq %rax, %rsi
movl -0xc(%rbp), %eax
subl -0x38(%rbp), %eax
subl $0x1, %eax
cltq
imulq $0x30, %rax, %rdx
callq 0x3450
movl -0xc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xc(%rbp)
cmpl $0x1, -0xc(%rbp)
jne 0x27e35
movl $0x1, -0x34(%rbp)
movq -0x48(%rbp), %rax
movq %rax, %rsp
jmp 0x27be2
movq -0x80(%rbp), %rdi
callq 0x27890
cmpl $0x0, %eax
jne 0x27e9b
jmp 0x27e51
movq -0x80(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x70(%rbp), %rdx
leaq -0x78(%rbp), %rcx
callq 0x275d0
movq -0x18(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x78(%rbp), %rcx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x25380
movq -0x80(%rbp), %rdi
callq 0x27680
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x27e51
jmp 0x27e9b
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)