// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, -0x2c(%rbp)
movl %r8d, -0x30(%rbp)
cmpl $0x0, 0x18(%rbp)
jle 0xbb7a
movl %edx, %r9d
movq %rsi, %r10
movq %rdi, %r11
movl %edx, %r15d
leaq (,%r15,8), %rax
movq %rax, -0x40(%rbp)
movl 0x10(%rbp), %edi
movl -0x2c(%rbp), %eax
movq %rax, -0x70(%rbp)
movl -0x30(%rbp), %ebx
xorl %eax, %eax
leaq 0xf(,%r15,8), %rdx
andq $-0x10, %rdx
movq %rdx, -0x68(%rbp)
movsd 0x1683(%rip), %xmm7     # 0xd008
movsd 0x2e4a(%rip), %xmm8     # 0xe7d8
movsd 0x2e49(%rip), %xmm9     # 0xe7e0
movq %rcx, -0x60(%rbp)
movl %r9d, -0x34(%rbp)
movq %rsi, -0x58(%rbp)
movq %r11, -0x50(%rbp)
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x2c(%rbp)
jle 0xbb6b
xorl %r12d, %r12d
movq %rsp, -0x78(%rbp)
movq %rsp, %r14
movq -0x68(%rbp), %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
testl %r9d, %r9d
jle 0xba20
movq %r14, %rdi
xorl %esi, %esi
movq -0x40(%rbp), %rdx
callq 0x2100
movq %r13, %rdi
xorl %esi, %esi
movq -0x40(%rbp), %rdx
callq 0x2100
movsd 0x2de4(%rip), %xmm9     # 0xe7e0
movsd 0x2dd3(%rip), %xmm8     # 0xe7d8
movsd 0x15fb(%rip), %xmm7     # 0xd008
movl 0x10(%rbp), %edi
movq -0x50(%rbp), %r11
movq -0x58(%rbp), %r10
movl -0x34(%rbp), %r9d
movq -0x60(%rbp), %rcx
cmpl $0x0, -0x30(%rbp)
jle 0xbb1f
xorl %eax, %eax
cmpq %rdi, %rax
je 0xbb13
testl %r9d, %r9d
setle %sil
movq 0x20(%rbp), %rdx
movq (%rdx,%r12,8), %rdx
cmpl $0x0, (%rdx,%rax,4)
setne %dl
orb %sil, %dl
jne 0xbb13
movq (%r11,%r12,8), %rdi
movq (%rcx,%rax,8), %rdx
xorpd %xmm1, %xmm1
xorl %esi, %esi
movsd (%rdi,%rsi,8), %xmm0
mulsd (%rdx,%rsi,8), %xmm0
addsd %xmm0, %xmm1
incq %rsi
cmpq %rsi, %r15
jne 0xba62
testl %r9d, %r9d
movl 0x10(%rbp), %edi
jle 0xbb13
movapd %xmm7, %xmm0
divsd %xmm1, %xmm0
movapd %xmm7, %xmm3
subsd %xmm1, %xmm3
movapd %xmm7, %xmm1
movq (%r10,%r12,8), %rdx
movzwl (%rdx,%rax,2), %esi
xorps %xmm2, %xmm2
cvtsi2sd %esi, %xmm2
divsd %xmm3, %xmm1
movq (%r11,%r12,8), %r8
movq (%rcx,%rax,8), %rdx
movl $0x2, %edi
subl %esi, %edi
xorps %xmm3, %xmm3
cvtsi2sd %edi, %xmm3
movl 0x10(%rbp), %edi
xorl %esi, %esi
movsd (%r8,%rsi,8), %xmm4
movapd %xmm7, %xmm5
subsd %xmm4, %xmm5
mulsd %xmm2, %xmm4
movsd (%rdx,%rsi,8), %xmm6
mulsd %xmm6, %xmm4
mulsd %xmm0, %xmm4
addsd (%r14,%rsi,8), %xmm4
mulsd %xmm3, %xmm6
mulsd %xmm6, %xmm5
mulsd %xmm1, %xmm5
addsd (%r13,%rsi,8), %xmm5
movsd %xmm4, (%r14,%rsi,8)
movsd %xmm5, (%r13,%rsi,8)
incq %rsi
cmpq %rsi, %r15
jne 0xbac6
incq %rax
cmpq %rbx, %rax
jne 0xba2c
testl %r9d, %r9d
jle 0xbb5a
movq (%r11,%r12,8), %rax
xorl %edx, %edx
movsd (%r14,%rdx,8), %xmm0
movsd (%r13,%rdx,8), %xmm1
addsd %xmm0, %xmm1
divsd %xmm1, %xmm0
movapd %xmm8, %xmm1
minsd %xmm0, %xmm1
maxsd %xmm9, %xmm1
movsd %xmm1, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %r15
jne 0xbb2a
movq -0x78(%rbp), %rsp
incq %r12
cmpq -0x70(%rbp), %r12
jne 0xb9b8
movq -0x48(%rbp), %rax
incl %eax
cmpl 0x18(%rbp), %eax
jne 0xb9a7
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq