// File: /workspace/github_c_makefile_star2_O3/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
subq $0x18, %rsp
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, %r14
movq %rbx, 0x8(%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
movq %rax, 0x10(%rsp)
testq %r15, %r15
je 0x3191
testq %r14, %r14
je 0x3191
cmpq $0x0, 0x10(%rsp)
je 0x3191
testq %r13, %r13
jle 0x313e
leaq 0x1(%r13), %rax
andq $-0x2, %rax
leaq -0x1(%r13), %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x5031(%rip), %xmm1     # 0x8010
xorl %ecx, %ecx
movdqa 0x5037(%rip), %xmm2     # 0x8020
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x5037(%rip), %xmm4     # 0x8030
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x302e
movq %rcx, (%r15,%rcx,8)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x3045
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r15,%rcx,8)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %rax
jne 0x2ff9
movq %r15, %rbp
movq %r13, %rdi
callq 0x7349
testq %rax, %rax
je 0x3074
movq (%rbp,%rax,8), %rcx
movq (%rbp), %rdx
movq %rdx, (%rbp,%rax,8)
movq %rcx, (%rbp)
addq $0x8, %rbp
decq %r13
jne 0x3055
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
jle 0x313e
leaq (%rax,%rax), %rcx
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %rdx
xorl %esi, %esi
leaq 0x1(%rsi), %rdi
movq (%r15,%rsi,8), %r8
cmpq %rax, %rdi
jge 0x312a
movq %r8, %r9
notq %r9
addq %rcx, %r9
imulq %r8, %r9
movq %rsi, %r10
notq %r10
addq %rcx, %r10
imulq %rsi, %r10
movq %r10, %r11
shrq $0x3f, %r11
addq %r10, %r11
andq $-0x2, %r11
movq 0x10(%rsp), %r10
leaq (%r10,%r11,2), %r10
movq 0x8(%rsp), %r11
movq 0x18(%r11), %r11
movq %rdi, %r13
movq (%r15,%r13,8), %rbx
movq %rbx, %r12
notq %r12
addq %rcx, %r12
imulq %rbx, %r12
cmpq %r8, %rbx
cmovgeq %r9, %r12
cmovleq %r8, %rbx
movq %r12, %rbp
shrq $0x3f, %rbp
addq %r12, %rbp
andq $-0x2, %rbp
leaq (%r11,%rbp,2), %r12
movd (%r12,%rbx,4), %xmm0
movd %xmm0, (%r10,%r13,4)
incq %r13
cmpq %rax, %r13
jl 0x30e8
movq (%rdx,%r8,8), %r8
movq %r8, (%r14,%rsi,8)
movq %rdi, %rsi
cmpq %rax, %rdi
jne 0x309e
movq %r15, %rdi
callq 0x2040
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
testq %rdi, %rdi
je 0x3159
callq 0x2040
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x3179
callq 0x2040
movq 0x8(%rsp), %rax
movq %r14, 0x10(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2289