// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam2bcf.c
// Function: calc_ReadPosBias
void calc_ReadPosBias(bcf_callaux_t *bca, bcf_call_t *call)
{
    int i, nref = 0, nalt = 0;
    unsigned long int U = 0;
    for (i=0; i<bca->npos; i++) 
    {
        nref += bca->ref_pos[i];
        nalt += bca->alt_pos[i];
        U += nref*bca->alt_pos[i];
        bca->ref_pos[i] = 0;
        bca->alt_pos[i] = 0;
    }
#if 0
//todo
    double var = 0, avg = (double)(nref+nalt)/bca->npos;
    for (i=0; i<bca->npos; i++) 
    {
        double ediff = bca->ref_pos[i] + bca->alt_pos[i] - avg;
        var += ediff*ediff;
        bca->ref_pos[i] = 0;
        bca->alt_pos[i] = 0;
    }
    call->read_pos.avg = avg;
    call->read_pos.var = sqrt(var/bca->npos);
    call->read_pos.dp  = nref+nalt;
#endif
    if ( !nref || !nalt )
    {
        call->read_pos_bias = -1;
        return;
    }

    if ( nref>=8 || nalt>=8 )
    {
        // normal approximation
        double mean = ((double)nref*nalt+1.0)/2.0;
        double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
        double z    = (U-mean)/sqrt(var2);
        call->read_pos_bias = z;
        //fprintf(stderr,"nref=%d  nalt=%d  U=%ld  mean=%e  var=%e  zval=%e\n", nref,nalt,U,mean,sqrt(var2),call->read_pos_bias);
    }
    else
    {
        double p = mann_whitney_1947(nalt,nref,U);
        // biased form claimed by GATK to behave better empirically
        // double var2 = (1.0+1.0/(nref+nalt+1.0))*(double)nref*nalt*(nref+nalt+1.0)/12.0;
        double var2 = (double)nref*nalt*(nref+nalt+1.0)/12.0;
        double z;
        if ( p >= 1./sqrt(var2*2*M_PI) ) z = 0;   // equal to mean
        else
        {
            if ( U >= nref*nalt/2. ) z = sqrt(-2*log(sqrt(var2*2*M_PI)*p));
            else z = -sqrt(-2*log(sqrt(var2*2*M_PI)*p));
        }
        call->read_pos_bias = z;
        //fprintf(stderr,"nref=%d  nalt=%d  U=%ld  p=%e var2=%e  zval=%e\n", nref,nalt,U, p,var2,call->read_pos_bias);
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movss 0x307d8(%rip), %xmm0    # 0x41ab0
cmpl $0x0, 0x48(%rdi)
jle 0x1153b
movq 0x38(%rdi), %rax
movq 0x40(%rdi), %rcx
xorl %edx, %edx
xorl %esi, %esi
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r14d, %r14d
addl (%rax,%rsi,4), %r15d
movl (%rcx,%rsi,4), %r8d
addl %r8d, %r12d
imull %r15d, %r8d
movslq %r8d, %r8
addq %r8, %r14
movl %edx, (%rax,%rsi,4)
movl %edx, (%rcx,%rsi,4)
incq %rsi
movslq 0x48(%rdi), %r8
cmpq %r8, %rsi
jl 0x112f7
testl %r15d, %r15d
je 0x1153b
testl %r12d, %r12d
je 0x1153b
leal (%r15,%r12), %eax
cvtsi2sd %eax, %xmm2
addsd 0x306b8(%rip), %xmm2    # 0x419f8
cmpl $0x7, %r15d
jg 0x113a4
cmpl $0x8, %r12d
jge 0x113a4
movl %r12d, %edi
movl %r15d, %esi
movl %r14d, %edx
movsd %xmm2, (%rsp)
callq 0x11213
cvtsi2sd %r15d, %xmm1
cvtsi2sd %r12d, %xmm3
mulsd %xmm1, %xmm3
mulsd (%rsp), %xmm3
divsd 0x3068e(%rip), %xmm3    # 0x41a08
addsd %xmm3, %xmm3
mulsd 0x3068a(%rip), %xmm3    # 0x41a10
movapd %xmm0, %xmm2
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm3
jb 0x11429
xorps %xmm0, %xmm0
sqrtsd %xmm3, %xmm0
jmp 0x1144c
cvtsi2sd %r15d, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
mulsd %xmm1, %xmm0
movsd 0x3063b(%rip), %xmm1    # 0x419f8
addsd %xmm0, %xmm1
mulsd 0x30657(%rip), %xmm1    # 0x41a20
mulsd %xmm2, %xmm0
divsd 0x30633(%rip), %xmm0    # 0x41a08
movq %r14, %xmm2
punpckldq 0x2fb8e(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x2fb96(%rip), %xmm2    # 0x40f80
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
addsd %xmm1, %xmm3
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1140a
sqrtsd %xmm0, %xmm0
jmp 0x11419
movapd %xmm3, (%rsp)
callq 0x37a0
movapd (%rsp), %xmm3
divsd %xmm0, %xmm3
xorps %xmm0, %xmm0
cvtsd2ss %xmm3, %xmm0
jmp 0x1153b
movapd %xmm3, %xmm0
movsd %xmm2, (%rsp)
movsd %xmm3, 0x20(%rsp)
callq 0x37a0
xorpd %xmm4, %xmm4
movsd 0x20(%rsp), %xmm3
movsd (%rsp), %xmm2
movsd 0x305a4(%rip), %xmm1    # 0x419f8
divsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jae 0x11534
movq %r14, %xmm5
punpckldq 0x2fb01(%rip), %xmm5 # xmm5 = xmm5[0],mem[0],xmm5[1],mem[1]
subpd 0x2fb09(%rip), %xmm5    # 0x40f80
movapd %xmm5, %xmm1
imull %r12d, %r15d
xorps %xmm4, %xmm4
cvtsi2sd %r15d, %xmm4
unpckhpd %xmm5, %xmm1            # xmm1 = xmm1[1],xmm5[1]
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm3
jb 0x1149e
xorps %xmm0, %xmm0
sqrtsd %xmm3, %xmm0
jmp 0x114d5
movapd %xmm3, %xmm0
movsd %xmm2, (%rsp)
movapd %xmm1, 0x20(%rsp)
movsd %xmm4, 0x18(%rsp)
movapd %xmm5, 0x30(%rsp)
callq 0x37a0
movapd 0x30(%rsp), %xmm5
movsd 0x18(%rsp), %xmm4
movapd 0x20(%rsp), %xmm1
movsd (%rsp), %xmm2
addsd %xmm5, %xmm1
movapd %xmm1, 0x20(%rsp)
mulsd 0x2db31(%rip), %xmm4    # 0x3f018
movsd %xmm4, 0x18(%rsp)
mulsd %xmm0, %xmm2
movapd %xmm2, %xmm0
callq 0x3330
mulsd 0x30516(%rip), %xmm0    # 0x41a18
ucomisd 0x3051e(%rip), %xmm0    # 0x41a28
jb 0x11515
xorps %xmm4, %xmm4
sqrtsd %xmm0, %xmm4
jmp 0x1151e
callq 0x37a0
movapd %xmm0, %xmm4
movapd 0x20(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jae 0x11534
xorpd 0x3055c(%rip), %xmm4    # 0x41a90
xorps %xmm0, %xmm0
cvtsd2ss %xmm4, %xmm0
movss %xmm0, 0x94(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq