// File: /workspace/github_cpp_makefile_star2_O3/CominLab[P]QCluster/freqs.cc
// Function: calculate_quality_expected_value(int, int, int, int, double**, double**, double**, double*, double**, double*)
void calculate_quality_expected_value(int method, int N, int K, int L, 
			double **freq, double **quality, double **freq_1,
			 double* avg_quality_1, double **avg_quality, double *expected_qual){
	switch (method){
	case 2:
		{
		int *times_to_use = new int[NUM_NT];
		for (int i=0; i<L; i++){
			for(int j=0; j<NUM_NT; j++) times_to_use[j] = 0;
			int divisore = 1;
			for(int k=0; k<K; k++){
				times_to_use[(i/divisore)%(NUM_NT)] += 1;
				divisore *= NUM_NT;
			}
			for(int j=0; j<NUM_NT; j++)
				if (times_to_use[j]!=0) avg_quality[i][j] /= times_to_use[j];
		}

		delete[] times_to_use;

		for (int i=0; i<L; i++){
			int divisore = 1;
			expected_qual[i] = 1;
			for(int k=0; k<K; k++){
				double num_occorrenze = 0;
				for (int j=0; j<N; j++) {num_occorrenze += freq[j][i];}
				expected_qual[i] *= 1 - 
					pow(10.0, -(avg_quality[i][(i/divisore)%(NUM_NT)])
									/(10.0*num_occorrenze));
				divisore *= NUM_NT;
			}
		}
		break;
		}
	case 3:
		{
		for(int i=0; i<NUM_NT; i++) {
			int tot = 0;
			for (int j=0; j<N; j++) tot += freq_1[j][i];
			avg_quality_1[i] = 1-pow(10.0,-avg_quality_1[i]/(10.0*tot));
		}
		int divisore = 1;
		
		for (int i=0; i<L; i++) expected_qual[i] = 1;
		for(int k=0; k<K; k++){
			for (int i=0; i<L; i++){
				expected_qual[i] *= avg_quality_1[(i/divisore)%(NUM_NT)];
			}
			divisore *= NUM_NT;
		}
		break;
		}
	case 1: //fall
		
	default:
		{
		for (int i=0; i<L; i++){ 
			expected_qual[i] = 0; 
			double denominatore = 0; 
			for (int j=0; j<N; j++) { 
				expected_qual[i] += quality[j][i]; 
				denominatore += freq[j][i]; 
			} 
			expected_qual[i] /= denominatore; 
		} 
		break;	
		}
	}//END SWITCH
	
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %r15
movl %ecx, %r14d
movl %edx, 0x4(%rsp)
movl %esi, (%rsp)
movq 0x68(%rsp), %r12
cmpl $0x2, %edi
je 0x5f81
cmpl $0x3, %edi
jne 0x60dd
movq 0x58(%rsp), %r15
movq 0x50(%rsp), %rbx
movl (%rsp), %r13d
xorl %ebp, %ebp
cmpl $0x0, (%rsp)
jle 0x5e4b
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x2d1e(%rip), %xmm0     # 0x8b40
movq (%rbx,%rax,8), %rdx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
addsd (%rdx,%rbp,8), %xmm1
cvttsd2si %xmm1, %ecx
incq %rax
cmpq %rax, %r13
jne 0x5e22
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
mulsd %xmm0, %xmm2
jmp 0x5e57
xorpd %xmm2, %xmm2
movsd 0x2ce9(%rip), %xmm0     # 0x8b40
movsd (%r15,%rbp,8), %xmm1
xorpd 0x2ceb(%rip), %xmm1     # 0x8b50
divsd %xmm2, %xmm1
callq 0x2320
movsd 0x2192(%rip), %xmm1     # 0x8008
subsd %xmm0, %xmm1
movsd %xmm1, (%r15,%rbp,8)
incq %rbp
cmpq $0x4, %rbp
jne 0x5e10
testl %r14d, %r14d
jle 0x5f2f
movl %r14d, %ecx
leal 0x1(%r14), %eax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %eax
shlq $0x4, %rax
movdqa 0x215c(%rip), %xmm1     # 0x8010
xorl %ecx, %ecx
movdqa 0x2162(%rip), %xmm2     # 0x8020
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
movdqa 0x2158(%rip), %xmm4     # 0x8030
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x5f0e
movq %rdx, (%r12,%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x5f22
movq %rdx, 0x8(%r12,%rcx)
paddq %xmm4, %xmm1
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x5ed8
cmpl $0x0, 0x4(%rsp)
jle 0x6140
movl %r14d, %esi
movl $0x1, %edi
xorl %r8d, %r8d
testl %r14d, %r14d
jle 0x5f6f
xorl %ecx, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %edi
andl $0x3, %eax
movsd (%r15,%rax,8), %xmm0
mulsd (%r12,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x5f4c
shll $0x2, %edi
incl %r8d
cmpl 0x4(%rsp), %r8d
jne 0x5f45
jmp 0x6140
movq 0x60(%rsp), %rbx
movl $0x10, %edi
callq 0x2030
movq %rax, %rdi
testl %r14d, %r14d
jle 0x614f
movl %r14d, %r13d
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
cmpl $0x0, 0x4(%rsp)
jle 0x5fce
movl $0x1, %esi
movl 0x4(%rsp), %r8d
movl %ecx, %eax
xorl %edx, %edx
divl %esi
andl $0x3, %eax
incl (%rdi,%rax,4)
shll $0x2, %esi
decl %r8d
jne 0x5fba
xorl %eax, %eax
movl (%rdi,%rax,4), %edx
testl %edx, %edx
je 0x5ff0
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movq (%rbx,%rcx,8), %rdx
movsd (%rdx,%rax,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rdx,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x5fd0
incq %rcx
cmpq %r13, %rcx
jne 0x5fa5
movq %rbx, 0x10(%rsp)
callq 0x2270
testl %r14d, %r14d
jle 0x6140
movl (%rsp), %ebx
xorl %r14d, %r14d
movq %r13, 0x8(%rsp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r12,%r14,8)
cmpl $0x0, 0x4(%rsp)
jle 0x60ca
movl $0x1, %ebp
xorl %r13d, %r13d
cmpl $0x0, (%rsp)
jle 0x606c
xorpd %xmm2, %xmm2
xorl %eax, %eax
movsd 0x2aec(%rip), %xmm0     # 0x8b40
movq (%r15,%rax,8), %rcx
addsd (%rcx,%r14,8), %xmm2
incq %rax
cmpq %rax, %rbx
jne 0x6054
mulsd %xmm0, %xmm2
jmp 0x6078
xorpd %xmm2, %xmm2
movsd 0x2ac8(%rip), %xmm0     # 0x8b40
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rcx
movl %r14d, %eax
xorl %edx, %edx
divl %ebp
andl $0x3, %eax
movsd (%rcx,%rax,8), %xmm1
xorpd 0x2ab8(%rip), %xmm1     # 0x8b50
divsd %xmm2, %xmm1
callq 0x2320
movsd 0x1f5f(%rip), %xmm1     # 0x8008
subsd %xmm0, %xmm1
mulsd (%r12,%r14,8), %xmm1
movsd %xmm1, (%r12,%r14,8)
shll $0x2, %ebp
incl %r13d
cmpl 0x4(%rsp), %r13d
jne 0x6040
incq %r14
movq 0x8(%rsp), %r13
cmpq %r13, %r14
jne 0x601f
jmp 0x6140
testl %r14d, %r14d
jle 0x6140
movl %r14d, %eax
movl (%rsp), %ecx
xorl %edx, %edx
movsd 0x2b8e(%rip), %xmm0     # 0x8c80
movq $0x0, (%r12,%rdx,8)
movapd %xmm0, %xmm1
cmpl $0x0, (%rsp)
jle 0x6132
xorpd %xmm1, %xmm1
xorl %esi, %esi
xorpd %xmm2, %xmm2
movq (%r9,%rsi,8), %rdi
addsd (%rdi,%rdx,8), %xmm1
movsd %xmm1, (%r12,%rdx,8)
movq (%r15,%rsi,8), %rdi
addsd (%rdi,%rdx,8), %xmm2
incq %rsi
cmpq %rsi, %rcx
jne 0x610e
divsd %xmm2, %xmm1
movsd %xmm1, (%r12,%rdx,8)
incq %rdx
cmpq %rax, %rdx
jne 0x60f2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2270