// File: /workspace/github_cpp_makefile_star2_O1/GenisGE[P]evalAdmix/evalAdmix.cpp
// Function: evalAdmix(double*, int, int, float**, float*, unsigned short**, int, double**, double**, int, int, int, int**, int, int*)
void evalAdmix(double *cor, int ind1u, int ind1, float **r, float *mean_r, unsigned short int **G, int K, double **Q, double **F, int nInd, int nSites,int nIts, int **isMissing, int nIndUse, int *useInd){

  float **rAdapted=allocFloat2(nIndUse, nSites);
  float *mean_rAdapted = new float[nIndUse];
  double **fAdapted=allocDouble2(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];
    }
  }

    
  // Estimate new f and calculate new residuals
  adaptF(fAdapted, G, K, Q, nInd, nSites, ind1, nIts,isMissing);
  calcRes(rAdapted, mean_rAdapted, G, Q, fAdapted, K, nSites, nInd, nIndUse, isMissing, useInd);

  for(int ind2=(ind1u+1); ind2<nIndUse; ind2++){
    cor[ind2] =  correlateRes(nSites, r[ind1u], rAdapted[ind2], mean_r[ind1u], mean_rAdapted[ind2], isMissing, ind1, ind2);
  }
  
  // Clean

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

  }

  for(int i = 0; i < nIndUse; 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, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, 0x60(%rsp)
movslq 0xe8(%rsp), %r12
movslq 0xd0(%rsp), %r13
movq 0xf0(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0xd8(%rsp), %eax
movq %rax, 0x30(%rsp)
movl 0xc8(%rsp), %r14d
movq 0xc0(%rsp), %rbx
movq 0xb8(%rsp), %r15
movl 0xb0(%rsp), %ebp
movq %r12, %rdi
movq %r13, %rsi
callq 0xb6ab
movq %rax, 0x28(%rsp)
leaq (,%r12,4), %rax
movq %r12, 0x18(%rsp)
testq %r12, %r12
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x20(%rsp)
movslq %ebp, %rsi
movq %r13, %rdi
callq 0xb630
movq %r13, %r9
movq %rax, %r13
movl %r9d, %r8d
testq %r9, %r9
jle 0xbd00
movl %ebp, %eax
xorl %ecx, %ecx
testl %ebp, %ebp
jle 0xbcf8
movq (%rbx,%rcx,8), %rdx
movq (%r13,%rcx,8), %rsi
xorl %edi, %edi
movsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0xbce6
incq %rcx
cmpq %r8, %rcx
jne 0xbcd7
movq %r8, 0x48(%rsp)
subq $0x8, %rsp
movq %r13, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movl %ebp, %edx
movq %r15, %rcx
movl %r14d, %r8d
movq %r15, 0x18(%rsp)
movq %r9, %r15
movq 0x58(%rsp), %r12
pushq %r12
pushq 0x40(%rsp)
pushq 0x70(%rsp)
callq 0xb71f
addq $0x18, %rsp
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rdx
movq 0x18(%rsp), %rcx
movq %r13, 0x18(%rsp)
movq %r13, %r8
movl %ebp, %r9d
pushq 0x40(%rsp)
pushq %r12
movq 0x30(%rsp), %rbx
pushq %rbx
pushq %r14
movq %r15, 0x30(%rsp)
pushq %r15
callq 0xb983
addq $0x30, %rsp
movq 0x40(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl %ebx, %eax
movq 0x70(%rsp), %r13
movq 0x68(%rsp), %r15
jge 0xbe21
movslq %ecx, %rbx
movslq %eax, %rcx
movq 0x28(%rsp), %rdx
leaq (%rdx,%rcx,8), %rdx
movq %rdx, 0x40(%rsp)
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,4), %rdx
movq %rdx, 0x38(%rsp)
movq 0x60(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, %r12d
movq 0x18(%rsp), %r14
negl %r14d
addq %r12, %r14
xorl %ebp, %ebp
movq (%r15,%rbx,8), %rsi
xorps %xmm0, %xmm0
cvtss2sd (%r13,%rbx,4), %xmm0
movq 0x40(%rsp), %rax
movq (%rax,%rbp,8), %rdx
movq 0x38(%rsp), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax,%rbp,4), %xmm1
leal (%r12,%rbp), %r9d
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r8
callq 0xbb26
movq 0x30(%rsp), %rax
movsd %xmm0, (%rax,%rbp,8)
incq %rbp
movl %r14d, %eax
addl %ebp, %eax
jne 0xbdd1
cmpl $0x0, 0x8(%rsp)
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %r12
movq 0x48(%rsp), %rax
jle 0xbe59
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
testq %rdi, %rdi
je 0xbe51
callq 0x22e0
movq 0x48(%rsp), %rax
incq %rbx
cmpq %rbx, %rax
jne 0xbe3e
testl %r14d, %r14d
jle 0xbe7a
movl %r14d, %ebx
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
testq %rdi, %rdi
je 0xbe72
callq 0x22e0
incq %r14
cmpq %r14, %rbx
jne 0xbe64
movq %r12, %rdi
callq 0x22e0
movq %r15, %rdi
callq 0x22e0
movq 0x20(%rsp), %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22e0
nop