// File: /workspace/github_cpp_makefile_star2_O1/splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: qfi_next
int qfi_next(QFi *qfi)
{
	if (qfi_end(qfi))
		return 1;
	else {
		/* move to the end of the current counter*/
		uint64_t current_remainder, current_count;
		qfi->current = decode_counter(qfi->qf, qfi->current, &current_remainder,
																	&current_count);
		
		if (!is_runend(qfi->qf, qfi->current)) {
			qfi->current++;
#ifdef LOG_CLUSTER_LENGTH
			qfi->cur_length++;
#endif
			if (qfi->current > qfi->qf->metadata->nslots)
				return 1;
			return 0;
		}
		else {
#ifdef LOG_CLUSTER_LENGTH
			/* save to check if the new current is the new cluster. */
			uint64_t old_current = qfi->current;
#endif
			uint64_t block_index = qfi->run / SLOTS_PER_BLOCK;
			uint64_t rank = bitrank(get_block(qfi->qf, block_index)->occupieds[0],
															qfi->run % SLOTS_PER_BLOCK);
			uint64_t next_run = bitselect(get_block(qfi->qf,
																							block_index)->occupieds[0],
																		rank);
			if (next_run == 64) {
				rank = 0;
				while (next_run == 64 && block_index < qfi->qf->metadata->nblocks) {
					block_index++;
					next_run = bitselect(get_block(qfi->qf, block_index)->occupieds[0],
															 rank);
				}
			}
			if (block_index == qfi->qf->metadata->nblocks) {
				/* set the index values to max. */
				qfi->run = qfi->current = qfi->qf->metadata->xnslots;
				return 1;
			}
			qfi->run = block_index * SLOTS_PER_BLOCK + next_run;
			qfi->current++;
			if (qfi->current < qfi->run)
				qfi->current = qfi->run;
#ifdef LOG_CLUSTER_LENGTH
			if (qfi->current > old_current + 1) { /* new cluster. */
				if (qfi->cur_length > 10) {
					qfi->c_info[qfi->num_clusters].start_index = qfi->cur_start_index;
					qfi->c_info[qfi->num_clusters].length = qfi->cur_length;
					qfi->num_clusters++;
				}
				qfi->cur_start_index = qfi->run;
				qfi->cur_length = 1;
			} else {
				qfi->cur_length++;
			}
#endif
			return 0;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq (%rdi), %r14
movq 0x10(%rdi), %rsi
movq 0x8(%r14), %r15
movq 0x18(%r15), %r12
movl $0x1, %ecx
cmpq %r12, %rsi
jae 0x12f02
movq %rdi, %rbx
leaq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r14, %rdi
callq 0x126cd
movq %rax, 0x10(%rbx)
movq %rax, %rcx
shrq $0x6, %rcx
movq 0x10(%r14), %r13
movabsq $0x3ffffffffffffff, %rdx # imm = 0x3FFFFFFFFFFFFFF
andq 0x38(%r15), %rdx
leaq 0x11(,%rdx,8), %rsi
imulq %rsi, %rcx
movq 0x9(%r13,%rcx), %rcx
btq %rax, %rcx
jae 0x12eba
movabsq $-0x7f7f7f7f7f7f7f80, %r8 # imm = 0x8080808080808080
movabsq $0x3333333333333333, %r10 # imm = 0x3333333333333333
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
shrq $0x6, %rdx
movq %rdx, %rdi
imulq %rsi, %rdi
movl $0x2, %ebp
shlq %cl, %rbp
movq 0x1(%r13,%rdi), %rdi
decq %rbp
andq %rdi, %rbp
popcntq %rbp, %rbp
movq %rdi, %rcx
popcntq %rcx, %rcx
movl $0x40, %r9d
cmpl %ebp, %ecx
jle 0x12dd3
movq %rdi, %rcx
shrq %rcx
movabsq $0x5555555555555555, %r9 # imm = 0x5555555555555555
andq %r9, %rcx
movq %rdi, %r11
subq %rcx, %r11
movq %r11, %rcx
andq %r10, %rcx
shrq $0x2, %r11
andq %r10, %r11
addq %rcx, %r11
movq %r11, %r9
shrq $0x4, %r9
addq %r11, %r9
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %r9
movq %r9, %rcx
movabsq $-0x101010101010101, %r11 # imm = 0xFEFEFEFEFEFEFEFF
imulq %r11, %rcx
movslq %ebp, %rbp
movabsq $0x101010101010100, %r14 # imm = 0x101010101010100
leaq 0x1(%r14), %r11
imulq %rbp, %r11
orq %r8, %r11
addq %rcx, %r11
andq %r8, %r11
popcntq %r11, %r11
shll $0x3, %r11d
movslq %r11d, %rcx
imulq %r14, %r9
shrq %cl, %r9
movl $0xff, %r11d
shrq %cl, %rdi
andq %r11, %r9
subq %r9, %rbp
andq %r11, %rdi
shlq $0x8, %rbp
orq %rdi, %rbp
leaq 0x1d75(%rip), %rdi      # 0x14b40
movzbl (%rdi,%rbp), %r9d
addq %rcx, %r9
cmpq $0x40, %r9
jne 0x12ed2
movq 0x50(%r15), %rbp
leaq 0x1(%rdx), %rcx
imulq %rsi, %rcx
addq %rcx, %r13
incq %r13
leaq 0x1d4a(%rip), %rdi      # 0x14b40
cmpq %rbp, %rdx
jae 0x12ecc
movq (%r13), %r9
movq %r9, %rcx
popcntq %rcx, %rcx
testl %ecx, %ecx
jle 0x12ea2
movq %r9, %rcx
shrq %rcx
movabsq $0x5555555555555555, %r11 # imm = 0x5555555555555555
andq %r11, %rcx
movq %r9, %r11
subq %rcx, %r11
movq %r11, %rcx
andq %r10, %rcx
shrq $0x2, %r11
andq %r10, %r11
addq %rcx, %r11
movq %r11, %r14
shrq $0x4, %r14
addq %r11, %r14
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %r14
movq %r14, %rcx
movabsq $-0x101010101010101, %r11 # imm = 0xFEFEFEFEFEFEFEFF
imulq %r11, %rcx
addq %r8, %rcx
andq %r8, %rcx
popcntq %rcx, %rcx
shll $0x3, %ecx
movslq %ecx, %rcx
movabsq $0x101010101010100, %r11 # imm = 0x101010101010100
imulq %r11, %r14
shrq %cl, %r14
shrq %cl, %r9
movzbl %r9b, %r9d
shll $0x8, %r14d
movzwl %r14w, %r11d
subq %r11, %r9
movzbl (%r9,%rdi), %r9d
addq %rcx, %r9
jmp 0x12ea8
movl $0x40, %r9d
addq %rsi, %r13
incq %rdx
cmpq $0x40, %r9
je 0x12df6
jmp 0x12ed2
incq %rax
movq %rax, 0x10(%rbx)
xorl %ecx, %ecx
cmpq 0x10(%r15), %rax
seta %cl
jmp 0x12f02
movl $0x40, %r9d
cmpq 0x50(%r15), %rdx
jne 0x12ee7
movq %r12, 0x10(%rbx)
movq %r12, 0x8(%rbx)
movl $0x1, %ecx
jmp 0x12f02
shlq $0x6, %rdx
addq %r9, %rdx
incq %rax
cmpq %rdx, %rax
cmovbeq %rdx, %rax
movq %rdx, 0x8(%rbx)
movq %rax, 0x10(%rbx)
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq