// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %r8, 0x38(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdi, %r12
movq 0x290(%rsp), %rcx
movabsq $0x1ffffffffffffff8, %r15 # imm = 0x1FFFFFFFFFFFFFF8
movq %r9, %r13
subq %rcx, %r13
je 0x13b16
movq %rdx, %rbp
movl %esi, 0x24(%rsp)
movq 0x10(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x8(%rsp)
testq %r13, %r13
je 0x13862
movq 0x8(%r12), %rax
movq 0x10(%r12), %rdx
movq 0x38(%rax), %rsi
shlq $0x3, %rsi
andq %r15, %rsi
addq $0x11, %rsi
movq $-0x1, %rdi
movq 0x8(%rsp), %r8
movq %r13, %r9
movq %r8, %r11
shrq $0x6, %r11
imulq %rsi, %r11
movl %r8d, %ecx
andl $0x3f, %ecx
movl $0x2, %r10d
shlq %cl, %r10
decq %r10
cmpq $0x3f, %rcx
cmoveq %rdi, %r10
movzbl (%rdx,%r11), %eax
andq 0x1(%rdx,%r11), %r10
cmpq %rcx, %rax
jbe 0x13824
subl %ecx, %eax
popcntq %r10, %r10
addl %eax, %r10d
jmp 0x13848
movq $-0x1, %r14
shlq %cl, %r14
notq %r14
andq 0x9(%rdx,%r11), %r14
movl %eax, %ecx
shrq %cl, %r14
popcntq %r10, %r10
popcntq %r14, %r14
subl %r14d, %r10d
movslq %r10d, %rax
addq %rax, %r8
testl %eax, %eax
jne 0x137e4
movq %r8, 0x38(%rsp,%r9,8)
decq %r9
incq %r8
testq %r9, %r9
jne 0x137e4
movq %r13, %rax
decq %rax
movq %rax, 0x28(%rsp)
je 0x13898
movl $0x1, %r14d
movq 0x38(%rsp,%r14,8), %rdx
movq 0x40(%rsp,%r14,8), %rsi
incq %rsi
decq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x13bc3
incq %r14
cmpq %r14, %r13
jne 0x13875
movq 0x38(%rsp,%r13,8), %rax
movq %rax, 0x18(%rsp)
leaq -0x1(%rax), %rdx
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %rdx, 0x30(%rsp)
movq %r13, %rcx
callq 0x13bc3
cmpq $0x0, 0x28(%rsp)
je 0x138ec
movl $0x1, %r14d
movq 0x38(%rsp,%r14,8), %rdx
movq 0x40(%rsp,%r14,8), %rsi
incq %rsi
decq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x1359a
incq %r14
cmpq %r14, %r13
jne 0x138c9
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r13, %rcx
callq 0x1359a
leaq -0x1(%rbx), %rax
cmpq 0x290(%rsp), %rax
movq 0x28(%rsp), %r10
jbe 0x13957
movq 0x8(%r12), %rax
movq 0x10(%r12), %rdx
movq 0x8(%rsp), %rcx
movq %r10, %rsi
movq $-0x2, %rdi
rolq %cl, %rdi
movq %rcx, %r8
shrq $0x6, %r8
movq 0x38(%rax), %r9
shlq $0x3, %r9
andq %r15, %r9
addq $0x11, %r9
imulq %r8, %r9
andq %rdi, 0x9(%rdx,%r9)
incq %rcx
decq %rsi
jne 0x13926
movl 0x24(%rsp), %eax
cmpl $0x2, %eax
je 0x139d3
cmpl $0x1, %eax
jne 0x13a16
movq 0x8(%rsp), %rcx
decq %rcx
movq $-0x2, %rax
rolq %cl, %rax
shrq $0x6, %rcx
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rsi
movq 0x38(%rdx), %rdi
shlq $0x3, %rdi
andq %r15, %rdi
addq $0x11, %rdi
imulq %rcx, %rdi
andq %rax, 0x9(%rsi,%rdi)
movq 0x10(%rsp), %rax
leaq (%rbx,%rax), %rcx
decq %rcx
movl $0x1, %eax
shlq %cl, %rax
shrq $0x6, %rcx
movq 0x38(%rdx), %rdx
shlq $0x3, %rdx
andq %r15, %rdx
addq $0x11, %rdx
imulq %rcx, %rdx
orq %rax, 0x9(%rsi,%rdx)
jmp 0x13a0f
movq 0x10(%rsp), %rax
leaq (%rbx,%rax), %rcx
decq %rcx
movq $-0x2, %rax
rolq %cl, %rax
shrq $0x6, %rcx
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rsi
movq 0x38(%rdx), %rdx
shlq $0x3, %rdx
andq %r15, %rdx
addq $0x11, %rdx
imulq %rcx, %rdx
andq %rax, 0x9(%rsi,%rdx)
movq 0x18(%rsp), %rdi
jmp 0x13a55
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rax
leaq (%rbx,%rax), %rcx
decq %rcx
movl $0x1, %eax
shlq %cl, %rax
shrq $0x6, %rcx
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rsi
movq 0x38(%rdx), %rdx
shlq $0x3, %rdx
andq %r15, %rdx
addq $0x11, %rdx
imulq %rcx, %rdx
orq %rax, 0x9(%rsi,%rdx)
shrq $0x6, %rbp
shrq $0x6, %rdi
cmpq %rdi, %rbp
jae 0x13aa3
negq %rdi
incq %rbp
movq 0x8(%r12), %rcx
movq 0x10(%r12), %rax
movq 0x38(%rcx), %rcx
shlq $0x3, %rcx
andq %r15, %rcx
addq $0x11, %rcx
imulq %rbp, %rcx
movb (%rax,%rcx), %dl
cmpb $-0x1, %dl
je 0x13a93
addb %r13b, %dl
movb %dl, (%rax,%rcx)
leaq (%rdi,%rbp), %rax
incq %rax
incq %rbp
cmpq $0x1, %rax
jne 0x13a68
testq %r10, %r10
je 0x13b16
xorl %eax, %eax
movq %r13, %rcx
subq %rax, %rcx
movq 0x30(%rsp,%rcx,8), %rdx
movq 0x38(%rsp,%rcx,8), %rsi
shrq $0x6, %rsi
shrq $0x6, %rdx
cmpq %rdx, %rsi
jae 0x13b0e
decq %rcx
negq %rdx
incq %rsi
movq 0x8(%r12), %r8
movq 0x10(%r12), %rdi
movq 0x38(%r8), %r8
shlq $0x3, %r8
andq %r15, %r8
addq $0x11, %r8
imulq %rsi, %r8
movb (%rdi,%r8), %r9b
cmpb $-0x1, %r9b
je 0x13afe
addb %cl, %r9b
movb %r9b, (%rdi,%r8)
leaq (%rdx,%rsi), %rdi
incq %rdi
incq %rsi
cmpq $0x1, %rdi
jne 0x13ad0
incq %rax
cmpq %r10, %rax
jne 0x13aaa
testq %rbx, %rbx
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %rbp
je 0x13bb1
movq 0x8(%r12), %rax
movq 0x10(%r12), %rdx
addq $0x11, %rdx
xorl %esi, %esi
movq $-0x1, %rdi
leaq (%rsi,%rbp), %r11
movq (%r13,%rsi,8), %r8
movq %r11, %r10
shrq $0x6, %r10
movq 0x38(%rax), %rcx
leaq (,%rcx,8), %r9
andq %r15, %r9
addq $0x11, %r9
imulq %r10, %r9
addq %rdx, %r9
movl %r11d, %r10d
andl $0x3f, %r10d
movq $-0x1, %r14
shlq %cl, %r14
imulq %rcx, %r10
shrq $0x3, %r10
cmpq $0x40, %rcx
notq %r14
cmoveq %rdi, %r14
imull %r11d, %ecx
andb $0x7, %cl
shlq %cl, %r14
shlq %cl, %r8
notq %r14
andq (%r10,%r9), %r14
orq %r8, %r14
movq %r14, (%r10,%r9)
incq %rsi
cmpq %rsi, %rbx
jne 0x13b40
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq