// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam2bcf.c
// Function: bcf_call_glfgen
int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r)
{
	int i, n, ref4, is_indel, ori_depth = 0;
	memset(r, 0, sizeof(bcf_callret1_t));
	if (ref_base >= 0) {
		ref4 = bam_nt16_nt4_table[ref_base];
		is_indel = 0;
	} else ref4 = 4, is_indel = 1;
	if (_n == 0) return -1;
	// enlarge the bases array if necessary
	if (bca->max_bases < _n) {
		bca->max_bases = _n;
		kroundup32(bca->max_bases);
		bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
	}
	// fill the bases array
	for (i = n = r->n_supp = 0; i < _n; ++i) {
		const bam_pileup1_t *p = pl + i;
		int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
		// set base
		if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
		++ori_depth;
		baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality
		seqQ = is_indel? (p->aux>>8&0xff) : 99;
		if (q < bca->min_baseQ) continue;
		if (q > seqQ) q = seqQ;
		mapQ = p->b->core.qual < 255? p->b->core.qual : DEF_MAPQ; // special case for mapQ==255
		mapQ = mapQ < bca->capQ? mapQ : bca->capQ;
		if (q > mapQ) q = mapQ;
		if (q > 63) q = 63;
		if (q < 4) q = 4;
		if (!is_indel) {
			b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
			b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
			is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
		} else {
			b = p->aux>>16&0x3f;
			is_diff = (b != 0);
		}
		if (is_diff) ++r->n_supp;
		bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
		// collect annotations
		if (b < 4) r->qsum[b] += q;
		++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
		min_dist = p->b->core.l_qseq - 1 - p->qpos;
		if (min_dist > p->qpos) min_dist = p->qpos;
		if (min_dist > CAP_DIST) min_dist = CAP_DIST;
		r->anno[1<<2|is_diff<<1|0] += baseQ;
		r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
		r->anno[2<<2|is_diff<<1|0] += mapQ;
		r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
		r->anno[3<<2|is_diff<<1|0] += min_dist;
		r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;

        // collect read positions for ReadPosBias
        int len, pos = get_position(p, &len);
        int epos = (double)pos/(len+1) * bca->npos;
        if ( bam1_seqi(bam1_seq(p->b),p->qpos) == ref_base )
            bca->ref_pos[epos]++;
        else
            bca->alt_pos[epos]++;
	}
	r->depth = n; r->ori_depth = ori_depth;
	// glfgen
	errmod_cal(bca->e, n, 5, bca->bases, r->p);
	return r->depth;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, %r15
movl %edi, %r12d
movl $0xc0, %edx
movq %r8, %rdi
xorl %esi, %esi
callq 0x3080
testl %ebp, %ebp
js 0x10e72
movl %ebp, %eax
leaq 0x40287(%rip), %rcx     # 0x510f0
movsbl (%rcx,%rax), %eax
movl %eax, (%rsp)
jmp 0x10e79
movl $0x4, (%rsp)
testl %r12d, %r12d
je 0x111d7
cmpl %r12d, 0x4c(%r14)
jge 0x10ecd
leal -0x1(%r12), %eax
movl %eax, %ecx
sarl %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x10, %ecx
orl %eax, %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x4c(%r14)
movq 0x78(%r14), %rdi
leal 0x2(,%rcx,2), %eax
movslq %eax, %rsi
callq 0x3720
movq %rax, 0x78(%r14)
movl $0x0, 0x4(%rbx)
testl %r12d, %r12d
jle 0x111de
movl %r12d, %eax
movsd 0x30b10(%rip), %xmm0    # 0x419f8
xorl %r9d, %r9d
xorl %esi, %esi
xorl %r13d, %r13d
movl %ebp, 0x8(%rsp)
movq %r14, 0x10(%rsp)
leaq (%r9,%r9,2), %rdx
movl 0x14(%r15,%rdx,8), %ecx
testb $0x9, %cl
jne 0x111c9
leaq (%r15,%rdx,8), %r11
movq (%r11), %r10
movq 0x8(%r10), %rdi
btq $0x22, %rdi
jb 0x111c9
testl %ebp, %ebp
js 0x10f5e
movq %rdi, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%r10), %rdx
movl %edi, %r12d
shrl $0x18, %r12d
movl 0x10(%r10), %r8d
incl %r8d
sarl %r8d
movslq %r8d, %r8
addq %r12, %r8
addq %rdx, %r8
movslq 0x8(%r11), %rdx
movzbl (%rdx,%r8), %r12d
movl $0x63, %edx
jmp 0x10f76
movl %ecx, %r12d
shrl $0x4, %r12d
movl $0xff, %r8d
andl %r8d, %r12d
movl %ecx, %edx
shrl $0xc, %edx
andl %r8d, %edx
incl %r13d
cmpl 0x4(%r14), %r12d
jl 0x111c9
movl %r13d, 0xc(%rsp)
movl %esi, 0x4(%rsp)
cmpl %edx, %r12d
cmovbl %r12d, %edx
movl %edi, %r13d
shrl $0x10, %r13d
movl $0xff, %esi
andl %esi, %r13d
cmpl %esi, %r13d
movl $0x14, %esi
cmovel %esi, %r13d
movl (%r14), %esi
cmpl %esi, %r13d
cmovgel %esi, %r13d
cmpl %r13d, %edx
cmovgel %r13d, %edx
cmpl $0x3f, %edx
movl $0x3f, %esi
cmovgel %esi, %edx
cmpl $0x5, %edx
movl $0x4, %esi
cmovll %esi, %edx
testl %ebp, %ebp
js 0x11033
movq %rdi, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%r10), %rcx
movl %edi, %esi
shrl $0x18, %esi
addq %rcx, %rsi
movl 0x8(%r11), %ecx
movl %ecx, %r8d
sarl %r8d
movslq %r8d, %r8
movzbl (%r8,%rsi), %esi
shll $0x2, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %esi
andl $0xf, %esi
cmovel %ebp, %esi
leaq 0x400d7(%rip), %rcx     # 0x510f0
movsbl (%rcx,%rsi), %ecx
movl (%rsp), %r8d
cmpl $0x3, %r8d
jg 0x1103b
cmpl %ecx, %r8d
jne 0x1103b
xorl %esi, %esi
movl %r8d, %ecx
jmp 0x1104d
shrl $0x14, %ecx
andl $0x3f, %ecx
je 0x11049
incl 0x4(%rbx)
movq 0x8(%r10), %rdi
movl $0x2, %esi
jmp 0x1104d
xorl %esi, %esi
xorl %ecx, %ecx
movl %edx, %r8d
shll $0x5, %r8d
shrq $0x20, %rdi
andl $0x10, %edi
orl %r8d, %edi
orl %ecx, %edi
movq 0x10(%rsp), %r8
movq 0x78(%r8), %r8
movslq 0x4(%rsp), %r14
movw %di, (%r8,%r14,2)
cmpl $0x3, %ecx
jg 0x1107f
movslq %ecx, %rcx
addl %edx, 0xc(%rbx,%rcx,4)
movq 0x8(%r10), %rcx
shrq $0x24, %rcx
andl $0x1, %ecx
orl %esi, %ecx
incl 0x1c(%rbx,%rcx,4)
movl 0x8(%r11), %ecx
movl %ecx, %edx
notl %edx
addl 0x10(%r10), %edx
cmpl %ecx, %edx
cmovgel %ecx, %edx
cmpl $0x19, %edx
movl $0x19, %ecx
cmovgel %ecx, %edx
movl %esi, %ecx
addl %r12d, 0x2c(%rbx,%rcx,4)
imull %r12d, %r12d
leaq (,%rcx,4), %rsi
movq %rsi, %rdi
orq $0x14, %rdi
addl %r12d, 0x1c(%rbx,%rdi)
addl %r13d, 0x3c(%rbx,%rcx,4)
imull %r13d, %r13d
movq %rsi, %rdi
orq $0x24, %rdi
addl %r13d, 0x1c(%rbx,%rdi)
addl %edx, 0x4c(%rbx,%rcx,4)
imull %edx, %edx
orq $0x34, %rsi
addl %edx, 0x1c(%rbx,%rsi)
movl 0x8(%r11), %ecx
leal 0x1(%rcx), %r11d
movq 0x8(%r10), %rdx
movq %rdx, %rdi
shrq $0x30, %rdi
jne 0x11111
movq 0x30(%r10), %r10
shrl $0x18, %edx
movapd %xmm0, %xmm1
jmp 0x11169
movq 0x30(%r10), %r10
shrl $0x18, %edx
leaq (%r10,%rdx), %r12
xorl %r8d, %r8d
xorl %r13d, %r13d
xorl %esi, %esi
movl (%r12,%r8,4), %r14d
movl %r14d, %ebp
andl $0xf, %ebp
shrl $0x4, %r14d
cmpl $0x4, %ebp
je 0x11148
cmpl $0x1, %ebp
je 0x11140
testl %ebp, %ebp
jne 0x11159
addl %r14d, %r13d
addl %r14d, %esi
jmp 0x11159
addl %r14d, %esi
cmpl %ecx, %esi
movl $0x0, %ebp
cmovgl %ebp, %r14d
subl %r14d, %r11d
incq %r8
cmpq %r8, %rdi
jne 0x11124
incl %r13d
cvtsi2sd %r13d, %xmm1
movl 0x4(%rsp), %esi
incl %esi
cvtsi2sd %r11d, %xmm2
divsd %xmm1, %xmm2
movq 0x10(%rsp), %r14
xorps %xmm1, %xmm1
cvtsi2sdl 0x48(%r14), %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %r8d
leaq (%r10,%rdi,4), %rdi
addq %rdx, %rdi
movl %ecx, %edx
sarl %edx
movslq %edx, %rdx
movzbl (%rdx,%rdi), %edx
shll $0x2, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edx
andl $0xf, %edx
xorl %ecx, %ecx
movl 0x8(%rsp), %ebp
cmpl %ebp, %edx
setne %cl
movslq %r8d, %rdx
movq 0x38(%r14,%rcx,8), %rcx
incl (%rcx,%rdx,4)
movl 0xc(%rsp), %r13d
incq %r9
cmpq %rax, %r9
jne 0x10ef9
jmp 0x111e3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x11204
xorl %r13d, %r13d
xorl %esi, %esi
movl %esi, (%rbx)
movl %r13d, 0x8(%rbx)
movq 0x78(%r14), %rcx
movq 0x80(%r14), %rdi
leaq 0x5c(%rbx), %r8
movl $0x5, %edx
callq 0x163bf
movl (%rbx), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq