// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movl %edx, %r12d
movl %esi, 0x54(%rsp)
movq %rdi, 0x58(%rsp)
movslq 0xe8(%rsp), %rbx
movq 0xe0(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0xd8(%rsp), %eax
movq %rax, 0x8(%rsp)
movl 0xd0(%rsp), %r15d
movl 0xc8(%rsp), %eax
movq %rax, 0x28(%rsp)
movq 0xc0(%rsp), %rbp
movq 0xb8(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0xb0(%rsp), %r14d
movslq %r15d, %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xa6e8
movq %rax, 0x60(%rsp)
leaq (,%rbx,8), %rax
movq %rbx, 0x68(%rsp)
testq %rbx, %rbx
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x30(%rsp)
movslq %r14d, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xa6e8
movq %r12, %r10
movq %rax, %r12
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
movl %r14d, %r9d
cmovgl %r14d, %ecx
testl %r13d, %r13d
movl $0x0, %edi
movl %r15d, 0x14(%rsp)
cmovgl %r15d, %edi
movl %r10d, %ebx
cmpq %rdi, %rax
je 0xad11
xorl %edx, %edx
cmpq %rdx, %rcx
je 0xad0c
movq (%rbp,%rax,8), %rsi
movsd (%rsi,%rdx,8), %xmm0
movq (%r12,%rax,8), %rsi
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
jmp 0xacef
incq %rax
jmp 0xace8
movq %rdi, 0x70(%rsp)
movq 0x8(%rsp), %rax
testl %eax, %eax
je 0xae24
subq $0x8, %rsp
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl 0x1c(%rsp), %r14d
movl %r14d, %ecx
movq 0x30(%rsp), %r8
movl %r9d, %ebp
pushq %rax
movq 0x58(%rsp), %r15
pushq %r15
pushq %r10
movq %r10, 0x28(%rsp)
callq 0xa75c
addq $0x18, %rsp
movq 0x68(%rsp), %r13
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq %r12, %rdx
movq 0x20(%rsp), %rcx
movl %ebp, %r8d
movl %r14d, %r9d
pushq %r15
pushq 0x30(%rsp)
pushq 0x80(%rsp)
callq 0xa99d
addq $0x20, %rsp
movslq 0x8(%rsp), %r15
movslq 0x28(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x1(%r15), %rax
movq %rax, 0x18(%rsp)
leaq 0x8(%r13,%r15,8), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rax
leaq (%rax,%r15,8), %r14
addq $0x8, %r14
movq 0x58(%rsp), %rax
leaq (%rax,%r15,8), %rbp
addq $0x8, %rbp
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
addq %r13, %rax
cmpq 0x20(%rsp), %rax
jge 0xaebd
movq 0x38(%rsp), %rax
movq (%rax,%r15,8), %rsi
movq 0x8(%rsp), %rax
movq (%rax,%r13,8), %rdx
movq 0x40(%rsp), %rax
movsd (%rax,%r15,8), %xmm0
movsd (%r14,%r13,8), %xmm1
leal 0x1(%rbx,%r13), %r9d
movl 0x14(%rsp), %edi
movq 0x48(%rsp), %rcx
movl %ebx, %r8d
callq 0xab33
movsd %xmm0, (%rbp,%r13,8)
incq %r13
jmp 0xadcc
movslq 0x54(%rsp), %rbp
movslq %r10d, %rax
movslq 0x28(%rsp), %r13
leaq 0x1(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x38(%rsp), %rcx
leaq 0x8(%rcx,%rax,8), %rcx
movq %rcx, 0x18(%rsp)
movq 0x40(%rsp), %rcx
leaq 0x8(%rcx,%rax,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax,8), %r14
addq $0x8, %r14
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
addq %r15, %rax
cmpq %r13, %rax
jge 0xaebd
movq 0x38(%rsp), %rax
movq (%rax,%rbp,8), %rsi
movq 0x18(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq 0x40(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movq 0x8(%rsp), %rax
movsd (%rax,%r15,8), %xmm1
leal 0x1(%rbx,%r15), %r9d
movl 0x14(%rsp), %edi
movq 0x48(%rsp), %rcx
movl %ebx, %r8d
callq 0xab33
movsd %xmm0, (%r14,%r15,8)
incq %r15
jmp 0xae68
xorl %ebx, %ebx
movq 0x28(%rsp), %r14
movq 0x60(%rsp), %r15
movq 0x70(%rsp), %r13
cmpq %rbx, %r13
je 0xaee6
movq (%r12,%rbx,8), %rdi
testq %rdi, %rdi
je 0xaee1
callq 0x22d0
incq %rbx
jmp 0xaece
xorl %ebx, %ebx
testl %r14d, %r14d
cmovlel %ebx, %r14d
cmpq %rbx, %r14
je 0xaf07
movq (%r15,%rbx,8), %rdi
testq %rdi, %rdi
je 0xaf02
callq 0x22d0
incq %rbx
jmp 0xaeef
movq %r12, %rdi
callq 0x22d0
movq %r15, %rdi
callq 0x22d0
movq 0x30(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22d0