// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %rsi, %rbp
movq %rdi, %r15
movq %rsi, %rdi
callq 0x2f1c
cmpl $0x0, 0x38(%r15)
je 0x2992
xorl %r13d, %r13d
jmp 0x29aa
movq 0x8(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x2160
movq %rax, %r13
testq %rax, %rax
je 0x2db5
movq %rbp, %rdi
callq 0x2fb9
movq %rax, %rbx
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x10(%rbx), %rax
cmpq $0x3, %rax
jl 0x2d4d
movl 0x38(%r15), %ecx
testl %ecx, %ecx
je 0x2a84
cmpl $0x1, %ecx
jne 0x29c5
movb $0x1, %bl
xorl %r14d, %r14d
movq 0x8(%rbp), %rax
decq %rax
cmpq %rax, %r14
jge 0x2d15
movq 0x18(%rsp), %rax
cmpq $0x3, 0x10(%rax)
jl 0x2d15
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq 0x3588
movq 0x48(%rsp), %r12
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r12, %rcx
movl 0x4(%rsp), %r8d
callq 0x36b4
testl %eax, %eax
jne 0x2a3e
incq %r14
jmp 0x29e5
xorl %ebx, %ebx
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x30b6
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x31a4
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x328c
subq $0x1, %r14
movl $0x0, %eax
cmovbq %rax, %r14
jmp 0x29e5
movq 0x8(%rbp), %rsi
decq %rsi
movq %r13, %rdi
callq 0x3509
xorl %r14d, %r14d
movl $0x0, 0x10(%rsp)
movq %r15, 0x58(%rsp)
movq %r13, 0x30(%rsp)
movq %rbp, 0x50(%rsp)
movq 0x8(%rbp), %rax
decq %rax
cmpq %rax, %r14
jge 0x2d32
cmpq $0x3, 0x10(%rbx)
jl 0x2d32
movq (%r13,%r14,8), %r12
movq %rbp, %rdi
movq %r12, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq 0x3588
movq %r12, %rax
testq %r12, %r12
movss %xmm0, 0x14(%rsp)
je 0x2c05
movq %r14, 0x60(%rsp)
movq 0x8(%rbp), %r13
movq 0x30(%rbp), %rdx
leaq (,%rax,4), %r12
addq 0x18(%rbp), %r12
testq %rax, %rax
movl $0x0, %ebx
cmovgq %rax, %rbx
decq %r13
movss 0x44e0(%rip), %xmm3     # 0x7004
movq $0x0, 0x20(%rsp)
xorl %ebp, %ebp
xorl %esi, %esi
movq %rdx, 0x38(%rsp)
cmpq %rbp, %rbx
je 0x2bdf
movss (%r12), %xmm0
movss (%rdx,%rbp,4), %xmm1
addss (%rdx,%rax,4), %xmm1
subss %xmm1, %xmm0
movaps %xmm0, %xmm1
subss %xmm3, %xmm1
andps 0x44bf(%rip), %xmm1     # 0x7020
movss 0x449f(%rip), %xmm2     # 0x7008
ucomiss %xmm1, %xmm2
ja 0x2b80
ucomiss %xmm0, %xmm3
jbe 0x2bd0
movaps %xmm0, %xmm3
movq %rbp, 0x20(%rsp)
pushq $0x1
popq %rsi
jmp 0x2bd0
movss %xmm3, 0x8(%rsp)
movq %rax, %r15
incq %rsi
movq %rsi, %r14
cvtsi2ss %rsi, %xmm0
movss 0x4470(%rip), %xmm1     # 0x700c
divss %xmm0, %xmm1
movss %xmm1, 0xc(%rsp)
callq 0x65db
cvtsd2ss %xmm0, %xmm0
movss 0xc(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x2bbf
movq %rbp, 0x20(%rsp)
movq %r15, %rax
movss 0x8(%rsp), %xmm3
movq 0x38(%rsp), %rdx
movq %r14, %rsi
leaq (%r12,%r13,4), %r12
incq %rbp
decq %r13
jmp 0x2b36
cvtsi2ss %rsi, %xmm4
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rbp
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %r12
movq 0x60(%rsp), %r14
movss 0x14(%rsp), %xmm0
jmp 0x2c10
xorps %xmm4, %xmm4
movq 0x28(%rsp), %r12
movaps %xmm0, %xmm3
movaps %xmm0, %xmm1
subss %xmm3, %xmm1
andps 0x4402(%rip), %xmm1     # 0x7020
movss 0x43e2(%rip), %xmm2     # 0x7008
ucomiss %xmm1, %xmm2
ja 0x2cbe
ucomiss %xmm0, %xmm3
jbe 0x2c3c
movq 0x28(%rsp), %r12
movaps %xmm0, %xmm3
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %r13
movq %rax, %rdx
movq %r12, %rcx
movaps %xmm3, %xmm0
movl 0x4(%rsp), %r8d
callq 0x36b4
testl %eax, %eax
je 0x2d08
cmpq %r13, %r12
movq %r12, %rax
movq %r13, %r12
cmovgq %rax, %r12
cmovlq %rax, %r13
movq %rbp, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
callq 0x30b6
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x31a4
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x328c
movq 0x8(%rbp), %rsi
decq %rsi
movq 0x30(%rsp), %r13
movq %r13, %rdi
callq 0x3509
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
jmp 0x2d0d
cvtsi2ssq 0x40(%rsp), %xmm1
addss %xmm1, %xmm4
divss %xmm4, %xmm1
movss %xmm1, 0xc(%rsp)
movq %rax, %r13
movss %xmm3, 0x8(%rsp)
callq 0x65db
movss 0x8(%rsp), %xmm3
movq %r13, %rax
cvtsd2ss %xmm0, %xmm0
movss 0xc(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movss 0x14(%rsp), %xmm0
ja 0x2c34
jmp 0x2c3c
movq 0x30(%rsp), %r13
incq %r14
jmp 0x2aad
testb $0x1, %bl
movl $0x0, %eax
movl 0x4(%rsp), %ecx
cmovnel %eax, %ecx
movl %ecx, 0x4(%rsp)
movq 0x18(%rsp), %rbx
jmp 0x29c1
movq %r12, 0x48(%rsp)
movl 0x10(%rsp), %ecx
testl %ecx, %ecx
movl 0x4(%rsp), %eax
cmovel %ecx, %eax
movl %eax, 0x4(%rsp)
jmp 0x29c1
pushq $0x1
popq %rcx
pushq $0x65
popq %r8
movq %rbp, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x30b6
movq %rax, %r14
cmpl $0x0, 0x20(%r15)
je 0x2d87
cmpl $0x0, 0x4(%rsp)
leaq 0x4611(%rip), %rax      # 0x7388
leaq 0x461f(%rip), %rdi      # 0x739d
cmoveq %rax, %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x38e5
testq %r13, %r13
je 0x2da3
cmpl $0x0, 0x38(%r15)
jne 0x2da3
movq %r13, %rdi
callq 0x2040
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x923c(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x43d0(%rip), %rdi      # 0x7196
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
xorl %r14d, %r14d
jmp 0x2da3