// File: /workspace/github_c_makefile_star2_O1/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:
cmpq $0x0, 0x8(%rdi)
jle 0x4f41
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorl %ebp, %ebp
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movss %xmm0, 0xc(%rsp)
leaq 0x1(%rbp), %r15
cmpq 0x8(%r14), %r15
movq %r15, 0x10(%rsp)
jge 0x4f24
movq %rbp, %r12
notq %r12
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x30(%rsp), %r9
callq 0x52bb
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x4dc2
xorps %xmm0, %xmm0
cvtsi2ssq 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
xorps %xmm2, %xmm2
cvtsi2ssq 0x20(%rsp), %xmm2
divss %xmm1, %xmm0
divss %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
xorps %xmm5, %xmm5
cvtss2sd %xmm2, %xmm5
jmp 0x4dc8
xorps %xmm1, %xmm1
xorps %xmm5, %xmm5
movaps 0x3251(%rip), %xmm3     # 0x8020
movss 0x3231(%rip), %xmm4     # 0x8008
addsd %xmm1, %xmm1
movapd %xmm1, %xmm0
addsd %xmm5, %xmm0
cvtsd2ss %xmm0, %xmm0
addss 0x35c9(%rip), %xmm0     # 0x83b8
andps %xmm3, %xmm0
ucomiss %xmm0, %xmm4
ja 0x4e1e
movsd 0x35e1(%rip), %xmm0     # 0x83e0
subsd %xmm1, %xmm0
subsd %xmm5, %xmm0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
xorps %xmm2, %xmm2
ucomiss %xmm1, %xmm2
jbe 0x4e23
andps %xmm3, %xmm1
ucomiss %xmm1, %xmm4
ja 0x4e23
xorl %r13d, %r13d
jmp 0x4e50
movsd %xmm5, 0x18(%rsp)
callq 0x2140
movsd 0x18(%rsp), %xmm5
movss 0x31cc(%rip), %xmm4     # 0x8008
movaps 0x31dd(%rip), %xmm3     # 0x8020
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movb $0x1, %r13b
addsd %xmm5, %xmm5
xorps %xmm1, %xmm1
cvtsd2ss %xmm5, %xmm1
movaps %xmm1, %xmm0
addss 0x3552(%rip), %xmm0     # 0x83b8
andps %xmm3, %xmm0
ucomiss %xmm0, %xmm4
ja 0x4e7c
ucomiss 0x3197(%rip), %xmm1     # 0x800c
jbe 0x4e81
ucomiss %xmm0, %xmm4
ja 0x4e81
xorl %r13d, %r13d
jmp 0x4eab
movsd 0x3557(%rip), %xmm0     # 0x83e0
subsd %xmm5, %xmm0
callq 0x2140
movss 0x316e(%rip), %xmm4     # 0x8008
movaps 0x317f(%rip), %xmm3     # 0x8020
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x34fd(%rip), %xmm0     # 0x83b0
testb %r13b, %r13b
je 0x4ee2
xorps %xmm0, %xmm0
cvtss2sd 0xc(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x8(%rsp), %xmm1
mulsd 0x352e(%rip), %xmm1     # 0x8400
mulsd 0x352e(%rip), %xmm0     # 0x8408
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movaps %xmm0, %xmm1
andps %xmm3, %xmm1
movq 0x8(%r14), %rax
cmpltss %xmm4, %xmm1
andnps %xmm0, %xmm1
leaq (%r12,%rax,2), %rax
imulq %rbp, %rax
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
andq $-0x2, %rcx
addq %rcx, %rcx
addq 0x18(%r14), %rcx
movss %xmm1, (%rcx,%r15,4)
incq %r15
cmpq 0x8(%r14), %r15
jl 0x4d67
movq 0x10(%rsp), %rbp
cmpq 0x8(%r14), %rbp
jl 0x4d4e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq