// File: /workspace/github_c_makefile_star2_O2/sheneman[P]clearcut/dist.c
// Function: NJ_compute_dmat
DMAT *
NJ_compute_dmat(NJ_ARGS *nj_args,
		NJ_alignment *alignment) {

  DMAT *dmat;
  long int i;
  
  
  /* allocate distance matrix here */
  dmat = (DMAT *)calloc(1, sizeof(DMAT));
  if(!dmat) {
    fprintf(stderr, "Clearcut: Memory allocation error in NJ_compute_dmat()\n");
    return(NULL);
  }
  
  dmat->ntaxa = alignment->nseq;
  dmat->size  = alignment->nseq;

  /* allocate memory to hold the taxa names */
  dmat->taxaname = (char **)calloc(alignment->nseq, sizeof(char *));
  if(!dmat->taxaname) {
    fprintf(stderr, "Clearcut: Memory allocation error in NJ_compute_dmat()\n");
    return(NULL);
  }
  
  /* copy sequence titles */
  for(i=0;i<alignment->nseq;i++) {
    dmat->taxaname[i] = (char *)calloc(strlen(alignment->titles[i])+1, sizeof(char));
    if(!dmat->taxaname[i]) {
      fprintf(stderr, "Clearcut: Memory allocation error in NJ_compute_dmat()\n");
      return(NULL);
    }

    strncpy(dmat->taxaname[i], alignment->titles[i], strlen(alignment->titles[i]));
  }

  /* allocate val matrix in dmat */
  dmat->val = (float *)calloc(dmat->ntaxa*dmat->ntaxa, sizeof(float));
  if(!dmat->val) {
    fprintf(stderr, "Clearcut: Memory allocation error in NJ_compute_dmat()\n");
    return(NULL);
  }

  /* now lets allocate space for the r and r2 columns */
  dmat->r  = (float *)calloc(dmat->ntaxa, sizeof(float));
  dmat->r2 = (float *)calloc(dmat->ntaxa, sizeof(float));

  /* track some memory addresses */
  dmat->rhandle   = dmat->r;
  dmat->r2handle  = dmat->r2;
  dmat->valhandle = dmat->val;
  
  /* apply model correction to matrix */
  switch(nj_args->correction_model) {

  case NJ_MODEL_JUKES:
    
    if(nj_args->dna_flag) {
      NJ_DNA_jc_correction(dmat, alignment);
    } else if(nj_args->protein_flag) {
      NJ_PROTEIN_jc_correction(dmat, alignment);
    } else {
      fprintf(stderr, "Clearcut: Need to know sequence type for Jukes-Cantor model correction.\n");
      return(NULL);
    }

    break;

  case NJ_MODEL_KIMURA:

    if(nj_args->dna_flag) {
      NJ_DNA_k2p_correction(dmat, alignment);
    } else if(nj_args->protein_flag) {
      NJ_PROTEIN_kimura_correction(dmat, alignment);
    } else {
      fprintf(stderr, "Clearcut: Need to know sequence type for Kimura model correction.\n");
      return(NULL);
    }

    break;

  case NJ_MODEL_NONE:

    NJ_no_correction(dmat, alignment);

    break;

  default:
    fprintf(stderr, "Clearcut: Invalid distance correction model.\n");
    return(NULL);
  }
  
  return(dmat);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %r14
pushq $0x1
popq %rdi
pushq $0x48
popq %rsi
callq 0x2160
testq %rax, %rax
je 0x3ecb
movq %rax, %rbx
movq %r14, 0x10(%rsp)
movq (%r15), %r14
movq %r14, (%rax)
movq %r14, 0x8(%rax)
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x2160
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x3ecb
movq %rax, %r13
movq %rbx, 0x8(%rsp)
xorl %r12d, %r12d
testq %r14, %r14
movq %r14, %rax
movl $0x0, %r14d
movq %rax, (%rsp)
cmovgq %rax, %r14
cmpq %r12, %r14
je 0x3e22
movq %r15, %rbp
movq 0x10(%r15), %rbx
movq (%rbx,%r12,8), %rdi
callq 0x20e0
leaq 0x1(%rax), %rdi
pushq $0x1
popq %rsi
callq 0x2160
movq %rax, (%r13,%r12,8)
testq %rax, %rax
je 0x3ecb
movq %rax, %r15
movq (%rbx,%r12,8), %rbx
movq %rbx, %rdi
callq 0x20e0
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2070
incq %r12
movq %rbp, %r15
jmp 0x3dce
movq (%rsp), %rbp
movq %rbp, %rdi
imulq %rbp, %rdi
pushq $0x4
popq %rsi
callq 0x2160
movq 0x8(%rsp), %r14
movq %rax, 0x18(%r14)
testq %rax, %rax
je 0x3ecb
movq %rax, %r13
movq %r15, %r12
pushq $0x4
popq %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2160
movq %rax, %r15
movq %rax, 0x28(%r14)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2160
movq %rax, 0x30(%r14)
movq %r15, 0x38(%r14)
movq %rax, 0x40(%r14)
movq %r13, 0x20(%r14)
movq 0x10(%rsp), %rcx
movl 0x4c(%rcx), %eax
cmpl $0x64, %eax
je 0x3eab
cmpl $0x66, %eax
je 0x3eb8
cmpl $0x65, %eax
jne 0x3f22
cmpl $0x0, 0x40(%rcx)
je 0x3eec
movq %r14, %rdi
movq %r12, %rsi
callq 0x3f64
jmp 0x3f10
movq %r14, %rdi
movq %r12, %rsi
callq 0x453f
jmp 0x3f10
cmpl $0x0, 0x40(%rcx)
je 0x3eff
movq %r14, %rdi
movq %r12, %rsi
callq 0x4184
jmp 0x3f10
movq 0x8126(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x357f(%rip), %rdi      # 0x745b
pushq $0x37
popq %rsi
pushq $0x1
popq %rdx
callq 0x2230
xorl %r14d, %r14d
jmp 0x3f10
cmpl $0x0, 0x44(%rcx)
je 0x3f37
movq %r14, %rdi
movq %r12, %rsi
callq 0x405d
jmp 0x3f10
cmpl $0x0, 0x44(%rcx)
je 0x3f4c
movq %r14, %rdi
movq %r12, %rsi
callq 0x4384
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x80cf(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x35ec(%rip), %rdi      # 0x751f
pushq $0x2d
jmp 0x3ede
movq 0x80ba(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x354b(%rip), %rdi      # 0x7493
pushq $0x48
jmp 0x3ede
movq 0x80a5(%rip), %rax      # 0xbff8
movq (%rax), %rcx
leaq 0x357f(%rip), %rdi      # 0x74dc
pushq $0x42
jmp 0x3ede