// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/bwashm.c
// Function: bwa_shm_stage
int bwa_shm_stage(bwaidx_t *idx, const char *hint, const char *_tmpfn)
{
	const char *name;
	uint8_t *shm, *shm_idx;
	uint16_t *cnt;
	int shmid, to_init = 0, l;
	char path[PATH_MAX + 1], *tmpfn = (char*)_tmpfn;

	if (hint == 0 || hint[0] == 0) return -1;
	for (name = hint + strlen(hint) - 1; name >= hint && *name != '/'; --name);
	++name;

	if ((shmid = shm_open("/bwactl", O_RDWR, 0)) < 0) {
		shmid = shm_open("/bwactl", O_CREAT|O_RDWR|O_EXCL, 0644);
		to_init = 1;
	}
	if (shmid < 0) return -1;
	ftruncate(shmid, BWA_CTL_SIZE);
	shm = mmap(0, BWA_CTL_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
	cnt = (uint16_t*)shm;
	if (to_init) {
		memset(shm, 0, BWA_CTL_SIZE);
		cnt[1] = 4;
	}

	if (idx->mem == 0) bwa_idx2mem(idx);

	if (tmpfn) {
		FILE *fp;
		if ((fp = fopen(tmpfn, "wb")) != 0) {
			int64_t rest = idx->l_mem;
			while (rest > 0) {
				int64_t l = rest < 0x1000000? rest : 0x1000000;
				rest -= fwrite(&idx->mem[idx->l_mem - rest], 1, l, fp);
			}
			fclose(fp);
			free(idx->mem); idx->mem = 0;
		} else {
			fprintf(stderr, "[W::%s] fail to create the temporary file. Option '-f' is ignored.\n", __func__);
			tmpfn = 0;
		}
	}

	strcat(strcpy(path, "/bwaidx-"), name);
	if ((shmid = shm_open(path, O_CREAT|O_RDWR|O_EXCL, 0644)) < 0) {
		shm_unlink(path);
		perror("shm_open()");
		return -1;
	}
	l = 8 + strlen(name) + 1;
	if (cnt[1] + l > BWA_CTL_SIZE) return -1;
	memcpy(shm + cnt[1], &idx->l_mem, 8);
	memcpy(shm + cnt[1] + 8, name, l - 8);
	cnt[1] += l; ++cnt[0];
	ftruncate(shmid, idx->l_mem);
	shm_idx = mmap(0, idx->l_mem, PROT_READ|PROT_WRITE, MAP_SHARED, shmid, 0);
	if (tmpfn) {
		FILE *fp;
		fp = fopen(tmpfn, "rb");
		int64_t rest = idx->l_mem;
		while (rest > 0) {
			int64_t l = rest < 0x1000000? rest : 0x1000000;
			rest -= fread(&shm_idx[idx->l_mem - rest], 1, l, fp);
		}
		fclose(fp);
		unlink(tmpfn);
	} else {
		memcpy(shm_idx, idx->mem, idx->l_mem);
		free(idx->mem);
	}
	bwa_mem2idx(idx->l_mem, shm_idx, idx);
	idx->is_shm = 1;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1018, %rsp           # imm = 0x1018
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0xbfdb
movq %rsi, %r12
cmpb $0x0, (%rsi)
je 0xbfdb
movq %rdx, %r14
movq %rdi, %rbx
movq %r12, %rdi
callq 0x3100
addq %r12, %rax
movq %rax, %r15
decq %rax
cmpq %r12, %rax
jb 0xbd54
cmpb $0x2f, (%rax)
jne 0xbd44
leaq 0x1bc19(%rip), %rdi     # 0x27974
movl $0x2, %esi
xorl %edx, %edx
callq 0x3650
movl %eax, %r13d
testl %eax, %eax
js 0xbd73
movl %r13d, %r12d
jmp 0xbd94
leaq 0x1bbfa(%rip), %rdi     # 0x27974
movl $0xc2, %esi
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3650
testl %eax, %eax
js 0xbfdb
movl %eax, %r12d
movl $0x10000, %esi          # imm = 0x10000
movl %r12d, %edi
callq 0x34a0
movl $0x10000, %esi          # imm = 0x10000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x36c0
movq %rax, %r12
testl %r13d, %r13d
jns 0xbddc
movl $0x10000, %edx          # imm = 0x10000
movq %r12, %rdi
xorl %esi, %esi
callq 0x3140
movw $0x4, 0x2(%r12)
cmpq $0x0, 0x28(%rbx)
jne 0xbdeb
movq %rbx, %rdi
callq 0x131f4
testq %r14, %r14
je 0xbe74
leaq 0x1bb81(%rip), %rsi     # 0x2797c
movq %r14, %rdi
callq 0x3380
testq %rax, %rax
je 0xbe6f
movq %rax, %r13
movq %r14, 0x8(%rsp)
movq 0x20(%rbx), %r14
testq %r14, %r14
jle 0xbe4a
movl $0x1000000, %ebp        # imm = 0x1000000
cmpq %rbp, %r14
movl $0x1000000, %edx        # imm = 0x1000000
cmovbq %r14, %rdx
movq 0x20(%rbx), %rdi
subq %r14, %rdi
addq 0x28(%rbx), %rdi
movl $0x1, %esi
movq %r13, %rcx
callq 0x3790
subq %rax, %r14
testq %r14, %r14
jg 0xbe1e
movq %r13, %rdi
callq 0x32f0
movq 0x28(%rbx), %rdi
callq 0x3390
movq $0x0, 0x28(%rbx)
movq 0x8(%rsp), %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xbe77
callq 0x3f80
xorl %r14d, %r14d
movabsq $0x2d7864696177622f, %rax # imm = 0x2D7864696177622F
leaq 0x10(%rsp), %r13
movq %rax, (%r13)
movb $0x0, 0x8(%r13)
movq %r13, %rdi
movq %r15, %rsi
callq 0x36b0
movq %r13, %rdi
movl $0xc2, %esi
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3650
testl %eax, %eax
js 0xbfef
movl %eax, %r13d
movq %r15, %rdi
callq 0x3100
movzwl 0x2(%r12), %ecx
leal (%rax,%rcx), %edx
addl $0x9, %edx
cmpl $0x10000, %edx          # imm = 0x10000
jg 0xbfdb
leal 0x9(%rax), %ebp
movq 0x20(%rbx), %rdx
movq %rdx, (%r12,%rcx)
movzwl 0x2(%r12), %ecx
leaq (%r12,%rcx), %rdi
addq $0x8, %rdi
incl %eax
movslq %eax, %rdx
movq %r15, %rsi
callq 0x3260
movdqa 0x1b3eb(%rip), %xmm0    # 0x272f0
pinsrw $0x1, %ebp, %xmm0
movd (%r12), %xmm1
paddw %xmm1, %xmm0
movd %xmm0, (%r12)
movq 0x20(%rbx), %rsi
movl %r13d, %edi
callq 0x34a0
movq 0x20(%rbx), %rsi
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x36c0
movq %rax, %r15
testq %r14, %r14
je 0xbfaa
leaq 0x1cb48(%rip), %rsi     # 0x28a98
movq %r14, %rbp
movq %r14, %rdi
callq 0x3380
movq %rax, %r12
movq 0x20(%rbx), %r14
testq %r14, %r14
jle 0xbf98
movl $0x1000000, %r13d       # imm = 0x1000000
cmpq %r13, %r14
movl $0x1000000, %edx        # imm = 0x1000000
cmovbq %r14, %rdx
movq 0x20(%rbx), %rdi
subq %r14, %rdi
addq %r15, %rdi
movl $0x1, %esi
movq %r12, %rcx
callq 0x3570
subq %rax, %r14
testq %r14, %r14
jg 0xbf6d
movq %r12, %rdi
callq 0x32f0
movq %rbp, %rdi
callq 0x3110
jmp 0xbfc3
movq 0x20(%rbx), %rdx
movq 0x28(%rbx), %rsi
movq %r15, %rdi
callq 0x3260
movq 0x28(%rbx), %rdi
callq 0x3390
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x13027
movl $0x1, 0x18(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %rdi
callq 0x3740
leaq 0x1b9da(%rip), %rdi     # 0x279da
callq 0x3280
jmp 0xbfdb