// File: /workspace/github_cpp_makefile_star2_O2/GenisGE[P]evalAdmix/ngsevalAdmix.cpp
// Function: ngscalcRes(double**, double*, double**, double**, int, int, int, double**, char**)
void ngscalcRes(double **r, double *mean_r, double **F, double **Q, int K, int nSites, int nInd, double **genos, char **keep){

  
  double sum_r[nInd];
  int usedSites[nInd];
  
  for(int i=0; i<nInd;i++){
    sum_r[i] = 0;
    usedSites[i] = 0;
  }
  
  for(int j=0; j<nSites;j++){
    for(int i=0; i<nInd;i++){
       if(keep[j][i] == 1){
	 
	usedSites[i]++;
	
	double fpart=0;
	
	for(int k=0;k<K;k++){
	  fpart+=F[j][k]*Q[i][k];
	  
	} 

	double pp0=(1-fpart)*(1-fpart)*genos[j][3*i+2];
	double pp1=2*(1-fpart)*fpart*genos[j][3*i+1];
	double pp2=fpart*fpart*genos[j][3*i+0];
	double sum=pp0+pp1+pp2;
	double eG=(pp1+2*pp2)/sum;
	
	r[i][j] = eG - 2*fpart;
	/*	if(isnan(r[i][j])){

	    fprintf(stderr, "nan in residual in site %i of individual %i when substracting ind freq of %.3f from expected geno %.3f \n",j,i,eG, fpart);
	    // fprintf(stderr, "fpart is %.3f, sum ");
	    exit(0);
	    
	    }*/
	sum_r[i] += r[i][j];

       }
    }
  }


  for(int i=0; i<nInd;i++)
      mean_r[i] = sum_r[i] / usedSites[i];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, -0x30(%rbp)
movl 0x10(%rbp), %r15d
movq 0x20(%rbp), %r11
movq 0x18(%rbp), %rsi
movq %rsp, %rax
leaq 0xf(,%r15,8), %r10
andq $-0x10, %r10
subq %r10, %rax
movq %rax, %rsp
movq %rsp, %r10
leaq 0xf(,%r15,4), %r14
andq $-0x10, %r14
subq %r14, %r10
movq %r10, %rsp
xorl %r12d, %r12d
testl %r15d, %r15d
movl $0x0, %r14d
cmovgl %r15d, %r14d
cmpq %r12, %r14
je 0xaa09
andq $0x0, (%rax,%r12,8)
andl $0x0, (%r10,%r12,4)
incq %r12
jmp 0xa9f5
xorl %r15d, %r15d
testl %r8d, %r8d
cmovlel %r15d, %r8d
testl %r9d, %r9d
cmovlel %r15d, %r9d
movsd 0x5e6(%rip), %xmm0      # 0xb008
cmpq %r9, %r15
je 0xab00
xorl %r12d, %r12d
cmpq %r14, %r12
je 0xaaf8
movq (%r11,%r15,8), %r13
cmpb $0x1, (%r13,%r12)
jne 0xaaf0
incl (%r10,%r12,4)
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
cmpq %r13, %r8
je 0xaa74
movq (%rdx,%r15,8), %rbx
movsd (%rbx,%r13,8), %xmm2
movq (%rcx,%r12,8), %rbx
mulsd (%rbx,%r13,8), %xmm2
addsd %xmm2, %xmm1
incq %r13
jmp 0xaa52
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
movq (%rsi,%r15,8), %r13
imulq $0x18, %r12, %rbx
mulsd 0x10(%r13,%rbx), %xmm3
addsd %xmm2, %xmm2
movapd %xmm1, %xmm4
unpcklpd %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0]
movapd %xmm1, %xmm5
unpcklpd %xmm2, %xmm5            # xmm5 = xmm5[0],xmm2[0]
mulpd %xmm4, %xmm5
movupd (%r13,%rbx), %xmm2
mulpd %xmm5, %xmm2
movapd %xmm2, %xmm4
unpckhpd %xmm2, %xmm4            # xmm4 = xmm4[1],xmm2[1]
addsd %xmm4, %xmm3
addsd %xmm2, %xmm3
addsd %xmm2, %xmm2
addsd %xmm4, %xmm2
divsd %xmm3, %xmm2
addsd %xmm1, %xmm1
subsd %xmm1, %xmm2
movq (%rdi,%r12,8), %rbx
movsd %xmm2, (%rbx,%r15,8)
addsd (%rax,%r12,8), %xmm2
movsd %xmm2, (%rax,%r12,8)
incq %r12
jmp 0xaa2e
incq %r15
jmp 0xaa22
xorl %ecx, %ecx
movq -0x30(%rbp), %rdx
cmpq %rcx, %r14
je 0xab24
cvtsi2sdl (%r10,%rcx,4), %xmm0
movsd (%rax,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
jmp 0xab06
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq