// File: /workspace/github_c_makefile_star2_O2/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, %r14d
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %rbx
movq (%rdi), %r15
movq %rsi, %rdi
callq 0x1060
movq %rax, %r13
addl %r14d, %eax
incl %eax
cmpl $0x7d0, %eax            # imm = 0x7D0
jb 0x2005
movq %rbx, %rdi
callq 0x1b2d
addq $0xc0, %r15
cmpl $0x3ff, %r14d           # imm = 0x3FF
ja 0x2116
cmpl $0xff, %r14d
ja 0x21d3
xorl %eax, %eax
cmpl $0x7f, %r14d
seta %al
jmp 0x21ec
movq %rbp, 0x18(%rsp)
incl %r13d
movq %r12, %rdi
callq *0x8(%rbx)
xorl %edx, %edx
divl 0x28(%r15)
movl %edx, %ebp
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x1b2d
movq %rbp, 0x20(%rsp)
shlq $0x4, %rbp
movq %r15, 0x8(%rsp)
addq %r15, %rbp
addq $0x150, %rbp            # imm = 0x150
movq %rbp, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x206d
addq %rbp, %rbx
je 0x206d
leaq 0x2c(%rbx), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x1080
testl %eax, %eax
je 0x2131
addq $0x10, %rbx
jmp 0x2043
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1b3d
movq %rbx, %rdi
callq 0x1ab7
testq %rax, %rax
je 0x2211
movq %rax, %r15
leaq 0x2c(%rax), %rdi
movq %r12, %rsi
callq 0x1040
movl %r13d, 0x24(%r15)
movl %r14d, 0x28(%r15)
movl %r13d, %eax
leaq (%r15,%rax), %rdi
addq $0x2c, %rdi
movl %r14d, %r12d
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x1090
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 0x1b4d
movq %rbx, %rdi
callq 0x1b2d
movq 0x8(%rsp), %rcx
addq $0x40, %rcx
cmpl $0x3ff, %r14d           # imm = 0x3FF
ja 0x2249
cmpl $0xff, %r14d
ja 0x2297
xorl %eax, %eax
cmpl $0x7f, %r14d
seta %al
jmp 0x22b0
cmpl $0xfff, %r14d           # imm = 0xFFF
ja 0x21de
cmpl $0x800, %r14d           # imm = 0x800
pushq $0x5
jmp 0x21e7
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq 0x8(%rsp), %r13
leaq 0x18(%r13), %rdx
leaq 0x10(%r13), %rsi
testq %rax, %rax
leaq 0x18(%rax,%r13), %rax
cmoveq %rdx, %rax
movq %rcx, (%rax)
movq (%rbx), %rax
movq %rax, 0x10(%rcx,%r13)
movq 0x10(%r13), %rax
movq %rax, (%rbx)
movq %rbx, %rcx
subq %rsi, %rcx
testq %rax, %rax
leaq 0x18(%rax,%r13), %rax
cmoveq %rdx, %rax
movq %rcx, (%rax)
movq %rcx, 0x10(%r13)
andq $0x0, 0x8(%rbx)
movl %r14d, 0x28(%rbx)
movl 0x24(%rbx), %edi
addq %r15, %rdi
movl %r14d, %r12d
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x1090
movl 0x24(%rbx), %eax
addq %r15, %rax
movb $0x0, (%r12,%rax)
subq $-0x80, %r13
cmpl $0x3ff, %r14d           # imm = 0x3FF
ja 0x21f8
cmpl $0xff, %r14d
movq 0x10(%rsp), %rbx
ja 0x225d
xorl %eax, %eax
cmpl $0x7f, %r14d
seta %al
jmp 0x2276
cmpl $0x200, %r14d           # imm = 0x200
pushq $0x3
jmp 0x21e7
cmpl $0x4000, %r14d          # imm = 0x4000
pushq $0x7
popq %rax
sbbq $0x0, %rax
leaq (%r15,%rax,8), %rax
pushq $-0x1
popq %rbp
jmp 0x22b6
cmpl $0xfff, %r14d           # imm = 0xFFF
movq 0x10(%rsp), %rbx
ja 0x2268
cmpl $0x800, %r14d           # imm = 0x800
pushq $0x5
jmp 0x2271
movq %rbx, %rdi
callq 0x1b2d
movq 0x8(%rsp), %rcx
addq $0xc0, %rcx
cmpl $0x3ff, %r14d           # imm = 0x3FF
ja 0x2283
cmpl $0xff, %r14d
ja 0x22d2
xorl %eax, %eax
cmpl $0x7f, %r14d
seta %al
jmp 0x22eb
cmpl $0xfff, %r14d           # imm = 0xFFF
ja 0x22a2
cmpl $0x800, %r14d           # imm = 0x800
pushq $0x5
jmp 0x22ab
cmpl $0x200, %r14d           # imm = 0x200
pushq $0x3
jmp 0x2271
cmpl $0x4000, %r14d          # imm = 0x4000
pushq $0x7
popq %rax
sbbq $0x0, %rax
leaq (,%rax,8), %rax
addq %r13, %rax
jmp 0x22b4
cmpl $0xfff, %r14d           # imm = 0xFFF
ja 0x22dd
cmpl $0x800, %r14d           # imm = 0x800
pushq $0x5
jmp 0x22e6
cmpl $0x200, %r14d           # imm = 0x200
pushq $0x3
jmp 0x22ab
cmpl $0x4000, %r14d          # imm = 0x4000
pushq $0x7
popq %rax
sbbq $0x0, %rax
leaq (%rcx,%rax,8), %rax
xorl %ebp, %ebp
incq (%rax)
movq %rbx, %rdi
callq 0x1b3d
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x200, %r14d           # imm = 0x200
pushq $0x3
jmp 0x22e6
cmpl $0x4000, %r14d          # imm = 0x4000
pushq $0x7
popq %rax
sbbq $0x0, %rax
leaq (%rcx,%rax,8), %rax
jmp 0x21f0