// File: /workspace/github_c_makefile_star2_O1/ryo[P]shmqueue/shmqueue.c
// Function: shmqueue_create
int
shmqueue_create(struct shmqueue *shmq_header, void *mem, size_t size)
{
	struct shmqueue_header *header;
	void *memend;
	struct shmqueue_item *itempool;
	size_t headersize, freespace;

	memend = (char *)mem + size;

	header = (struct shmqueue_header *)mem;
	headersize = sizeof(struct shmqueue_header);	/* temporary */
#ifdef SHMQUEUE_HASHNUM
	header->sha_hashnum = SHMQUEUE_HASHNUM;
#else
	freespace = size - headersize;
	header->sha_hashnum = freespace / sizeof(struct shmqueue_item);
#endif
	headersize += header->sha_hashnum * sizeof(header->sha_hashlist[0]);

	itempool = (struct shmqueue_item *)SHMQUEUE_ALIGN(
	    (char *)mem + headersize, SHMQUEUE_ALIGNSIZE);

	if ((char *)memend < (char *)itempool) {
		printf("cannot allocate hash table\n");
		return -1;
	}

	freespace = (char *)memend - (char *)itempool;

	header->sha_memsize = size;
	header->sha_itemsize = sizeof(struct shmqueue_item);

	if (sizeof(struct shmqueue_item) > SHMQUEUE_ITEMSIZE) {
		fprintf(stderr, "panic: sizeof(struct shmqueue_item) too large\n");
		exit(99);
	}

	header->sha_itemnum = freespace / sizeof(struct shmqueue_item);
	header->sha_pool = itempool;

	shmq_header->header = header;
	shmq_header->hash_callback = shmqueue_hash_string;

	shmqueue_lockinit(shmq_header);

	shmqueue_init(header);

	printf("=============================================\n");
	printf("sizeof header = %d\n", (int)sizeof(struct shmqueue_header));
	printf("\n");
	printf("header              = %p\n", header);
	printf("size                = %llu\n", (unsigned long long)size);
	printf("headersize          = %d\n", (int)sizeof(struct shmqueue_header));
	printf("hashentrysize       = %d (%.1g%%)\n",
	    (int)sizeof(struct sha_hashlist) * header->sha_hashnum,
	    (sizeof(struct sha_hashlist) * header->sha_hashnum) * 100.0 / size);
	printf("freespace           = %llu\n", (unsigned long long)freespace);
	printf("header->sha_itemsize= %u\n", header->sha_itemsize);
	printf("header->sha_itemnum = %u\n", header->sha_itemnum);
	printf("header->sha_hashnum = %u\n", header->sha_hashnum);
	printf("header->sha_pool    = %p\n", header->sha_pool);
	printf("=============================================\n");

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
leaq (%rsi,%rdx), %r15
movl $0x30d3f, 0x28(%rsi)    # imm = 0x30D3F
leaq 0x30e53f(%rsi), %rax
andq $-0x1000, %rax          # imm = 0xF000
subq %rax, %r15
jae 0x1958
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0x1b39(%rip), %r14      # 0x348c
jmp 0x1a99
movq %rdx, %r12
movq %rsi, %rbx
movq %rdx, 0x20(%rsi)
movl $0x800, 0x2c(%rsi)      # imm = 0x800
movq %r15, %rcx
shrq $0xb, %rcx
movl %ecx, 0x30(%rsi)
movq %rax, 0x38(%rsi)
movq %rsi, (%rdi)
leaq -0x69d(%rip), %rax      # 0x12e4
movq %rax, 0x8(%rdi)
leaq 0x148(%rsi), %rdi
callq 0x2690
movq %rbx, %rdi
callq 0x1abd
leaq 0x1abe(%rip), %r14      # 0x345e
movq %r14, %rdi
callq 0x1050
leaq 0x1948(%rip), %rdi      # 0x32f7
xorl %ebp, %ebp
movl $0x150, %esi            # imm = 0x150
xorl %eax, %eax
callq 0x1070
movl $0xa, %edi
callq 0x1030
leaq 0x193d(%rip), %rdi      # 0x330b
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1070
leaq 0x1946(%rip), %rdi      # 0x3325
movq %r12, %rsi
xorl %eax, %eax
callq 0x1070
leaq 0x1951(%rip), %rdi      # 0x3341
movl $0x150, %esi            # imm = 0x150
xorl %eax, %eax
callq 0x1070
movl 0x28(%rbx), %eax
imulq $0x640, %rax, %rcx      # imm = 0x640
movl %eax, %esi
shll $0x4, %esi
cvtsi2sd %rcx, %xmm0
movq %r12, %xmm1
punpckldq 0x1613(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x161b(%rip), %xmm1     # 0x3040
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x191f(%rip), %rdi      # 0x335b
movb $0x1, %al
callq 0x1070
leaq 0x1934(%rip), %rdi      # 0x337e
movq %r15, %rsi
xorl %eax, %eax
callq 0x1070
movl 0x2c(%rbx), %esi
leaq 0x193c(%rip), %rdi      # 0x339a
xorl %eax, %eax
callq 0x1070
movl 0x30(%rbx), %esi
leaq 0x1945(%rip), %rdi      # 0x33b4
xorl %eax, %eax
callq 0x1070
movl 0x28(%rbx), %esi
leaq 0x194e(%rip), %rdi      # 0x33ce
xorl %eax, %eax
callq 0x1070
movq 0x38(%rbx), %rsi
leaq 0x1956(%rip), %rdi      # 0x33e8
xorl %eax, %eax
callq 0x1070
movq %r14, %rdi
callq 0x1050
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq