// File: /workspace/github_c_makefile_star2_O3/gaultier[P]lox-c/hashtab.c
// Function: ht_insert
void *ht_insert(hashtab_t * hashtable,
		void *key, size_t keylen, void *value, size_t vallen)
{
    int index = ht_hash(key, keylen, hashtable->size);

    hashtab_node_t *next_node, *last_node;
    next_node = hashtable->arr[index];
    last_node = NULL;

    /* Search for an existing key. */
    while (next_node != NULL) {
	/* only compare matching keylens */
	if (next_node->keylen == keylen) {
	    /* compare keys */
	    if (memcmp(key, next_node->key, keylen) == 0) {
		/* this key already exists, replace it */
		if (next_node->vallen != vallen) {
		    /* new value is a different size */
		    free(next_node->value);
		    next_node->value = malloc(vallen);
		    if (next_node->value == NULL)
			return NULL;
		}
		memcpy(next_node->value, value, vallen);
		next_node->vallen = vallen;
		return next_node->value;
	    }
	}

	last_node = next_node;
	next_node = next_node->next;
    }

    /* create a new node */
    hashtab_node_t *new_node;
    new_node = (hashtab_node_t *) malloc(sizeof(hashtab_node_t));
    if (new_node == NULL)
	return NULL;

    /* get some memory for the new node data */
    new_node->key = malloc(keylen);
    new_node->value = malloc(vallen);
    if (new_node->key == NULL || new_node->key == NULL) {
	free(new_node->key);
	free(new_node->value);
	free(new_node);
	return NULL;
    }

    /* copy over the value and key */
    memcpy(new_node->key, key, keylen);
    memcpy(new_node->value, value, vallen);
    new_node->keylen = keylen;
    new_node->vallen = vallen;

    /* no next node */
    new_node->next = NULL;

    /* Tack the new node on the end or right on the table. */
    if (last_node != NULL)
	last_node->next = new_node;
    else
	hashtable->arr[index] = new_node;

    hashtable->count++;
    return new_node->value;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %rbp
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0x8(%rdi), %rcx
testl %r12d, %r12d
jle 0x1efb
movl %r12d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %eax, %eax
movzbl (%r13,%rsi), %edi
incq %rsi
movl %esi, %r8d
imull %edi, %r8d
addl %r8d, %eax
cmpq %rsi, %rdx
jne 0x1ee1
jmp 0x1efd
xorl %eax, %eax
cltd
idivl %ecx
movq (%r14), %rax
movslq %edx, %rcx
movq %rax, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rax
testq %rax, %rax
je 0x1f43
movq %rax, %r15
cmpq %r12, 0x8(%rax)
jne 0x1f38
movq (%r15), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x1180
testl %eax, %eax
je 0x1fed
movq 0x20(%r15), %rax
testq %rax, %rax
jne 0x1f19
jmp 0x1f46
xorl %r15d, %r15d
movq %r14, 0x18(%rsp)
movl $0x28, %edi
callq 0x1170
testq %rax, %rax
je 0x2035
movq %rax, %r14
movq %rbx, 0x10(%rsp)
movq %r12, %rdi
callq 0x1170
movq %rax, %rbx
movq %rax, (%r14)
movq %rbp, %rdi
callq 0x1170
movq %rbp, %rdx
movq %rax, %rbp
movq %rax, 0x10(%r14)
testq %rbx, %rbx
je 0x2025
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
movq %rdx, %rbx
movq %r12, %rdx
callq 0x1160
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1160
movq %r12, 0x8(%r14)
movq %rbx, 0x18(%r14)
movq $0x0, 0x20(%r14)
leaq 0x20(%r15), %rax
testq %r15, %r15
cmoveq 0x8(%rsp), %rax
movq %r14, (%rax)
movq 0x18(%rsp), %rax
incl 0x10(%rax)
jmp 0x2037
movq 0x10(%r15), %rdi
cmpq %rbp, 0x18(%r15)
je 0x2010
callq 0x1030
movq %rbp, %rdi
callq 0x1170
movq %rax, %rdi
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x2035
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1160
movq %rbp, 0x18(%r15)
movq 0x10(%r15), %rbp
jmp 0x2037
movq %rbp, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq