// File: /workspace/github_c_makefile_star2_O1/ryo[P]shmqueue/shmqueue.c
// Function: shmqueue_keyvalue_store
int
shmqueue_keyvalue_store(struct shmqueue *shmq_header, uint8_t *key, uint8_t *storage, uint32_t storage_size)
{
	struct shmqueue_header *header;
	struct shmqueue_item *item;
	uint32_t hashidx;
	int keylen;

	header = shmq_header->header;

	keylen = strlen((char *)key) + 1;
	if (storage_size + keylen >= SHMQUEUE_KEYVALUE_DATASIZE) {
		/* update statistics */
		shmqueue_lock(shmq_header);
		header->sha_stat.ss_store_failure[SIZE2BUCKET(storage_size)]++;
		shmqueue_unlock(shmq_header);

		return -1;
	}

	hashidx = shmq_header->hash_callback(key) % header->sha_hashnum;

	shmqueue_lock(shmq_header);
	for (item = RTAILQ_FIRST(&header->sha_hashlist[hashidx], shmqueue_item);
	    item != NULL;
	    item = RTAILQ_NEXT(item, &header->sha_hashlist[hashidx], shmqueue_item, shi_hashlist)) {

		if (SHMQUEUE_STRCMP(KEYVALUE_KEY(&item->shi_keyvalue), key) == 0) {
			/* already exists! */

			/*
			 * move to top of LRU.
			 * delete from LRU, and insert to top
			 */
			RTAILQ_REMOVE(&header->sha_lrulist,
			    item, shmqueue_item, shi_list);
			RTAILQ_INSERT_HEAD(&header->sha_lrulist,
			    item, shmqueue_item, shi_list);

			/* copy from caller */
			item->shi_keyvalue.kv_storagesize = storage_size;
			memcpy(KEYVALUE_STORAGE(&item->shi_keyvalue), storage, storage_size);
			KEYVALUE_STORAGE(&item->shi_keyvalue)[storage_size] = '\0';

			/* update statistics */
			header->sha_stat.ss_store_reuse_success[SIZE2BUCKET(storage_size)]++;

			shmqueue_unlock(shmq_header);
			return 0;
		}
	}
	/* not found */
	shmqueue_unlock(shmq_header);


	/* allocate pool */
	item = shmqueue_remove_from_freelist(shmq_header);
	if (item == NULL) {
		/* update statistics */
		shmqueue_lock(shmq_header);
		header->sha_stat.ss_store_failure[SIZE2BUCKET(storage_size)]++;
		shmqueue_unlock(shmq_header);

		return -1;
	}

	/* update data, and store */
	SHMQUEUE_STRCPY(KEYVALUE_KEY(&item->shi_keyvalue), key);
	item->shi_keyvalue.kv_keysize = keylen;
	item->shi_keyvalue.kv_storagesize = storage_size;
	memcpy(KEYVALUE_STORAGE(&item->shi_keyvalue), storage, storage_size);
	KEYVALUE_STORAGE(&item->shi_keyvalue)[storage_size] = '\0';

	shmqueue_add_to_lru(shmq_header, item, hashidx);

	/* update statistics */
	shmqueue_lock(shmq_header);
	header->sha_stat.ss_store_new_success[SIZE2BUCKET(storage_size)]++;
	shmqueue_unlock(shmq_header);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r15d
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
movq (%rdi), %r14
movq %rsi, %rdi
callq 0x1060
movq %rax, %rbp
addl %r15d, %eax
incl %eax
cmpl $0x7d0, %eax            # imm = 0x7D0
jb 0x211b
leaq 0x148(%r14), %rdi
callq 0x2694
addq $0xc0, %r14
cmpl $0x3ff, %r15d           # imm = 0x3FF
ja 0x2252
cmpl $0xff, %r15d
ja 0x2362
xorl %eax, %eax
cmpl $0x7f, %r15d
seta %al
jmp 0x2380
movq %r13, 0x18(%rsp)
movq %r15, (%rsp)
movq %r12, %rdi
callq *0x8(%rbx)
xorl %edx, %edx
divl 0x28(%r14)
movl %edx, %r15d
movl $0x148, %r13d           # imm = 0x148
movq %rbx, 0x10(%rsp)
movq (%rbx), %rdi
addq %r13, %rdi
callq 0x2694
movq %r15, 0x20(%rsp)
shlq $0x4, %r15
movq %r14, 0x8(%rsp)
addq %r15, %r14
addq $0x150, %r14            # imm = 0x150
movq (%r14), %rbx
testq %rbx, %rbx
sete %al
addq %r14, %rbx
sete %cl
orb %al, %cl
jne 0x219f
leaq 0x2c(%rbx), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x1090
testl %eax, %eax
je 0x22b2
movq 0x10(%rbx), %rax
leaq (%rax,%r14), %rbx
testq %rax, %rax
cmoveq %rax, %rbx
testq %rbx, %rbx
jne 0x2174
movq 0x10(%rsp), %rbx
addq (%rbx), %r13
movq %r13, %rdi
callq 0x26b4
movq %rbx, %rdi
callq 0x1b42
testq %rax, %rax
je 0x2270
movq %rax, %r15
incl %ebp
leaq 0x2c(%rax), %rdi
movq %r12, %rsi
callq 0x1040
movl %ebp, 0x24(%r15)
movq (%rsp), %r14
movl %r14d, 0x28(%r15)
movl %ebp, %eax
leaq 0x2c(%r15,%rax), %rdi
movl %r14d, %r12d
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x10a0
movl 0x24(%r15), %eax
addq %r15, %rax
addq $0x2c, %rax
movb $0x0, (%r12,%rax)
movq %rbx, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %rdx
callq 0x1bcd
movl $0x148, %edi            # imm = 0x148
addq (%rbx), %rdi
callq 0x2694
movq 0x8(%rsp), %rcx
addq $0x40, %rcx
cmpl $0x3ff, %r14d           # imm = 0x3FF
ja 0x2389
cmpl $0xff, %r14d
ja 0x23d1
xorl %eax, %eax
cmpl $0x7f, %r14d
seta %al
jmp 0x23ef
cmpl $0xfff, %r15d           # imm = 0xFFF
ja 0x2370
cmpl $0x800, %r15d           # imm = 0x800
movl $0x5, %eax
jmp 0x237c
movl $0x148, %edi            # imm = 0x148
addq (%rbx), %rdi
callq 0x2694
movq 0x8(%rsp), %rcx
addq $0xc0, %rcx
movq (%rsp), %rdx
cmpl $0x3ff, %edx            # imm = 0x3FF
ja 0x23a0
cmpl $0xff, %edx
ja 0x23f5
xorl %eax, %eax
cmpl $0x7f, %edx
seta %al
jmp 0x2411
movq (%rbx), %rdx
movq 0x8(%rbx), %rsi
movq 0x8(%rsp), %r14
leaq 0x18(%r14), %rax
leaq 0x10(%r14), %rcx
testq %rdx, %rdx
leaq 0x18(%rdx,%r14), %rdx
cmoveq %rax, %rdx
movq %rsi, (%rdx)
movq (%rbx), %rdx
movq 0x8(%rbx), %rsi
movq %rdx, 0x10(%rsi,%r14)
movq 0x10(%r14), %rsi
movq %rsi, (%rbx)
movq %rbx, %rdx
subq %rcx, %rdx
testq %rsi, %rsi
je 0x22fd
movq (%rcx), %rax
addq %rcx, %rax
addq $0x8, %rax
movq (%rsp), %r13
movq %rdx, (%rax)
movq %rdx, 0x10(%r14)
movq $0x0, 0x8(%rbx)
movl %r13d, 0x28(%rbx)
movl 0x24(%rbx), %edi
addq %r15, %rdi
movl %r13d, %r12d
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x10a0
movl 0x24(%rbx), %eax
addq %r15, %rax
movb $0x0, (%r12,%rax)
subq $-0x80, %r14
cmpl $0x3ff, %r13d           # imm = 0x3FF
ja 0x23b5
cmpl $0xff, %r13d
movq 0x10(%rsp), %rbx
ja 0x241c
xorl %eax, %eax
cmpl $0x7f, %r13d
seta %al
jmp 0x243a
cmpl $0x200, %r15d           # imm = 0x200
movl $0x3, %eax
jmp 0x237c
cmpl $0x4000, %r15d          # imm = 0x4000
movl $0x7, %eax
sbbq $0x0, %rax
leaq (%r14,%rax,8), %rax
jmp 0x2415
cmpl $0xfff, %r14d           # imm = 0xFFF
ja 0x23df
cmpl $0x800, %r14d           # imm = 0x800
movl $0x5, %eax
jmp 0x23eb
cmpl $0xfff, %edx            # imm = 0xFFF
ja 0x2402
cmpl $0x800, %edx            # imm = 0x800
movl $0x5, %eax
jmp 0x240d
cmpl $0xfff, %r13d           # imm = 0xFFF
movq 0x10(%rsp), %rbx
ja 0x242a
cmpl $0x800, %r13d           # imm = 0x800
movl $0x5, %eax
jmp 0x2436
cmpl $0x200, %r14d           # imm = 0x200
movl $0x3, %eax
jmp 0x23eb
cmpl $0x4000, %r14d          # imm = 0x4000
movl $0x7, %eax
sbbq $0x0, %rax
leaq (%rcx,%rax,8), %rax
jmp 0x243e
cmpl $0x200, %edx            # imm = 0x200
movl $0x3, %eax
jmp 0x240d
cmpl $0x4000, %edx           # imm = 0x4000
movl $0x7, %eax
sbbq $0x0, %rax
leaq (%rcx,%rax,8), %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2440
cmpl $0x200, %r13d           # imm = 0x200
movl $0x3, %eax
jmp 0x2436
cmpl $0x4000, %r13d          # imm = 0x4000
movl $0x7, %eax
sbbq $0x0, %rax
leaq (%r14,%rax,8), %rax
xorl %ebp, %ebp
incq (%rax)
movl $0x148, %edi            # imm = 0x148
addq (%rbx), %rdi
callq 0x26b4
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq