// File: /workspace/github_c_makefile_star2_O2/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
movq %rdx, %rbp
pushq $-0x1
popq %rdx
testq %rsi, %rsi
je 0xa7af
movq %rsi, %r12
cmpb $0x0, (%rsi)
je 0xa7af
movq %rdi, %rbx
movq %r12, %rdi
callq 0x3100
addq %r12, %rax
movq %rax, %r15
decq %rax
cmpq %r12, %rax
jb 0xa51a
cmpb $0x2f, (%rax)
jne 0xa50a
leaq 0x18403(%rip), %rdi     # 0x22924
pushq $0x2
popq %rsi
xorl %edx, %edx
callq 0x3650
movl %eax, %r13d
testl %eax, %eax
js 0xa537
movl %r13d, %r14d
jmp 0xa558
leaq 0x183e6(%rip), %rdi     # 0x22924
movl $0xc2, %esi
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3650
testl %eax, %eax
js 0xa7ac
movl %eax, %r14d
movl $0x10000, %esi          # imm = 0x10000
movl %r14d, %edi
callq 0x34a0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x10000, %esi          # imm = 0x10000
xorl %edi, %edi
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x36c0
movq %rax, %r14
testl %r13d, %r13d
jns 0xa59b
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
xorl %esi, %esi
callq 0x3140
movw $0x4, 0x2(%r14)
cmpq $0x0, 0x28(%rbx)
jne 0xa5aa
movq %rbx, %rdi
callq 0x1120b
testq %rbp, %rbp
movq %r14, 0x8(%rsp)
je 0xa629
leaq 0x18371(%rip), %rsi     # 0x2292c
movq %rbp, %rdi
callq 0x3380
testq %rax, %rax
je 0xa7c3
movq %rax, %r13
movq %rbp, (%rsp)
movq 0x20(%rbx), %rbp
movl $0x1000000, %r12d       # imm = 0x1000000
pushq $0x1
popq %r14
testq %rbp, %rbp
jle 0xa60d
cmpq %r12, %rbp
movl $0x1000000, %edx        # imm = 0x1000000
cmovbq %rbp, %rdx
movq 0x20(%rbx), %rdi
subq %rbp, %rdi
addq 0x28(%rbx), %rdi
movq %r14, %rsi
movq %r13, %rcx
callq 0x3790
subq %rax, %rbp
jmp 0xa5e1
movq %r13, %rdi
callq 0x32f0
movq 0x28(%rbx), %rdi
callq 0x3390
andq $0x0, 0x28(%rbx)
movq (%rsp), %r12
jmp 0xa62c
xorl %r12d, %r12d
movabsq $0x2d7864696177622f, %rax # imm = 0x2D7864696177622F
leaq 0x10(%rsp), %r14
movq %rax, (%r14)
movb $0x0, 0x8(%r14)
movq %r14, %rdi
movq %r15, %rsi
callq 0x36b0
movq %r14, %rdi
movl $0xc2, %esi
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3650
testl %eax, %eax
js 0xa796
movl %eax, %r13d
movq %r15, %rdi
callq 0x3100
movq 0x8(%rsp), %r14
movzwl 0x2(%r14), %ecx
leal (%rax,%rcx), %edx
addl $0x9, %edx
cmpl $0x10000, %edx          # imm = 0x10000
pushq $-0x1
popq %rdx
jg 0xa7af
movq %r12, (%rsp)
leal 0x9(%rax), %ebp
movq 0x20(%rbx), %rdx
movq %rdx, (%r14,%rcx)
movzwl 0x2(%r14), %ecx
leaq (%r14,%rcx), %rdi
addq $0x8, %rdi
incl %eax
movslq %eax, %rdx
movq %r15, %rsi
callq 0x3260
movdqa 0x17bdd(%rip), %xmm0    # 0x222a0
pinsrw $0x1, %ebp, %xmm0
movq %r14, %rax
movq (%rsp), %r14
movd (%rax), %xmm1
paddw %xmm1, %xmm0
movd %xmm0, (%rax)
movq 0x20(%rbx), %rsi
movl %r13d, %edi
callq 0x34a0
movq 0x20(%rbx), %rsi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movl %r13d, %r8d
xorl %r9d, %r9d
callq 0x36c0
movq %rax, %r15
testq %r14, %r14
je 0xa763
leaq 0x1931b(%rip), %rsi     # 0x23a28
movq %r14, %rdi
callq 0x3380
movq %rax, %r12
movq 0x20(%rbx), %r13
movl $0x1000000, %ebp        # imm = 0x1000000
pushq $0x1
popq %r14
testq %r13, %r13
jle 0xa750
cmpq %rbp, %r13
movl $0x1000000, %edx        # imm = 0x1000000
cmovbq %r13, %rdx
movq 0x20(%rbx), %rdi
subq %r13, %rdi
addq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x3570
subq %rax, %r13
jmp 0xa725
movq %r12, %rdi
callq 0x32f0
movq (%rsp), %rdi
callq 0x3110
jmp 0xa77c
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 0x11055
movl $0x1, 0x18(%rbx)
xorl %edx, %edx
jmp 0xa7af
leaq 0x10(%rsp), %rdi
callq 0x3740
leaq 0x181e3(%rip), %rdi     # 0x2298a
callq 0x3280
pushq $-0x1
popq %rdx
movl %edx, %eax
addq $0x1018, %rsp           # imm = 0x1018
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x21806(%rip), %rax     # 0x2bfd0
movq (%rax), %rdi
leaq 0x1815b(%rip), %rsi     # 0x2292f
leaq 0x18198(%rip), %rdx     # 0x22973
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x3500
jmp 0xa62c