// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3fc0
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x38(%rbp), %rax
movl -0x3c(%rbp), %ecx
imull -0x3c(%rbp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
andq %rcx, %rax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x4c(%rbp), %esi
leaq -0x48(%rbp), %rdx
callq 0x3460
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x3dbd
cmpq $0x0, -0x58(%rbp)
jne 0x3dbd
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x4c(%rbp), %esi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %r9
movq -0x38(%rbp), %rax
movq %rax, (%rsp)
callq 0x4030
cmpl $0x0, %eax
je 0x3db8
jmp 0x3eb3
jmp 0x3fa5
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x20(%rbp), %rax
jbe 0x3de1
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
jmp 0x3de9
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
cmpq -0x38(%rbp), %rax
jne 0x3e83
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x30b0
cmpl $0x0, %eax
jne 0x3e83
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x4c(%rbp), %esi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %r9
movq -0x38(%rbp), %rax
movq %rax, (%rsp)
callq 0x4030
cmpl $0x0, %eax
je 0x3e7e
movq -0x70(%rbp), %rdi
callq 0x3050
movq -0x78(%rbp), %rdi
callq 0x3050
movl $0x1, -0x4(%rbp)
jmp 0x3fac
jmp 0x3fa5
jmp 0x3e85
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jbe 0x3eae
leaq 0x8162(%rip), %rdi      # 0xc004
movb $0x0, %al
callq 0x31c0
jmp 0x3fa5
jmp 0x3d37
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
incq %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x90(%rbp)
movq 0x8(%rax), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x88(%rbp)
testq %rax, %rax
movss %xmm0, -0x84(%rbp)
js 0x3f15
movss -0x88(%rbp), %xmm0
movss %xmm0, -0x84(%rbp)
movq -0x90(%rbp), %rax
movss -0x84(%rbp), %xmm0
movss %xmm0, -0x9c(%rbp)
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, -0x98(%rbp)
testq %rax, %rax
movss %xmm0, -0x94(%rbp)
js 0x3f6d
movss -0x98(%rbp), %xmm0
movss %xmm0, -0x94(%rbp)
movss -0x9c(%rbp), %xmm0
movss -0x94(%rbp), %xmm1
divss %xmm1, %xmm0
movss 0x8077(%rip), %xmm1     # 0xc000
ucomiss %xmm1, %xmm0
jb 0x3f9c
movq -0x10(%rbp), %rdi
callq 0x4120
movl %eax, -0x4(%rbp)
jmp 0x3fac
movl $0x1, -0x4(%rbp)
jmp 0x3fac
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)