// File: /workspace/github_c_makefile_star2_O1/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 0x1146a
movl %ecx, %eax
leaq 0x3ec9a(%rip), %rdx     # 0x500f0
movsbl (%rdx,%rax), %edx
movl %edx, 0x30(%rbx)
cmpl $0x4, %edx
movl $0x4, %eax
cmovll %edx, %eax
jmp 0x11473
movl $0xffffffff, 0x30(%rbx) # imm = 0xFFFFFFFF
xorl %eax, %eax
pxor %xmm0, %xmm0
movdqa %xmm0, (%rsp)
movl %r12d, %r13d
testl %r12d, %r12d
jle 0x114ac
leaq 0xc(%r15), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rdx,%rdi,4), %r8d
addl %r8d, (%rsp,%rdi,4)
incq %rdi
cmpq $0x4, %rdi
jne 0x1148c
incq %rsi
addq $0xc0, %rdx
cmpq %r13, %rsi
jne 0x1148a
movdqu %xmm0, 0x14(%rbx)
xorl %esi, %esi
xorl %edx, %edx
addl (%rsp,%rsi,4), %edx
incq %rsi
cmpq $0x4, %rsi
jne 0x114b5
xorl %esi, %esi
movl (%rsp,%rsi,4), %edi
leal (%rsi,%rdi,4), %edi
movl %edi, (%rsp,%rsi,4)
incq %rsi
cmpq $0x4, %rsi
jne 0x114c3
movl $0x1, %esi
movl $0x2, %edi
movl %edi, %r8d
movl %r8d, %r9d
leal -0x1(%r9), %r8d
movl (%rsp,%r8,4), %r10d
addl $-0x2, %r9d
movl (%rsp,%r9,4), %r11d
cmpl %r11d, %r10d
jge 0x11508
movl %r11d, (%rsp,%r8,4)
movl %r10d, (%rsp,%r9,4)
cmpl $0x1, %r8d
jg 0x114e2
incl %esi
incl %edi
cmpl $0x4, %esi
jne 0x114df
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rbx)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %esi, 0x10(%rbx)
movl %esi, 0x34(%rbx)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movl %eax, (%rbx)
movl $0x1, %edx
movb $0x1, %dil
movl $0x3, %esi
movl (%rsp,%rsi,4), %r10d
movl %r10d, %r8d
andl $0x3, %r8d
movl %r10d, %r9d
sarl $0x2, %r9d
cmpl %eax, %r8d
jne 0x11564
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0x14(%rbx)
jmp 0x1158b
cmpl $0x4, %r10d
jb 0x115a4
movslq %edx, %rdi
cmpl $0x3, %edx
jg 0x11584
xorps %xmm1, %xmm1
cvtsi2ss %r9d, %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0x14(%rbx,%rdi,4)
leal 0x1(%rdi), %edx
movl %r8d, (%rbx,%rdi,4)
leaq -0x1(%rsi), %rdi
cmpq $-0x1, %rdi
setne %dil
addq $-0x1, %rsi
jb 0x1153a
movq $-0x1, %rsi
testl %ecx, %ecx
js 0x115c2
cmpl $0x4, %eax
setl %cl
cmpl $0x4, %edx
setl %r8b
testb %r8b, %cl
je 0x115d4
testb $0x1, %dil
jne 0x115e7
jmp 0x115fb
movl %edx, 0x28(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, %edx
jne 0x115fe
jmp 0x117c5
cmpl $0x4, %eax
sete %al
cmpl $0x5, %edx
setl %cl
andb %al, %cl
testb %dil, %cl
je 0x115fb
movl %edx, 0x34(%rbx)
movslq %esi, %rax
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 0x11625
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)
movslq 0x28(%rbx), %rcx
testq %rcx, %rcx
jle 0x1166d
movl $0x1, %eax
xorl %edx, %edx
xorl %esi, %esi
movl (%rbx,%rdx,4), %r8d
movslq %esi, %rsi
leaq (%rsp,%rsi,4), %r9
addq $0x10, %r9
xorl %edi, %edi
movl (%rbx,%rdi,4), %r10d
leal (%r10,%r10,4), %r10d
addl %r8d, %r10d
movl %r10d, (%r9,%rdi,4)
incq %rdi
cmpq %rdi, %rax
jne 0x11648
incq %rdx
incq %rax
addq %rdi, %rsi
cmpq %rcx, %rdx
jne 0x11637
testl %r12d, %r12d
jle 0x1173f
leal 0x1(%rcx), %eax
imull %ecx, %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 0x2f3b1(%rip), %xmm1    # 0x40a48
movsd 0x2c981(%rip), %xmm2    # 0x3e020
movl $0xff, %edi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r9
shlq $0x6, %r9
addq %r15, %r9
movaps %xmm1, %xmm3
cmpl $0x2, %eax
jl 0x116db
xorl %r10d, %r10d
movaps %xmm1, %xmm4
movslq 0x10(%rsp,%r10,4), %r11
movss 0x5c(%r9,%r11,4), %xmm3
minss %xmm4, %xmm3
incq %r10
movaps %xmm3, %xmm4
cmpq %r10, %rdx
jne 0x116c0
cmpl $0x2, %eax
jl 0x1171a
movq 0x88(%rbx), %r10
addq %rsi, %r10
xorl %r11d, %r11d
movslq 0x10(%rsp,%r11,4), %rbp
movss 0x5c(%r9,%rbp,4), %xmm4
subss %xmm3, %xmm4
cvtss2sd %xmm4, %xmm4
addsd %xmm2, %xmm4
cvttsd2si %xmm4, %ebp
cmpl %edi, %ebp
cmovgel %edi, %ebp
movb %bpl, (%r10,%r11)
incq %r11
cmpq %r11, %rdx
jne 0x116ed
cvtss2sd %xmm3, %xmm3
addsd %xmm3, %xmm0
incq %r8
addq %rcx, %rsi
cmpq %r13, %r8
jne 0x116a7
addsd 0x2c8e7(%rip), %xmm0    # 0x3e020
cvttsd2si %xmm0, %eax
jmp 0x11741
xorl %eax, %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 0x117ad
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 0x1178f
incq %rdx
addq $0xc0, %rcx
cmpq %r13, %rdx
jne 0x11773
movq %r14, %rdi
movq %rbx, %rsi
callq 0x11349
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10f1c
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq