// File: /workspace/github_c_makefile_star2_O3/qpfiffer[P]38-Moths/src/simple_sparsehash.c
// Function: sparse_dict_set
int sparse_dict_set(struct sparse_dict *dict,
		const char *key, const size_t klen,
		const void *value, const size_t vlen) {
	const uint64_t key_hash = hash_fnv1a(key, klen);
	unsigned int num_probes = 0;

	/* First check the array to see if we have an object already stored in
	 * 'out' position.
	 */
	while (1) {
		size_t current_value_siz = 0;
		/* Use quadratic probing here to insert into the table.
		 * Further reading: https://en.wikipedia.org/wiki/Quadratic_probing
		 */
		const unsigned int probed_val = QUADRATIC_PROBE(dict->bucket_max);
		const void *current_value = sparse_array_get(dict->buckets, probed_val, &current_value_siz);

		if (current_value_siz == 0 && current_value == NULL) {
			/* Awesome, the slot we want is empty. Insert as normal. */
			if (_create_and_insert_new_bucket(dict->buckets, probed_val, key, klen, value, vlen, key_hash))
				break;
			else
				goto error;
		} else {
			/* We found a bucket. Check to see if it has the same key as we do. */
			struct sparse_bucket *existing_bucket = (struct sparse_bucket *)current_value;
			const size_t lrgr_key = existing_bucket->klen > klen ? existing_bucket->klen : klen;
			if (existing_bucket->hash == key_hash && strncmp(existing_bucket->key, key, lrgr_key) == 0) {
				/* Great, we probed along the hashtable and found a bucket with the same key as
				 * the key we want to insert. Replace it. */
				char *existing_key = existing_bucket->key;
				void *existing_val = existing_bucket->val;
				if (_create_and_insert_new_bucket(dict->buckets, probed_val, key, klen, value, vlen, key_hash)) {
					/* We return here because we don't want to execute the 'resize the table'
					 * logic. We overwrote a bucket instead of adding a new one, so we know
					 * we don't need to resize anything.
					 */
					free(existing_key);
					free(existing_val);
					return 1;
				} else {
					goto error;
				}
			}
		}

		num_probes++;

		if (num_probes > dict->bucket_count) {
			/* If this ever happens something has gone very, very wrong.
			 * The hash table is full.
			 */
			printf("Could not find an open slot in the table.\n");
			goto error;
		}
	}

	dict->bucket_count++;

	/* See if we've hit our 'we should rehash the table' occupancy number: */
	if (dict->bucket_count / (float)dict->bucket_max >= RESIZE_PERCENT/100.0f)
		return _rehash_and_grow_table(dict);

	return 1;

error:
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, %r12
movabsq $-0x340d631b7bdddcdb, %rdx # imm = 0xCBF29CE484222325
testl %r12d, %r12d
jle 0x3cbb
movb $0x1, %cl
movabsq $0x100000001b3, %rax    # imm = 0x100000001B3
leal -0x1(%rcx), %r8d
movzbl %r8b, %r8d
movsbq (%rsi,%r8), %rbp
xorq %rdx, %rbp
imulq %rax, %rbp
movzbl %cl, %r8d
leal 0x1(%r8), %ecx
movq %rbp, %rdx
cmpl %r12d, %r8d
jb 0x3c95
jmp 0x3cbe
movq %rdx, %rbp
movq %rsi, 0x18(%rsp)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq (%rdi), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x38(%rsp)
addq %rcx, %rdx
movq %rdi, 0x30(%rsp)
movq 0x10(%rdi), %rdi
leaq 0x10(%rsp), %r15
movq $0x0, (%r15)
movl %ebp, %r14d
movq %rdx, 0x40(%rsp)
andl %edx, %r14d
movq %rdi, 0x20(%rsp)
movl %r14d, %esi
movq %r15, %rdx
callq 0x3430
movq %rax, %rbx
cmpq $0x0, (%r15)
jne 0x3d16
testq %rbx, %rbx
je 0x3d94
movl $0x1, %r13d
cmpq %rbp, 0x20(%rbx)
jne 0x3d45
movq (%rbx), %r15
movq 0x8(%rbx), %rdx
cmpq %r12, %rdx
cmovbeq %r12, %rdx
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x30b0
testl %eax, %eax
je 0x3f90
movl %r13d, %eax
movq 0x30(%rsp), %rcx
cmpq %rax, 0x8(%rcx)
jb 0x3f82
movq $0x0, 0x10(%rsp)
movl %r13d, %r14d
imull %r13d, %r14d
addl %ebp, %r14d
andl 0x40(%rsp), %r14d
movq 0x20(%rsp), %rdi
movl %r14d, %esi
leaq 0x10(%rsp), %rdx
callq 0x3430
movq %rax, %rbx
incl %r13d
cmpq $0x0, 0x10(%rsp)
jne 0x3d1c
testq %rbx, %rbx
jne 0x3d1c
movq %rbp, (%rsp)
movq 0x20(%rsp), %rdi
movl %r14d, %esi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %r9
callq 0x3feb
testl %eax, %eax
je 0x3fda
movq 0x30(%rsp), %rbx
movq (%rbx), %r14
movq 0x8(%rbx), %rax
incq %rax
movq %rax, 0x8(%rbx)
js 0x3ddb
cvtsi2ss %rax, %xmm0
jmp 0x3df0
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
testq %r14, %r14
js 0x3dfc
cvtsi2ss %r14, %xmm1
jmp 0x3e14
movq %r14, %rax
shrq %rax
movl %r14d, %ecx
andl $0x1, %ecx
orq %rax, %rcx
cvtsi2ss %rcx, %xmm1
addss %xmm1, %xmm1
divss %xmm1, %xmm0
movl $0x1, %eax
ucomiss 0x61dc(%rip), %xmm0     # 0xa000
jb 0x3fdc
addq %r14, %r14
movl $0x28, %edi
movl %r14d, %esi
callq 0x3570
testq %rax, %rax
je 0x3fda
movq %rax, %r15
movq %r14, 0x18(%rsp)
cmpq $0x0, (%rbx)
je 0x3f6b
movq 0x18(%rsp), %rax
addq %rax, 0x38(%rsp)
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
xorl %ebp, %ebp
movq $0x0, 0x10(%rsp)
movq 0x10(%rbx), %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x3430
cmpq $0x0, 0x10(%rsp)
setne %dl
testq %rax, %rax
setne %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x3f57
movl %ebp, 0x40(%rsp)
movq %rax, 0x20(%rsp)
movq 0x20(%rax), %r14
movl %r14d, %ebx
andl 0x38(%rsp), %ebx
movq $0x0, 0x28(%rsp)
movq %r15, %rdi
movl %ebx, %esi
leaq 0x28(%rsp), %rdx
callq 0x3430
cmpq $0x0, 0x28(%rsp)
jne 0x3ed4
testq %rax, %rax
je 0x3f2b
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rbp
movl $0x1, %r13d
leal -0x1(%r13), %eax
cmpq %rax, %rbp
jb 0x3fd2
movl %r13d, %eax
imull %r13d, %eax
addl %r14d, %eax
movq 0x38(%rsp), %rcx
movl %ecx, %ebx
andl %eax, %ebx
movq $0x0, 0x28(%rsp)
movq %r15, %rdi
movl %ebx, %esi
leaq 0x28(%rsp), %rdx
callq 0x3430
incl %r13d
cmpq $0x0, 0x28(%rsp)
jne 0x3ee3
testq %rax, %rax
jne 0x3ee3
movl $0x28, %ecx
movq %r15, %rdi
movl %ebx, %esi
movq 0x20(%rsp), %rdx
callq 0x3070
testl %eax, %eax
je 0x3fd2
movl 0x40(%rsp), %ebp
incl %ebp
movq 0x30(%rsp), %rbx
leaq 0x10(%rsp), %r14
movl %ebp, %eax
cmpq %rax, 0x8(%rbx)
je 0x3f6b
incl %r12d
cmpq %r12, (%rbx)
ja 0x3e69
movq 0x10(%rbx), %rdi
callq 0x3230
movq %r15, 0x10(%rbx)
movq 0x18(%rsp), %rax
movq %rax, (%rbx)
jmp 0x3fcb
leaq 0x607b(%rip), %rdi      # 0xa004
callq 0x30c0
jmp 0x3fda
movq 0x10(%rbx), %rbx
movq %rbp, (%rsp)
movq 0x20(%rsp), %rdi
movl %r14d, %esi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %r9
callq 0x3feb
testl %eax, %eax
je 0x3fda
movq %r15, %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
movl $0x1, %eax
jmp 0x3fdc
movq %r15, %rdi
callq 0x3230
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq