// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdx
callq 0x13d0
movl %eax, 0x24(%rsp)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
cmpq $0x0, 0x18(%rsp)
je 0x1650
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
cmpq 0x38(%rsp), %rax
jne 0x1633
movq 0x40(%rsp), %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x38(%rsp), %rdx
callq 0x1110
cmpl $0x0, %eax
jne 0x1631
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
cmpq 0x28(%rsp), %rax
je 0x15f8
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1170
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x15f6
movq $0x0, 0x50(%rsp)
jmp 0x178b
jmp 0x15f8
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x1160
movq 0x28(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x50(%rsp)
jmp 0x178b
jmp 0x1633
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x1568
movl $0x28, %edi
callq 0x1170
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x1675
movq $0x0, 0x50(%rsp)
jmp 0x178b
movq 0x38(%rsp), %rdi
callq 0x1170
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
movq 0x28(%rsp), %rdi
callq 0x1170
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax)
je 0x16b6
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x16e9
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x1030
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x1030
movq 0x8(%rsp), %rdi
callq 0x1030
movq $0x0, 0x50(%rsp)
jmp 0x178b
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x1160
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x1160
movq 0x38(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x8(%rsp), %rax
movq $0x0, 0x20(%rax)
cmpq $0x0, 0x10(%rsp)
je 0x1759
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x20(%rax)
jmp 0x176f
movq 0x8(%rsp), %rdx
movq 0x48(%rsp), %rax
movq (%rax), %rax
movslq 0x24(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x48(%rsp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)