// File: 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 %rbx
subq $0xa8, %rsp
movq 0x100(%rsp), %rax
movl 0xf8(%rsp), %eax
movq 0xf0(%rsp), %rax
movl 0xe8(%rsp), %eax
movl 0xe0(%rsp), %eax
movl 0xd8(%rsp), %eax
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rax
movl 0xc0(%rsp), %eax
movq %rdi, 0xa0(%rsp)
movl %esi, 0x9c(%rsp)
movl %edx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movslq 0xf8(%rsp), %rdi
movslq 0xe0(%rsp), %rsi
callq 0x159c0
movq %rax, 0x78(%rsp)
movslq 0xf8(%rsp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, 0x70(%rsp)
movslq 0xe0(%rsp), %rdi
movslq 0xc0(%rsp), %rsi
callq 0x15920
movq %rax, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0xe0(%rsp), %eax
jge 0x16395
movl $0x0, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0xc0(%rsp), %eax
jge 0x16386
movq 0xd0(%rsp), %rax
movslq 0x64(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x60(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x68(%rsp), %rax
movslq 0x64(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x60(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x16339
jmp 0x16388
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x16324
movq 0x68(%rsp), %rdi
movq 0x80(%rsp), %rsi
movl 0xc0(%rsp), %edx
movq 0xc8(%rsp), %rcx
movl 0xd8(%rsp), %r8d
movl 0xe0(%rsp), %r9d
movl 0x98(%rsp), %r11d
movl 0xe8(%rsp), %r10d
movq 0xf0(%rsp), %rax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x15a60
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq 0xc8(%rsp), %rcx
movq 0x68(%rsp), %r8
movl 0xc0(%rsp), %r9d
movl 0xe0(%rsp), %ebp
movl 0xd8(%rsp), %ebx
movl 0xf8(%rsp), %r11d
movq 0xf0(%rsp), %r10
movq 0x100(%rsp), %rax
movl %ebp, (%rsp)
movl %ebx, 0x8(%rsp)
movl %r11d, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x15e10
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jge 0x16504
movl 0xe0(%rsp), %edi
movq 0x90(%rsp), %rax
movslq 0x9c(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x78(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x88(%rsp), %rax
movslq 0x9c(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x70(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movss (%rax,%rcx,4), %xmm1
cvtss2sd %xmm1, %xmm1
movq 0xf0(%rsp), %rcx
movl 0x98(%rsp), %r8d
movl 0x5c(%rsp), %r9d
callq 0x16070
movq 0xa0(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x16462
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0xe0(%rsp), %eax
jge 0x1654b
movq 0x68(%rsp), %rax
movslq 0x58(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x1653c
movq 0x48(%rsp), %rdi
callq 0x3390
jmp 0x1653e
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x1650c
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0xf8(%rsp), %eax
jge 0x16592
movq 0x78(%rsp), %rax
movslq 0x54(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x16583
movq 0x40(%rsp), %rdi
callq 0x3390
jmp 0x16585
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x16553
movq 0x68(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x165ac
movq 0x38(%rsp), %rdi
callq 0x3390
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x165c6
movq 0x30(%rsp), %rdi
callq 0x3390
movq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x165e0
movq 0x28(%rsp), %rdi
callq 0x3390
addq $0xa8, %rsp
popq %rbx
popq %rbp
retq
nopw (%rax,%rax)