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

  double sum_r[nIndUse];
  int usedSites[nIndUse];


  for(int i=0; i<nIndUse;i++){
    sum_r[i] = 0;
    usedSites[i] = 0;
  }

  for(int j=0; j<nSites;j++){
    //int u=0;
    for(int i=0; i<nInd;i++){
      if(useInd[i]){
      if(!isMissing[j][i]){
	
	double fpart=0; // individual freq pi
	
	for(int k=0; k<K; k++){
	  fpart += Q[i][k]*F[j][k];

	  
	}

	 
	r[i][j] = G[j][i] - 2 * fpart;
	sum_r[i] += r[i][j];
	usedSites[i]++;
	//	u++;
      }
      //else u++;
      }
      
    }
  }

  for(int i=0; i<nIndUse;i++){
    //fprintf(stderr, "ind %i has %i sites with data\n", i, usedSites[i]); 
    mean_r[i] = sum_r[i]/usedSites[i];

  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl 0x20(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x38(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl 0x20(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x20(%rbp), %eax
jge 0x15ec4
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movslq -0x4c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x15e92
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x16010
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x16000
movq 0x30(%rbp), %rax
movslq -0x54(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x15ff0
movq 0x28(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x54(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x15fee
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x15f74
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x60(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x15f25
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
movq -0x18(%rbp), %rdx
movslq -0x50(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x54(%rbp), %rsi
movzwl (%rdx,%rsi,2), %edx
cvtsi2sd %edx, %xmm0
movsd -0x60(%rbp), %xmm1
addsd %xmm1, %xmm1
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x8(%rbp), %rdx
movslq -0x54(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x50(%rbp), %rsi
movss %xmm0, (%rdx,%rsi,4)
movq -0x8(%rbp), %rdx
movslq -0x54(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x50(%rbp), %rsi
movss (%rdx,%rsi,4), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x54(%rbp), %rdx
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x15ff0
jmp 0x15ff2
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x15ede
jmp 0x16002
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x15ecb
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl 0x20(%rbp), %eax
jge 0x16059
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
movslq -0x68(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x68(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x16017
movq -0x38(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)