// File: /workspace/github_c_makefile_star2_O3/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 %r15d, %r15d
movq (%r14), %rdi
movq 0x8(%r14), %rsi
testq %rsi, %rsi
je 0x9907
movq %rbx, %rdx
callq 0x2acb6
jmp 0x990f
movq %rbx, %rsi
callq 0x2393b
movl %eax, %ebp
testl %eax, %eax
js 0x9acf
movslq (%rbx), %rax
testq %rax, %rax
js 0x98f1
movq 0x8(%rbx), %rsi
movq %rsi, %rdx
shrq $0x20, %rdx
testb $0x4, %dl
jne 0x98f1
andl $0xfffb, %edx           # imm = 0xFFFB
movq 0x28(%r14), %rcx
movl 0x20(%rcx), %edi
testl %edi, %edi
je 0x9946
andl %edx, %edi
je 0x98f1
testl %edx, 0x24(%rcx)
jne 0x98f1
movq 0x60(%rcx), %r12
testq %r12, %r12
je 0x9992
movq 0x10(%r14), %rcx
movq 0x8(%rcx), %rcx
movq (%rcx,%rax,8), %r13
movl 0x4(%rbx), %r15d
shrl $0x18, %esi
addq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x2343c
movq %r12, %rdi
movq %r13, %rsi
movl %r15d, %edx
xorl %r15d, %r15d
movl %eax, %ecx
callq 0x30e93
testl %eax, %eax
je 0x98f1
movq 0x28(%r14), %rcx
cmpq $0x0, 0x68(%rcx)
je 0x99cc
movq %rbx, %rdi
leaq 0x383ee(%rip), %rsi     # 0x41d91
callq 0x22374
movq 0x28(%r14), %rcx
testq %rax, %rax
je 0x99cc
movq 0x68(%rcx), %rdi
incq %rax
movq %rax, %rsi
callq 0x3c6bc
testl %eax, %eax
jns 0x98f1
movq 0x28(%r14), %rcx
testb $0x10, 0x9(%rcx)
je 0x9a19
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0x9a19
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 %r15d, %edx
movb %dl, (%rax,%rcx)
incq %rcx
movslq 0x10(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x99fd
movq 0x20(%r14), %rsi
testq %rsi, %rsi
je 0x9a68
movl 0x18(%r14), %ecx
movq 0x28(%r14), %rax
cmpl (%rbx), %ecx
jne 0x9a6c
movl 0x8(%rax), %edx
testb %dl, %dl
jns 0x9a4a
shrl $0x9, %edx
andl $0x4, %edx
orl $0x3, %edx
movq %rbx, %rdi
callq 0x2e0ec
movq 0x28(%r14), %rax
movl 0x10(%rax), %edx
cmpl $0xb, %edx
jl 0x9a6c
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0x2de16
testl %eax, %eax
js 0x98f1
jmp 0x9aab
movq 0x28(%r14), %rax
movq 0x8(%rbx), %rcx
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
cmpl 0x4(%rax), %edx
jl 0x98f1
movabsq $0x300000000, %rdx      # imm = 0x300000000
andq %rdx, %rcx
movabsq $0x100000000, %rdx      # imm = 0x100000000
cmpq %rdx, %rcx
jne 0x9acf
movl 0x8(%rax), %eax
andl $0x40, %eax
jne 0x98f1
jmp 0x9acf
movq 0x8(%rbx), %rcx
movl %ecx, %edx
shrl $0x10, %edx
movzbl %dl, %edx
cmpl %eax, %edx
jbe 0x9acf
movzbl %al, %eax
shll $0x10, %eax
andq $-0xff0001, %rcx        # imm = 0xFF00FFFF
orq %rax, %rcx
movq %rcx, 0x8(%rbx)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq