// File: /workspace/github_c_makefile_star2_O2/sheneman[P]clearcut/clearcut.c
// Function: NJ_shuffle_distance_matrix
void
NJ_shuffle_distance_matrix(DMAT *dmat) {

  
  long int *perm      = NULL;
  char **tmp_taxaname = NULL;
  float *tmp_val      = NULL;
  long int i, j;

  
  /* alloc the random permutation and a new matrix to hold the shuffled vals */
  perm         = (long int *)calloc(dmat->size, sizeof(long int));
  tmp_taxaname = (char **)calloc(dmat->size, sizeof(char *));
  tmp_val      = (float *)calloc(NJ_NCELLS(dmat->ntaxa), sizeof(float));
  if(!tmp_taxaname || !perm || !tmp_val) {
    fprintf(stderr, "Clearcut: Memory allocation error in NJ_shuffle_distance_matrix()\n");
    exit(-1);
  }

  /* compute a permutation which will describe how to shuffle the matrix */
  NJ_permute(perm, dmat->size);

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

      if(perm[j] < perm[i]) {
	tmp_val[NJ_MAP(i, j, dmat->size)] = dmat->val[NJ_MAP(perm[j], perm[i], dmat->size)];
      } else {
	tmp_val[NJ_MAP(i, j, dmat->size)] = dmat->val[NJ_MAP(perm[i], perm[j], dmat->size)];
      }

    }
    
    tmp_taxaname[i] = dmat->taxaname[perm[i]];
  }

  /* free our random permutation */
  if(perm) {
    free(perm);
  }
  
  /* free the old value matrix */
  if(dmat->val) {
    free(dmat->val);
  }

  /* re-assign the value matrix pointers */
  dmat->val = tmp_val;
  dmat->valhandle = dmat->val;
  
  /* 
   * Free our old taxaname with its particular ordering
   * and re-assign to the new.
   */
  if(dmat->taxaname) {
    free(dmat->taxaname);
  }
  dmat->taxaname = tmp_taxaname;

  return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x8(%rdi), %r13
pushq $0x8
popq %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x2160
movq %rax, %r15
movq %r13, %rdi
movq %r14, %rsi
callq 0x2160
movq %rax, %r14
movq (%rbx), %rcx
leaq 0x1(%rcx), %rax
imulq %rcx, %rax
pushq $0x2
popq %rbp
cqto
idivq %rbp
pushq $0x4
popq %rsi
movq %rax, %rdi
callq 0x2160
testq %r15, %r15
je 0x286f
testq %r14, %r14
je 0x286f
movq %rax, %r12
testq %rax, %rax
je 0x286f
movq %r15, %rdi
movq %r13, %rsi
callq 0x3509
movq 0x8(%rbx), %rcx
leaq (%rcx,%rcx), %rsi
xorl %edi, %edi
testq %rcx, %rcx
movl $0x0, %r8d
cmovgq %rcx, %r8
cmpq %r8, %rdi
je 0x283a
leaq 0x1(%rdi), %r9
movq %rdi, %rax
notq %rax
addq %rsi, %rax
imulq %rdi, %rax
cqto
idivq %rbp
leaq (%r12,%rax,4), %r10
movq %r9, %r11
cmpq %rcx, %r11
jge 0x2825
movq (%r15,%r11,8), %rax
movq (%r15,%rdi,8), %r13
cmpq %r13, %rax
movq %r13, %rdx
cmovlq %rax, %rdx
cmovgq %rax, %r13
movq %rdx, %rax
notq %rax
addq %rsi, %rax
imulq %rdx, %rax
cqto
idivq %rbp
shlq $0x2, %rax
addq 0x18(%rbx), %rax
movss (%rax,%r13,4), %xmm0
movss %xmm0, (%r10,%r11,4)
incq %r11
jmp 0x27df
movq 0x10(%rbx), %rax
movq (%r15,%rdi,8), %rdx
movq (%rax,%rdx,8), %rax
movq %rax, (%r14,%rdi,8)
movq %r9, %rdi
jmp 0x27bd
movq %r15, %rdi
callq 0x2040
movq 0x18(%rbx), %rdi
callq 0x2040
movq %r12, 0x18(%rbx)
movq %r12, 0x20(%rbx)
movq 0x10(%rbx), %rdi
callq 0x2040
movq %r14, 0x10(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9782(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x4a1e(%rip), %rdi      # 0x729e
pushq $0x42
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
pushq $-0x1
popq %rdi
callq 0x2220