// File: /workspace/github_cpp_makefile_star2_O2/splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: insert_replace_slots_and_shift_remainders_and_runends_and_offsets(quotient_filter*, int, unsigned long, unsigned long, unsigned long const*, unsigned long, unsigned long)
static inline void insert_replace_slots_and_shift_remainders_and_runends_and_offsets(QF		*qf, 
																																										 int		 operation, 
																																										 uint64_t		 bucket_index,
																																										 uint64_t		 overwrite_index,
																																										 const uint64_t	*remainders, 
																																										 uint64_t		 total_remainders,
																																										 uint64_t		 noverwrites)
{
	uint64_t empties[67];
	uint64_t i, j;
	uint64_t ninserts = total_remainders - noverwrites;
	uint64_t insert_index = overwrite_index + noverwrites;

	if (ninserts > 0) {
		/* First, shift things to create n empty spaces where we need them. */
		find_next_n_empty_slots(qf, insert_index, ninserts, empties);

		for (i = 0; i < ninserts - 1; i++)
			shift_slots(qf, empties[i+1] + 1, empties[i] - 1, i + 1);
		if (ninserts > 0)
			shift_slots(qf, insert_index, empties[ninserts - 1] - 1, ninserts);

		for (i = 0; i < ninserts - 1; i++)
			shift_runends(qf, empties[i+1] + 1, empties[i] - 1, i + 1);
		if (ninserts > 0)
			shift_runends(qf, insert_index, empties[ninserts - 1] - 1, ninserts);


		for (i = noverwrites; i < total_remainders - 1; i++)
			METADATA_WORD(qf, runends, overwrite_index + i) &= ~(1ULL <<
																													 (((overwrite_index
																															+ i) %
																														 SLOTS_PER_BLOCK)
																														% 64));

		switch (operation) {
			case 0: /* insert into empty bucket */
				assert (noverwrites == 0);
				METADATA_WORD(qf, runends, overwrite_index + total_remainders - 1) |=
					1ULL << (((overwrite_index + total_remainders - 1) %
										SLOTS_PER_BLOCK) % 64);
				break;
			case 1: /* append to bucket */
				METADATA_WORD(qf, runends, overwrite_index + noverwrites - 1)      &=
					~(1ULL << (((overwrite_index + noverwrites - 1) % SLOTS_PER_BLOCK) %
										 64));
				METADATA_WORD(qf, runends, overwrite_index + total_remainders - 1) |=
					1ULL << (((overwrite_index + total_remainders - 1) %
										SLOTS_PER_BLOCK) % 64);
				break;
			case 2: /* insert into bucket */
				METADATA_WORD(qf, runends, overwrite_index + total_remainders - 1) &=
					~(1ULL << (((overwrite_index + total_remainders - 1) %
											SLOTS_PER_BLOCK) % 64));
				break;
			default:
				fprintf(stderr, "Invalid operation %d\n", operation);
				abort();
		}

		if (ninserts > 0) {
			for (i = bucket_index / SLOTS_PER_BLOCK + 1; i <= empties[ninserts -
					 1]/SLOTS_PER_BLOCK; i++) {
				if (get_block(qf, i)->offset < BITMASK(8*sizeof(qf->blocks[0].offset)))
					get_block(qf, i)->offset += ninserts;
			}
			for (j = 0; j < ninserts -1 ; j++)
				for (i = empties[ninserts - j - 1] / SLOTS_PER_BLOCK + 1; i <=
						 empties[ninserts - j - 2]/SLOTS_PER_BLOCK; i++) {
					if (get_block(qf, i)->offset < BITMASK(8*sizeof(qf->blocks[0].offset)))
						get_block(qf, i)->offset += ninserts - j - 1;
				}
		}
	}
	for (i = 0; i < total_remainders; i++)
		set_slot(qf, overwrite_index + i, remainders[i]);

	/*modify_metadata(qf, &qf->metadata->noccupied_slots, ninserts);*/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %r9, %r13
movq %r8, 0x38(%rsp)
movq %rcx, %r15
movq %rdi, %r12
movq 0x290(%rsp), %rbx
movq %r9, 0x20(%rsp)
subq %rbx, %r13
jne 0xfd7e
xorl %ebx, %ebx
movq 0x20(%rsp), %r14
movq 0x38(%rsp), %r13
cmpq %rbx, %r14
je 0xfd6c
leaq (%r15,%rbx), %rsi
movq (%r13,%rbx,8), %rdx
movq %r12, %rdi
callq 0xf826
incq %rbx
jmp 0xfd51
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rbp
movl %esi, 0x1c(%rsp)
leaq (%rbx,%r15), %rax
movq %r13, %r14
movq %rax, 0x10(%rsp)
subq $0x1, %r14
jb 0xfdac
movq %r12, %rdi
movq %rax, %rsi
callq 0xf8f1
movq %rax, 0x40(%rsp,%r14,8)
incq %rax
jmp 0xfd91
leaq -0x1(%r13), %rax
movq %rax, 0x30(%rsp)
pushq $0x1
popq %r14
cmpq %r14, %r13
je 0xfdde
movq 0x38(%rsp,%r14,8), %rdx
movq 0x40(%rsp,%r14,8), %rsi
incq %rsi
decq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x1000b
incq %r14
jmp 0xfdb9
movq 0x38(%rsp,%r13,8), %rax
movq %rax, 0x8(%rsp)
leaq -0x1(%rax), %rdx
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %rdx, 0x28(%rsp)
movq %r13, %rcx
callq 0x1000b
pushq $0x1
popq %r14
cmpq %r14, %r13
je 0xfe2a
movq 0x38(%rsp,%r14,8), %rdx
movq 0x40(%rsp,%r14,8), %rsi
incq %rsi
decq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0xfa1b
incq %r14
jmp 0xfe05
movabsq $0x1ffffffffffffff8, %r14 # imm = 0x1FFFFFFFFFFFFFF8
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r13, %rcx
callq 0xfa1b
movq 0x20(%rsp), %rax
decq %rax
pushq $-0x2
popq %rdx
movq 0x30(%rsp), %r10
cmpq %rax, %rbx
jae 0xfe93
leaq (%r15,%rbx), %rcx
movq %rdx, %rsi
rolq %cl, %rsi
shrq $0x6, %rcx
movq 0x8(%r12), %rdi
movq 0x10(%r12), %r8
movq 0x38(%rdi), %rdi
shlq $0x3, %rdi
andq %r14, %rdi
addq $0x11, %rdi
imulq %rcx, %rdi
andq %rsi, 0x9(%r8,%rdi)
incq %rbx
jmp 0xfe59
movl 0x1c(%rsp), %ecx
cmpl $0x2, %ecx
je 0xfeff
cmpl $0x1, %ecx
jne 0xff34
movq 0x10(%rsp), %rcx
decq %rcx
rolq %cl, %rdx
shrq $0x6, %rcx
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdi
movq 0x38(%rsi), %r8
shlq $0x3, %r8
andq %r14, %r8
addq $0x11, %r8
imulq %rcx, %r8
andq %rdx, 0x9(%rdi,%r8)
addq %r15, %rax
pushq $0x1
popq %rdx
movl %eax, %ecx
shlq %cl, %rdx
shrq $0x6, %rax
movq 0x38(%rsi), %rcx
shlq $0x3, %rcx
andq %r14, %rcx
addq $0x11, %rcx
imulq %rax, %rcx
orq %rdx, 0x9(%rdi,%rcx)
jmp 0xff2d
addq %r15, %rax
movl %eax, %ecx
rolq %cl, %rdx
shrq $0x6, %rax
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rsi
movq 0x38(%rcx), %rcx
shlq $0x3, %rcx
andq %r14, %rcx
addq $0x11, %rcx
imulq %rax, %rcx
andq %rdx, 0x9(%rsi,%rcx)
movq 0x8(%rsp), %rdi
jmp 0xff6a
movq 0x8(%rsp), %rdi
addq %r15, %rax
pushq $0x1
popq %rdx
movl %eax, %ecx
shlq %cl, %rdx
shrq $0x6, %rax
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rsi
movq 0x38(%rcx), %rcx
shlq $0x3, %rcx
andq %r14, %rcx
addq $0x11, %rcx
imulq %rax, %rcx
orq %rdx, 0x9(%rsi,%rcx)
shrq $0x6, %rbp
shrq $0x6, %rdi
cmpq %rdi, %rbp
jae 0xffa7
incq %rbp
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rax
movq 0x38(%rcx), %rcx
shlq $0x3, %rcx
andq %r14, %rcx
addq $0x11, %rcx
imulq %rbp, %rcx
movb (%rax,%rcx), %dl
cmpb $-0x1, %dl
je 0xff72
addb %r13b, %dl
movb %dl, (%rax,%rcx)
jmp 0xff72
xorl %eax, %eax
cmpq %r10, %rax
je 0xfd45
movq %r13, %rsi
subq %rax, %rsi
leaq -0x1(%rsi), %rcx
movq 0x30(%rsp,%rsi,8), %rdx
movq 0x38(%rsp,%rsi,8), %rsi
shrq $0x6, %rsi
shrq $0x6, %rdx
cmpq %rdx, %rsi
jae 0x10006
incq %rsi
movq 0x8(%r12), %r8
movq 0x10(%r12), %rdi
movq 0x38(%r8), %r8
shlq $0x3, %r8
andq %r14, %r8
addq $0x11, %r8
imulq %rsi, %r8
movb (%rdi,%r8), %r9b
cmpb $-0x1, %r9b
je 0xffce
addb %cl, %r9b
movb %r9b, (%rdi,%r8)
jmp 0xffce
incq %rax
jmp 0xffa9