// File: /workspace/github_c_makefile_star2_O1/sheneman[P]clearcut/clearcut.c
// Function: NJ_relaxed_nj
NJ_TREE *
NJ_relaxed_nj(NJ_ARGS *nj_args,
	      DMAT *dmat) {

  
  NJ_TREE *tree;
  NJ_VERTEX *vertex;
  long int a, b, t, bh, bv, i;
  float hmin, vmin, hvmin;
  float p, q, x;
  int join_flag;
  int additivity_mode;
  long int hmincount, vmincount;
  long int *permutation = NULL;



  /* initialize the r and r2 vectors */
  NJ_init_r(dmat);

  additivity_mode = 1;

  /* allocate the permutation vector, if we are in randomize mode */
  if(!nj_args->norandom) {
    permutation = (long int *)calloc(dmat->size, sizeof(long int));
    if(!permutation) {
      fprintf(stderr, "Clearcut:  Memory allocation error in NJ_relaxed_nj()\n");
      return(NULL);
    }
  }

  /* allocate and initialize our vertex vector used for tree construction */
  vertex = NJ_init_vertex(dmat);
  
  /* loop until there are only 2 nodes left to join */
  while(vertex->nactive > 2) {

    switch(nj_args->norandom) {

      /* RANDOMIZED JOINS */
    case 0:

      join_flag = 0;

      NJ_permute(permutation, dmat->size-1);
      for(i=0;i<dmat->size-1 && (vertex->nactive>2) ;i++) {

	a = permutation[i];

	/* find min trans dist along horiz. of row a */
	hmin = NJ_find_hmin(dmat, a, &bh, &hmincount);   
	if(a) {
	  /* find min trans dist along vert. of row a */
	  vmin = NJ_find_vmin(dmat, a, &bv, &vmincount); 
	} else {
	  vmin = hmin;
	  bv = bh;
	  vmincount = 0;
	}
	
	if(NJ_FLT_EQ(hmin, vmin)) {

	  /* 
	   * The minima along the vertical and horizontal are 
	   * the same.  Compute the proportion of minima along
	   * the horizonal (p) and the proportion of minima 
	   * along the vertical (q).
	   * 
	   * If the same minima exist along the horizonal and
	   * vertical, we break the tie in a way which is
	   * non-biased.  That is, we break the tie based on the
	   * proportion of horiz. minima versus vertical minima.
	   * 
	   */
	  p = (float)hmincount / ((float)hmincount + (float)vmincount);
	  q = 1.0 - p;
	  x = genrand_real2();
	  
	  if(x < p) {
	    hvmin = hmin;
	    b     = bh;
	  } else {
	    hvmin = vmin;
	    b     = bv;
	  }
	} else if(NJ_FLT_LT(hmin, vmin) ) {
	  hvmin   = hmin;
	  b       = bh;
	} else {
	  hvmin   = vmin;
	  b       = bv;
	}
	
	if(NJ_check(nj_args, dmat, a, b, hvmin, additivity_mode)) {

	  /* swap a and b, if necessary, to make sure a < b */
	  if(b < a) {
	    t = a;
	    a = b;
	    b = t;
	  }

	  join_flag = 1;
	
	  /* join taxa from rows a and b */
	  NJ_decompose(dmat, vertex, a, b, 0);

	  /* collapse matrix */
	  NJ_compute_r(dmat, a, b);
	  NJ_collapse(dmat, vertex, a, b);
	  
	  NJ_permute(permutation, dmat->size-1);
	}
      }
      
      /* turn off additivity if go through an entire cycle without joining */
      if(!join_flag) {
	additivity_mode = 0;
      }
      
      break;



      /* DETERMINISTIC JOINS */
    case 1:
      
      join_flag = 0;

      for(a=0;a<dmat->size-1 && (vertex->nactive > 2) ;) {
      
	/* find the min along the horizontal of row a */
	hmin = NJ_find_hmin(dmat, a, &b, &hmincount);
      
	if(NJ_check(nj_args, dmat, a, b, hmin, additivity_mode)) {
	
	  join_flag = 1;
	
	  /* join taxa from rows a and b */
	  NJ_decompose(dmat, vertex, a, b, 0);

	  /* collapse matrix */
	  NJ_compute_r(dmat, a, b);
	  NJ_collapse(dmat, vertex, a, b);

	  if(a) { 
	    a--; 
	  }
	
	} else {
	  a++;
	}
      }
    
      /* turn off additivity if go through an entire cycle without joining */
      if(!join_flag) {
	additivity_mode = 0;
      }

      break;
    }
 
  }  /* WHILE */

  /* Join the last two nodes on the vertex list */
  tree = NJ_decompose(dmat, vertex, 0, 1, NJ_LAST);
  
  if(nj_args->verbose_flag) {
    if(additivity_mode) {
      printf("Tree is additive\n");
    } else {
      printf("Tree is not additive\n");
    }
  }
  
  if(vertex) {
    NJ_free_vertex(vertex);
  }
  
  if(!nj_args->norandom && permutation) {
    free(permutation);
  }
  
  return(tree);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r12
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x3cc9
movq %rbx, 0x20(%rsp)
cmpl $0x0, 0x38(%rbx)
je 0x32c4
xorl %r15d, %r15d
jmp 0x32df
movq 0x8(%r12), %rdi
movl $0x8, %esi
callq 0x2160
movq %rax, %r15
testq %rax, %rax
je 0x3b71
movq %r12, %rdi
callq 0x3d40
movq %rax, %r14
cmpq $0x3, 0x10(%rax)
jl 0x3b05
movl $0x1, (%rsp)
movq %r15, 0x58(%rsp)
movq %r12, 0x50(%rsp)
movq %r14, 0x8(%rsp)
movq 0x20(%rsp), %rax
movl 0x38(%rax), %eax
testl %eax, %eax
je 0x35c6
cmpl $0x1, %eax
jne 0x3ae2
movq 0x8(%r12), %rax
cmpq $0x2, %rax
jl 0x3acd
movq $0x0, 0x10(%rsp)
xorl %edx, %edx
cmpq $0x3, 0x10(%r14)
jl 0x35bc
leaq 0x1(%rdx), %r13
movslq %eax, %rsi
cmpq %rsi, %r13
jge 0x3463
movq 0x30(%r12), %rdi
shlq $0x20, %rax
sarq $0x1f, %rax
movq %rdx, %rcx
notq %rcx
addq %rax, %rcx
imulq %rdx, %rcx
movq %rcx, %rbp
shrq $0x3f, %rbp
addq %rcx, %rbp
addq %rbp, %rbp
andq $-0x4, %rbp
addq 0x18(%r12), %rbp
xorl %ecx, %ecx
movq %r13, %r12
movl $0x0, 0x4(%rsp)
movss 0x4c66(%rip), %xmm0     # 0x8004
movq %rdi, 0x18(%rsp)
movss (%rbp,%r12,4), %xmm3
movss (%rdi,%rdx,4), %xmm1
addss (%rdi,%r12,4), %xmm1
subss %xmm1, %xmm3
movaps %xmm3, %xmm1
subss %xmm0, %xmm1
andps 0x4c59(%rip), %xmm1     # 0x8020
movss 0x4c39(%rip), %xmm2     # 0x8008
ucomiss %xmm1, %xmm2
ja 0x33e8
ucomiss %xmm3, %xmm0
jbe 0x3441
movl $0x1, %ecx
movaps %xmm3, %xmm0
movl %r12d, 0x4(%rsp)
jmp 0x3441
movss %xmm0, 0x30(%rsp)
movq %rsi, %r14
movq %rdx, %rbx
incq %rcx
movq %rcx, %r15
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
movss 0x4c02(%rip), %xmm1     # 0x800c
divss %xmm0, %xmm1
movss %xmm1, 0x40(%rsp)
callq 0x7048
cvtsd2ss %xmm0, %xmm0
movss 0x40(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x342d
movl %r12d, 0x4(%rsp)
movq %rbx, %rdx
movq %r14, %rsi
movq 0x18(%rsp), %rdi
movss 0x30(%rsp), %xmm0
movq %r15, %rcx
incq %r12
cmpq %r12, %rsi
jne 0x33a3
movslq 0x4(%rsp), %rbx
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %r12
movq 0x8(%rsp), %r14
jmp 0x346d
movss 0x4b99(%rip), %xmm0     # 0x8004
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rcx
movl (%rsp), %r8d
movq %rdx, %rbp
callq 0x41ec
testl %eax, %eax
je 0x35a7
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x3e48
movq 0x8(%r12), %rax
movq 0x18(%r12), %rdx
movq 0x28(%r12), %rcx
cmpq %rax, %r13
jge 0x352a
movq %rbx, %rdi
notq %rdi
leaq (%rdi,%rax,2), %r8
imulq %rbx, %r8
movq %r8, %rdi
shrq $0x3f, %rdi
addq %r8, %rdi
andq $-0x2, %rdi
leaq (%rdx,%rdi,2), %rdi
leaq (%rdi,%rbx,4), %r9
addq $0x4, %r9
movq %rbp, %rdi
notq %rdi
leaq (%rdi,%rax,2), %rdi
imulq %rbp, %rdi
movq %rdi, %r8
shrq $0x3f, %r8
addq %rdi, %r8
andq $-0x2, %r8
leaq (%rdx,%r8,2), %rdi
movss (%rcx,%r13,4), %xmm0
subss (%rdi,%r13,4), %xmm0
movss %xmm0, (%rcx,%r13,4)
cmpq %rbx, %r13
jle 0x3522
subss (%r9), %xmm0
addq $0x4, %r9
movss %xmm0, (%rcx,%r13,4)
incq %r13
cmpq %r13, %rax
jne 0x34fc
testq %rbx, %rbx
jle 0x3581
leaq (%rdx,%rbx,4), %r9
leaq (%rdx,%rbp,4), %rdx
leaq -0x4(,%rax,4), %rdi
decq %rax
xorl %r8d, %r8d
cmpq %rbp, %r8
jge 0x355d
movss (%rcx,%r8,4), %xmm0
subss (%rdx), %xmm0
movss %xmm0, (%rcx,%r8,4)
addq %rdi, %rdx
movss (%rcx,%r8,4), %xmm0
subss (%r9), %xmm0
movss %xmm0, (%rcx,%r8,4)
leaq (%r9,%rax,4), %r9
incq %r8
addq $-0x4, %rdi
decq %rax
cmpq %r8, %rbx
jne 0x3545
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x3f65
subq $0x1, %rbp
movl $0x0, %r13d
cmovaeq %rbp, %r13
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movq 0x8(%r12), %rax
leaq -0x1(%rax), %rcx
movq %r13, %rdx
cmpq %rcx, %r13
jl 0x333e
movq 0x10(%rsp), %rcx
jmp 0x3ac0
testq %r15, %r15
je 0x3b7a
movq 0x8(%r12), %rax
cmpq $0x2, %rax
jl 0x3619
leaq -0x1(%rax), %rbx
xorl %ecx, %ecx
movq %rcx, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x35e0
movq %r15, %r14
cmpq $0x2, %rax
jl 0x3619
movq %rbx, %rdi
callq 0x70e8
testq %rax, %rax
je 0x3610
movq (%r14,%rax,8), %rcx
movq (%r14), %rdx
movq %rdx, (%r14,%rax,8)
movq %rcx, (%r14)
addq $0x8, %r14
decq %rbx
jne 0x35f5
movq 0x8(%r12), %rax
cmpq $0x2, %rax
jl 0x3ad6
movq $0x0, 0x60(%rsp)
xorl %edx, %edx
movq 0x8(%rsp), %r14
cmpq $0x3, 0x10(%r14)
jl 0x3abb
movq %rdx, 0x70(%rsp)
movq (%r15,%rdx,8), %rbp
leaq 0x1(%rbp), %rbx
movslq %eax, %r13
xorps %xmm0, %xmm0
movss %xmm0, 0x2c(%rsp)
cmpq %r13, %rbx
jge 0x375c
shlq $0x20, %rax
sarq $0x1f, %rax
movq %rbp, %rcx
notq %rcx
addq %rax, %rcx
imulq %rbp, %rcx
movq %rcx, %r14
shrq $0x3f, %r14
addq %rcx, %r14
addq %r14, %r14
andq $-0x4, %r14
addq 0x18(%r12), %r14
movq 0x30(%r12), %r15
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
movss 0x495c(%rip), %xmm3     # 0x8004
movss (%r14,%rbx,4), %xmm0
movss (%r15,%rbp,4), %xmm1
addss (%r15,%rbx,4), %xmm1
subss %xmm1, %xmm0
movaps %xmm0, %xmm1
subss %xmm3, %xmm1
andps 0x4954(%rip), %xmm1     # 0x8020
movss 0x4934(%rip), %xmm2     # 0x8008
ucomiss %xmm1, %xmm2
ja 0x36ed
ucomiss %xmm0, %xmm3
jbe 0x372c
movl $0x1, %r12d
movaps %xmm0, %xmm3
movl %ebx, 0x4(%rsp)
jmp 0x372c
movaps %xmm3, 0x30(%rsp)
incq %r12
xorps %xmm0, %xmm0
cvtsi2ss %r12, %xmm0
movss 0x4907(%rip), %xmm1     # 0x800c
divss %xmm0, %xmm1
movss %xmm1, 0x40(%rsp)
callq 0x7048
cvtsd2ss %xmm0, %xmm0
movss 0x40(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x3727
movl %ebx, 0x4(%rsp)
movaps 0x30(%rsp), %xmm3
incq %rbx
cmpq %rbx, %r13
jne 0x36a8
movslq 0x4(%rsp), %r13
xorps %xmm0, %xmm0
cvtsi2ss %r12, %xmm0
movss %xmm0, 0x10(%rsp)
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %r12
movq 0x8(%rsp), %r14
jmp 0x376d
movss 0x48a0(%rip), %xmm3     # 0x8004
xorl %r13d, %r13d
movss %xmm0, 0x10(%rsp)
testq %rbp, %rbp
movaps %xmm3, 0x30(%rsp)
je 0x387e
jle 0x3886
movq %r13, 0x68(%rsp)
movq 0x8(%r12), %r14
movq 0x30(%r12), %r13
leaq (,%rbp,4), %r15
addq 0x18(%r12), %r15
decq %r14
xorl %r12d, %r12d
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
movss 0x484e(%rip), %xmm4     # 0x8004
movss (%r15), %xmm0
movss (%r13,%rbx,4), %xmm1
addss (%r13,%rbp,4), %xmm1
subss %xmm1, %xmm0
movaps %xmm0, %xmm1
subss %xmm4, %xmm1
andps 0x4845(%rip), %xmm1     # 0x8020
movss 0x4825(%rip), %xmm2     # 0x8008
ucomiss %xmm1, %xmm2
ja 0x37fd
ucomiss %xmm0, %xmm4
jbe 0x3842
movl $0x1, %r12d
movaps %xmm0, %xmm4
movq %rbx, 0x18(%rsp)
jmp 0x3842
movaps %xmm4, 0x40(%rsp)
incq %r12
xorps %xmm0, %xmm0
cvtsi2ss %r12, %xmm0
movss 0x47f7(%rip), %xmm1     # 0x800c
divss %xmm0, %xmm1
movss %xmm1, 0x4(%rsp)
callq 0x7048
cvtsd2ss %xmm0, %xmm0
movss 0x4(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x3838
movq %rbx, 0x18(%rsp)
movaps 0x30(%rsp), %xmm3
movaps 0x40(%rsp), %xmm4
leaq (%r15,%r14,4), %r15
incq %rbx
decq %r14
cmpq %rbx, %rbp
jne 0x37b6
xorps %xmm0, %xmm0
cvtsi2ss %r12, %xmm0
movss %xmm0, 0x2c(%rsp)
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %r12
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rbx
movq 0x68(%rsp), %r13
jmp 0x3890
movq %r13, %rbx
movaps %xmm3, %xmm4
jmp 0x3890
xorl %ebx, %ebx
movss 0x4774(%rip), %xmm4     # 0x8004
movaps %xmm3, %xmm0
subss %xmm4, %xmm0
andps 0x4782(%rip), %xmm0     # 0x8020
movss 0x4762(%rip), %xmm1     # 0x8008
ucomiss %xmm0, %xmm1
ja 0x38d5
movss 0x4755(%rip), %xmm1     # 0x8008
cmpltps %xmm1, %xmm0
movaps %xmm3, %xmm1
cmpnltps %xmm4, %xmm1
orps %xmm0, %xmm1
movd %xmm1, %eax
testb $0x1, %al
cmoveq %r13, %rbx
jne 0x38d0
movaps %xmm3, %xmm4
movaps %xmm4, %xmm1
jmp 0x3916
movaps %xmm4, 0x40(%rsp)
movss 0x2c(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
addss %xmm1, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x10(%rsp)
callq 0x7048
movaps 0x30(%rsp), %xmm1
cvtsd2ss %xmm0, %xmm0
movss 0x10(%rsp), %xmm2
ucomiss %xmm0, %xmm2
cmovaq %r13, %rbx
ja 0x3916
movaps 0x40(%rsp), %xmm1
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movaps %xmm1, %xmm0
movl (%rsp), %r8d
callq 0x41ec
testl %eax, %eax
je 0x3aa1
cmpq %rbp, %rbx
movq %rbp, %r13
cmovgq %rbx, %r13
cmovlq %rbx, %rbp
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r13, %rcx
xorl %r8d, %r8d
callq 0x3e48
movq 0x8(%r12), %rax
movq 0x18(%r12), %rdx
movq 0x28(%r12), %rcx
leaq 0x1(%rbp), %rsi
cmpq %rax, %rsi
jge 0x39e4
movq %r13, %rdi
notq %rdi
leaq (%rdi,%rax,2), %rdi
imulq %r13, %rdi
movq %rdi, %r8
shrq $0x3f, %r8
addq %rdi, %r8
andq $-0x2, %r8
leaq (%rdx,%r8,2), %rdi
leaq (%rdi,%r13,4), %rdi
addq $0x4, %rdi
movq %rbp, %r8
notq %r8
leaq (%r8,%rax,2), %r8
imulq %rbp, %r8
movq %r8, %r9
shrq $0x3f, %r9
addq %r8, %r9
andq $-0x2, %r9
leaq (%rdx,%r9,2), %r8
movss (%rcx,%rsi,4), %xmm0
subss (%r8,%rsi,4), %xmm0
movss %xmm0, (%rcx,%rsi,4)
cmpq %r13, %rsi
jle 0x39dc
subss (%rdi), %xmm0
addq $0x4, %rdi
movss %xmm0, (%rcx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x39ba
testq %r13, %r13
jle 0x3a3a
leaq (%rdx,%r13,4), %rsi
leaq (%rdx,%rbp,4), %rdx
leaq -0x4(,%rax,4), %rdi
decq %rax
xorl %r8d, %r8d
cmpq %rbp, %r8
jge 0x3a17
movss (%rcx,%r8,4), %xmm0
subss (%rdx), %xmm0
movss %xmm0, (%rcx,%r8,4)
addq %rdi, %rdx
movss (%rcx,%r8,4), %xmm0
subss (%rsi), %xmm0
movss %xmm0, (%rcx,%r8,4)
leaq (%rsi,%rax,4), %rsi
incq %r8
addq $-0x4, %rdi
decq %rax
cmpq %r8, %r13
jne 0x39ff
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x3f65
movq 0x8(%r12), %rax
movl $0x1, %ecx
movq %rcx, 0x60(%rsp)
cmpq $0x2, %rax
jl 0x3aa1
leaq -0x1(%rax), %rbx
xorl %ecx, %ecx
movq %rcx, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x3a63
movq %r15, %r14
cmpq $0x2, %rax
jl 0x3a9c
movq %rbx, %rdi
callq 0x70e8
testq %rax, %rax
je 0x3a93
movq (%r14,%rax,8), %rcx
movq (%r14), %rdx
movq %rdx, (%r14,%rax,8)
movq %rcx, (%r14)
addq $0x8, %r14
decq %rbx
jne 0x3a78
movq 0x8(%rsp), %r14
movq 0x70(%rsp), %rdx
incq %rdx
movq 0x8(%r12), %rax
leaq -0x1(%rax), %rcx
cmpq %rcx, %rdx
jl 0x3638
movq 0x60(%rsp), %rcx
testl %ecx, %ecx
movl (%rsp), %eax
cmovel %ecx, %eax
movl %eax, (%rsp)
jmp 0x3ae2
movl $0x0, (%rsp)
jmp 0x3ae2
movl $0x0, (%rsp)
movq 0x8(%rsp), %r14
cmpq $0x2, 0x10(%r14)
jg 0x330b
cmpl $0x0, (%rsp)
leaq 0x4890(%rip), %rax      # 0x8388
leaq 0x489e(%rip), %r13      # 0x839d
cmoveq %rax, %r13
jmp 0x3b0c
leaq 0x4891(%rip), %r13      # 0x839d
movl $0x1, %ecx
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl $0x65, %r8d
callq 0x3e48
movq %rax, %rbx
movq 0x20(%rsp), %rax
cmpl $0x0, 0x20(%rax)
je 0x3b3a
movq %r13, %rdi
callq 0x20b0
testq %r14, %r14
je 0x3b47
movq %r14, %rdi
callq 0x444d
testq %r15, %r15
je 0x3b5f
movq 0x20(%rsp), %rax
cmpl $0x0, 0x38(%rax)
jne 0x3b5f
movq %r15, %rdi
callq 0x2040
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22ef
xorl %ebx, %ebx
jmp 0x3b5f
callq 0x22ca