// File: 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
subq $0xa0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl 0x10(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa0(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl 0x10(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x98(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x16c0a
movq -0x98(%rbp), %rax
movq -0xa0(%rbp), %rcx
movslq -0x44(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movslq -0x44(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x16bd2
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x16e0c
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x16dfc
movq 0x20(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x16dec
movq -0x98(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x16cbf
movq -0x18(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x16c70
movq -0xa0(%rbp), %rax
movsd 0x133a(%rip), %xmm0     # 0x18008
subsd -0x58(%rbp), %xmm0
movsd 0x132d(%rip), %xmm1     # 0x18008
subsd -0x58(%rbp), %xmm1
mulsd %xmm1, %xmm0
movq 0x18(%rbp), %rcx
movslq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
imull $0x3, -0x4c(%rbp), %edx
addl $0x2, %edx
movslq %edx, %rdx
mulsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, -0x68(%rbp)
movsd 0x12fc(%rip), %xmm1     # 0x18008
subsd -0x58(%rbp), %xmm1
movsd 0x12f7(%rip), %xmm0     # 0x18010
mulsd %xmm1, %xmm0
mulsd -0x58(%rbp), %xmm0
movq 0x18(%rbp), %rcx
movslq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
imull $0x3, -0x4c(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
mulsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x58(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movq 0x18(%rbp), %rcx
movslq -0x48(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
imull $0x3, -0x4c(%rbp), %edx
addl $0x0, %edx
movslq %edx, %rdx
mulsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x68(%rbp), %xmm0
addsd -0x70(%rbp), %xmm0
addsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x70(%rbp), %xmm1
movsd -0x78(%rbp), %xmm0
addsd %xmm0, %xmm0
addsd %xmm1, %xmm0
divsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
addsd %xmm1, %xmm1
subsd %xmm1, %xmm0
movq -0x8(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x48(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq -0x8(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x48(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x4c(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x16dee
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x16c24
jmp 0x16dfe
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x16c11
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x16e6c
movq -0x98(%rbp), %rax
movq -0xa0(%rbp), %rcx
movslq -0x8c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x8c(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x16e16
movq -0x30(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)