// File: /workspace/github_cpp_makefile_star2_O3/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
subq $0x28, %rsp
movl %r8d, %r15d
movq %rcx, -0x48(%rbp)
movq %rdx, -0x40(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdi, %r13
movl 0x10(%rbp), %ebx
movq %rsp, %r14
leaq 0xf(,%rbx,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r12
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
testl %ebx, %ebx
jle 0xc435
leaq (,%rbx,8), %rdx
movq %r14, %rdi
xorl %esi, %esi
movl %r9d, -0x2c(%rbp)
callq 0x2100
leaq (,%rbx,4), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x2100
movl -0x2c(%rbp), %r9d
testl %r9d, %r9d
jle 0xc540
movq 0x18(%rbp), %rcx
movl %r9d, %edx
movl %r15d, %esi
xorl %edi, %edi
movsd 0xbb6(%rip), %xmm0      # 0xd008
testl %ebx, %ebx
jle 0xc534
movq 0x20(%rbp), %rax
movq (%rax,%rdi,8), %r8
xorl %r9d, %r9d
cmpb $0x1, (%r8,%r9)
jne 0xc528
incl (%r12,%r9,4)
testl %r15d, %r15d
jle 0xc4a9
movq -0x40(%rbp), %rax
movq (%rax,%rdi,8), %r10
movq -0x48(%rbp), %rax
movq (%rax,%r9,8), %r11
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd (%r10,%rax,8), %xmm2
mulsd (%r11,%rax,8), %xmm2
addsd %xmm2, %xmm1
incq %rax
cmpq %rax, %rsi
jne 0xc48f
jmp 0xc4ad
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
movq (%rcx,%rdi,8), %rax
leaq (%r9,%r9,2), %r10
mulsd 0x10(%rax,%r10,8), %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 (%rax,%r10,8), %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 (%r13,%r9,8), %rax
movsd %xmm2, (%rax,%rdi,8)
addsd (%r14,%r9,8), %xmm2
movsd %xmm2, (%r14,%r9,8)
incq %r9
cmpq %rbx, %r9
jne 0xc465
incq %rdi
cmpq %rdx, %rdi
jne 0xc452
testl %ebx, %ebx
movq -0x38(%rbp), %rcx
jle 0xc56a
xorl %eax, %eax
movsd (%r14,%rax,8), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl (%r12,%rax,4), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0xc54a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq