// File: /workspace/github_cpp_makefile_star2_O1/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 $0x278, %rsp            # imm = 0x278
movl %r8d, %r12d
movl %ecx, %r14d
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x38(%rax), %rcx
movq $-0x1, %rax
movq $-0x1, %r15
shlq %cl, %r15
cmpq $0x40, %rcx
notq %r15
cmoveq %rax, %r15
movq %rsi, %rbp
xorl %eax, %eax
movq %rsi, %r13
shrdq %cl, %rax, %r13
movq %rdx, 0x20(%rsp)
testb $0x40, %cl
cmovneq %rax, %r13
movq %r13, %rsi
callq 0x12418
movl %r13d, %edi
andl $0xfff, %edi            # imm = 0xFFF
testl %r14d, %r14d
je 0x111da
movq (%rbx), %rcx
movq 0x8(%rcx), %rdx
movq %r13, %rcx
shrq $0xc, %rcx
movl $0x1, %esi
xchgl %esi, (%rdx,%rcx,4)
testb %r12b, %r12b
je 0x11186
testl %esi, %esi
je 0x11191
cmpl $0x0, (%rdx,%rcx,4)
jne 0x11176
movl $0x1, %esi
xchgl %esi, (%rdx,%rcx,4)
jmp 0x11172
testl %esi, %esi
je 0x11191
xorl %eax, %eax
jmp 0x119bd
cmpl $0xf01, %edi            # imm = 0xF01
jb 0x111da
movq (%rbx), %rdx
movq 0x8(%rdx), %rdx
movl $0x1, %esi
xchgl %esi, 0x4(%rdx,%rcx,4)
testb %r12b, %r12b
je 0x111c4
testl %esi, %esi
je 0x111da
cmpl $0x0, 0x4(%rdx,%rcx,4)
jne 0x111b2
movl $0x1, %esi
xchgl %esi, 0x4(%rdx,%rcx,4)
jmp 0x111ae
testl %esi, %esi
je 0x111da
movq (%rbx), %rax
shlq $0x2, %rcx
addq 0x8(%rax), %rcx
xorl %eax, %eax
jmp 0x119b7
movl %r14d, 0x14(%rsp)
movl %r13d, %r14d
andl $0x3f, %r14d
movq %r13, %r8
shrq $0x6, %r8
movq 0x8(%rbx), %r11
movq %rbx, 0x18(%rsp)
movq 0x10(%rbx), %rbx
movabsq $0x3ffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFF
andq 0x38(%r11), %rcx
leaq 0x11(,%rcx,8), %rdx
imulq %r8, %rdx
movq 0x1(%rbx,%rdx), %rsi
movl $0x1, %r9d
movl %r14d, %ecx
shlq %cl, %r9
btq %r14, %rsi
jae 0x1122f
xorl %ecx, %ecx
jmp 0x1123b
movq 0x9(%rbx,%rdx), %rcx
btq %r14, %rcx
setae %cl
andq %rbp, %r15
movabsq $0x1ffffffffffffff8, %r10 # imm = 0x1FFFFFFFFFFFFFF8
cmpq %r13, %rax
jne 0x11339
testb %cl, %cl
je 0x11339
orq %r9, 0x9(%rbx,%rdx)
movq 0x38(%r11), %rcx
leaq (,%rcx,8), %rax
andq %r10, %rax
addq $0x11, %rax
imulq %r8, %rax
addq %rbx, %rax
imulq %rcx, %r14
movq $-0x1, %rdx
shlq %cl, %rdx
shrq $0x3, %r14
movq $-0x1, %rsi
cmpq $0x40, %rcx
notq %rdx
cmoveq %rsi, %rdx
imull %r13d, %ecx
andb $0x7, %cl
shlq %cl, %rdx
shlq %cl, %r15
notq %rdx
andq 0x11(%r14,%rax), %rdx
orq %r15, %rdx
movq %rdx, 0x11(%r14,%rax)
movq 0x38(%r11), %rax
shlq $0x3, %rax
andq %r10, %rax
addq $0x11, %rax
imulq %r8, %rax
orq %r9, 0x1(%rbx,%rax)
cmpq $0x2, 0x20(%rsp)
movq 0x18(%rsp), %rbx
movl 0x14(%rsp), %r14d
jb 0x11984
movq (%rbx), %rax
movq 0x8(%rax), %rcx
movq %r13, %rax
shrq $0xc, %rax
movl $0x0, (%rcx,%rax,4)
movq %rdi, %r15
cmpl $0xf01, %edi            # imm = 0xF01
jb 0x11316
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
movl $0x0, 0x4(%rcx,%rax,4)
movq 0x20(%rsp), %rdx
decq %rdx
movzbl %r14b, %ecx
movzbl %r12b, %r8d
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x110e7
movq %r15, %rdi
jmp 0x11984
movq %r9, 0x38(%rsp)
movq %rdi, 0x48(%rsp)
testq %r13, %r13
je 0x11374
leaq -0x1(%r13), %rsi
movq 0x18(%rsp), %rdi
movq %r8, %r12
movq %r11, %rbp
callq 0x12418
movq %rbp, %r11
movabsq $0x1ffffffffffffff8, %r10 # imm = 0x1FFFFFFFFFFFFFF8
movq %r12, %r8
movq %rax, %r12
incq %r12
jmp 0x11377
xorl %r12d, %r12d
movq 0x38(%r11), %rcx
leaq (,%rcx,8), %rbp
andq %r10, %rbp
addq $0x11, %rbp
movq %rbp, %rax
movq %r8, 0x40(%rsp)
imulq %r8, %rax
movq 0x1(%rbx,%rax), %rax
btq %r14, %rax
jae 0x114ae
movq %rcx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
leaq 0x58(%rsp), %r14
leaq 0x50(%rsp), %rcx
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x126cd
movq %rax, %r10
movq (%r14), %rax
cmpq %r15, %rax
setb %cl
jae 0x1150a
movq %r10, %rdx
shrq $0x6, %rdx
imulq %rbp, %rdx
movq 0x9(%rbx,%rdx), %rdx
btq %r10, %rdx
jb 0x1143b
leaq 0x50(%rsp), %r14
movq %r10, %r12
incq %r12
movq 0x18(%rsp), %rdi
movq %r12, %rsi
leaq 0x58(%rsp), %rdx
movq %r14, %rcx
callq 0x126cd
movq %rax, %r10
movq 0x58(%rsp), %rax
cmpq %r15, %rax
setb %cl
jae 0x1150a
movq %r10, %rdx
shrq $0x6, %rdx
imulq %rbp, %rdx
movq 0x9(%rbx,%rdx), %rdx
btq %r10, %rdx
jae 0x113f6
testb %cl, %cl
movq 0x30(%rsp), %rdi
je 0x1151b
movl $0x1, %esi
movq 0x28(%rsp), %rcx
shlq %cl, %rsi
leaq 0x278(%rsp), %r9
movq %r9, %r8
movq 0x20(%rsp), %r11
testq %r11, %r11
movq 0x18(%rsp), %rbx
movl 0x14(%rsp), %r14d
je 0x116f9
leaq 0x270(%rsp), %r8
movq %r15, (%r8)
cmpq $0x1, %r11
je 0x116f9
cmpq $0x3, %r11
je 0x1168f
cmpq $0x2, %r11
jne 0x116a4
leaq 0x268(%rsp), %r8
jmp 0x1169f
movq $-0x1, %rsi
shlq %cl, %rsi
leaq 0x278(%rsp), %r9
movq %r9, %r8
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
movq 0x18(%rsp), %rbx
movl 0x14(%rsp), %r14d
je 0x117e6
leaq 0x270(%rsp), %r8
movq %r15, (%r8)
cmpq $0x1, %rdx
je 0x117e6
cmpq $0x2, %rdx
jne 0x115b3
leaq 0x268(%rsp), %r8
movq %r15, (%r8)
jmp 0x117e6
movl 0x14(%rsp), %r14d
movq 0x20(%rsp), %rdx
movq 0x30(%rsp), %rdi
jmp 0x11525
movl 0x14(%rsp), %r14d
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq $-0x1, %rsi
shlq %cl, %rsi
leaq 0x278(%rsp), %r9
movq %r9, %r8
cmpq %r15, %rax
jne 0x1157a
addq 0x50(%rsp), %rdx
je 0x118f2
leaq 0x270(%rsp), %r8
movq %r15, (%r8)
cmpq $0x1, %rdx
je 0x118f2
cmpq $0x2, %rdx
jne 0x115d2
leaq 0x268(%rsp), %r8
movq %r15, (%r8)
jmp 0x118f2
testq %rdx, %rdx
movq 0x18(%rsp), %rbx
je 0x11868
leaq 0x270(%rsp), %r8
movq %r15, (%r8)
cmpq $0x1, %rdx
je 0x11868
cmpq $0x2, %rdx
jne 0x115f6
leaq 0x268(%rsp), %r8
movq %r15, (%r8)
jmp 0x11868
cmpq $0x3, %rdx
jne 0x11615
testq %r15, %r15
jne 0x11615
leaq 0x260(%rsp), %r8
xorps %xmm0, %xmm0
movaps %xmm0, (%r8)
jmp 0x117e6
movq %rdx, %rax
xorq $0x3, %rax
orq %r15, %rax
jne 0x11665
leaq 0x260(%rsp), %r8
xorps %xmm0, %xmm0
movaps %xmm0, (%r8)
jmp 0x118f2
cmpq $0x3, %rdx
jne 0x1163d
testq %r15, %r15
jne 0x1163d
leaq 0x260(%rsp), %r8
xorps %xmm0, %xmm0
movaps %xmm0, (%r8)
jmp 0x11868
cmpq $0x3, %rdx
jne 0x1171e
testq %r15, %r15
je 0x1171e
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
jmp 0x11502
cmpq $0x3, %rdx
jne 0x11739
testq %r15, %r15
je 0x11739
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
jmp 0x115ab
cmpq $0x3, %rdx
setne %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x1175b
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
jmp 0x11572
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
movq %r15, (%r8)
jmp 0x116f9
addq $-0x2, %rsi
addq $-0x3, %r11
leaq -0x1(%r15), %rcx
movq %r11, %rax
xorl %edx, %edx
divq %rsi
leaq 0x1(%rdx), %rdi
addq $0x2, %rdx
cmpq %rdi, %rcx
cmovaeq %rdi, %rdx
movq %rdx, -0x8(%r8)
addq $-0x8, %r8
cmpq %r11, %rsi
movq %rax, %r11
jbe 0x116b0
movq $-0x8, %rax
cmpq %rdx, %rcx
jae 0x116f2
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
movq %r15, (%r8,%rax)
addq %rax, %r8
incq %r10
subq %r8, %r9
sarq $0x3, %r9
movq $0x0, (%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movq %r13, %rdx
movq %r10, %rcx
jmp 0x11946
testq %r15, %r15
je 0x1177d
movq $-0x2, %rax
subq %rsi, %rax
movq $-0x3, %rcx
movq %rax, %rsi
jmp 0x11796
testq %r15, %r15
je 0x117ff
movq $-0x2, %rax
subq %rsi, %rax
movq $-0x3, %rcx
movq %rax, %rsi
jmp 0x11818
testq %r15, %r15
je 0x11884
movq $-0x2, %rax
subq %rsi, %rax
movq $-0x3, %rcx
movq %rax, %rsi
jmp 0x1189d
notq %rsi
leaq 0x268(%rsp), %r8
movq $0x0, (%r8)
movq $-0x4, %rcx
addq %rdx, %rcx
leaq -0x1(%r15), %rdi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
leaq 0x1(%rdx), %r10
addq $0x2, %rdx
cmpq %r10, %rdi
cmovaeq %r10, %rdx
movq %rdx, -0x8(%r8)
addq $-0x8, %r8
cmpq %rcx, %rsi
movq %rax, %rcx
jbe 0x1179d
movq $-0x8, %rax
cmpq %rdx, %rdi
jae 0x117df
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
movq %r15, (%r8,%rax)
addq %rax, %r8
subq %r8, %r9
sarq $0x3, %r9
movq $0x0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x11940
notq %rsi
leaq 0x268(%rsp), %r8
movq $0x0, (%r8)
movq $-0x4, %rcx
addq %rdx, %rcx
leaq -0x1(%r15), %rdi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
leaq 0x1(%rdx), %r10
addq $0x2, %rdx
cmpq %r10, %rdi
cmovaeq %r10, %rdx
movq %rdx, -0x8(%r8)
addq $-0x8, %r8
cmpq %rcx, %rsi
movq %rax, %rcx
jbe 0x1181f
movq $-0x8, %rax
cmpq %rdx, %rdi
jae 0x11861
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
movq %r15, (%r8,%rax)
addq %rax, %r8
subq %r8, %r9
sarq $0x3, %r9
movq $0x0, (%rsp)
movq %rbx, %rdi
movl $0x2, %esi
jmp 0x11940
notq %rsi
leaq 0x268(%rsp), %r8
movq $0x0, (%r8)
movq $-0x4, %rcx
addq %rdx, %rcx
leaq -0x1(%r15), %rdi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
leaq 0x1(%rdx), %r11
addq $0x2, %rdx
cmpq %r11, %rdi
cmovaeq %r11, %rdx
movq %rdx, -0x8(%r8)
addq $-0x8, %r8
cmpq %rcx, %rsi
movq %rax, %rcx
jbe 0x118a4
movq $-0x8, %rax
cmpq %rdx, %rdi
jae 0x118e6
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
movq %r15, (%r8,%rax)
addq %rax, %r8
movq 0x30(%rsp), %rdi
movq %r10, %rax
shrq $0x6, %rax
movq 0x38(%rdi), %rcx
shlq $0x3, %rcx
movabsq $0x1ffffffffffffff8, %rdx # imm = 0x1FFFFFFFFFFFFFF8
andq %rdx, %rcx
addq $0x11, %rcx
imulq %rax, %rcx
movq 0x9(%rbx,%rcx), %rax
btq %r10, %rax
movl $0x1, %esi
sbbl $-0x1, %esi
subq %r8, %r9
sarq $0x3, %r9
subq %r12, %r10
incq %r10
movq %r10, (%rsp)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
movq %r12, %rcx
callq 0x13760
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rsi
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x38(%rax), %rax
shlq $0x3, %rax
movabsq $0x1ffffffffffffff8, %r8 # imm = 0x1FFFFFFFFFFFFFF8
andq %r8, %rax
addq $0x11, %rax
imulq %rdx, %rax
orq %rsi, 0x1(%rcx,%rax)
movb $0x1, %al
testb %r14b, %r14b
je 0x119bd
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
shrq $0xc, %r13
movl $0x0, (%rcx,%r13,4)
cmpl $0xf01, %edi            # imm = 0xF01
jb 0x119bd
movq (%rbx), %rax
movq 0x8(%rax), %rax
leaq (%rax,%r13,4), %rcx
addq $0x4, %rcx
movb $0x1, %al
movl $0x0, (%rcx)
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq