// File: /workspace/github_cpp_makefile_star2_O3/splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: run_end(quotient_filter const*, unsigned long)
static inline uint64_t run_end(const QF *qf, uint64_t hash_bucket_index)
{
	uint64_t bucket_block_index       = hash_bucket_index / SLOTS_PER_BLOCK;
	uint64_t bucket_intrablock_offset = hash_bucket_index % SLOTS_PER_BLOCK;
	uint64_t bucket_blocks_offset = block_offset(qf, bucket_block_index);

	// uint64_t bucket_intrablock_rank   = bitrankv(get_block(qf, bucket_block_index)->occupieds, METADATA_WORDS_PER_BLOCK, bucket_intrablock_offset);
	uint64_t bucket_intrablock_rank   = bitrank(get_block(qf,
																												bucket_block_index)->occupieds[0],
																							bucket_intrablock_offset);

	if (bucket_intrablock_rank == 0) {
		if (bucket_blocks_offset <= bucket_intrablock_offset)
			return hash_bucket_index;
		else
			return SLOTS_PER_BLOCK * bucket_block_index + bucket_blocks_offset - 1;
	}

	/* FIXME: Should handle offset overflow  --- Ah screw it, just use 16-bit offset.  That'll never overflow.  */
	uint64_t runend_block_index  = bucket_block_index + bucket_blocks_offset /
		SLOTS_PER_BLOCK;
	uint64_t runend_ignore_bits  = bucket_blocks_offset % SLOTS_PER_BLOCK;
	uint64_t runend_rank         = bucket_intrablock_rank - 1;
	// uint64_t runend_block_offset = bitselectv(get_block(qf, runend_block_index)->runends, METADATA_WORDS_PER_BLOCK, runend_ignore_bits, runend_rank);
	uint64_t runend_block_offset = bitselectv(get_block(qf,
																											runend_block_index)->runends[0],
																						runend_ignore_bits, runend_rank);
	if (runend_block_offset == SLOTS_PER_BLOCK) {
		if (bucket_blocks_offset == 0 && bucket_intrablock_rank == 0) {
			/* The block begins in empty space, and this bucket is in that region of empty space */
			return hash_bucket_index;
		} else {
			do {
				// runend_rank        -= popcntv(get_block(qf, runend_block_index)->runends, METADATA_WORDS_PER_BLOCK, runend_ignore_bits);
				runend_rank        -= popcntv(get_block(qf,
																								runend_block_index)->runends[0],
																			runend_ignore_bits);
				runend_block_index++;
				runend_ignore_bits  = 0;
				// runend_block_offset = bitselectv(get_block(qf, runend_block_index)->runends, METADATA_WORDS_PER_BLOCK, runend_ignore_bits, runend_rank);
				runend_block_offset = bitselectv(get_block(qf,
																									 runend_block_index)->runends[0],
																				 runend_ignore_bits, runend_rank);
			} while (runend_block_offset == SLOTS_PER_BLOCK);
		}
	}

	uint64_t runend_index = SLOTS_PER_BLOCK * runend_block_index + runend_block_offset;
	if (runend_index < hash_bucket_index)
		return hash_bucket_index;
	else
		return runend_index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %rbp
shrq $0x6, %rbp
movl %edx, %ecx
andl $0x3f, %ecx
movabsq $0x3ffffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFFF
andq %rdi, %rax
leaq 0x11(,%rax,8), %r8
movq %r8, %r9
imulq %rbp, %r9
movzbl (%rsi,%r9), %edi
movl $0x2, %eax
shlq %cl, %rax
decq %rax
andq 0x1(%rsi,%r9), %rax
popcntq %rax, %rax
testl %eax, %eax
je 0x12464
movq %rdx, -0x8(%rsp)
movabsq $-0x7f7f7f7f7f7f7f80, %r10 # imm = 0x8080808080808080
movabsq $0x5555555555555555, %r14 # imm = 0x5555555555555555
movabsq $0x3333333333333333, %rbx # imm = 0x3333333333333333
movabsq $0xf0f0f0f0f0f0f0f, %r15 # imm = 0xF0F0F0F0F0F0F0F
movabsq $0x101010101010100, %rdx # imm = 0x101010101010100
movslq %eax, %r12
movl %edi, %r9d
shrl $0x6, %r9d
leaq (%r9,%rbp), %rax
movq %rax, %r13
imulq %r8, %r13
movq 0x9(%rsi,%r13), %r11
movl %edi, %ecx
shrq %cl, %r11
shlq %cl, %r11
decq %r12
movq %r11, %rcx
popcntq %rcx, %rcx
cmpl %r12d, %ecx
jle 0x12339
movq %r11, %rcx
shrq %rcx
andq %r14, %rcx
movq %r11, %rdx
subq %rcx, %rdx
movq %rdx, %rcx
andq %rbx, %rcx
shrq $0x2, %rdx
andq %rbx, %rdx
addq %rcx, %rdx
movq %rdx, %r14
shrq $0x4, %r14
addq %rdx, %r14
andq %r15, %r14
movq %r14, %rcx
movabsq $-0x101010101010101, %rdx # imm = 0xFEFEFEFEFEFEFEFF
imulq %rdx, %rcx
movslq %r12d, %r15
movabsq $0x101010101010100, %rdx # imm = 0x101010101010100
incq %rdx
imulq %r15, %rdx
orq %r10, %rdx
addq %rcx, %rdx
andq %r10, %rdx
popcntq %rdx, %rdx
shll $0x3, %edx
movslq %edx, %rcx
movabsq $0x101010101010100, %rdx # imm = 0x101010101010100
imulq %rdx, %r14
shrq %cl, %r14
movl $0xff, %edx
andq %rdx, %r14
shrq %cl, %r11
subq %r14, %r15
movabsq $0x5555555555555555, %r14 # imm = 0x5555555555555555
andq %rdx, %r11
shlq $0x8, %r15
orq %r11, %r15
leaq 0x2833(%rip), %rdx      # 0x14b50
movzbl (%rdx,%r15), %r15d
movabsq $0x101010101010100, %rdx # imm = 0x101010101010100
addq %rcx, %r15
cmpq $0x40, %r15
jne 0x1244f
andl $0x3f, %edi
addq $0x9, %rsi
addq %r9, %rbp
incq %rbp
imulq %r8, %rbp
movq %rdx, %r10
leaq 0x1(%rdx), %r9
movq (%rsi,%r13), %rdx
testl %edi, %edi
je 0x12368
movl %edi, %ecx
shrq %cl, %rdx
shlq %cl, %rdx
popcntq %rdx, %rdx
jmp 0x1236d
popcntq %rdx, %rdx
movslq %edx, %rcx
subq %rcx, %r12
movq (%rsi,%rbp), %rdi
movq %rdi, %rcx
popcntq %rcx, %rcx
movl $0x40, %r15d
cmpl %r12d, %ecx
jle 0x1243d
movq %rdi, %rcx
shrq %rcx
andq %r14, %rcx
movq %rdi, %r15
subq %rcx, %r15
movq %r15, %rcx
andq %rbx, %rcx
shrq $0x2, %r15
andq %rbx, %r15
addq %rcx, %r15
movq %r15, %rdx
shrq $0x4, %rdx
addq %r15, %rdx
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rdx
movq %rdx, %rcx
movabsq $-0x101010101010101, %r14 # imm = 0xFEFEFEFEFEFEFEFF
imulq %r14, %rcx
movslq %r12d, %r14
movq %r14, %r15
imulq %r9, %r15
movq %rbx, %r11
movabsq $-0x7f7f7f7f7f7f7f80, %rbx # imm = 0x8080808080808080
orq %rbx, %r15
addq %rcx, %r15
andq %rbx, %r15
movq %r11, %rbx
popcntq %r15, %r15
shll $0x3, %r15d
movslq %r15d, %rcx
imulq %r10, %rdx
shrq %cl, %rdx
movl $0xff, %r11d
andq %r11, %rdx
subq %rdx, %r14
shrq %cl, %rdi
andq %r11, %rdi
shlq $0x8, %r14
orq %rdi, %r14
leaq 0x2725(%rip), %rdx      # 0x14b50
movzbl (%rdx,%r14), %r15d
movabsq $0x5555555555555555, %r14 # imm = 0x5555555555555555
addq %rcx, %r15
addq %r8, %rsi
incq %rax
xorl %edi, %edi
cmpq $0x40, %r15
je 0x12351
shlq $0x6, %rax
addq %r15, %rax
movq -0x8(%rsp), %rcx
cmpq %rcx, %rax
cmovbeq %rcx, %rax
jmp 0x12475
cmpl %edi, %ecx
jae 0x12472
andq $-0x40, %rdx
addq %rdi, %rdx
decq %rdx
movq %rdx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq