// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam2bcf.c
// Function: bcf_call_combine
int bcf_call_combine(int n, const bcf_callret1_t *calls, bcf_callaux_t *bca, int ref_base /*4-bit*/, bcf_call_t *call)
{
	int ref4, i, j, qsum[4];
	int64_t tmp;
	if (ref_base >= 0) {
		call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
		if (ref4 > 4) ref4 = 4;
	} else call->ori_ref = -1, ref4 = 0;
	// calculate qsum
	memset(qsum, 0, 4 * sizeof(int));
	for (i = 0; i < n; ++i)
		for (j = 0; j < 4; ++j)
			qsum[j] += calls[i].qsum[j];
    int qsum_tot=0;
    for (j=0; j<4; j++) { qsum_tot += qsum[j]; call->qsum[j] = 0; }
	for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
	// find the top 2 alleles
	for (i = 1; i < 4; ++i) // insertion sort
		for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
			tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
	// set the reference allele and alternative allele(s)
	for (i = 0; i < 5; ++i) call->a[i] = -1;
	call->unseen = -1;
	call->a[0] = ref4;
	for (i = 3, j = 1; i >= 0; --i) {
		if ((qsum[i]&3) != ref4) {
			if (qsum[i]>>2 != 0) 
            {
                if ( j<4 ) call->qsum[j] = (float)(qsum[i]>>2)/qsum_tot; // ref N can make j>=4
                call->a[j++]  = qsum[i]&3;
            }
			else break;
		}
        else 
            call->qsum[0] = (float)(qsum[i]>>2)/qsum_tot;
	}
	if (ref_base >= 0) { // for SNPs, find the "unseen" base
		if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
			call->unseen = j, call->a[j++] = qsum[i]&3;
		call->n_alleles = j;
	} else {
		call->n_alleles = j;
		if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
	}
	// set the PL array
	if (call->n < n) {
		call->n = n;
		call->PL = realloc(call->PL, 15 * n);
	}
	{
		int x, g[15], z;
		double sum_min = 0.;
		x = call->n_alleles * (call->n_alleles + 1) / 2;
		// get the possible genotypes
		for (i = z = 0; i < call->n_alleles; ++i)
			for (j = 0; j <= i; ++j)
				g[z++] = call->a[j] * 5 + call->a[i];
		for (i = 0; i < n; ++i) {
			uint8_t *PL = call->PL + x * i;
			const bcf_callret1_t *r = calls + i;
			float min = 1e37;
			for (j = 0; j < x; ++j)
				if (min > r->p[g[j]]) min = r->p[g[j]];
			sum_min += min;
			for (j = 0; j < x; ++j) {
				int y;
				y = (int)(r->p[g[j]] - min + .499);
				if (y > 255) y = 255;
				PL[j] = y;
			}
		}
//		if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
		call->shift = (int)(sum_min + .499);
	}
	// combine annotations
	memset(call->anno, 0, 16 * sizeof(int));
	for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
		call->depth += calls[i].depth;
		call->ori_depth += calls[i].ori_depth;
		for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
	}

    calc_vdb(bca, call);
    calc_ReadPosBias(bca, call);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbx
movq %rdx, %r14
movq %rsi, %r15
movl %edi, %r12d
testl %ecx, %ecx
js 0x11807
movl %ecx, %eax
leaq 0x3f8fa(%rip), %rdx     # 0x510f0
movsbl (%rdx,%rax), %edx
cmpl $0x4, %edx
movl $0x4, %eax
cmovll %edx, %eax
jmp 0x1180e
xorl %eax, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, 0x30(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl %r12d, %r13d
testl %r12d, %r12d
jle 0x1184e
movdqa (%rsp), %xmm1
movq %r13, %rdx
shlq $0x6, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movdqu 0xc(%r15,%rsi), %xmm2
paddd %xmm2, %xmm1
addq $0xc0, %rsi
cmpq %rsi, %rdx
jne 0x11832
movdqa %xmm1, (%rsp)
leaq 0x14(%rbx), %rsi
movups %xmm0, 0x14(%rbx)
movdqa (%rsp), %xmm0
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm2     # xmm2 = xmm1[1,1,1,1]
paddd %xmm1, %xmm2
pslld $0x2, %xmm0
por 0x2d7d6(%rip), %xmm0    # 0x3f050
movd %xmm2, %edx
movdqa %xmm0, (%rsp)
movl $0x1, %edi
xorl %r8d, %r8d
movl %edi, %r9d
movl (%rsp,%r9,4), %r9d
movq %r8, %r10
movl %r10d, %r11d
movl (%rsp,%r11,4), %ebp
cmpl %ebp, %r9d
jge 0x118b7
movl %ebp, 0x4(%rsp,%r10,4)
movl %r9d, (%rsp,%r11,4)
decq %r10
leal 0x2(%r10), %r11d
cmpl $0x1, %r11d
jg 0x11895
incl %edi
incq %r8
cmpl $0x4, %edi
jne 0x1188b
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x4(%rbx)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movl $0xffffffff, 0x34(%rbx) # imm = 0xFFFFFFFF
movl %eax, (%rbx)
movl $0x1, %edx
movl $0x3, %edi
movl (%rsp,%rdi,4), %r10d
movl %r10d, %r8d
andl $0x3, %r8d
movl %r10d, %r9d
sarl $0x2, %r9d
cmpl %eax, %r8d
jne 0x1190d
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rsi)
jmp 0x11938
cmpl $0x4, %r10d
jb 0x11947
cmpl $0x4, %edx
jge 0x1192f
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
divss %xmm0, %xmm1
movslq %edx, %r9
movss %xmm1, (%rsi,%r9,4)
jmp 0x11932
movl %edx, %r9d
incl %edx
movl %r8d, (%rbx,%r9,4)
addq $-0x1, %rdi
jb 0x118e4
xorl %esi, %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
jmp 0x1194a
movb $0x1, %sil
testl %ecx, %ecx
js 0x11967
cmpl $0x4, %eax
setl %cl
cmpl $0x4, %edx
setl %r8b
testb %r8b, %cl
je 0x11979
testb %sil, %sil
jne 0x1198c
jmp 0x1199f
movl %edx, 0x28(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, %edx
jne 0x119a2
jmp 0x11b98
cmpl $0x4, %eax
sete %al
cmpl $0x5, %edx
setl %cl
andb %al, %cl
testb %sil, %cl
je 0x1199f
movl %edx, 0x34(%rbx)
movl %edi, %eax
movl (%rsp,%rax,4), %eax
andl $0x3, %eax
movslq %edx, %rcx
incl %edx
movl %eax, (%rbx,%rcx,4)
movl %edx, 0x28(%rbx)
cmpl %r12d, 0x24(%rbx)
jge 0x119cc
movl %r12d, 0x24(%rbx)
movq 0x88(%rbx), %rdi
leal (%r12,%r12,4), %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rsi
callq 0x3720
movq %rax, 0x88(%rbx)
movl 0x28(%rbx), %edx
testl %edx, %edx
jle 0x11a12
movl %edx, %eax
movl $0x1, %ecx
xorl %esi, %esi
xorl %edi, %edi
movl (%rbx,%rsi,4), %r9d
movslq %edi, %rdi
leaq (%rsp,%rdi,4), %r10
addq $0x10, %r10
xorl %r8d, %r8d
movl (%rbx,%r8,4), %r11d
leal (%r11,%r11,4), %r11d
addl %r9d, %r11d
movl %r11d, (%r10,%r8,4)
incq %r8
cmpq %r8, %rcx
jne 0x119ed
incq %rsi
incq %rcx
addq %r8, %rdi
cmpq %rax, %rsi
jne 0x119db
testl %r12d, %r12d
jle 0x11b5e
leal 0x1(%rdx), %eax
imull %edx, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movslq %ecx, %rcx
movl %ecx, %edx
xorps %xmm0, %xmm0
xorl %esi, %esi
movss 0x3007c(%rip), %xmm1    # 0x41ab8
movsd 0x2d5ec(%rip), %xmm2    # 0x3f030
movl $0xff, %edi
movsd 0x30027(%rip), %xmm3    # 0x41a78
xorl %r8d, %r8d
cmpl $0x1, %eax
jle 0x11ad1
leaq (%r8,%r8,2), %r9
shlq $0x6, %r9
addq %r15, %r9
movq 0x88(%rbx), %r10
xorl %r11d, %r11d
movaps %xmm1, %xmm4
movaps %xmm4, %xmm5
movslq 0x10(%rsp,%r11,4), %rbp
movss 0x5c(%r9,%rbp,4), %xmm4
minss %xmm5, %xmm4
incq %r11
cmpq %r11, %rdx
jne 0x11a71
xorps %xmm5, %xmm5
cvtss2sd %xmm4, %xmm5
addsd %xmm5, %xmm0
cmpl $0x2, %eax
jl 0x11ad5
addq %rsi, %r10
xorl %r11d, %r11d
movslq 0x10(%rsp,%r11,4), %rbp
movss 0x5c(%r9,%rbp,4), %xmm5
subss %xmm4, %xmm5
cvtss2sd %xmm5, %xmm5
addsd %xmm2, %xmm5
cvttsd2si %xmm5, %ebp
cmpl %edi, %ebp
cmovgel %edi, %ebp
movb %bpl, (%r10,%r11)
incq %r11
cmpq %r11, %rdx
jne 0x11aa2
jmp 0x11ad5
addsd %xmm3, %xmm0
incq %r8
addq %rcx, %rsi
cmpq %r13, %r8
jne 0x11a54
addsd 0x2d544(%rip), %xmm0    # 0x3f030
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x6c(%rbx)
movupd %xmm0, 0x5c(%rbx)
movupd %xmm0, 0x4c(%rbx)
movupd %xmm0, 0x3c(%rbx)
movq $0x0, 0x7c(%rbx)
testl %r12d, %r12d
jle 0x11b80
leaq 0x3c(%rbx), %rax
leaq 0x1c(%r15), %rcx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
shlq $0x6, %rsi
movl (%r15,%rsi), %edi
addl %edi, 0x7c(%rbx)
movl 0x8(%r15,%rsi), %esi
addl %esi, 0x80(%rbx)
xorl %esi, %esi
movl (%rcx,%rsi,4), %edi
addl %edi, (%rax,%rsi,4)
incq %rsi
cmpq $0x10, %rsi
jne 0x11b3e
incq %rdx
addq $0xc0, %rcx
cmpq %r13, %rdx
jne 0x11b22
jmp 0x11b80
movl $0x0, 0x2c(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x6c(%rbx)
movups %xmm0, 0x5c(%rbx)
movups %xmm0, 0x4c(%rbx)
movups %xmm0, 0x3c(%rbx)
movq $0x0, 0x7c(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x116e8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x112c2
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq