// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x8(%rdi), %rcx
xorl %r12d, %r12d
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
cmpq %rcx, %r12
jge 0x4375
leaq 0x1(%r12), %r15
movq %r12, %rbp
notq %rbp
movq %r15, 0x10(%rsp)
cmpq %rcx, %r15
jge 0x436b
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x46c6
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x4213
cvtsi2ss %rax, %xmm0
cvtsi2ssq 0x18(%rsp), %xmm1
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 0x4217
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 0x3181(%rip), %xmm0     # 0x73b8
andps 0x2de2(%rip), %xmm0     # 0x7020
movss 0x2dc2(%rip), %xmm2     # 0x7008
ucomiss %xmm0, %xmm2
ja 0x427f
addsd %xmm1, %xmm1
movsd 0x3181(%rip), %xmm0     # 0x73d8
subsd %xmm1, %xmm0
subsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm1
xorps %xmm2, %xmm2
ucomiss %xmm1, %xmm2
jbe 0x4284
andps 0x2dae(%rip), %xmm1     # 0x7020
movss 0x2d8e(%rip), %xmm2     # 0x7008
ucomiss %xmm1, %xmm2
ja 0x4284
xorl %r13d, %r13d
jmp 0x42a2
movapd %xmm3, 0x30(%rsp)
callq 0x2140
movapd 0x30(%rsp), %xmm3
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movb $0x1, %r13b
addsd %xmm3, %xmm3
cvtsd2ss %xmm3, %xmm0
movaps %xmm0, %xmm1
addss 0x3103(%rip), %xmm1     # 0x73b8
andps 0x2d64(%rip), %xmm1     # 0x7020
movss 0x2d44(%rip), %xmm2     # 0x7008
ucomiss %xmm1, %xmm2
movss 0x30e1(%rip), %xmm2     # 0x73b0
movaps %xmm2, %xmm1
ja 0x4324
ucomiss 0x2d31(%rip), %xmm0     # 0x700c
movaps %xmm2, %xmm1
ja 0x4324
movsd 0x30f0(%rip), %xmm0     # 0x73d8
subsd %xmm3, %xmm0
callq 0x2140
movss 0x30b7(%rip), %xmm1     # 0x73b0
testb %r13b, %r13b
je 0x4324
cvtsd2ss %xmm0, %xmm0
cvtss2sd 0xc(%rsp), %xmm1
cvtss2sd %xmm0, %xmm0
mulsd 0x30e4(%rip), %xmm0     # 0x73f8
mulsd 0x30e4(%rip), %xmm1     # 0x7400
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movaps %xmm1, %xmm0
andps 0x2cf2(%rip), %xmm0     # 0x7020
movq 0x8(%r14), %rcx
leaq (,%rcx,2), %rax
addq %rbp, %rax
imulq %r12, %rax
cqto
pushq $0x2
popq %rsi
idivq %rsi
shlq $0x2, %rax
addq 0x18(%r14), %rax
cmpltss 0x2cae(%rip), %xmm0     # 0x7008
andnps %xmm1, %xmm0
movss %xmm0, (%rax,%r15,4)
incq %r15
jmp 0x41c1
movq 0x10(%rsp), %r12
jmp 0x41a8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq