// File: /workspace/github_cpp_makefile_star2_O1/splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: qf_init
void qf_init(QF *qf, uint64_t nslots, uint64_t key_bits, uint64_t value_bits,
						 bool mem, const char * path, uint32_t seed)
{
	uint64_t num_slots, xnslots, nblocks;
	uint64_t key_remainder_bits, bits_per_slot;
	uint64_t size;

	assert(popcnt(nslots) == 1); /* nslots must be a power of 2 */
	num_slots = nslots;
	assert(popcnt(nslots) == 1); /* nslots must be a power of 2 */
	xnslots = nslots + 10*sqrt((double)nslots);
	nblocks = (xnslots + SLOTS_PER_BLOCK - 1) / SLOTS_PER_BLOCK;
	key_remainder_bits = key_bits;
	while (nslots > 1) {
		assert(key_remainder_bits > 0);
		key_remainder_bits--;
		nslots >>= 1;
	}

	bits_per_slot = key_remainder_bits + value_bits;
	assert (BITS_PER_SLOT == 0 || BITS_PER_SLOT == qf->metadata->bits_per_slot);
	assert(bits_per_slot > 1);
#if BITS_PER_SLOT == 8 || BITS_PER_SLOT == 16 || BITS_PER_SLOT == 32 || BITS_PER_SLOT == 64
	size = nblocks * sizeof(qfblock);
#else
	size = nblocks * (sizeof(qfblock) + SLOTS_PER_BLOCK * bits_per_slot / 8);
#endif

	qf->mem = (qfmem *)calloc(sizeof(qfmem), 1);
	
	if (mem) {
		qf->metadata = (qfmetadata *)calloc(sizeof(qfmetadata), 1);

		qf->metadata->size = size;
		qf->metadata->seed = seed;
		qf->metadata->nslots = num_slots;
		qf->metadata->xnslots = qf->metadata->nslots +
			10*sqrt((double)qf->metadata->nslots);
		qf->metadata->key_bits = key_bits;
		qf->metadata->value_bits = value_bits;
		qf->metadata->key_remainder_bits = key_remainder_bits;
		qf->metadata->bits_per_slot = bits_per_slot;

		qf->metadata->range = qf->metadata->nslots;
		qf->metadata->range <<= qf->metadata->bits_per_slot;
		qf->metadata->nblocks = (qf->metadata->xnslots + SLOTS_PER_BLOCK - 1) /
			SLOTS_PER_BLOCK;
		qf->metadata->nelts = 0;
		qf->metadata->ndistinct_elts = 0;
		qf->metadata->noccupied_slots = 0;
		qf->metadata->num_locks = (qf->metadata->xnslots/NUM_SLOTS_TO_LOCK)+2;

		qf->blocks = (qfblock *)calloc(size, 1);

	} else {
		int ret;

		qf->mem->fd = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
		if (qf->mem->fd < 0) {
			perror("Couldn't open file:\n");
			exit(EXIT_FAILURE);
		}
		ret = fallocate(qf->mem->fd, 0, 0, size+sizeof(qfmetadata));
		if (ret < 0) {
			perror("Couldn't fallocate file:\n");
			exit(EXIT_FAILURE);
		}
		qf->metadata = (qfmetadata *)mmap(NULL, size+sizeof(qfmetadata), PROT_READ |
																			PROT_WRITE, MAP_SHARED, qf->mem->fd, 0);

		qf->metadata->seed = seed;
		qf->metadata->nslots = num_slots;
		qf->metadata->xnslots = qf->metadata->nslots +
														10*sqrt((double)qf->metadata->nslots);
		qf->metadata->key_bits = key_bits;
		qf->metadata->value_bits = value_bits;
		qf->metadata->key_remainder_bits = key_remainder_bits;
		qf->metadata->bits_per_slot = bits_per_slot;

		qf->metadata->range = qf->metadata->nslots;
		qf->metadata->range <<= qf->metadata->bits_per_slot;
		qf->metadata->nblocks = (qf->metadata->xnslots + SLOTS_PER_BLOCK - 1) /
			SLOTS_PER_BLOCK;
		qf->metadata->nelts = 0;
		qf->metadata->ndistinct_elts = 0;
		qf->metadata->noccupied_slots = 0;
		qf->metadata->num_locks = (qf->metadata->xnslots/NUM_SLOTS_TO_LOCK)+2;

		qf->blocks = (qfblock *)(qf->metadata + sizeof(qfmetadata));
	}
	
	/* initialize all the locks to 0 */
	qf->mem->metadata_lock = 0;
	qf->mem->locks = (volatile int *)calloc(qf->metadata->num_locks,
																					sizeof(volatile int));
#ifdef LOG_WAIT_TIME
	qf->wait_times = (wait_time_data* )calloc(qf->metadata->num_locks+1,
																						sizeof(wait_time_data));
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movl %r8d, 0xc(%rsp)
movq %rcx, %r12
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %rbx
movq %rsi, %xmm0
punpckldq 0x4ceb(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4cf3(%rip), %xmm0     # 0x14a60
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
jb 0xfd92
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0xfda1
movapd %xmm1, %xmm0
callq 0x21c0
movapd 0x10(%rsp), %xmm1
mulsd 0x4cc7(%rip), %xmm0     # 0x14a70
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x4cb7(%rip), %xmm0     # 0x14a78
cvttsd2si %xmm0, %rax
andq %rdx, %rax
orq %rcx, %rax
addq $0x3f, %rax
shrq $0x6, %rax
movq %rbp, %r15
cmpq $0x2, %r13
jb 0xfdf5
movq %r13, %rcx
movq %rbp, %r15
movq %r13, %rdx
decq %r15
shrq %rdx
cmpq $0x3, %rcx
movq %rdx, %rcx
ja 0xfde6
movq %rbp, (%rsp)
movl 0x70(%rsp), %r14d
movq %r12, 0x28(%rsp)
addq %r15, %r12
movabsq $0x3ffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFF
andq %r12, %rcx
leaq 0x11(,%rcx,8), %rbp
imulq %rax, %rbp
movl $0x18, %edi
movl $0x1, %esi
callq 0x2130
movq %rax, (%rbx)
cmpb $0x0, 0xc(%rsp)
je 0xfe76
movl $0x80, %edi
movl $0x1, %esi
callq 0x2130
movq %rax, 0x8(%rbx)
movq %rbp, (%rax)
movl %r14d, 0x8(%rax)
movq %r13, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0xfefc
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0xff0b
movq 0x30(%rsp), %rdi
movl $0x242, %esi            # imm = 0x242
movl $0x1c0, %edx            # imm = 0x1C0
xorl %eax, %eax
callq 0x2190
movq (%rbx), %rcx
movl %eax, (%rcx)
testl %eax, %eax
js 0x10096
subq $-0x80, %rbp
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
movq %rbp, %rcx
callq 0x21b0
testl %eax, %eax
js 0x1009b
movq (%rbx), %rax
movl (%rax), %r8d
xorl %edi, %edi
movq %rbp, %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
callq 0x23d0
movq %rax, 0x8(%rbx)
movl %r14d, 0x8(%rax)
movq %r13, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0xffb5
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0xffc4
movapd %xmm1, %xmm0
callq 0x21c0
movapd 0x10(%rsp), %xmm1
mulsd 0x4b5d(%rip), %xmm0     # 0x14a70
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd 0x4b51(%rip), %xmm0     # 0x14a78
cvttsd2si %xmm0, %rax
sarq $0x3f, %rdx
andq %rdx, %rax
orq %rcx, %rax
movq 0x8(%rbx), %rdx
movq %rax, 0x18(%rdx)
movq (%rsp), %rcx
movq %rcx, 0x20(%rdx)
movq 0x28(%rsp), %rcx
movq %rcx, 0x28(%rdx)
movq %r15, 0x30(%rdx)
movq %r12, 0x38(%rdx)
movq 0x10(%rdx), %rsi
xorl %edi, %edi
movl %r12d, %ecx
shldq %cl, %rsi, %rdi
xorl %r8d, %r8d
shlq %cl, %rsi
testb $0x40, %r12b
cmovneq %rsi, %rdi
cmovneq %r8, %rsi
movq %rdi, 0x48(%rdx)
movq %rsi, 0x40(%rdx)
leaq 0x3f(%rax), %rcx
shrq $0x6, %rcx
movq %rcx, 0x50(%rdx)
shrq $0xc, %rax
addq $0x2, %rax
xorpd %xmm0, %xmm0
movupd %xmm0, 0x58(%rdx)
movq %r8, 0x68(%rdx)
movq %rax, 0x70(%rdx)
movl $0x1, %esi
movq %rbp, %rdi
callq 0x2130
jmp 0x10062
movapd %xmm1, %xmm0
callq 0x21c0
movapd 0x10(%rsp), %xmm1
movq (%rsp), %rsi
mulsd 0x4aa0(%rip), %xmm0     # 0x14a70
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0x4a94(%rip), %xmm0     # 0x14a78
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movq 0x8(%rbx), %rax
movq %rdx, 0x18(%rax)
movq %rsi, 0x20(%rax)
movq 0x28(%rsp), %rcx
movq %rcx, 0x28(%rax)
movq %r15, 0x30(%rax)
movq %r12, 0x38(%rax)
movq 0x10(%rax), %rsi
xorl %edi, %edi
movl %r12d, %ecx
shldq %cl, %rsi, %rdi
xorl %r8d, %r8d
shlq %cl, %rsi
testb $0x40, %r12b
cmovneq %rsi, %rdi
cmovneq %r8, %rsi
movq %rdi, 0x48(%rax)
movq %rsi, 0x40(%rax)
leaq 0x3f(%rdx), %rcx
shrq $0x6, %rcx
movq %rcx, 0x50(%rax)
shrq $0xc, %rdx
addq $0x2, %rdx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x58(%rax)
movq %r8, 0x68(%rax)
movq %rdx, 0x70(%rax)
addq $0x4000, %rax           # imm = 0x4000
movq %rax, 0x10(%rbx)
movq (%rbx), %r14
movl $0x0, 0x4(%r14)
movq 0x8(%rbx), %rax
movq 0x70(%rax), %rdi
movl $0x4, %esi
callq 0x2130
movq %rax, 0x8(%r14)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x24a9
callq 0x2494