// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq $0x150, -0x40(%rbp)     # imm = 0x150
movq -0x28(%rbp), %rax
movl $0x30d3f, 0x28(%rax)    # imm = 0x30D3F
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
shlq $0x4, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
addq -0x40(%rbp), %rax
addq $0x1000, %rax           # imm = 0x1000
subq $0x1, %rax
andq $-0x1000, %rax          # imm = 0xF000
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x1eae
leaq 0x24af(%rip), %rdi      # 0x434a
movb $0x0, %al
callq 0x1050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2062
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movl $0x800, 0x2c(%rax)      # imm = 0x800
movq -0x48(%rbp), %rax
shrq $0xb, %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
leaq -0xbc7(%rip), %rcx      # 0x1340
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rdi
callq 0x20a0
movq -0x28(%rbp), %rdi
callq 0x20d0
leaq 0x2442(%rip), %rdi      # 0x4366
xorl %eax, %eax
movb %al, -0x49(%rbp)
callq 0x1050
movb -0x49(%rbp), %al
leaq 0x245d(%rip), %rdi      # 0x4395
movl $0x150, %esi            # imm = 0x150
movl %esi, -0x50(%rbp)
callq 0x1050
movb -0x49(%rbp), %al
leaq 0x21bb(%rip), %rdi      # 0x410a
callq 0x1050
movb -0x49(%rbp), %al
movq -0x28(%rbp), %rsi
leaq 0x2447(%rip), %rdi      # 0x43a9
callq 0x1050
movb -0x49(%rbp), %al
movq -0x20(%rbp), %rsi
leaq 0x244e(%rip), %rdi      # 0x43c3
callq 0x1050
movl -0x50(%rbp), %esi
movb -0x49(%rbp), %al
leaq 0x2458(%rip), %rdi      # 0x43df
callq 0x1050
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %esi
shll $0x4, %esi
shlq $0x4, %rax
cvtsi2sd %rax, %xmm0
movsd 0x207f(%rip), %xmm1     # 0x4028
mulsd %xmm1, %xmm0
movaps 0x207c(%rip), %xmm2     # 0x4030
movq -0x20(%rbp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x207b(%rip), %xmm2     # 0x4040
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0x241a(%rip), %rdi      # 0x43f9
movb $0x1, %al
callq 0x1050
movq -0x48(%rbp), %rsi
leaq 0x242b(%rip), %rdi      # 0x441c
movb $0x0, %al
callq 0x1050
movq -0x28(%rbp), %rax
movl 0x2c(%rax), %esi
leaq 0x2432(%rip), %rdi      # 0x4438
movb $0x0, %al
callq 0x1050
movq -0x28(%rbp), %rax
movl 0x30(%rax), %esi
leaq 0x2437(%rip), %rdi      # 0x4452
movb $0x0, %al
callq 0x1050
movq -0x28(%rbp), %rax
movl 0x28(%rax), %esi
leaq 0x243c(%rip), %rdi      # 0x446c
movb $0x0, %al
callq 0x1050
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rsi
leaq 0x2440(%rip), %rdi      # 0x4486
movb $0x0, %al
callq 0x1050
leaq 0x2312(%rip), %rdi      # 0x4366
movb $0x0, %al
callq 0x1050
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)