// File: /workspace/github_c_makefile_star2_O1/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 0xbd68
movq %rsi, %r12
cmpb $0x0, (%rsi)
je 0xbd68
movq %rdx, %r14
movq %rdi, %rbx
movq %r12, %rdi
callq 0x3100
addq %r12, %rax
movq %rax, %r15
decq %rax
cmpq %r12, %rax
jb 0xbaf0
cmpb $0x2f, (%rax)
jne 0xbae0
leaq 0x1bdfd(%rip), %rdi     # 0x278f4
movl $0x2, %esi
xorl %edx, %edx
callq 0x3650
movl %eax, %r13d
movl %eax, %r12d
testl %eax, %eax
jns 0xbb26
leaq 0x1bde0(%rip), %rdi     # 0x278f4
movl $0xc2, %esi
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3650
movl %eax, %r12d
testl %r12d, %r12d
js 0xbd68
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 0xbb77
movl $0x10000, %edx          # imm = 0x10000
movq %r12, %rdi
xorl %esi, %esi
callq 0x3140
movw $0x4, 0x2(%r12)
cmpq $0x0, 0x28(%rbx)
jne 0xbb86
movq %rbx, %rdi
callq 0x12e2d
testq %r14, %r14
je 0xbc13
leaq 0x1bd66(%rip), %rsi     # 0x278fc
movq %r14, %rdi
callq 0x3380
testq %rax, %rax
je 0xbc0e
movq %rax, %r13
movq %r12, (%rsp)
movq %r14, 0x8(%rsp)
movq 0x20(%rbx), %r14
testq %r14, %r14
jle 0xbbea
movl $0x1000000, %r12d       # imm = 0x1000000
cmpq %r12, %r14
movl $0x1000000, %edx        # imm = 0x1000000
cmovlq %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 0xbbbe
movq %r13, %rdi
callq 0x32f0
movq 0x28(%rbx), %rdi
callq 0x3390
movq $0x0, 0x28(%rbx)
movq 0x8(%rsp), %r14
movq (%rsp), %r12
jmp 0xbc16
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 0xbd7c
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 0xbd68
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
addw %bp, 0x2(%r12)
incw (%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 0xbd37
leaq 0x1cccb(%rip), %rsi     # 0x289a8
movq %r14, %rbp
movq %r14, %rdi
callq 0x3380
movq %rax, %r12
movq 0x20(%rbx), %r14
testq %r14, %r14
jle 0xbd25
movl $0x1000000, %r13d       # imm = 0x1000000
cmpq %r13, %r14
movl $0x1000000, %edx        # imm = 0x1000000
cmovlq %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 0xbcfa
movq %r12, %rdi
callq 0x32f0
movq %rbp, %rdi
callq 0x3110
jmp 0xbd50
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 0x12c63
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 0x1bbcd(%rip), %rdi     # 0x2795a
callq 0x3280
jmp 0xbd68