// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, 0x8(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
movl %edx, %ebp
movl %esi, 0x20(%rsp)
movq %rdi, 0x58(%rsp)
movslq 0xe8(%rsp), %r14
movslq 0xd0(%rsp), %r12
movq 0xe0(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0xd8(%rsp), %r15d
movl 0xc8(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0xc0(%rsp), %rbx
movq 0xb8(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0xb0(%rsp), %r13d
movq %r14, %rdi
movq %r12, %rsi
callq 0xc0a0
movq %rax, 0x28(%rsp)
leaq (,%r14,8), %rax
movq %r14, 0x10(%rsp)
testq %r14, %r14
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %r14
movslq %r13d, %rsi
movq %r12, %rdi
callq 0xc0a0
movq %rbp, %r10
movq %r12, %r8
movq %rax, 0x50(%rsp)
movl %r10d, %ebp
movl %r8d, %r9d
testq %r12, %r12
jle 0xc751
movl %r13d, %eax
xorl %ecx, %ecx
testl %r13d, %r13d
jle 0xc749
movq (%rbx,%rcx,8), %rdx
movq 0x50(%rsp), %rsi
movq (%rsi,%rcx,8), %rsi
xorl %edi, %edi
movsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0xc737
incq %rcx
cmpq %r9, %rcx
jne 0xc723
movq %r9, 0x70(%rsp)
movq %rbp, 0x60(%rsp)
movq %r15, %rax
testl %eax, %eax
movq %r8, 0x18(%rsp)
movq %r14, 0x68(%rsp)
je 0xc894
subq $0x8, %rsp
movq 0x58(%rsp), %rbp
movq %rbp, %rdi
movq 0x48(%rsp), %r15
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movl %r8d, %ecx
movl 0xc(%rsp), %r12d
movl %r12d, %r8d
movl %r13d, %r9d
pushq %rax
movq 0x58(%rsp), %rbx
pushq %rbx
pushq %r10
movq %r10, 0x40(%rsp)
callq 0xc11b
addq $0x18, %rsp
movq 0x30(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movl %r13d, %r8d
movq 0x20(%rsp), %r9
pushq %rbx
pushq 0x18(%rsp)
pushq 0x28(%rsp)
callq 0xc3b5
movq 0x40(%rsp), %r15
movq 0x38(%rsp), %r8
addq $0x20, %rsp
leal 0x1(%r15), %eax
cmpl %r12d, %eax
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %r12
movq 0x60(%rsp), %rbp
jge 0xc93b
movslq %r15d, %rbx
cltq
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x68(%rsp), %rcx
leaq (%rcx,%rax,8), %rcx
movq %rcx, 0x10(%rsp)
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x40(%rsp)
movl $0x1, %r13d
subl 0x4(%rsp), %r13d
addq %rbp, %r13
xorl %r14d, %r14d
movq (%r12,%rbx,8), %rsi
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %rdx
movsd (%rdi,%rbx,8), %xmm0
movq 0x10(%rsp), %rax
movsd (%rax,%r14,8), %xmm1
leal 0x1(%rbp,%r14), %r9d
movq %rdi, %r15
movl %r8d, %edi
movq 0x48(%rsp), %rcx
movl %ebp, %r8d
callq 0xc579
movq %r15, %rdi
movq 0x18(%rsp), %r8
movq 0x40(%rsp), %rax
movsd %xmm0, (%rax,%r14,8)
incq %r14
movl %r13d, %eax
addl %r14d, %eax
jne 0xc83c
jmp 0xc93b
incl %r10d
cmpl 0x4(%rsp), %r10d
movq 0x48(%rsp), %r13
movq 0x60(%rsp), %r12
jge 0xc93b
movslq 0x20(%rsp), %rbx
movslq %r10d, %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq %rax, 0x10(%rsp)
movl $0x1, %r14d
subl 0x4(%rsp), %r14d
addq %r12, %r14
xorl %r15d, %r15d
movq 0x38(%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq (%rbp,%r15,8), %rdx
movq 0x30(%rsp), %rax
movsd (%rax,%rbx,8), %xmm0
movq 0x8(%rsp), %rax
movsd (%rax,%r15,8), %xmm1
leal 0x1(%r12,%r15), %r9d
movl %r8d, %edi
movq %r13, %rcx
movl %r12d, %r8d
callq 0xc579
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
movl %r14d, %eax
addl %r15d, %eax
jne 0xc8ea
testl %r8d, %r8d
movq 0x28(%rsp), %r15
movq 0x50(%rsp), %r12
movq 0x70(%rsp), %rax
jle 0xc96c
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
testq %rdi, %rdi
je 0xc964
callq 0x22e0
movq 0x70(%rsp), %rax
incq %rbx
cmpq %rbx, %rax
jne 0xc951
movl 0x4(%rsp), %eax
testl %eax, %eax
jle 0xc98f
movl %eax, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
testq %rdi, %rdi
je 0xc987
callq 0x22e0
incq %r14
cmpq %r14, %rbx
jne 0xc979
movq %r12, %rdi
callq 0x22e0
movq %r15, %rdi
callq 0x22e0
movq 0x68(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22e0