// File: GenisGE[P]evalAdmix/ngsevalAdmix.cpp
// Function: ngsevalAdmix(double*, int, int, double**, double*, double**, int, double**, double**, int, int, int, char**, int)
void ngsevalAdmix(double *cor, int ind1u, int ind1, double **r, double *mean_r, double **genos, int K, double **Q, double **F, int nInd, int nSites, int nIts, char **keep, int nIndUse){


  double **rAdapted=allocDouble2b(nIndUse, nSites);
  double *mean_rAdapted = new double[nIndUse];
  double **fAdapted=allocDouble2b(nSites,K);


  for(int j=0; j<nSites;j++){ // initial values for adapted F are normal F
    for(int k=0; k<K;k++){
      fAdapted[j][k] = F[j][k];
      //newF[j][k] = (F[j][k]*2*init_Nk[k]-G[withoutInd][j]*Q[withoutInd][k])/(2*init_Nk[k]-Q[withoutInd][k]);
    }
  }

  if(nIts==0){
    
  for(int ind2=(ind1+1); ind2<nInd; ind2++){
    cor[ind2] =  correlateNGSRes(nSites, r[ind1u], r[ind2], mean_r[ind1u], mean_r[ind2], keep, ind1, ind2);
  }

  }
  else{
  // Estimate new f and calculate new residuals
  adaptFandeG(fAdapted, Q, genos, nSites, nInd,K, ind1, keep, nIts);
  ngscalcRes(rAdapted, mean_rAdapted, fAdapted, Q, K, nSites, nIndUse,  genos, keep);
 
  for(int ind2=(ind1+1); ind2<nInd; ind2++){
    cor[ind2] =  correlateNGSRes(nSites, r[ind1], rAdapted[ind2], mean_r[ind1], mean_rAdapted[ind2], keep, ind1, ind2);
  }
  }

  // Clean

  for(int j = 0; j < nSites; j++){ 
    delete[] fAdapted[j];

  }

  for(int i = 0; i < nInd; i++){
    delete[] rAdapted[i];
  }
 
  delete[] fAdapted;
  delete[] rAdapted;
  delete[] mean_rAdapted;
 
}

// Assembly:
subq $0x98, %rsp
movl 0xd8(%rsp), %eax
movq 0xd0(%rsp), %rax
movl 0xc8(%rsp), %eax
movl 0xc0(%rsp), %eax
movl 0xb8(%rsp), %eax
movq 0xb0(%rsp), %rax
movq 0xa8(%rsp), %rax
movl 0xa0(%rsp), %eax
movq %rdi, 0x90(%rsp)
movl %esi, 0x8c(%rsp)
movl %edx, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movq %r9, 0x70(%rsp)
movslq 0xd8(%rsp), %rdi
movslq 0xc0(%rsp), %rsi
callq 0x165f0
movq %rax, 0x68(%rsp)
movslq 0xd8(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, 0x60(%rsp)
movslq 0xc0(%rsp), %rdi
movslq 0xa0(%rsp), %rsi
callq 0x165f0
movq %rax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0xc0(%rsp), %eax
jge 0x17195
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jge 0x17186
movq 0xb0(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x50(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x58(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x50(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x17139
jmp 0x17188
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x17124
cmpl $0x0, 0xc8(%rsp)
jne 0x17250
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0xb8(%rsp), %eax
jge 0x1724b
movl 0xc0(%rsp), %edi
movq 0x80(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x80(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x78(%rsp), %rax
movslq 0x8c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x78(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0xd0(%rsp), %rcx
movl 0x88(%rsp), %r8d
movl 0x4c(%rsp), %r9d
callq 0x16e80
movq 0x90(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x171b1
jmp 0x17398
movq 0x58(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x70(%rsp), %rdx
movl 0xc0(%rsp), %ecx
movl 0xb8(%rsp), %r8d
movl 0xa0(%rsp), %r9d
movl 0x88(%rsp), %r11d
movq 0xd0(%rsp), %r10
movl 0xc8(%rsp), %eax
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x16690
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0xa8(%rsp), %rcx
movl 0xa0(%rsp), %r8d
movl 0xc0(%rsp), %r9d
movl 0xd8(%rsp), %r11d
movq 0x70(%rsp), %r10
movq 0xd0(%rsp), %rax
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x16b50
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0xb8(%rsp), %eax
jge 0x17396
movl 0xc0(%rsp), %edi
movq 0x80(%rsp), %rax
movslq 0x88(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x68(%rsp), %rax
movslq 0x48(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x78(%rsp), %rax
movslq 0x88(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
movslq 0x48(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0xd0(%rsp), %rcx
movl 0x88(%rsp), %r8d
movl 0x48(%rsp), %r9d
callq 0x16e80
movq 0x90(%rsp), %rax
movslq 0x48(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x172ff
jmp 0x17398
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0xc0(%rsp), %eax
jge 0x173df
movq 0x58(%rsp), %rax
movslq 0x44(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x173d0
movq 0x38(%rsp), %rdi
callq 0x3390
jmp 0x173d2
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x173a0
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0xb8(%rsp), %eax
jge 0x17426
movq 0x68(%rsp), %rax
movslq 0x40(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x17417
movq 0x30(%rsp), %rdi
callq 0x3390
jmp 0x17419
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x173e7
movq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x17440
movq 0x28(%rsp), %rdi
callq 0x3390
movq 0x68(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x1745a
movq 0x20(%rsp), %rdi
callq 0x3390
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x17474
movq 0x18(%rsp), %rdi
callq 0x3390
addq $0x98, %rsp
retq