// File: splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: insert(quotient_filter*, unsigned __int128, unsigned long, bool, bool)
static inline bool insert(QF *qf, __uint128_t hash, uint64_t count, bool lock,
													bool spin)
{
	uint64_t hash_remainder           = hash & BITMASK(qf->metadata->bits_per_slot);
	uint64_t hash_bucket_index        = hash >> qf->metadata->bits_per_slot;
	uint64_t hash_bucket_block_offset = hash_bucket_index % SLOTS_PER_BLOCK;
	uint64_t runend_index             = run_end(qf, hash_bucket_index);
	uint64_t hash_bucket_lock_offset  = hash_bucket_index % NUM_SLOTS_TO_LOCK;

	if (lock) {
		/* take the lock for two lock-blocks; the lock-block in which the
		 * hash_bucket_index falls and the next lock-block */

#ifdef LOG_WAIT_TIME
		if (!qf_spin_lock(qf, &qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK],
											hash_bucket_index/NUM_SLOTS_TO_LOCK, spin))
			return false;
		if (hash_bucket_lock_offset > (NUM_SLOTS_TO_LOCK - CLUSTER_SIZE)) {
			if (!qf_spin_lock(qf, &qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1],
												hash_bucket_index/NUM_SLOTS_TO_LOCK+1, spin)) {
				qf_spin_unlock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
				return false;
			}
		}
#else
		if (!qf_spin_lock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK], spin))
			return false;
		if (hash_bucket_lock_offset > (NUM_SLOTS_TO_LOCK - CLUSTER_SIZE)) {
			if (!qf_spin_lock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1], spin)) {
				qf_spin_unlock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
				return false;
			}
		}
#endif
	}
	/* Empty slot */
	if (might_be_empty(qf, hash_bucket_index) && runend_index ==
			hash_bucket_index) {
		METADATA_WORD(qf, runends, hash_bucket_index) |= 1ULL <<
			(hash_bucket_block_offset % 64);
		set_slot(qf, hash_bucket_index, hash_remainder);
		METADATA_WORD(qf, occupieds, hash_bucket_index) |= 1ULL <<
			(hash_bucket_block_offset % 64);
		
		/*modify_metadata(qf, &qf->metadata->ndistinct_elts, 1);*/
		/*modify_metadata(qf, &qf->metadata->noccupied_slots, 1);*/
		/*modify_metadata(qf, &qf->metadata->nelts, 1);*/
		/* This trick will, I hope, keep the fast case fast. */
		if (count > 1) {
			qf_spin_unlock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
			if (hash_bucket_lock_offset > (NUM_SLOTS_TO_LOCK - CLUSTER_SIZE))
				qf_spin_unlock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1]);
			insert(qf, hash, count - 1, lock, spin);
		}
	} else { /* Non-empty slot */
		uint64_t new_values[67];
		int64_t runstart_index = hash_bucket_index == 0 ? 0 : run_end(qf,
																																	hash_bucket_index
																																	- 1) + 1;

		if (!is_occupied(qf, hash_bucket_index)) { /* Empty bucket, but its slot is occupied. */
			uint64_t *p = encode_counter(qf, hash_remainder, count, &new_values[67]);
			insert_replace_slots_and_shift_remainders_and_runends_and_offsets(qf, 
																																				0, 
																																				hash_bucket_index, 
																																				runstart_index, 
																																				p, 
																																				&new_values[67] - p, 
																																				0);
			/*modify_metadata(qf, &qf->metadata->ndistinct_elts, 1);*/
		} else { /* Non-empty bucket */

			uint64_t current_remainder, current_count, current_end;

			/* Find the counter for this remainder, if one exists. */
			current_end = decode_counter(qf, runstart_index, &current_remainder,
																	 &current_count);
			while (current_remainder < hash_remainder && !is_runend(qf, current_end)) {
				runstart_index = current_end + 1;
				current_end = decode_counter(qf, runstart_index, &current_remainder,
																		 &current_count);	
			}

			/* If we reached the end of the run w/o finding a counter for this remainder,
				 then append a counter for this remainder to the run. */
			if (current_remainder < hash_remainder) {
				uint64_t *p = encode_counter(qf, hash_remainder, count, &new_values[67]);
				insert_replace_slots_and_shift_remainders_and_runends_and_offsets(qf, 
																																					1, /* Append to bucket */
																																					hash_bucket_index, 
																																					current_end + 1, 
																																					p, 
																																					&new_values[67] - p, 
																																					0);
				/*modify_metadata(qf, &qf->metadata->ndistinct_elts, 1);*/
				/* Found a counter for this remainder.  Add in the new count. */
			} else if (current_remainder == hash_remainder) {
				uint64_t *p = encode_counter(qf, hash_remainder, current_count + count, &new_values[67]);
				insert_replace_slots_and_shift_remainders_and_runends_and_offsets(qf, 
																																					is_runend(qf, current_end) ? 1 : 2, 
																																					hash_bucket_index, 
																																					runstart_index, 
																																					p, 
																																					&new_values[67] - p, 
																																					current_end - runstart_index + 1);
				/* No counter for this remainder, but there are larger
					 remainders, so we're not appending to the bucket. */
			} else {
				uint64_t *p = encode_counter(qf, hash_remainder, count, &new_values[67]);
				insert_replace_slots_and_shift_remainders_and_runends_and_offsets(qf, 
																																					2, /* Insert to bucket */
																																					hash_bucket_index, 
																																					runstart_index, 
																																					p, 
																																					&new_values[67] - p, 
																																					0);	
				/*modify_metadata(qf, &qf->metadata->ndistinct_elts, 1);*/
			}
		}
		METADATA_WORD(qf, occupieds, hash_bucket_index) |= 1ULL << (hash_bucket_block_offset % 64);
		
		/*modify_metadata(qf, &qf->metadata->nelts, count);*/
	}

	if (lock) {
		qf_spin_unlock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK]);
		if (hash_bucket_lock_offset > (NUM_SLOTS_TO_LOCK - CLUSTER_SIZE))
			qf_spin_unlock(&qf->mem->locks[hash_bucket_index/NUM_SLOTS_TO_LOCK+1]);
	}

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x360, %rsp            # imm = 0x360
movl %r8d, -0x314(%rbp)
movq %rcx, %rax
movl -0x314(%rbp), %ecx
movq %rax, -0x310(%rbp)
movq %rdx, %rax
movq -0x310(%rbp), %rdx
movq %rax, -0x308(%rbp)
movq %rsi, %rax
movq -0x308(%rbp), %rsi
movq %rax, -0x300(%rbp)
movq %rdi, %r8
movq -0x300(%rbp), %rdi
movb %r9b, %al
movq %rdi, -0x20(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdi
movq %r8, -0x28(%rbp)
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
andb $0x1, %cl
movb %cl, -0x49(%rbp)
andb $0x1, %al
movb %al, -0x4a(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x2f8(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
cmpq $0x40, 0x38(%rax)
jne 0x2615a
movq $-0x1, %rax
movq %rax, -0x320(%rbp)
jmp 0x26179
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rax), %rcx
movl $0x1, %eax
shlq %cl, %rax
subq $0x1, %rax
movq %rax, -0x320(%rbp)
movq -0x2f8(%rbp), %rax
movq -0x320(%rbp), %rcx
andq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movb 0x38(%rcx), %dl
movb %dl, %cl
movq %rsi, %rdi
shrq %cl, %rdi
movq %rdi, -0x328(%rbp)
movb %dl, %cl
shrdq %cl, %rsi, %rax
movq -0x328(%rbp), %rcx
testb $0x40, %dl
cmovneq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
andq $0x3f, %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x26ea0
movq %rax, -0x70(%rbp)
movq -0x60(%rbp), %rax
andq $0xfff, %rax            # imm = 0xFFF
movq %rax, -0x78(%rbp)
testb $0x1, -0x49(%rbp)
je 0x26296
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rax
shrq $0xc, %rax
shlq $0x2, %rax
addq %rax, %rdi
movb -0x4a(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x27ff0
testb $0x1, %al
jne 0x26231
movb $0x0, -0x1(%rbp)
jmp 0x2679f
cmpq $0xf00, -0x78(%rbp)     # imm = 0xF00
jbe 0x26294
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rax
shrq $0xc, %rax
addq $0x1, %rax
shlq $0x2, %rax
addq %rax, %rdi
movb -0x4a(%rbp), %al
andb $0x1, %al
movzbl %al, %esi
callq 0x27ff0
testb $0x1, %al
jne 0x26292
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rax
shrq $0xc, %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x28050
movb $0x0, -0x1(%rbp)
jmp 0x2679f
jmp 0x26294
jmp 0x26296
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x289a0
cmpl $0x0, %eax
je 0x263fa
movq -0x70(%rbp), %rax
cmpq -0x60(%rbp), %rax
jne 0x263fa
movq -0x68(%rbp), %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x338(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x338(%rbp), %rdx
movq -0x60(%rbp), %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
orq 0x9(%rax,%rcx,8), %rdx
movq %rdx, 0x9(%rax,%rcx,8)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x280a0
movq -0x68(%rbp), %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x330(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x330(%rbp), %rdx
movq -0x60(%rbp), %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
orq 0x1(%rax,%rcx,8), %rdx
movq %rdx, 0x1(%rax,%rcx,8)
cmpq $0x1, -0x48(%rbp)
jbe 0x263f5
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rax
shrq $0xc, %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x28050
cmpq $0xf00, -0x78(%rbp)     # imm = 0xF00
jbe 0x263ac
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rax
shrq $0xc, %rax
addq $0x1, %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x28050
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rcx
decq %rcx
movb -0x49(%rbp), %r8b
movb -0x4a(%rbp), %al
movq %rsi, -0x88(%rbp)
movq %rdx, -0x90(%rbp)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
andb $0x1, %r8b
andb $0x1, %al
movzbl %r8b, %r8d
movzbl %al, %r9d
callq 0x260b0
jmp 0x26747
cmpq $0x0, -0x60(%rbp)
jne 0x2640c
xorl %eax, %eax
movq %rax, -0x340(%rbp)
jmp 0x26428
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
subq $0x1, %rsi
callq 0x26ea0
addq $0x1, %rax
movq %rax, -0x340(%rbp)
movq -0x340(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x26e50
cmpl $0x0, %eax
jne 0x264b6
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x2b0(%rbp), %rcx
addq $0x218, %rcx            # imm = 0x218
callq 0x289f0
movq %rax, -0x2c0(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rdx
movq -0x2b8(%rbp), %rcx
movq -0x2c0(%rbp), %r8
leaq -0x2b0(%rbp), %r9
addq $0x218, %r9             # imm = 0x218
movq -0x2c0(%rbp), %rax
subq %rax, %r9
sarq $0x3, %r9
xorl %esi, %esi
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x28c30
jmp 0x26702
movq -0x28(%rbp), %rdi
movq -0x2b8(%rbp), %rsi
leaq -0x2c8(%rbp), %rdx
leaq -0x2d0(%rbp), %rcx
callq 0x27050
movq %rax, -0x2d8(%rbp)
movq -0x2c8(%rbp), %rcx
xorl %eax, %eax
cmpq -0x58(%rbp), %rcx
movb %al, -0x341(%rbp)
jae 0x2650e
movq -0x28(%rbp), %rdi
movq -0x2d8(%rbp), %rsi
callq 0x27380
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x341(%rbp)
movb -0x341(%rbp), %al
testb $0x1, %al
jne 0x2651a
jmp 0x26553
movq -0x2d8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x2b8(%rbp)
movq -0x28(%rbp), %rdi
movq -0x2b8(%rbp), %rsi
leaq -0x2c8(%rbp), %rdx
leaq -0x2d0(%rbp), %rcx
callq 0x27050
movq %rax, -0x2d8(%rbp)
jmp 0x264db
movq -0x2c8(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x265d5
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x2b0(%rbp), %rcx
addq $0x218, %rcx            # imm = 0x218
callq 0x289f0
movq %rax, -0x2e0(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rdx
movq -0x2d8(%rbp), %rcx
addq $0x1, %rcx
movq -0x2e0(%rbp), %r8
leaq -0x2b0(%rbp), %r9
addq $0x218, %r9             # imm = 0x218
movq -0x2e0(%rbp), %rax
subq %rax, %r9
sarq $0x3, %r9
movl $0x1, %esi
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x28c30
jmp 0x26700
movq -0x2c8(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x26692
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x2d0(%rbp), %rdx
addq -0x48(%rbp), %rdx
leaq -0x2b0(%rbp), %rcx
addq $0x218, %rcx            # imm = 0x218
callq 0x289f0
movq %rax, -0x2e8(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x350(%rbp)
movq -0x28(%rbp), %rdi
movq -0x2d8(%rbp), %rsi
callq 0x27380
movq -0x350(%rbp), %rdi
movl %eax, %ecx
movl $0x2, %esi
movl $0x1, %eax
cmpl $0x0, %ecx
cmovnel %eax, %esi
movq -0x60(%rbp), %rdx
movq -0x2b8(%rbp), %rcx
movq -0x2e8(%rbp), %r8
leaq -0x2b0(%rbp), %r9
addq $0x218, %r9             # imm = 0x218
movq -0x2e8(%rbp), %rax
subq %rax, %r9
sarq $0x3, %r9
movq -0x2d8(%rbp), %rax
subq -0x2b8(%rbp), %rax
addq $0x1, %rax
movq %rax, (%rsp)
callq 0x28c30
jmp 0x266fe
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x2b0(%rbp), %rcx
addq $0x218, %rcx            # imm = 0x218
callq 0x289f0
movq %rax, -0x2f0(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rdx
movq -0x2b8(%rbp), %rcx
movq -0x2f0(%rbp), %r8
leaq -0x2b0(%rbp), %r9
addq $0x218, %r9             # imm = 0x218
movq -0x2f0(%rbp), %rax
subq %rax, %r9
sarq $0x3, %r9
movl $0x2, %esi
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x28c30
jmp 0x26700
jmp 0x26702
movq -0x68(%rbp), %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x358(%rbp)
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
shrq $0x6, %rsi
callq 0x27590
movq -0x358(%rbp), %rdx
movq -0x60(%rbp), %rcx
andq $0x3f, %rcx
shrq $0x6, %rcx
orq 0x1(%rax,%rcx,8), %rdx
movq %rdx, 0x1(%rax,%rcx,8)
testb $0x1, -0x49(%rbp)
je 0x2679b
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rax
shrq $0xc, %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x28050
cmpq $0xf00, -0x78(%rbp)     # imm = 0xF00
jbe 0x26799
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rax
shrq $0xc, %rax
addq $0x1, %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x28050
jmp 0x2679b
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x360, %rsp            # imm = 0x360
popq %rbp
retq
nopl (%rax)