// File: /workspace/github_c_makefile_star2_O1/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 0x10aeb
movl %ebp, %eax
leaq 0x3f60f(%rip), %rcx     # 0x500f0
movsbl (%rcx,%rax), %eax
movl %eax, 0x8(%rsp)
jmp 0x10af3
movl $0x4, 0x8(%rsp)
testl %r12d, %r12d
je 0x10e31
cmpl %r12d, 0x4c(%r14)
movl %ebp, (%rsp)
jge 0x10b4d
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
movl (%rsp), %ebp
movq %rax, 0x78(%r14)
movl $0x0, 0x4(%rbx)
testl %r12d, %r12d
jle 0x10e38
movl %r12d, %eax
movsd 0x2fe50(%rip), %xmm0    # 0x409b8
xorl %r10d, %r10d
xorl %esi, %esi
xorl %r13d, %r13d
movq %r14, 0x10(%rsp)
leaq (%r10,%r10,2), %rdx
movl 0x14(%r15,%rdx,8), %ecx
testb $0x9, %cl
jne 0x10e23
leaq (%r15,%rdx,8), %r12
movq (%r12), %r11
movq 0x8(%r11), %rdx
btq $0x22, %rdx
jb 0x10e23
testl %ebp, %ebp
js 0x10bd5
movq %rdx, %r9
shrq $0x30, %r9
shlq $0x2, %r9
addq 0x30(%r11), %r9
movl %edx, %edi
shrl $0x18, %edi
movl 0x10(%r11), %r8d
incl %r8d
sarl %r8d
movslq %r8d, %r8
addq %rdi, %r8
addq %r9, %r8
movslq 0x8(%r12), %rdi
movzbl (%rdi,%r8), %r8d
jmp 0x10bde
movl %ecx, %edi
shrl $0x4, %edi
movzbl %dil, %r8d
incl %r13d
cmpl 0x4(%r14), %r8d
jl 0x10e23
movl %r13d, 0xc(%rsp)
movl %esi, 0x4(%rsp)
movl %ecx, %r13d
shrl $0xc, %r13d
movl $0xff, %esi
andl %esi, %r13d
testl %ebp, %ebp
movl $0x63, %edi
cmovnsl %edi, %r13d
cmpl %r13d, %r8d
cmovbl %r8d, %r13d
movl %edx, %edi
shrl $0x10, %edi
andl %esi, %edi
cmpl %esi, %edi
movl $0x14, %esi
cmovel %esi, %edi
movl (%r14), %esi
cmpl %esi, %edi
cmovgel %esi, %edi
cmpl %edi, %r13d
cmovgel %edi, %r13d
cmpl $0x3f, %r13d
movl $0x3f, %esi
cmovgel %esi, %r13d
cmpl $0x5, %r13d
movl $0x4, %esi
cmovll %esi, %r13d
testl %ebp, %ebp
js 0x10cac
movl 0x8(%rsp), %ebp
cmpl $0x4, %ebp
setge %sil
movq %rdx, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%r11), %rcx
shrl $0x18, %edx
addq %rcx, %rdx
movl 0x8(%r12), %ecx
movl %ecx, %r9d
sarl %r9d
movslq %r9d, %r9
movzbl (%r9,%rdx), %edx
shll $0x2, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edx
andl $0xf, %edx
cmovel (%rsp), %edx
leaq 0x3f452(%rip), %rcx     # 0x500f0
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %ebp
setne %dl
orb %sil, %dl
jmp 0x10cb5
shrl $0x14, %ecx
andl $0x3f, %ecx
setne %dl
testb %dl, %dl
je 0x10cbc
incl 0x4(%rbx)
movl %r13d, %esi
shll $0x5, %esi
movl 0xc(%r11), %r9d
andl $0x10, %r9d
orl %esi, %r9d
orl %ecx, %r9d
movq 0x10(%rsp), %rsi
movq 0x78(%rsi), %rsi
movslq 0x4(%rsp), %r14
movw %r9w, (%rsi,%r14,2)
cmpl $0x3, %ecx
jg 0x10cf0
movslq %ecx, %rcx
addl %r13d, 0xc(%rbx,%rcx,4)
movzbl %dl, %ecx
movq 0x8(%r11), %rdx
shrq $0x24, %rdx
andl $0x1, %edx
shll $0x2, %edx
leal (%rdx,%rcx,8), %edx
incl 0x1c(%rbx,%rdx)
movl 0x8(%r12), %edx
movl %edx, %esi
notl %esi
addl 0x10(%r11), %esi
cmpl %edx, %esi
cmovgel %edx, %esi
cmpl $0x19, %esi
movl $0x19, %edx
cmovgel %edx, %esi
addl %r8d, 0x2c(%rbx,%rcx,8)
imull %r8d, %r8d
addl %r8d, 0x30(%rbx,%rcx,8)
addl %edi, 0x3c(%rbx,%rcx,8)
imull %edi, %edi
addl %edi, 0x40(%rbx,%rcx,8)
addl %esi, 0x4c(%rbx,%rcx,8)
imull %esi, %esi
addl %esi, 0x50(%rbx,%rcx,8)
movl 0x8(%r12), %ecx
leal 0x1(%rcx), %r12d
movq 0x8(%r11), %rdi
movq %rdi, %r8
shrq $0x30, %r8
jne 0x10d65
movapd %xmm0, %xmm1
jmp 0x10dbb
movl %edi, %edx
shrl $0x18, %edx
addq 0x30(%r11), %rdx
xorl %r9d, %r9d
xorl %r13d, %r13d
xorl %esi, %esi
movl (%rdx,%r9,4), %r14d
movl %r14d, %ebp
andl $0xf, %ebp
shrl $0x4, %r14d
cmpl $0x4, %ebp
je 0x10d9a
cmpl $0x1, %ebp
je 0x10d92
testl %ebp, %ebp
jne 0x10dab
addl %r14d, %r13d
addl %r14d, %esi
jmp 0x10dab
addl %r14d, %esi
cmpl %ecx, %esi
movl $0x0, %ebp
cmovgl %ebp, %r14d
subl %r14d, %r12d
incq %r9
cmpq %r9, %r8
jne 0x10d76
incl %r13d
cvtsi2sd %r13d, %xmm1
movl (%rsp), %ebp
movl 0x4(%rsp), %esi
cvtsi2sd %r12d, %xmm2
incl %esi
divsd %xmm1, %xmm2
movq 0x10(%rsp), %r14
xorps %xmm1, %xmm1
cvtsi2sdl 0x48(%r14), %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %edx
shlq $0x2, %r8
addq 0x30(%r11), %r8
shrl $0x18, %edi
addq %r8, %rdi
movl %ecx, %r8d
sarl %r8d
movslq %r8d, %r8
movzbl (%r8,%rdi), %edi
shll $0x2, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %edi
andl $0xf, %edi
xorl %ecx, %ecx
cmpl %ebp, %edi
setne %cl
movslq %edx, %rdx
movq 0x38(%r14,%rcx,8), %rcx
incl (%rcx,%rdx,4)
movl 0xc(%rsp), %r13d
incq %r10
cmpq %rax, %r10
jne 0x10b75
jmp 0x10e3d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x10e5e
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 0x160f4
movl (%rbx), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq