// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %r8, %rbp
movq %rcx, (%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movq 0x8(%rdi), %rdx
movq %rsi, %rdi
movq %r12, %rsi
callq 0x136e
movslq %eax, %r15
shlq $0x3, %r15
movq %rbx, 0x10(%rsp)
addq (%rbx), %r15
xorl %ebx, %ebx
movq %r15, %rax
movq (%rax), %r14
testq %r14, %r14
je 0x146f
cmpq %r12, 0x8(%r14)
jne 0x1466
movq (%r14), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x1180
testl %eax, %eax
je 0x14f1
leaq 0x20(%r14), %rax
movq %r14, %rbx
jmp 0x1442
pushq $0x28
popq %rdi
callq 0x1170
testq %rax, %rax
je 0x153a
movq %rax, %r14
movq %r12, %rdi
callq 0x1170
movq %rax, 0x8(%rsp)
movq %rax, (%r14)
movq %rbp, %rdi
callq 0x1170
movq %rbp, %rcx
movq %rax, %rbp
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%r14)
testq %rdi, %rdi
je 0x152a
movq %r13, %rsi
movq %rcx, %r13
movq %r12, %rdx
callq 0x1160
movq %rbp, %rdi
movq (%rsp), %rsi
movq %r13, %rdx
callq 0x1160
movq %r12, 0x8(%r14)
movq %r13, 0x18(%r14)
andq $0x0, 0x20(%r14)
leaq 0x20(%rbx), %rax
testq %rbx, %rbx
cmoveq %r15, %rax
movq %r14, (%rax)
movq 0x10(%rsp), %rax
incl 0x10(%rax)
jmp 0x153c
movq 0x10(%r14), %rdi
cmpq %rbp, 0x18(%r14)
je 0x1514
callq 0x1030
movq %rbp, %rdi
callq 0x1170
movq %rax, %rdi
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0x153a
movq (%rsp), %rsi
movq %rbp, %rdx
callq 0x1160
movq %rbp, 0x18(%r14)
movq 0x10(%r14), %rbp
jmp 0x153c
movq %rbp, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq