// File: GenisGE[P]evalAdmix/evalAdmix.cpp
// Function: adaptF(double**, unsigned short**, int, double**, int, int, int, int, int**)
void adaptF(double **F, unsigned short int **G, int K, double **Q, int nInd, int nSites, int withoutInd, int nIts, int **isMissing){


  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(!isMissing[j][i]){
	    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);

	    for(int k=0; k<K; k++){
	    sumAG[k] += G[j][i]*F[j][k]*Q[i][k]*aNorm; // Minor allele
	    sumBG[k] += (2-G[j][i])*Q[i][k]*(1-F[j][k])*bNorm; // Major allele
	    }
	   
	
	  }
	}

	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 $0xa0, %rsp
movq 0x20(%rbp), %rax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x15e02
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x15df2
movl -0x14(%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, -0x98(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x15b3f
movq -0x90(%rbp), %rax
movq -0x98(%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 0x15b06
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x15d15
movl -0x50(%rbp), %eax
cmpl 0x10(%rbp), %eax
jne 0x15b5f
jmp 0x15d07
movq 0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x50(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x15d05
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x15bd7
movq -0x8(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%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 0x15b88
movsd 0x2429(%rip), %xmm0     # 0x18008
divsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movsd 0x2417(%rip), %xmm1     # 0x18008
subsd -0x58(%rbp), %xmm1
movsd 0x240a(%rip), %xmm0     # 0x18008
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x15d03
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
movq -0x10(%rbp), %rdx
movslq -0x30(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x50(%rbp), %rsi
movzwl (%rdx,%rsi,2), %edx
cvtsi2sd %edx, %xmm0
movq -0x8(%rbp), %rdx
movslq -0x30(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x70(%rbp), %rsi
mulsd (%rdx,%rsi,8), %xmm0
movq -0x20(%rbp), %rdx
movslq -0x50(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq -0x70(%rbp), %rsi
mulsd (%rdx,%rsi,8), %xmm0
movsd -0x60(%rbp), %xmm2
movslq -0x70(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movq -0x10(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x50(%rbp), %rdx
movzwl (%rcx,%rdx,2), %edx
movl $0x2, %ecx
subl %edx, %ecx
cvtsi2sd %ecx, %xmm0
movq -0x20(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x70(%rbp), %rdx
mulsd (%rcx,%rdx,8), %xmm0
movq -0x8(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x70(%rbp), %rdx
movsd 0x2337(%rip), %xmm1     # 0x18008
subsd (%rcx,%rdx,8), %xmm1
mulsd %xmm1, %xmm0
movsd -0x68(%rbp), %xmm2
movslq -0x70(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x15c0e
jmp 0x15d05
jmp 0x15d07
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x15b46
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x15ddd
movq -0x90(%rbp), %rax
movq -0x98(%rbp), %rcx
movslq -0x74(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x74(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm1
movslq -0x74(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm1
divsd %xmm1, %xmm0
movq -0x8(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x74(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x3a06(%rip), %xmm0     # 0x19778
movsd %xmm0, -0x80(%rbp)
movq -0x8(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movslq -0x74(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movsd 0x39da(%rip), %xmm0     # 0x19770
movsd %xmm0, -0x88(%rbp)
leaq -0x88(%rbp), %rsi
callq 0x91a0
movq %rax, %rsi
leaq -0x80(%rbp), %rdi
callq 0x91e0
movsd (%rax), %xmm0
movq -0x8(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x74(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x15d1c
movq -0x38(%rbp), %rax
movq %rax, %rsp
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x15aa6
jmp 0x15df4
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x15a93
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)