// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x10b0, %rsp           # imm = 0x10B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x1058(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0xe36c
movq -0x18(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0xe378
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe825
movq -0x18(%rbp), %rax
movq %rax, -0x1090(%rbp)
movq -0x18(%rbp), %rdi
callq 0x30f0
movq %rax, %rcx
movq -0x1090(%rbp), %rax
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
xorl %eax, %eax
cmpq -0x18(%rbp), %rcx
movb %al, -0x1091(%rbp)
jb 0xe3c6
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
setne %al
movb %al, -0x1091(%rbp)
movb -0x1091(%rbp), %al
testb $0x1, %al
jne 0xe3d2
jmp 0xe3e2
jmp 0xe3d4
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xe3a1
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq 0x2b3df(%rip), %rdi     # 0x397d4
movl $0x2, %esi
xorl %edx, %edx
callq 0x3630
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
jge 0xe429
leaq 0x2b3c4(%rip), %rdi     # 0x397d4
movl $0xc2, %esi
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3630
movl %eax, -0x44(%rbp)
movl $0x1, -0x48(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0xe43b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe825
movl -0x44(%rbp), %edi
movl $0x10000, %esi          # imm = 0x10000
callq 0x3480
movl -0x44(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x10000, %esi          # imm = 0x10000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x36a0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0xe494
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x3130
movq -0x40(%rbp), %rax
movw $0x4, 0x2(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0xe4a8
movq -0x10(%rbp), %rdi
callq 0x1a420
cmpq $0x0, -0x1058(%rbp)
je 0xe5ce
movq -0x1058(%rbp), %rdi
leaq 0x2b318(%rip), %rsi     # 0x397dc
callq 0x3370
movq %rax, -0x1060(%rbp)
cmpq $0x0, %rax
je 0xe5a2
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x1068(%rbp)
cmpq $0x0, -0x1068(%rbp)
jle 0xe57b
cmpq $0x1000000, -0x1068(%rbp) # imm = 0x1000000
jge 0xe514
movq -0x1068(%rbp), %rax
movq %rax, -0x10a0(%rbp)
jmp 0xe522
movl $0x1000000, %eax        # imm = 0x1000000
movq %rax, -0x10a0(%rbp)
jmp 0xe522
movq -0x10a0(%rbp), %rax
movq %rax, -0x1070(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
subq -0x1068(%rbp), %rax
addq %rax, %rdi
movq -0x1070(%rbp), %rdx
movq -0x1060(%rbp), %rcx
movl $0x1, %esi
callq 0x3770
movq %rax, %rcx
movq -0x1068(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x1068(%rbp)
jmp 0xe4e9
movq -0x1060(%rbp), %rdi
callq 0x32e0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x3380
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
jmp 0xe5cc
movq 0x31a2f(%rip), %rax     # 0x3ffd8
movq (%rax), %rdi
leaq 0x2b22c(%rip), %rsi     # 0x397df
leaq 0x2b269(%rip), %rdx     # 0x39823
movb $0x0, %al
callq 0x34e0
movq $0x0, -0x1058(%rbp)
jmp 0xe5ce
leaq -0x1050(%rbp), %rdi
leaq 0x2b255(%rip), %rsi     # 0x39831
callq 0x32b0
movq %rax, %rdi
movq -0x28(%rbp), %rsi
callq 0x3690
leaq -0x1050(%rbp), %rdi
movl $0xc2, %esi
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3630
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
jge 0xe62f
leaq -0x1050(%rbp), %rdi
callq 0x3720
leaq 0x2b21c(%rip), %rdi     # 0x3983a
callq 0x3270
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe825
movq -0x28(%rbp), %rdi
callq 0x30f0
addq $0x8, %rax
addq $0x1, %rax
movl %eax, -0x4c(%rbp)
movq -0x40(%rbp), %rax
movzwl 0x2(%rax), %eax
addl -0x4c(%rbp), %eax
cmpl $0x10000, %eax          # imm = 0x10000
jle 0xe661
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe825
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
movslq %ecx, %rcx
movq -0x10(%rbp), %rdx
movq 0x20(%rdx), %rdx
movq %rdx, (%rax,%rcx)
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rax
movzwl 0x2(%rax), %eax
cltq
addq %rax, %rdi
addq $0x8, %rdi
movq -0x28(%rbp), %rsi
movl -0x4c(%rbp), %eax
subl $0x8, %eax
movslq %eax, %rdx
callq 0x3250
movl -0x4c(%rbp), %edx
movq -0x40(%rbp), %rax
movzwl 0x2(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x2(%rax)
movq -0x40(%rbp), %rax
movw (%rax), %cx
addw $0x1, %cx
movw %cx, (%rax)
movl -0x44(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x3480
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movl -0x44(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x36a0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x1058(%rbp)
je 0xe7d8
movq -0x1058(%rbp), %rdi
leaq 0x2c17b(%rip), %rsi     # 0x3a890
callq 0x3370
movq %rax, -0x1078(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x1080(%rbp)
cmpq $0x0, -0x1080(%rbp)
jle 0xe7be
cmpq $0x1000000, -0x1080(%rbp) # imm = 0x1000000
jge 0xe75b
movq -0x1080(%rbp), %rax
movq %rax, -0x10a8(%rbp)
jmp 0xe769
movl $0x1000000, %eax        # imm = 0x1000000
movq %rax, -0x10a8(%rbp)
jmp 0xe769
movq -0x10a8(%rbp), %rax
movq %rax, -0x1088(%rbp)
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
subq -0x1080(%rbp), %rax
addq %rax, %rdi
movq -0x1088(%rbp), %rdx
movq -0x1078(%rbp), %rcx
movl $0x1, %esi
callq 0x3550
movq %rax, %rcx
movq -0x1080(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x1080(%rbp)
jmp 0xe730
movq -0x1078(%rbp), %rdi
callq 0x32e0
movq -0x1058(%rbp), %rdi
callq 0x3100
jmp 0xe7fe
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x3250
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x3380
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %rdx
callq 0x1a110
movq -0x10(%rbp), %rax
movl $0x1, 0x18(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10b0, %rsp           # imm = 0x10B0
popq %rbp
retq
nopw %cs:(%rax,%rax)