// File: /workspace/github_c_makefile_star2_O0/nmmmnu[P]HM2/hm_skiplist.c
// Function: hm_skiplist_put
int hm_skiplist_put(hm_skiplist_t *l, hm_pair_t *newdata)
{
	if (newdata == NULL)
		return 0;

	const char *key = hm_pair_getkey(newdata);

	hm_skiplist_node_t *node = (hm_skiplist_node_t *) _hm_skiplist_locate(l, key, 0);

	if (node){
		// update in place. node MUST be not NULL...

		hm_pair_t *olddata = node->data;

		// check if the data in database is valid
		if (! hm_pair_valid(newdata, olddata)){
			// prevent memory leak
			hm_pair_free(newdata);
			return 0;
		}

		// leave node, update only the data
		node->data = newdata;

		l->datasize = l->datasize
			- hm_pair_sizeof(olddata)
			+ hm_pair_sizeof(newdata);

		// release old data
		hm_pair_free(olddata);

		return 1;
	}

	// create new node

	unsigned char height = _hm_skiplist_height(l);

	hm_skiplist_node_t *newnode = malloc(sizeof(hm_skiplist_node_t) + height * sizeof(void *) );
	if (newnode == NULL){
		// prevent memory leak
		hm_pair_free(newdata);
		return 0;
	}

	newnode->height = height;
	newnode->data = newdata;

	// place new node where it belongs

	hm_skiplist_height_t i;
	for(i = 0; i < height; ++i){
		if (l->loc[i]){
			// we are at the middle
			hm_skiplist_node_t *node = l->loc[i];

			newnode->next[i] = node->next[i];
			node->next[i] = newnode;
		}else{
			newnode->next[i] = l->heads[i];
			l->heads[i] = newnode;
		}
	}

	l->datasize += hm_pair_sizeof(newdata);
	l->datacount++;

	return 1;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
jne 0x2d43
movl $0x0, 0x64(%rsp)
jmp 0x2f61
movq 0x50(%rsp), %rdi
callq 0x2190
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %rsi
xorl %edx, %edx
callq 0x2f70
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0x2e11
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x2290
cmpl $0x0, %eax
jne 0x2dac
movq 0x50(%rsp), %rdi
callq 0x2120
movl $0x0, 0x64(%rsp)
jmp 0x2f61
movq 0x50(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, (%rax)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rdi
callq 0x2170
movq %rax, %rcx
movq 0x8(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x50(%rsp), %rdi
callq 0x2170
movq 0x10(%rsp), %rcx
addq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, (%rax)
movq 0x38(%rsp), %rdi
callq 0x2120
movl $0x1, 0x64(%rsp)
jmp 0x2f61
movq 0x58(%rsp), %rdi
callq 0x30e0
movb %al, 0x37(%rsp)
movzbl 0x37(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
addq $0x10, %rdi
callq 0x1120
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x2e57
movq 0x50(%rsp), %rdi
callq 0x2120
movl $0x0, 0x64(%rsp)
jmp 0x2f61
movb 0x37(%rsp), %cl
movq 0x28(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x50(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, (%rax)
movb $0x0, 0x27(%rsp)
movzbl 0x27(%rsp), %eax
movzbl 0x37(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2f30
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movzbl 0x27(%rsp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x2ee7
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
movzbl 0x27(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movzbl 0x27(%rsp), %ecx
movq 0x10(%rax,%rcx,8), %rdx
movq 0x28(%rsp), %rax
movzbl 0x27(%rsp), %ecx
movq %rdx, 0x10(%rax,%rcx,8)
movq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rax
movzbl 0x27(%rsp), %ecx
movq %rdx, 0x10(%rax,%rcx,8)
jmp 0x2f1f
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rax
movzbl 0x27(%rsp), %ecx
movq (%rax,%rcx,8), %rdx
movq 0x28(%rsp), %rax
movzbl 0x27(%rsp), %ecx
movq %rdx, 0x10(%rax,%rcx,8)
movq 0x28(%rsp), %rdx
movq 0x58(%rsp), %rax
movq 0x18(%rax), %rax
movzbl 0x27(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
jmp 0x2f21
movb 0x27(%rsp), %al
addb $0x1, %al
movb %al, 0x27(%rsp)
jmp 0x2e75
movq 0x50(%rsp), %rdi
callq 0x2170
movq %rax, %rcx
movq 0x58(%rsp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw (%rax,%rax)