// File: 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
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rax
subq 0x10(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x20(%rbp), %rax
addq 0x10(%rbp), %rax
movq %rax, -0x270(%rbp)
cmpq $0x0, -0x268(%rbp)
jbe 0x29217
movq -0x8(%rbp), %rdi
movq -0x270(%rbp), %rsi
movq -0x268(%rbp), %rdx
leaq -0x250(%rbp), %rcx
callq 0x29270
movq $0x0, -0x258(%rbp)
movq -0x258(%rbp), %rax
movq -0x268(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x28d10
movq -0x8(%rbp), %rdi
movq -0x258(%rbp), %rax
movq -0x248(%rbp,%rax,8), %rsi
addq $0x1, %rsi
movq -0x258(%rbp), %rax
movq -0x250(%rbp,%rax,8), %rdx
subq $0x1, %rdx
movq -0x258(%rbp), %rcx
addq $0x1, %rcx
callq 0x292e0
movq -0x258(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
jmp 0x28cab
cmpq $0x0, -0x268(%rbp)
jbe 0x28d48
movq -0x8(%rbp), %rdi
movq -0x270(%rbp), %rsi
movq -0x268(%rbp), %rax
subq $0x1, %rax
movq -0x250(%rbp,%rax,8), %rdx
subq $0x1, %rdx
movq -0x268(%rbp), %rcx
callq 0x292e0
movq $0x0, -0x258(%rbp)
movq -0x258(%rbp), %rax
movq -0x268(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x28db8
movq -0x8(%rbp), %rdi
movq -0x258(%rbp), %rax
movq -0x248(%rbp,%rax,8), %rsi
addq $0x1, %rsi
movq -0x258(%rbp), %rax
movq -0x250(%rbp,%rax,8), %rdx
subq $0x1, %rdx
movq -0x258(%rbp), %rcx
addq $0x1, %rcx
callq 0x28510
movq -0x258(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
jmp 0x28d53
cmpq $0x0, -0x268(%rbp)
jbe 0x28df0
movq -0x8(%rbp), %rdi
movq -0x270(%rbp), %rsi
movq -0x268(%rbp), %rax
subq $0x1, %rax
movq -0x250(%rbp,%rax,8), %rdx
subq $0x1, %rdx
movq -0x268(%rbp), %rcx
callq 0x28510
movq 0x10(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x28e88
movq -0x20(%rbp), %rcx
addq -0x258(%rbp), %rcx
andq $0x3f, %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
xorq $-0x1, %rax
movq %rax, -0x278(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq -0x258(%rbp), %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x278(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq -0x258(%rbp), %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
andq 0x9(%rax,%rcx,8), %rdx
movq %rdx, 0x9(%rax,%rcx,8)
movq -0x258(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
jmp 0x28dfb
movl -0xc(%rbp), %eax
movl %eax, -0x27c(%rbp)
testl %eax, %eax
je 0x28eb8
jmp 0x28e97
movl -0x27c(%rbp), %eax
subl $0x1, %eax
je 0x28f1e
jmp 0x28ea4
movl -0x27c(%rbp), %eax
subl $0x2, %eax
je 0x28fe9
jmp 0x29050
movq -0x20(%rbp), %rcx
addq -0x30(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x288(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq -0x30(%rbp), %rsi
subq $0x1, %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x288(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq -0x30(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
orq 0x9(%rax,%rcx,8), %rdx
movq %rdx, 0x9(%rax,%rcx,8)
jmp 0x29070
movq -0x20(%rbp), %rcx
addq 0x10(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
xorq $-0x1, %rax
movq %rax, -0x298(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq 0x10(%rbp), %rsi
subq $0x1, %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x298(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq 0x10(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
andq 0x9(%rax,%rcx,8), %rdx
movq %rdx, 0x9(%rax,%rcx,8)
movq -0x20(%rbp), %rcx
addq -0x30(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x290(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq -0x30(%rbp), %rsi
subq $0x1, %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x290(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq -0x30(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
orq 0x9(%rax,%rcx,8), %rdx
movq %rdx, 0x9(%rax,%rcx,8)
jmp 0x29070
movq -0x20(%rbp), %rcx
addq -0x30(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
xorq $-0x1, %rax
movq %rax, -0x2a0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq -0x30(%rbp), %rsi
subq $0x1, %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x2a0(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq -0x30(%rbp), %rcx
subq $0x1, %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
andq 0x9(%rax,%rcx,8), %rdx
movq %rdx, 0x9(%rax,%rcx,8)
jmp 0x29070
movq 0x10f81(%rip), %rax     # 0x39fd8
movq (%rax), %rdi
movl -0xc(%rbp), %edx
leaq 0x1aca(%rip), %rsi      # 0x2ab2e
movb $0x0, %al
callq 0x33d0
callq 0x3110
cmpq $0x0, -0x268(%rbp)
jbe 0x29215
movq -0x18(%rbp), %rax
shrq $0x6, %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movq -0x268(%rbp), %rcx
subq $0x1, %rcx
movq -0x250(%rbp,%rcx,8), %rcx
shrq $0x6, %rcx
cmpq %rcx, %rax
ja 0x29115
movq -0x8(%rbp), %rdi
movq -0x258(%rbp), %rsi
callq 0x27590
movzbl (%rax), %eax
cmpq $0xff, %rax
jae 0x290fc
movq -0x268(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movq -0x8(%rbp), %rdi
movq -0x258(%rbp), %rsi
callq 0x27590
movq -0x2a8(%rbp), %rdx
movzbl (%rax), %ecx
addq %rdx, %rcx
movb %cl, (%rax)
jmp 0x290fe
movq -0x258(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
jmp 0x29091
movq $0x0, -0x260(%rbp)
movq -0x260(%rbp), %rax
movq -0x268(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x29213
movq -0x268(%rbp), %rax
subq -0x260(%rbp), %rax
subq $0x1, %rax
movq -0x250(%rbp,%rax,8), %rax
shrq $0x6, %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movq -0x268(%rbp), %rcx
subq -0x260(%rbp), %rcx
subq $0x2, %rcx
movq -0x250(%rbp,%rcx,8), %rcx
shrq $0x6, %rcx
cmpq %rcx, %rax
ja 0x291fa
movq -0x8(%rbp), %rdi
movq -0x258(%rbp), %rsi
callq 0x27590
movzbl (%rax), %eax
cmpq $0xff, %rax
jae 0x291e1
movq -0x268(%rbp), %rax
subq -0x260(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x2b0(%rbp)
movq -0x8(%rbp), %rdi
movq -0x258(%rbp), %rsi
callq 0x27590
movq -0x2b0(%rbp), %rdx
movzbl (%rax), %ecx
addq %rdx, %rcx
movb %cl, (%rax)
jmp 0x291e3
movq -0x258(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
jmp 0x29164
jmp 0x291fc
movq -0x260(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x260(%rbp)
jmp 0x29120
jmp 0x29215
jmp 0x29217
movq $0x0, -0x258(%rbp)
movq -0x258(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x29266
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq -0x258(%rbp), %rsi
movq -0x28(%rbp), %rax
movq -0x258(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x280a0
movq -0x258(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x258(%rbp)
jmp 0x29222
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
nop