// File: /workspace/github_cpp_makefile_star2_O3/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 $0x268, %rsp            # imm = 0x268
movq %r9, %r13
movq %r8, 0x48(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdi, %r12
movq 0x2a0(%rsp), %rbx
movabsq $0x1ffffffffffffff8, %r15 # imm = 0x1FFFFFFFFFFFFFF8
subq %rbx, %r9
je 0x137e8
movq %rdx, %rbp
movl %esi, 0x34(%rsp)
movq 0x10(%rsp), %rax
leaq (%rbx,%rax), %r8
testq %r9, %r9
movq %r9, 0x18(%rsp)
movq %r8, 0x8(%rsp)
je 0x13516
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 %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 0x1348f
subl %ecx, %eax
popcntq %r10, %r10
addl %eax, %r10d
jmp 0x134b3
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 0x1344f
movq %r8, 0x48(%rsp,%r9,8)
decq %r9
incq %r8
testq %r9, %r9
jne 0x1344f
movq 0x18(%rsp), %rcx
movq %rcx, %rax
decq %rax
jne 0x1351d
movq 0x48(%rsp,%rcx,8), %rax
movq %rax, 0x20(%rsp)
leaq -0x1(%rax), %rdx
movq %r12, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
movq %rdx, 0x38(%rsp)
callq 0x13894
movq %r14, %rsi
movq 0x18(%rsp), %rcx
movb $0x1, %r14b
movq $0x0, 0x28(%rsp)
jmp 0x135c8
movq $-0x1, %rax
movq %rax, 0x28(%rsp)
movq %r13, 0x40(%rsp)
movl $0x1, %r14d
movq 0x50(%rsp), %rdx
movq 0x18(%rsp), %r15
movq 0x50(%rsp,%r14,8), %r13
leaq 0x1(%r13), %rsi
decq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x13894
incq %r14
movq %r13, %rdx
cmpq %r14, %r15
jne 0x13537
movq 0x28(%rsp), %rax
movq 0x50(%rsp,%rax,8), %rax
movq %rax, 0x20(%rsp)
leaq -0x1(%rax), %rdx
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %rdx, 0x38(%rsp)
movq %r15, %rcx
callq 0x13894
movl $0x1, %r14d
movq 0x50(%rsp), %rdx
movq 0x50(%rsp,%r14,8), %r13
leaq 0x1(%r13), %rsi
decq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x1321f
incq %r14
movq %r13, %rdx
cmpq %r14, %r15
jne 0x1358c
xorl %r14d, %r14d
movq 0x40(%rsp), %r13
movq %r15, %rcx
movabsq $0x1ffffffffffffff8, %r15 # imm = 0x1FFFFFFFFFFFFFF8
movq 0x8(%rsp), %rsi
movq %r12, %rdi
movq 0x38(%rsp), %rdx
callq 0x1321f
leaq -0x1(%r13), %rax
cmpq %rbx, %rax
jbe 0x13624
movq 0x8(%r12), %rdx
movq 0x10(%r12), %rsi
notq %rbx
addq %r13, %rbx
movq 0x8(%rsp), %rcx
movq $-0x2, %rdi
rolq %cl, %rdi
movq %rcx, %r8
shrq $0x6, %r8
movq 0x38(%rdx), %r9
shlq $0x3, %r9
andq %r15, %r9
addq $0x11, %r9
imulq %r8, %r9
andq %rdi, 0x9(%rsi,%r9)
incq %rcx
decq %rbx
jne 0x135f3
movl 0x34(%rsp), %ecx
cmpl $0x2, %ecx
je 0x136a5
cmpl $0x1, %ecx
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r11
jne 0x136ed
movq 0x8(%rsp), %rcx
decq %rcx
movq $-0x2, %rdx
rolq %cl, %rdx
shrq $0x6, %rcx
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdi
movq 0x38(%rsi), %r8
shlq $0x3, %r8
andq %r15, %r8
addq $0x11, %r8
imulq %rcx, %r8
andq %rdx, 0x9(%rdi,%r8)
addq 0x10(%rsp), %rax
movl $0x1, %edx
movl %eax, %ecx
shlq %cl, %rdx
shrq $0x6, %rax
movq 0x38(%rsi), %rcx
shlq $0x3, %rcx
andq %r15, %rcx
addq $0x11, %rcx
imulq %rax, %rcx
orq %rdx, 0x9(%rdi,%rcx)
jmp 0x136e6
addq 0x10(%rsp), %rax
movq $-0x2, %rdx
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 %r15, %rcx
addq $0x11, %rcx
imulq %rax, %rcx
andq %rdx, 0x9(%rsi,%rcx)
movq 0x18(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0x20(%rsp), %rdi
jmp 0x13727
movq 0x20(%rsp), %rdi
addq 0x10(%rsp), %rax
movl $0x1, %edx
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 %r15, %rcx
addq $0x11, %rcx
imulq %rax, %rcx
orq %rdx, 0x9(%rsi,%rcx)
shrq $0x6, %rbp
shrq $0x6, %rdi
cmpq %rdi, %rbp
jae 0x13775
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 0x13765
addb %r10b, %dl
movb %dl, (%rax,%rcx)
leaq (%rdi,%rbp), %rax
incq %rax
incq %rbp
cmpq $0x1, %rax
jne 0x1373a
testb %r14b, %r14b
jne 0x137e8
xorl %eax, %eax
movq %r10, %rcx
subq %rax, %rcx
movq 0x40(%rsp,%rcx,8), %rdx
movq 0x48(%rsp,%rcx,8), %rsi
shrq $0x6, %rsi
shrq $0x6, %rdx
cmpq %rdx, %rsi
jae 0x137e0
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 0x137d0
addb %cl, %r9b
movb %r9b, (%rdi,%r8)
leaq (%rdx,%rsi), %rdi
incq %rdi
incq %rsi
cmpq $0x1, %rdi
jne 0x137a2
incq %rax
cmpq %r11, %rax
jne 0x1377c
testq %r13, %r13
movq 0x48(%rsp), %r14
movq 0x10(%rsp), %rbp
je 0x13882
movq 0x8(%r12), %rax
movq 0x10(%r12), %rdx
addq $0x11, %rdx
xorl %esi, %esi
movq $-0x1, %rdi
leaq (%rsi,%rbp), %r11
movq (%r14,%rsi,8), %r8
movq 0x38(%rax), %rcx
movq %r11, %r10
shrq $0x6, %r10
leaq (,%rcx,8), %r9
andq %r15, %r9
addq $0x11, %r9
imulq %r10, %r9
addq %rdx, %r9
movl %r11d, %r10d
andl $0x3f, %r10d
movq $-0x1, %rbx
shlq %cl, %rbx
imulq %rcx, %r10
shrq $0x3, %r10
cmpq $0x40, %rcx
notq %rbx
cmoveq %rdi, %rbx
imull %r11d, %ecx
andb $0x7, %cl
shlq %cl, %rbx
shlq %cl, %r8
notq %rbx
andq (%r10,%r9), %rbx
orq %r8, %rbx
movq %rbx, (%r10,%r9)
incq %rsi
cmpq %rsi, %r13
jne 0x13812
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq