// File: /workspace/github_c_makefile_star2_O3/sheneman[P]clearcut/dist.c
// Function: NJ_DNA_k2p_correction
void
NJ_DNA_k2p_correction(DMAT *dmat,
		      NJ_alignment *alignment) {

  long int i, j;
  float P;  /* proportion of transitions   */
  float Q;  /* proportion of transversions */
  long int nucleotides;
  long int transitions, transversions;
  float dist;
  float log_x = 0.0;  /* the params for the first log  */
  float log_y = 0.0;  /* the params for the second log */

  int blowup;   /* a flag to specify if we have a log blowup */

  
  for(i=0;i<dmat->size;i++) {
    for(j=i+1;j<dmat->size;j++) {

      blowup = 0;

      /* count the number of transitions and transversions */
      NJ_DNA_count_tt(alignment, i, j, &transitions, &transversions, &nucleotides);

      if(!nucleotides) {   /* sequences have no non-ambiguous overlap in alignment */
	P = 0.0;
	Q = 0.0;
      } else {
	P = (float)transitions   / (float)nucleotides;
	Q = (float)transversions / (float)nucleotides;
      }

      /* the first log blows up if 2*P+Q = 1.0 */
      if(NJ_FLT_EQ((2.0 * P + Q), 1.0)) {
	blowup = 1;
      } else {
	if( NJ_FLT_LT(1.0 - 2.0*P - Q, 0.0) ) {
	  blowup = 1;
	} else {
	  log_x = log(1.0 - 2.0*P - Q);
	}
      }

      /* the second log blows up if 2*Q >= 1.0 */
      if( NJ_FLT_EQ((2.0 * Q), 1.0) ||
	  NJ_FLT_GT((2.0 * Q), 1.0) ) {
	blowup = 1;
      } else {
	log_y = log(1.0 - 2.0*Q);
      }
      
      /* if our logarithms blow up, we just set the distance to the max */
      if(blowup) {
	dist = NJ_BIGDIST;
      } else {
	dist = (-0.5)*log_x - 0.25*log_y;
      }
      
      if(fabs(dist) < FLT_EPSILON) {
	dmat->val[NJ_MAP(i, j, dmat->size)] = 0.0;
      } else {
	dmat->val[NJ_MAP(i, j, dmat->size)] = dist;
      }
    }
  }
  
  return;
}

// Assembly:
movq 0x8(%rdi), %rax
testq %rax, %rax
jle 0x50f1
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorl %ebp, %ebp
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
leaq 0x1(%rbp), %r15
cmpq %rax, %r15
movq %r15, 0x10(%rsp)
jge 0x50d5
movq %rbp, %r12
notq %r12
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x5455
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x4f72
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ssq 0x18(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtsi2ssq 0x20(%rsp), %xmm2
unpcklps %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
shufps $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
divps %xmm0, %xmm2
cvtps2pd %xmm2, %xmm1
jmp 0x4f76
xorpd %xmm1, %xmm1
movapd %xmm1, %xmm3
unpckhpd %xmm1, %xmm3            # xmm3 = xmm3[1],xmm1[1]
movapd %xmm1, %xmm0
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
addss 0x3462(%rip), %xmm0     # 0x83f8
andps 0x30a3(%rip), %xmm0     # 0x8040
movss 0x30cf(%rip), %xmm2     # 0x8074
ucomiss %xmm0, %xmm2
ja 0x4fe1
addsd %xmm1, %xmm1
movsd 0x346a(%rip), %xmm0     # 0x8420
subsd %xmm1, %xmm0
subsd %xmm3, %xmm0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
xorps %xmm2, %xmm2
ucomiss %xmm1, %xmm2
jbe 0x4fe6
andps 0x306c(%rip), %xmm1     # 0x8040
movss 0x3098(%rip), %xmm2     # 0x8074
ucomiss %xmm1, %xmm2
ja 0x4fe6
xorl %r13d, %r13d
jmp 0x5004
movapd %xmm3, 0x30(%rsp)
callq 0x2140
movapd 0x30(%rsp), %xmm3
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movb $0x1, %r13b
addsd %xmm3, %xmm3
xorps %xmm0, %xmm0
cvtsd2ss %xmm3, %xmm0
movaps %xmm0, %xmm1
addss 0x33de(%rip), %xmm1     # 0x83f8
andps 0x301f(%rip), %xmm1     # 0x8040
movss 0x304b(%rip), %xmm2     # 0x8074
ucomiss %xmm1, %xmm2
movss 0x33bc(%rip), %xmm2     # 0x83f0
movaps %xmm2, %xmm1
ja 0x508c
ucomiss 0x3038(%rip), %xmm0     # 0x8078
movaps %xmm2, %xmm1
ja 0x508c
movsd 0x33d3(%rip), %xmm0     # 0x8420
subsd %xmm3, %xmm0
callq 0x2140
movss 0x3392(%rip), %xmm1     # 0x83f0
testb %r13b, %r13b
je 0x508c
cvtsd2ss %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0xc(%rsp), %xmm1
cvtss2sd %xmm0, %xmm0
mulsd 0x33c4(%rip), %xmm0     # 0x8440
mulsd 0x33c4(%rip), %xmm1     # 0x8448
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movaps %xmm1, %xmm0
andps 0x2faa(%rip), %xmm0     # 0x8040
movq 0x8(%r14), %rax
leaq (%r12,%rax,2), %rcx
imulq %rbp, %rcx
movq %rcx, %rdx
shrq $0x3f, %rdx
addq %rcx, %rdx
andq $-0x2, %rdx
addq %rdx, %rdx
addq 0x18(%r14), %rdx
cmpltss 0x2fb4(%rip), %xmm0     # 0x8074
andnps %xmm1, %xmm0
movss %xmm0, (%rdx,%r15,4)
incq %r15
cmpq %rax, %r15
jl 0x4f20
movq 0x10(%rsp), %rbp
cmpq %rax, %rbp
jl 0x4f08
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq