// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam_plcmd.c
// Function: mplp_func
static int mplp_func(void *data, bam1_t *b)
{
	extern int bam_realn(bam1_t *b, const char *ref);
	extern int bam_prob_realn_core(bam1_t *b, const char *ref, int);
	extern int bam_cap_mapQ(bam1_t *b, char *ref, int thres);
	mplp_aux_t *ma = (mplp_aux_t*)data;
	int ret, skip = 0;
	do {
		int has_ref;
		ret = ma->iter? bam_iter_read(ma->fp, ma->iter, b) : bam_read1(ma->fp, b);
		if (ret < 0) break;
		if (b->core.tid < 0 || (b->core.flag&BAM_FUNMAP)) { // exclude unmapped reads
			skip = 1;
			continue;
		}
        if (ma->conf->rflag_require && !(ma->conf->rflag_require&b->core.flag)) { skip = 1; continue; }
        if (ma->conf->rflag_filter && ma->conf->rflag_filter&b->core.flag) { skip = 1; continue; }
		if (ma->conf->bed) { // test overlap
			skip = !bed_overlap(ma->conf->bed, ma->h->target_name[b->core.tid], b->core.pos, bam_calend(&b->core, bam1_cigar(b)));
			if (skip) continue;
		}
		if (ma->conf->rghash) { // exclude read groups
			uint8_t *rg = bam_aux_get(b, "RG");
			skip = (rg && bcf_str2id(ma->conf->rghash, (const char*)(rg+1)) >= 0);
			if (skip) continue;
		}
		if (ma->conf->flag & MPLP_ILLUMINA13) {
			int i;
			uint8_t *qual = bam1_qual(b);
			for (i = 0; i < b->core.l_qseq; ++i)
				qual[i] = qual[i] > 31? qual[i] - 31 : 0;
		}
		has_ref = (ma->ref && ma->ref_id == b->core.tid)? 1 : 0;
		skip = 0;
		if (has_ref && (ma->conf->flag&MPLP_REALN)) bam_prob_realn_core(b, ma->ref, (ma->conf->flag & MPLP_REDO_BAQ)? 7 : 3);
		if (has_ref && ma->conf->capQ_thres > 10) {
			int q = bam_cap_mapQ(b, ma->ref, ma->conf->capQ_thres);
			if (q < 0) skip = 1;
			else if (b->core.qual > q) b->core.qual = q;
		}
		else if (b->core.qual < ma->conf->min_mq) skip = 1; 
		else if ((ma->conf->flag&MPLP_NO_ORPHAN) && (b->core.flag&1) && !(b->core.flag&2)) skip = 1;
	} while (skip);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
xorl %r13d, %r13d
xorl %r15d, %r15d
movq (%r14), %rdi
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x96ac
movq %rbx, %rdx
callq 0x2a30d
jmp 0x96b4
movq %rbx, %rsi
callq 0x23238
movl %eax, %ebp
testl %eax, %eax
js 0x98b4
movslq (%rbx), %rax
movl $0x1, %r15d
testq %rax, %rax
js 0x98b4
movq 0x8(%rbx), %rsi
movq %rsi, %rcx
shrq $0x20, %rcx
testb $0x4, %cl
jne 0x98b4
movq 0x28(%r14), %rdx
movl 0x20(%rdx), %edi
testl %edi, %edi
je 0x96fd
andl %ecx, %edi
andl $0xfffb, %edi           # imm = 0xFFFB
je 0x98b4
movl 0x24(%rdx), %edi
testl %edi, %edi
je 0x9712
andl %edi, %ecx
testl $0xfffb, %ecx           # imm = 0xFFFB
jne 0x98b4
movq 0x60(%rdx), %r12
testq %r12, %r12
je 0x9759
movq 0x10(%r14), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%rax,8), %r13
movl 0x4(%rbx), %eax
movl %eax, 0x4(%rsp)
shrl $0x18, %esi
addq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x22d1c
movq %r12, %rdi
movq %r13, %rsi
xorl %r13d, %r13d
movl 0x4(%rsp), %edx
movl %eax, %ecx
callq 0x305ac
testl %eax, %eax
je 0x98b4
movq 0x28(%r14), %rax
cmpq $0x0, 0x68(%rax)
je 0x97a1
movq %rbx, %rdi
leaq 0x375db(%rip), %rsi     # 0x40d49
callq 0x21c82
testq %rax, %rax
je 0x9792
movq 0x28(%r14), %rcx
movq 0x68(%rcx), %rdi
incq %rax
movq %rax, %rsi
callq 0x3bed5
testl %eax, %eax
setns %al
jmp 0x9794
xorl %eax, %eax
testb %al, %al
je 0x97a1
movzbl %al, %r15d
jmp 0x98b4
movq 0x28(%r14), %rax
testb $0x10, 0x9(%rax)
je 0x97f2
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0x97f2
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%rbx), %rdx
shrl $0x18, %ecx
incl %eax
sarl %eax
cltq
addq %rcx, %rax
addq %rdx, %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
subb $0x1f, %dl
movzbl %dl, %edx
cmovbl %r13d, %edx
movb %dl, (%rax,%rcx)
incq %rcx
movslq 0x10(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x97d6
movq 0x20(%r14), %rsi
testq %rsi, %rsi
je 0x9807
movl 0x18(%r14), %eax
cmpl (%rbx), %eax
sete %r12b
jmp 0x980a
xorl %r12d, %r12d
testb %r12b, %r12b
je 0x9875
movq 0x28(%r14), %rax
movl 0x8(%rax), %edx
testb %dl, %dl
jns 0x982b
shrl $0x9, %edx
andl $0x4, %edx
orl $0x3, %edx
movq %rbx, %rdi
callq 0x2d7a9
testb %r12b, %r12b
je 0x9875
movq 0x28(%r14), %rax
movl 0x10(%rax), %edx
cmpl $0xb, %edx
jl 0x9875
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0x2d466
testl %eax, %eax
js 0x98b4
movq 0x8(%rbx), %rcx
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
xorl %r15d, %r15d
cmpl %eax, %edx
jle 0x98b4
movzbl %al, %eax
shll $0x10, %eax
andq $-0xff0001, %rcx        # imm = 0xFF00FFFF
orq %rax, %rcx
movq %rcx, 0x8(%rbx)
jmp 0x98b4
movq 0x8(%rbx), %rax
movl %eax, %ecx
shrl $0x10, %ecx
movzbl %cl, %edx
movq 0x28(%r14), %rcx
cmpl 0x4(%rcx), %edx
jl 0x98b4
xorl %r15d, %r15d
testb $0x40, 0x8(%rcx)
je 0x98b4
movabsq $0x300000000, %rcx      # imm = 0x300000000
andq %rcx, %rax
xorl %r15d, %r15d
movabsq $0x100000000, %rcx      # imm = 0x100000000
cmpq %rcx, %rax
sete %r15b
testl %ebp, %ebp
js 0x98c7
testl %r15d, %r15d
movl $0x1, %r15d
jne 0x9696
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq