// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movq 0x8(%rdi), %rcx
testl %r12d, %r12d
jle 0x1f4b
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 0x1f31
jmp 0x1f4d
xorl %eax, %eax
cltd
idivl %ecx
movq (%rbp), %rax
movslq %edx, %rcx
movq %rax, 0x8(%rsp)
movq %rcx, 0x20(%rsp)
movq (%rax,%rcx,8), %rax
testq %rax, %rax
je 0x1f94
movq %rax, %r15
cmpq %r12, 0x8(%rax)
jne 0x1f89
movq (%r15), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x1180
testl %eax, %eax
je 0x2043
movq 0x20(%r15), %rax
testq %rax, %rax
jne 0x1f6a
jmp 0x1f97
xorl %r15d, %r15d
movq %rbp, 0x18(%rsp)
movl $0x28, %edi
callq 0x1170
testq %rax, %rax
je 0x2094
movq %rax, %rbp
movq %rbx, 0x10(%rsp)
movq %r12, %rdi
callq 0x1170
movq %rax, %rbx
movq %rax, (%rbp)
movq %r14, %rdi
callq 0x1170
movq %r14, %rdx
movq %rax, %r14
movq %rax, 0x10(%rbp)
testq %rbx, %rbx
je 0x207c
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 %r14, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x1160
movq %r12, 0x8(%rbp)
movq %rbx, 0x18(%rbp)
movq $0x0, 0x20(%rbp)
leaq 0x20(%r15), %rax
testq %r15, %r15
cmoveq 0x8(%rsp), %rax
movq %rbp, (%rax)
movq 0x18(%rsp), %rax
incl 0x10(%rax)
movq 0x10(%rbp), %rax
jmp 0x2096
cmpq %r14, 0x18(%r15)
je 0x2063
movq 0x10(%r15), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1170
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x2094
movq 0x10(%r15), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1160
movq %r14, 0x18(%r15)
movq 0x10(%r15), %rax
jmp 0x2096
movq %rbx, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq