// File: GenisGE[P]evalAdmix/ngsevalAdmix.cpp
// Function: adaptFandeG(double**, double**, double**, int, int, int, int, char**, int)
void adaptFandeG(double **F, double **Q, double **genos, int nSites, int nInd, int K, int withoutInd, char **keep, int nIts){
  
  for(int it=0; it<nIts; it++){
    
    for(int j=0; j<nSites; j++){

      
	
	double sumAG[K];
	double sumBG[K];
    
	for(int k=0; k<K;k++){
	  sumAG[k] = 0;
	  sumBG[k] = 0;
	}

	for(int i=0; i<nInd;i++){
	
	  if(i == withoutInd)
	    continue;

	  if(keep[j][i] == 1){
	    double fpart=0;
	    double aNorm;
	    double bNorm;
	
	    for(int k=0;k<K;k++){
	      fpart+=F[j][k]*Q[i][k];
	    }
	
	    aNorm = 1.0/fpart;
	    bNorm = 1.0/(1-fpart);

	    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;
	  
	    for(int k=0; k<K;k++){
	      sumAG[k] += eG*F[j][k]*Q[i][k]*aNorm;
	      sumBG[k] += (2-eG)*(1-F[j][k])*Q[i][k]*bNorm;
	    }
	
	  }

	}

	for(int k=0;k<K;k++){
	  F[j][k] = sumAG[k]/(sumAG[k]+sumBG[k]);
	  F[j][k] = std::max(1e-6, std::min(F[j][k],0.999999)); // make sure value is in (1e6, 0.999999) interval 
	    //if(F[j][k]<=0)
	    //F[j][k] = 1e-6;
	}
	
      }    
    }  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl 0x20(%rbp), %eax
jge 0x16b40
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x16b30
movl -0x24(%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, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl -0x24(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1676f
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rcx
movslq -0x4c(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movslq -0x4c(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x16736
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x16a2f
movl -0x50(%rbp), %eax
cmpl 0x10(%rbp), %eax
jne 0x1678f
jmp 0x16a21
movq 0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x50(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x1, %eax
jne 0x16a1f
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x1680a
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movslq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x167bb
movsd 0x17f6(%rip), %xmm0     # 0x18008
divsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd 0x17e4(%rip), %xmm1     # 0x18008
subsd -0x58(%rbp), %xmm1
movsd 0x17d7(%rip), %xmm0     # 0x18008
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd 0x17c6(%rip), %xmm0     # 0x18008
subsd -0x58(%rbp), %xmm0
movsd 0x17b9(%rip), %xmm1     # 0x18008
subsd -0x58(%rbp), %xmm1
mulsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
imull $0x3, -0x50(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd 0x1788(%rip), %xmm1     # 0x18008
subsd -0x58(%rbp), %xmm1
movsd 0x1783(%rip), %xmm0     # 0x18010
mulsd %xmm1, %xmm0
mulsd -0x58(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
imull $0x3, -0x50(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x58(%rbp), %xmm0
mulsd -0x58(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
imull $0x3, -0x50(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x78(%rbp), %xmm0
addsd -0x80(%rbp), %xmm0
addsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x80(%rbp), %xmm1
movsd -0x88(%rbp), %xmm0
addsd %xmm0, %xmm0
addsd %xmm1, %xmm0
divsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x16a1d
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rcx
movsd -0x98(%rbp), %xmm0
movq -0x8(%rbp), %rdx
movslq -0x2c(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x9c(%rbp), %rsi
mulsd (%rdx,%rsi,8), %xmm0
movq -0x10(%rbp), %rdx
movslq -0x50(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x9c(%rbp), %rsi
mulsd (%rdx,%rsi,8), %xmm0
movsd -0x60(%rbp), %xmm2
movslq -0x9c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movsd 0x1669(%rip), %xmm0     # 0x18010
subsd -0x98(%rbp), %xmm0
movq -0x8(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x9c(%rbp), %rdx
movsd 0x163e(%rip), %xmm1     # 0x18008
subsd (%rcx,%rdx,8), %xmm1
mulsd %xmm1, %xmm0
movq -0x10(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x9c(%rbp), %rdx
mulsd (%rcx,%rdx,8), %xmm0
movsd -0x68(%rbp), %xmm2
movslq -0x9c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x1692c
jmp 0x16a1f
jmp 0x16a21
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x16776
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x16b1b
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rcx
movslq -0xa0(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0xa0(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm1
movslq -0xa0(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm1
divsd %xmm1, %xmm0
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0xa0(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x2cda(%rip), %xmm0     # 0x19778
movsd %xmm0, -0xa8(%rbp)
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0xa0(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movsd 0x2ca8(%rip), %xmm0     # 0x19770
movsd %xmm0, -0xb0(%rbp)
leaq -0xb0(%rbp), %rsi
callq 0x91a0
movq %rax, %rsi
leaq -0xa8(%rbp), %rdi
callq 0x91e0
movsd (%rax), %xmm0
movq -0x8(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0xa0(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x16a39
movq -0x38(%rbp), %rax
movq %rax, %rsp
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x166d6
jmp 0x16b32
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x166c3
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)