// File: /workspace/github_c_makefile_star2_O3/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 0x20e6
leaq 0x148(%r14), %rdi
callq 0x2648
addq $0xc0, %r14
cmpl $0x3ff, %r15d           # imm = 0x3FF
ja 0x2220
cmpl $0xff, %r15d
ja 0x2329
xorl %eax, %eax
cmpl $0x7f, %r15d
seta %al
jmp 0x2347
movq %r13, 0x18(%rsp)
movq %r15, (%rsp)
movq %r12, %rdi
callq *0x8(%rbx)
xorl %edx, %edx
divl 0x28(%r14)
movl %edx, %r13d
movl $0x148, %r15d           # imm = 0x148
movq %rbx, 0x10(%rsp)
movq (%rbx), %rdi
addq %r15, %rdi
callq 0x2648
movq %r13, 0x20(%rsp)
shlq $0x4, %r13
movq %r14, 0x8(%rsp)
addq %r13, %r14
addq $0x150, %r14            # imm = 0x150
movq (%r14), %rbx
testq %rbx, %rbx
sete %al
addq %r14, %rbx
sete %cl
orb %al, %cl
je 0x21f6
movq 0x10(%rsp), %rbx
addq (%rbx), %r15
movq %r15, %rdi
callq 0x2668
movq %rbx, %rdi
callq 0x1b26
testq %rax, %rax
je 0x223e
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 0x1bad
movl $0x148, %edi            # imm = 0x148
addq (%rbx), %rdi
callq 0x2648
movq 0x8(%rsp), %rcx
addq $0x40, %rcx
cmpl $0x3ff, %r14d           # imm = 0x3FF
ja 0x2350
cmpl $0xff, %r14d
ja 0x2396
xorl %eax, %eax
cmpl $0x7f, %r14d
seta %al
jmp 0x23b4
leaq 0x2c(%rbx), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x1090
testl %eax, %eax
je 0x2280
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
je 0x2143
addq %r14, %rbx
jne 0x21f6
jmp 0x2143
cmpl $0xfff, %r15d           # imm = 0xFFF
ja 0x2337
cmpl $0x800, %r15d           # imm = 0x800
movl $0x5, %eax
jmp 0x2343
movl $0x148, %edi            # imm = 0x148
addq (%rbx), %rdi
callq 0x2648
movq 0x8(%rsp), %rcx
addq $0xc0, %rcx
movq (%rsp), %rdx
cmpl $0x3ff, %edx            # imm = 0x3FF
ja 0x2367
cmpl $0xff, %edx
ja 0x23ba
xorl %eax, %eax
cmpl $0x7f, %edx
seta %al
jmp 0x23d6
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq 0x8(%rsp), %r14
leaq 0x18(%r14), %rdx
leaq 0x10(%r14), %rsi
testq %rax, %rax
leaq 0x18(%rax,%r14), %rax
cmoveq %rdx, %rax
movq %rcx, (%rax)
movq (%rbx), %rax
movq %rax, 0x10(%rcx,%r14)
movq 0x10(%r14), %rax
movq %rbx, %rcx
subq %rsi, %rcx
testq %rax, %rax
leaq 0x18(%rax,%r14), %rsi
cmoveq %rdx, %rsi
movq %rax, (%rbx)
movq %rcx, (%rsi)
movq %rcx, 0x10(%r14)
movq $0x0, 0x8(%rbx)
movq (%rsp), %r12
movl %r12d, 0x28(%rbx)
movl 0x24(%rbx), %edi
addq %r13, %rdi
movl %r12d, %r15d
movq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x10a0
movl 0x24(%rbx), %eax
addq %r13, %rax
movq %r12, %rcx
movb $0x0, (%r15,%rax)
subq $-0x80, %r14
cmpl $0x3ff, %ecx            # imm = 0x3FF
ja 0x237c
cmpl $0xff, %ecx
movq 0x10(%rsp), %rbx
ja 0x23e1
xorl %eax, %eax
cmpl $0x7f, %ecx
seta %al
jmp 0x23fd
cmpl $0x200, %r15d           # imm = 0x200
movl $0x3, %eax
jmp 0x2343
cmpl $0x4000, %r15d          # imm = 0x4000
movl $0x7, %eax
sbbq $0x0, %rax
leaq (%r14,%rax,8), %rax
jmp 0x23da
cmpl $0xfff, %r14d           # imm = 0xFFF
ja 0x23a4
cmpl $0x800, %r14d           # imm = 0x800
movl $0x5, %eax
jmp 0x23b0
cmpl $0xfff, %edx            # imm = 0xFFF
ja 0x23c7
cmpl $0x800, %edx            # imm = 0x800
movl $0x5, %eax
jmp 0x23d2
cmpl $0xfff, %ecx            # imm = 0xFFF
movq 0x10(%rsp), %rbx
ja 0x23ee
cmpl $0x800, %ecx            # imm = 0x800
movl $0x5, %eax
jmp 0x23f9
cmpl $0x200, %r14d           # imm = 0x200
movl $0x3, %eax
jmp 0x23b0
cmpl $0x4000, %r14d          # imm = 0x4000
movl $0x7, %eax
sbbq $0x0, %rax
leaq (%rcx,%rax,8), %rax
jmp 0x2401
cmpl $0x200, %edx            # imm = 0x200
movl $0x3, %eax
jmp 0x23d2
cmpl $0x4000, %edx           # imm = 0x4000
movl $0x7, %eax
sbbq $0x0, %rax
leaq (%rcx,%rax,8), %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x2403
cmpl $0x200, %ecx            # imm = 0x200
movl $0x3, %eax
jmp 0x23f9
cmpl $0x4000, %ecx           # 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 0x2668
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq