// File: /workspace/github_c_makefile_star2_O2/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, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdi, %rbx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %rsi, 0x20(%rsp)
movq %rsi, %rdi
movq %rdx, 0x10(%rsp)
movq %rdx, %rsi
callq 0x3c75
movq %rax, %rbp
movq (%rbx), %r12
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x48(%rsp)
addq %r14, %r12
movq %rbx, 0x30(%rsp)
movq 0x10(%rbx), %rax
movq %rax, 0x28(%rsp)
pushq $0x1
popq %r14
andq $0x0, 0x18(%rsp)
leal -0x1(%r14), %ebx
imull %ebx, %ebx
addl %ebp, %ebx
andl %r12d, %ebx
movq 0x28(%rsp), %rdi
movl %ebx, %esi
leaq 0x18(%rsp), %rdx
callq 0x3430
movq %rax, %r13
cmpq $0x0, 0x18(%rsp)
jne 0x3a2d
testq %r13, %r13
je 0x3a6b
cmpq %rbp, 0x20(%r13)
jne 0x3a58
movq 0x8(%r13), %rdx
cmpq 0x10(%rsp), %rdx
ja 0x3a43
movq 0x10(%rsp), %rdx
movq (%r13), %r15
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x30b0
testl %eax, %eax
je 0x3ace
movl %r14d, %eax
movq 0x30(%rsp), %rcx
cmpq %rax, 0x8(%rcx)
jb 0x3aaf
incl %r14d
jmp 0x39fa
movq %rbp, (%rsp)
movq 0x28(%rsp), %rdi
movl %ebx, %esi
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x3ca7
testl %eax, %eax
je 0x3abb
movq 0x30(%rsp), %rbx
movq (%rbx), %r14
movq 0x8(%rbx), %rax
incq %rax
movq %rax, 0x8(%rbx)
js 0x3b0f
cvtsi2ss %rax, %xmm0
jmp 0x3b24
leaq 0x554e(%rip), %rdi      # 0x9004
callq 0x30c0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r13), %r13
movq %rbp, (%rsp)
movq 0x28(%rsp), %rdi
movl %ebx, %esi
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x3ca7
testl %eax, %eax
je 0x3abb
movq %r15, %rdi
callq 0x3050
movq %r13, %rdi
callq 0x3050
pushq $0x1
popq %rbp
jmp 0x3abd
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
testq %r14, %r14
js 0x3b30
cvtsi2ss %r14, %xmm1
jmp 0x3b48
movq %r14, %rax
shrq %rax
movl %r14d, %ecx
andl $0x1, %ecx
orq %rax, %rcx
cvtsi2ss %rcx, %xmm1
addss %xmm1, %xmm1
divss %xmm1, %xmm0
pushq $0x1
popq %rbp
ucomiss 0x54aa(%rip), %xmm0     # 0x9000
jb 0x3abd
addq %r14, %r14
pushq $0x28
popq %rdi
movl %r14d, %esi
callq 0x3570
testq %rax, %rax
je 0x3abb
movq %rax, %r15
movq %r14, 0x20(%rsp)
addq %r14, 0x48(%rsp)
xorl %r14d, %r14d
leaq 0x18(%rsp), %r13
xorl %r12d, %r12d
movl %r12d, %eax
movq 0x10(%rbx), %rdi
cmpq %rax, (%rbx)
jbe 0x3c52
andq $0x0, 0x18(%rsp)
movl %r12d, %esi
movq %r13, %rdx
callq 0x3430
cmpq $0x0, 0x18(%rsp)
setne %dl
testq %rax, %rax
setne %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x3c3e
movl %r14d, 0x10(%rsp)
movq %rax, 0x28(%rsp)
movq 0x20(%rax), %r13
xorl %ebx, %ebx
movl %ebx, %r14d
imull %ebx, %r14d
addl %r13d, %r14d
andl 0x48(%rsp), %r14d
andq $0x0, 0x50(%rsp)
movq %r15, %rdi
movl %r14d, %esi
leaq 0x50(%rsp), %rdx
callq 0x3430
cmpq $0x0, 0x50(%rsp)
jne 0x3c04
testq %rax, %rax
je 0x3c15
movl %ebx, %eax
movq 0x30(%rsp), %rcx
cmpq %rax, 0x8(%rcx)
jb 0x3c68
incl %ebx
jmp 0x3bd2
movq %r15, %rdi
movl %r14d, %esi
movq 0x28(%rsp), %rdx
pushq $0x28
popq %rcx
callq 0x3070
testl %eax, %eax
je 0x3c68
movl 0x10(%rsp), %r14d
incl %r14d
movq 0x30(%rsp), %rbx
leaq 0x18(%rsp), %r13
movl %r14d, %eax
incl %r12d
cmpq %rax, 0x8(%rbx)
jne 0x3b8b
movq 0x10(%rbx), %rdi
callq 0x3230
movq %r15, 0x10(%rbx)
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
jmp 0x3abd
movq %r15, %rdi
callq 0x3230
jmp 0x3abb