// File: /workspace/github_cpp_makefile_star2_O3/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, %r13d
movl %ecx, %r14d
movq %rdx, 0x10(%rsp)
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x38(%rax), %rcx
movq $-0x1, %rax
movq $-0x1, %rbp
shlq %cl, %rbp
cmpq $0x40, %rcx
notq %rbp
cmoveq %rax, %rbp
movq %rsi, %r12
xorl %eax, %eax
movq %rsi, %r15
shrdq %cl, %rax, %r15
movq 0x10(%rdi), %rsi
testb $0x40, %cl
cmovneq %rax, %r15
movq %rcx, %rdi
movq %r15, %rdx
callq 0x121bd
movq %r15, %r11
movl %r11d, %r10d
andl $0xfff, %r10d           # imm = 0xFFF
testl %r14d, %r14d
je 0x10f3b
movq (%rbx), %rcx
movq 0x8(%rcx), %rsi
movq %r11, %rcx
shrq $0xc, %rcx
movl $0x1, %edi
xchgl %edi, (%rsi,%rcx,4)
movl %r13d, %edx
xorb $0x1, %dl
jne 0x1122c
testl %edi, %edi
je 0x1122c
cmpl $0x0, (%rsi,%rcx,4)
jne 0x10ed6
movl $0x1, %edi
xchgl %edi, (%rsi,%rcx,4)
testl %edi, %edi
jne 0x10ed6
cmpl $0xf01, %r10d           # imm = 0xF01
jb 0x10f3b
movq (%rbx), %rsi
movq 0x8(%rsi), %rsi
movl $0x1, %edi
xchgl %edi, 0x4(%rsi,%rcx,4)
testl %edi, %edi
sete %r8b
orb %r8b, %dl
je 0x10f27
testb %r13b, %r13b
jne 0x10f3b
testl %edi, %edi
je 0x10f3b
movq (%rbx), %rax
shlq $0x2, %rcx
addq 0x8(%rax), %rcx
xorl %eax, %eax
jmp 0x116f6
cmpl $0x0, 0x4(%rsi,%rcx,4)
jne 0x10f27
movl $0x1, %edx
xchgl %edx, 0x4(%rsi,%rcx,4)
testl %edx, %edx
jne 0x10f27
movl %r13d, %esi
movq %r10, 0x38(%rsp)
movl %r14d, 0xc(%rsp)
andq %r12, %rbp
movl %r11d, %r14d
andl $0x3f, %r14d
movabsq $0x1ffffffffffffff8, %r8 # imm = 0x1FFFFFFFFFFFFFF8
movq 0x8(%rbx), %rcx
movq %rbx, 0x20(%rsp)
movq 0x10(%rbx), %r13
movq %rcx, 0x28(%rsp)
movq 0x38(%rcx), %rdi
movq %r11, %r9
shrq $0x6, %r9
movabsq $0x3ffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFF
andq %rdi, %rcx
leaq 0x11(,%rcx,8), %rbx
movq %rbx, %rdx
imulq %r9, %rdx
movq 0x1(%r13,%rdx), %r15
movl $0x1, %r10d
movl %r14d, %ecx
shlq %cl, %r10
btq %r14, %r15
jae 0x10fe6
movq %r10, 0x40(%rsp)
movq %r9, 0x48(%rsp)
testq %r11, %r11
movq %r11, 0x18(%rsp)
movq %rdi, 0x30(%rsp)
je 0x110e1
leaq -0x1(%r11), %rdx
movq %r13, %rsi
callq 0x121bd
movq %rax, %r12
incq %r12
movl %r14d, %ecx
shrq %cl, %r15
jmp 0x110e8
cmpq %r11, %rax
jne 0x10fac
movq 0x9(%r13,%rdx), %rax
movq %rax, %rcx
andq %r10, %rcx
jne 0x10fac
orq %r10, %rax
movq %rax, 0x9(%r13,%rdx)
movq 0x28(%rsp), %rdi
movq 0x38(%rdi), %rcx
leaq (,%rcx,8), %rax
andq %r8, %rax
addq $0x11, %rax
imulq %r9, %rax
addq %r13, %rax
imulq %rcx, %r14
movq $-0x1, %rdx
shlq %cl, %rdx
shrq $0x3, %r14
movq $-0x1, %rbx
cmpq $0x40, %rcx
notq %rdx
cmoveq %rbx, %rdx
imull %r11d, %ecx
andb $0x7, %cl
shlq %cl, %rdx
shlq %cl, %rbp
notq %rdx
andq 0x11(%r14,%rax), %rdx
orq %rbp, %rdx
movq %rdx, 0x11(%r14,%rax)
movq 0x38(%rdi), %rax
shlq $0x3, %rax
andq %r8, %rax
addq $0x11, %rax
imulq %r9, %rax
orq %r10, 0x1(%r13,%rax)
cmpq $0x2, 0x10(%rsp)
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %r14d
jb 0x116c1
movq (%rbx), %rax
movq 0x8(%rax), %rcx
movq %r11, %r15
shrq $0xc, %r11
movl $0x0, (%rcx,%r11,4)
cmpl $0xf01, 0x38(%rsp)      # imm = 0xF01
jb 0x110be
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
movl $0x0, 0x4(%rcx,%r11,4)
movq 0x10(%rsp), %rdx
decq %rdx
movzbl %r14b, %ecx
movzbl %sil, %r8d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x10e31
movq %r15, %r11
jmp 0x116c1
movq 0x1(%r13), %r15
xorl %r12d, %r12d
testb $0x1, %r15b
jne 0x1114f
movq $-0x1, %rsi
movq 0x30(%rsp), %rcx
shlq %cl, %rsi
leaq 0x278(%rsp), %r9
movq %r9, %r8
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %r14d
je 0x114e9
leaq 0x270(%rsp), %r8
movq %rbp, (%r8)
cmpq $0x1, %rdx
je 0x114e9
cmpq $0x2, %rdx
jne 0x112f5
leaq 0x268(%rsp), %r8
movq %rbp, (%r8)
jmp 0x114e9
leaq 0x58(%rsp), %r14
leaq 0x50(%rsp), %r8
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x12480
movq %rax, %r10
movq (%r14), %rax
cmpq %rbp, %rax
jae 0x111c4
leaq 0x58(%rsp), %r14
leaq 0x50(%rsp), %r15
movq %r10, %rax
shrq $0x6, %rax
imulq %rbx, %rax
movq 0x9(%r13,%rax), %rax
btq %r10, %rax
jb 0x1128f
movq %r10, %r12
incq %r12
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %r15, %r8
callq 0x12480
movq %rax, %r10
movq 0x58(%rsp), %rax
cmpq %rbp, %rax
jb 0x11181
cmpq %rbp, %rax
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %r14d
movq 0x30(%rsp), %rcx
movq $-0x1, %rsi
jne 0x11244
shlq %cl, %rsi
leaq 0x278(%rsp), %r9
movq %r9, %r8
movq 0x10(%rsp), %rdx
addq 0x50(%rsp), %rdx
je 0x1162d
leaq 0x270(%rsp), %r8
movq %rbp, (%r8)
cmpq $0x1, %rdx
je 0x1162d
cmpq $0x2, %rdx
jne 0x11334
leaq 0x268(%rsp), %r8
movq %rbp, (%r8)
jmp 0x1162d
testb %r13b, %r13b
jne 0x10ee8
testl %edi, %edi
je 0x10ee8
xorl %eax, %eax
jmp 0x116fc
shlq %cl, %rsi
leaq 0x278(%rsp), %r9
movq %r9, %r8
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
je 0x115a8
leaq 0x270(%rsp), %r8
movq %rbp, (%r8)
cmpq $0x1, %rdx
je 0x115a8
cmpq $0x2, %rdx
jne 0x11358
leaq 0x268(%rsp), %r8
movq %rbp, (%r8)
jmp 0x115a8
movl $0x1, %esi
movq 0x30(%rsp), %rcx
shlq %cl, %rsi
leaq 0x278(%rsp), %r9
movq %r9, %r8
movq 0x10(%rsp), %r11
testq %r11, %r11
movq 0x20(%rsp), %rbx
movl 0xc(%rsp), %r14d
je 0x11407
leaq 0x270(%rsp), %r8
movq %rbp, (%r8)
cmpq $0x1, %r11
je 0x11407
cmpq $0x3, %r11
je 0x113a1
cmpq $0x2, %r11
jne 0x113b3
leaq 0x268(%rsp), %r8
jmp 0x11404
cmpq $0x3, %rdx
jne 0x11314
testq %rbp, %rbp
jne 0x11314
leaq 0x260(%rsp), %r8
xorps %xmm0, %xmm0
movaps %xmm0, (%r8)
jmp 0x114e9
cmpq $0x3, %rdx
jne 0x1137f
testq %rbp, %rbp
je 0x1137f
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
jmp 0x11147
movq %rdx, %rax
xorq $0x3, %rax
orq %rbp, %rax
jne 0x11456
leaq 0x260(%rsp), %r8
xorps %xmm0, %xmm0
movaps %xmm0, (%r8)
jmp 0x1162d
cmpq $0x3, %rdx
jne 0x1142e
testq %rbp, %rbp
jne 0x1142e
leaq 0x260(%rsp), %r8
xorps %xmm0, %xmm0
movaps %xmm0, (%r8)
jmp 0x115a8
testq %rbp, %rbp
je 0x11480
movq $-0x2, %rax
subq %rsi, %rax
movq $-0x3, %rcx
movq %rax, %rsi
jmp 0x11499
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
jmp 0x11404
addq $-0x2, %rsi
addq $-0x3, %r11
leaq -0x1(%rbp), %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 0x113bf
movq $-0x8, %rax
cmpq %rdx, %rcx
jae 0x11401
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
addq %rax, %r8
movq %rbp, (%r8)
incq %r10
subq %r8, %r9
sarq $0x3, %r9
movq $0x0, (%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movq 0x18(%rsp), %rdx
movq %r10, %rcx
jmp 0x11683
cmpq $0x3, %rdx
jne 0x11502
testq %rbp, %rbp
je 0x11502
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
jmp 0x11287
cmpq $0x3, %rdx
setne %al
testq %rbp, %rbp
sete %cl
orb %al, %cl
jne 0x1151d
leaq 0x260(%rsp), %r8
movq $0x0, 0x8(%r8)
jmp 0x11224
notq %rsi
leaq 0x268(%rsp), %r8
movq $0x0, (%r8)
movq $-0x4, %rcx
addq %rdx, %rcx
leaq -0x1(%rbp), %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 0x114a0
movq $-0x8, %rax
cmpq %rdx, %rdi
jae 0x114e2
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
movq %rbp, (%r8,%rax)
addq %rax, %r8
subq %r8, %r9
sarq $0x3, %r9
movq $0x0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x1167b
testq %rbp, %rbp
je 0x1153f
movq $-0x2, %rax
subq %rsi, %rax
movq $-0x3, %rcx
movq %rax, %rsi
jmp 0x11558
testq %rbp, %rbp
je 0x115c4
movq $-0x2, %rax
subq %rsi, %rax
movq $-0x3, %rcx
movq %rax, %rsi
jmp 0x115dd
notq %rsi
leaq 0x268(%rsp), %r8
movq $0x0, (%r8)
movq $-0x4, %rcx
addq %rdx, %rcx
leaq -0x1(%rbp), %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 0x1155f
movq $-0x8, %rax
cmpq %rdx, %rdi
jae 0x115a1
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
movq %rbp, (%r8,%rax)
addq %rax, %r8
subq %r8, %r9
sarq $0x3, %r9
movq $0x0, (%rsp)
movq %rbx, %rdi
movl $0x2, %esi
jmp 0x1167b
notq %rsi
leaq 0x268(%rsp), %r8
movq $0x0, (%r8)
movq $-0x4, %rcx
addq %rdx, %rcx
leaq -0x1(%rbp), %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 0x115e4
movq $-0x8, %rax
cmpq %rdx, %rdi
jae 0x11626
movq $0x0, -0x8(%r8)
movq $-0x10, %rax
movq %rbp, (%r8,%rax)
addq %rax, %r8
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq %r10, %rcx
shrq $0x6, %rcx
shlq $0x3, %rax
movabsq $0x1ffffffffffffff8, %rdx # imm = 0x1FFFFFFFFFFFFFF8
andq %rdx, %rax
addq $0x11, %rax
imulq %rcx, %rax
movq 0x9(%r13,%rax), %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 %rbx, %rdi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
callq 0x133d0
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rdi
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movq 0x38(%rax), %rax
shlq $0x3, %rax
movabsq $0x1ffffffffffffff8, %rsi # imm = 0x1FFFFFFFFFFFFFF8
andq %rsi, %rax
addq $0x11, %rax
imulq %rdx, %rax
orq %rdi, 0x1(%rcx,%rax)
movq 0x18(%rsp), %r11
movb $0x1, %al
testb %r14b, %r14b
je 0x116fc
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
shrq $0xc, %r11
movl $0x0, (%rcx,%r11,4)
cmpl $0xf01, 0x38(%rsp)      # imm = 0xF01
jb 0x116fc
movq (%rbx), %rax
movq 0x8(%rax), %rax
leaq (%rax,%r11,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