// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movl %r8d, %r12d
movl %ecx, %r14d
movq %rdx, 0x28(%rsp)
movq %rsi, %rbp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x38(%rax), %rcx
pushq $-0x1
popq %rax
movq %rax, %r13
shlq %cl, %r13
cmpq $0x40, %rcx
notq %r13
cmoveq %rax, %r13
movzbl %cl, %edx
movq %rsi, %rdi
xorl %esi, %esi
callq 0x21a0
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0xec56
movq %rax, 0x10(%rsp)
movl %r15d, %eax
andl $0xfff, %eax            # imm = 0xFFF
movq %rax, 0x30(%rsp)
movzbl %r12b, %eax
movl %eax, 0x18(%rsp)
movl %r14d, 0x24(%rsp)
testl %r14d, %r14d
je 0xe260
movq %rbp, 0x38(%rsp)
movq (%rbx), %rax
movq %r15, %rbp
movq %r15, %r12
shrq $0xc, %r12
leaq (,%r12,4), %r14
movq 0x8(%rax), %rdi
addq %r14, %rdi
movl 0x18(%rsp), %esi
callq 0xf805
testb %al, %al
je 0xe381
cmpl $0xf01, 0x30(%rsp)      # imm = 0xF01
movq %rbp, %r15
movq 0x38(%rsp), %rbp
jb 0xe260
movq (%rbx), %rax
movq 0x8(%rax), %rax
leaq (%rax,%r12,4), %rdi
addq $0x4, %rdi
movl 0x18(%rsp), %esi
callq 0xf805
testb %al, %al
je 0xe57d
andq %rbp, %r13
movl %r15d, %r12d
andl $0x3f, %r12d
movq %rbx, %rdi
movq %r15, %rsi
callq 0xec1e
testl %eax, %eax
je 0xe299
testq %r15, %r15
je 0xe388
leaq -0x1(%r15), %rsi
movq %rbx, %rdi
callq 0xec56
movq %rax, %rbp
incq %rbp
jmp 0xe38a
movq %rbx, %rdi
movq %r15, %rsi
callq 0xef61
cmpq %r15, 0x10(%rsp)
jne 0xe279
testl %eax, %eax
jne 0xe279
pushq $0x1
popq %r14
movl %r12d, %ecx
shlq %cl, %r14
movq %r15, 0x10(%rsp)
movq 0x10(%rsp), %r15
shrq $0x6, %r15
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movabsq $0x3ffffffffffffff, %r12 # imm = 0x3FFFFFFFFFFFFFF
movq 0x38(%rax), %rax
andq %r12, %rax
leaq 0x11(,%rax,8), %rax
imulq %r15, %rax
orq %r14, 0x9(%rcx,%rax)
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r13, %rdx
callq 0xf826
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
andq 0x38(%rax), %r12
leaq 0x11(,%r12,8), %rax
imulq %r15, %rax
movq 0x10(%rsp), %r15
orq %r14, 0x1(%rcx,%rax)
movq 0x28(%rsp), %rdx
cmpq $0x2, %rdx
movl 0x24(%rsp), %r14d
jb 0xe546
movq (%rbx), %rax
movq 0x8(%rax), %rcx
movq %r15, %rax
shrq $0xc, %rax
movl $0x0, (%rcx,%rax,4)
cmpl $0xf01, 0x30(%rsp)      # imm = 0xF01
jb 0xe365
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
movl $0x0, 0x4(%rcx,%rax,4)
decq %rdx
movzbl %r14b, %ecx
movq %rbx, %rdi
movq %rbp, %rsi
movl 0x18(%rsp), %r8d
callq 0xe17f
jmp 0xe546
xorl %eax, %eax
jmp 0xe58d
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0xec1e
testl %eax, %eax
je 0xe3e3
movq %r12, 0x18(%rsp)
movq %r15, 0x10(%rsp)
leaq 0x48(%rsp), %r12
leaq 0x40(%rsp), %r14
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xed85
movq %rax, %r15
cmpq %r13, 0x48(%rsp)
jae 0xe425
movq %rbx, %rdi
movq %r15, %rsi
callq 0xef61
testl %eax, %eax
jne 0xe485
movq %r15, %rbp
incq %rbp
jmp 0xe3ad
leaq 0x268(%rsp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movq %r14, %rcx
callq 0xfc36
subq %rax, %r14
sarq $0x3, %r14
andq $0x0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbp, %rcx
movq %rax, %r8
movq %r14, %r9
callq 0xfd14
jmp 0xe50a
jne 0xe4c2
movq 0x28(%rsp), %rdx
addq 0x40(%rsp), %rdx
leaq 0x268(%rsp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0xfc36
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0xef61
xorl %esi, %esi
cmpl $0x1, %eax
adcl $0x1, %esi
subq %r12, %r14
sarq $0x3, %r14
subq %rbp, %r15
incq %r15
movq %r15, (%rsp)
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rdx
movq %rbp, %rcx
movq %r12, %r8
jmp 0xe4fd
leaq 0x268(%rsp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movq %r14, %rcx
callq 0xfc36
incq %r15
subq %rax, %r14
sarq $0x3, %r14
andq $0x0, (%rsp)
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
movq %r15, %rcx
movq %rdx, %r15
jmp 0xe4fa
leaq 0x268(%rsp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %rdx
movq %r14, %rcx
callq 0xfc36
subq %rax, %r14
sarq $0x3, %r14
andq $0x0, (%rsp)
pushq $0x2
popq %rsi
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rdx
movq %rbp, %rcx
movq %rax, %r8
movq %r14, %r9
callq 0xfd14
movq 0x18(%rsp), %r12
movl 0x24(%rsp), %r14d
pushq $0x1
popq %rax
movl %r12d, %ecx
shlq %cl, %rax
movq %r15, %rcx
shrq $0x6, %rcx
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rsi
movabsq $0x3ffffffffffffff, %rdi # imm = 0x3FFFFFFFFFFFFFF
andq 0x38(%rdx), %rdi
leaq 0x11(,%rdi,8), %rdx
imulq %rcx, %rdx
orq %rax, 0x1(%rsi,%rdx)
movb $0x1, %al
testb %r14b, %r14b
je 0xe58d
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
shrq $0xc, %r15
movl $0x0, (%rcx,%r15,4)
cmpl $0xf01, 0x30(%rsp)      # imm = 0xF01
jb 0xe58d
movq (%rbx), %rax
movq 0x8(%rax), %rax
leaq (%rax,%r15,4), %r14
addq $0x4, %r14
movb $0x1, %al
jmp 0xe586
movq (%rbx), %rax
addq 0x8(%rax), %r14
xorl %eax, %eax
movl $0x0, (%r14)
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq