// File: /workspace/github_c_makefile_star2_O1/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, %r12
movabsq $-0x340d631b7bdddcdb, %rdi # imm = 0xCBF29CE484222325
testl %edx, %edx
jle 0x3bdd
movb $0x1, %cl
movabsq $0x100000001b3, %rax    # imm = 0x100000001B3
leal -0x1(%rcx), %r8d
movzbl %r8b, %r8d
movsbq (%rsi,%r8), %rbp
xorq %rdi, %rbp
imulq %rax, %rbp
movzbl %cl, %r8d
leal 0x1(%r8), %ecx
movq %rbp, %rdi
cmpl %edx, %r8d
jl 0x3bb7
jmp 0x3be0
movq %rdi, %rbp
movq %rsi, 0x28(%rsp)
movq %rdx, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x30(%rsp)
movq %r12, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
movq 0x20(%rsp), %rax
imull %eax, %eax
addl %ebp, %eax
movl (%r12), %r14d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
addl %ecx, %r14d
andl %eax, %r14d
movq 0x10(%r12), %r15
movq %r15, %rdi
movl %r14d, %esi
leaq 0x8(%rsp), %rdx
callq 0x3430
movq %rax, %rbx
cmpq $0x0, 0x8(%rsp)
jne 0x3c7e
testq %rbx, %rbx
jne 0x3c7e
movq %rbp, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x3f3d
cmpl $0x1, %eax
movl $0x0, %r13d
adcl $0x3, %r13d
jmp 0x3d3a
xorl %r13d, %r13d
movb $0x1, %r12b
cmpq %rbp, 0x20(%rbx)
jne 0x3d01
movq (%rbx), %rdi
movq 0x8(%rbx), %rdx
movq 0x18(%rsp), %rax
cmpq %rax, %rdx
cmovbeq %rax, %rdx
movq %rdi, 0x50(%rsp)
movq 0x28(%rsp), %rsi
callq 0x30b0
testl %eax, %eax
jne 0x3d01
movq 0x50(%rsp), %r12
movq 0x10(%rbx), %rbx
movq %rbp, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0x3f3d
testl %eax, %eax
je 0x3cf8
movq %r12, %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
movl $0x1, %r13d
jmp 0x3cfe
movl $0x4, %r13d
xorl %r12d, %r12d
testb %r12b, %r12b
je 0x3d35
movq 0x20(%rsp), %rax
incl %eax
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
movq %rax, 0x20(%rsp)
cmpq %rax, 0x8(%r12)
jae 0x3d3a
leaq 0x62dc(%rip), %rdi      # 0xa004
callq 0x30c0
movl $0x4, %r13d
jmp 0x3d3a
movq 0x10(%rsp), %r12
testl %r13d, %r13d
je 0x3c02
cmpl $0x4, %r13d
je 0x3f2a
cmpl $0x3, %r13d
jne 0x3f1b
movq (%r12), %rbx
movq 0x8(%r12), %rax
incq %rax
movq %rax, 0x8(%r12)
js 0x3d71
cvtsi2ss %rax, %xmm0
jmp 0x3d86
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
testq %rbx, %rbx
js 0x3d92
cvtsi2ss %rbx, %xmm1
jmp 0x3da9
movq %rbx, %rax
shrq %rax
movl %ebx, %ecx
andl $0x1, %ecx
orq %rax, %rcx
cvtsi2ss %rcx, %xmm1
addss %xmm1, %xmm1
divss %xmm1, %xmm0
movl $0x1, %eax
ucomiss 0x6247(%rip), %xmm0     # 0xa000
jb 0x3f2c
addq %rbx, %rbx
movl $0x28, %edi
movl %ebx, %esi
callq 0x3570
testq %rax, %rax
je 0x3f2a
movq %rax, %r15
movq %rbx, 0x18(%rsp)
cmpq $0x0, (%r12)
movq %r12, %rbx
je 0x3f06
movq 0x18(%rsp), %rax
addq %rax, 0x30(%rsp)
xorl %r12d, %r12d
leaq 0x8(%rsp), %r13
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
movq 0x10(%rbx), %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x3430
movq %rax, %r14
cmpq $0x0, 0x8(%rsp)
setne %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x3ecd
movl %ebp, 0x20(%rsp)
movq 0x20(%r14), %rbx
xorl %r13d, %r13d
movl %r13d, %ebp
imull %r13d, %ebp
addl %ebx, %ebp
andl 0x30(%rsp), %ebp
movq $0x0, 0x48(%rsp)
movq %r15, %rdi
movl %ebp, %esi
leaq 0x48(%rsp), %rdx
callq 0x3430
cmpq $0x0, 0x48(%rsp)
jne 0x3e78
movl $0x7, %ecx
testq %rax, %rax
je 0x3e8f
movl %r13d, %eax
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
cmpq %rax, 0x8(%rdx)
setb %cl
sbbl $-0x1, %r13d
addl %ecx, %ecx
testl %ecx, %ecx
je 0x3e41
cmpl $0x7, %ecx
jne 0x3ec0
movl $0x28, %ecx
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x3070
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movl 0x20(%rsp), %ebp
addl %eax, %ebp
addl %ecx, %ecx
leaq 0x8(%rsp), %r13
jmp 0x3ec9
leaq 0x8(%rsp), %r13
movl 0x20(%rsp), %ebp
testl %ecx, %ecx
jne 0x3ee0
movl %ebp, %eax
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
cmpq %rax, 0x8(%rdx)
sete %cl
leal (%rcx,%rcx,2), %ecx
testl %ecx, %ecx
jne 0x3ef7
incl %r12d
movq 0x10(%rsp), %rbx
cmpq %r12, (%rbx)
ja 0x3e01
jmp 0x3f06
cmpl $0x2, %ecx
je 0x3f22
cmpl $0x3, %ecx
movq 0x10(%rsp), %rbx
jne 0x3f3b
movq 0x10(%rbx), %rdi
callq 0x3230
movq %r15, 0x10(%rbx)
movq 0x18(%rsp), %rax
movq %rax, (%rbx)
movl $0x1, %eax
jmp 0x3f2c
movq %r15, %rdi
callq 0x3230
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3f2c