// File: /workspace/github_c_makefile_star2_O2/ryo[P]shmqueue/shmqueue.c
// Function: shmqueue_keyvalue_fetch
struct keyvalue *
shmqueue_keyvalue_fetch(struct shmqueue *shmq_header, uint8_t *key, struct keyvalue *keyvalue)
{
	struct shmqueue_header *header;
	struct shmqueue_item *item;
	uint32_t hashidx;

	header = shmq_header->header;
	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) {
			/* found! */

			/*
			 * 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 to caller */
			keyvalue->kv_keysize = item->shi_keyvalue.kv_keysize;
			keyvalue->kv_storagesize = item->shi_keyvalue.kv_storagesize;
			memcpy(keyvalue->kv_data, item->shi_keyvalue.kv_data, keyvalue->kv_keysize + keyvalue->kv_storagesize);
			KEYVALUE_STORAGE(keyvalue)[keyvalue->kv_storagesize] = '\0';

			/* update statistics */
			header->sha_stat.ss_fetch_success[SIZE2BUCKET(keyvalue->kv_storagesize)]++;

			shmqueue_unlock(shmq_header);
			return keyvalue;
		}
	}

	/* update statistics */
	header->sha_stat.ss_fetch_failure++;

	shmqueue_unlock(shmq_header);
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %r14
movq (%rdi), %r13
movq %rsi, %rdi
callq *0x8(%r14)
xorl %edx, %edx
divl 0x28(%r13)
movl %edx, %r12d
movq %r14, %rdi
callq 0x1b2d
shlq $0x4, %r12
leaq (%r12,%r13), %rbx
addq $0x150, %rbx            # imm = 0x150
movq %rbx, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x1e8f
addq %rbx, %rbp
je 0x1e8f
leaq 0x2c(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
je 0x1e9d
addq $0x10, %rbp
jmp 0x1e68
addq $0x140, %r13            # imm = 0x140
xorl %ebx, %ebx
jmp 0x1f7c
movq %r14, 0x8(%rsp)
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
leaq 0x18(%r13), %rdx
leaq 0x10(%r13), %rsi
testq %rax, %rax
leaq 0x18(%rax,%r13), %rax
cmoveq %rdx, %rax
movq %rcx, (%rax)
movq (%rbp), %rax
movq %rax, 0x10(%rcx,%r13)
movq 0x10(%r13), %rax
movq %rax, (%rbp)
movq %rbp, %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(%rbp)
movl 0x24(%rbp), %r14d
movq 0x10(%rsp), %rbx
movl %r14d, (%rbx)
movl 0x28(%rbp), %r15d
movl %r15d, 0x4(%rbx)
leaq 0x8(%rbx), %rdi
leal (%r15,%r14), %edx
movq %r12, %rsi
callq 0x1090
addq $0x100, %r13            # imm = 0x100
leaq (%rbx,%r14), %rax
addq $0x8, %rax
movb $0x0, (%r15,%rax)
cmpq $0x3ff, %r15            # imm = 0x3FF
ja 0x1f45
cmpl $0xff, %r15d
ja 0x1f59
xorl %eax, %eax
cmpl $0x7f, %r15d
seta %al
jmp 0x1f72
cmpl $0xfff, %r15d           # imm = 0xFFF
ja 0x1f64
cmpl $0x800, %r15d           # imm = 0x800
pushq $0x5
jmp 0x1f6d
cmpl $0x200, %r15d           # imm = 0x200
pushq $0x3
jmp 0x1f6d
cmpl $0x4000, %r15d          # imm = 0x4000
pushq $0x7
popq %rax
sbbq $0x0, %rax
movq 0x8(%rsp), %r14
leaq (%r13,%rax,8), %r13
incq (%r13)
movq %r14, %rdi
callq 0x1b3d
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq