// File: /workspace/github_c_makefile_star2_O1/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
movl $0x8, %esi
movq %r13, %rdi
callq 0x2160
movq %rax, %r15
movl $0x8, %esi
movq %r13, %rdi
callq 0x2160
movq %rax, (%rsp)
movq (%rbx), %rax
leaq 0x1(%rax), %rcx
imulq %rax, %rcx
movq %rcx, %rdi
shrq $0x3f, %rdi
addq %rcx, %rdi
sarq %rdi
movl $0x4, %esi
callq 0x2160
testq %r15, %r15
je 0x30f3
cmpq $0x0, (%rsp)
je 0x30f3
movq %rax, %r12
testq %rax, %rax
je 0x30f3
testq %r13, %r13
jle 0x2fd1
xorl %eax, %eax
movq %rax, (%r15,%rax,8)
incq %rax
cmpq %rax, %r13
jne 0x2f95
testq %r13, %r13
jle 0x2fd1
movq %r15, %rbp
movq %r13, %rdi
callq 0x70e8
testq %rax, %rax
je 0x2fc8
movq (%rbp,%rax,8), %rcx
movq (%rbp), %rdx
movq %rdx, (%rbp,%rax,8)
movq %rcx, (%rbp)
addq $0x8, %rbp
decq %r13
jne 0x2fa9
movq 0x8(%rbx), %rax
testq %rax, %rax
jle 0x30b0
leaq (%rax,%rax), %rcx
movq 0x10(%rbx), %rdx
xorl %edi, %edi
leaq 0x1(%rdi), %r14
cmpq %rax, %r14
jge 0x3094
movq (%r15,%rdi,8), %r8
movq %r8, %r10
notq %r10
addq %rcx, %r10
imulq %r8, %r10
movq %r10, %r9
shrq $0x3f, %r9
addq %r10, %r9
sarq %r9
movq %rdi, %rsi
notq %rsi
addq %rcx, %rsi
shlq $0x2, %r9
movq %r14, %r11
movq (%r15,%r11,8), %r13
cmpq %r8, %r13
jge 0x3055
movq %r13, %rbp
notq %rbp
addq %rcx, %rbp
imulq %r13, %rbp
movq %rbp, %r13
shrq $0x3f, %r13
addq %rbp, %r13
andq $-0x2, %r13
addq %r13, %r13
addq 0x18(%rbx), %r13
leaq (%r13,%r8,4), %r13
jmp 0x3067
movq 0x18(%rbx), %rbp
addq %r9, %rbp
leaq (,%r13,4), %r13
addq %rbp, %r13
movq %rsi, %r10
imulq %rdi, %r10
movq %r10, %rbp
shrq $0x3f, %rbp
addq %r10, %rbp
movss (%r13), %xmm0
andq $-0x2, %rbp
leaq (%r12,%rbp,2), %r10
movss %xmm0, (%r10,%r11,4)
incq %r11
cmpq %rax, %r11
jl 0x3023
movq (%r15,%rdi,8), %rsi
movq (%rdx,%rsi,8), %rsi
movq (%rsp), %r8
movq %rsi, (%r8,%rdi,8)
movq %r14, %rdi
cmpq %rax, %r14
jne 0x2fe8
movq %r15, %rdi
callq 0x2040
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x30c6
callq 0x2040
movq %r12, 0x18(%rbx)
movq %r12, 0x20(%rbx)
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x30dc
callq 0x2040
movq (%rsp), %rax
movq %rax, 0x10(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2289