// File: /workspace/github_cpp_makefile_star2_O2/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, %ebp
movl %edx, 0xc(%rsp)
movl %esi, %r14d
movq 0x68(%rsp), %r13
cmpl $0x2, %edi
je 0x5133
cmpl $0x3, %edi
jne 0x5274
movq 0x58(%rsp), %rbx
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
movq 0x50(%rsp), %r12
xorl %eax, %eax
cmpq $0x4, %r15
je 0x50cc
xorl %ecx, %ecx
cvtsi2sd %ecx, %xmm2
cmpq %rax, %r14
je 0x5092
movq (%r12,%rax,8), %rcx
addsd (%rcx,%r15,8), %xmm2
cvttsd2si %xmm2, %ecx
incq %rax
jmp 0x5076
movsd (%rbx,%r15,8), %xmm1
xorpd 0x2a80(%rip), %xmm1     # 0x7b20
movsd 0x2a68(%rip), %xmm0     # 0x7b10
mulsd %xmm0, %xmm2
divsd %xmm2, %xmm1
callq 0x2310
movsd 0x1f4b(%rip), %xmm1     # 0x7008
subsd %xmm0, %xmm1
movsd %xmm1, (%rbx,%r15,8)
incq %r15
jmp 0x506c
testl %ebp, %ebp
cmovlel %eax, %ebp
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
cmpq %rax, %rbp
je 0x50ea
movq %rcx, (%r13,%rax,8)
incq %rax
jmp 0x50db
xorl %esi, %esi
movl 0xc(%rsp), %r8d
testl %r8d, %r8d
cmovlel %esi, %r8d
pushq $0x1
popq %rdi
cmpl %r8d, %esi
je 0x52ca
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0x512c
movl %ecx, %eax
xorl %edx, %edx
divl %edi
andl $0x3, %eax
movsd (%rbx,%rax,8), %xmm0
mulsd (%r13,%rcx,8), %xmm0
movsd %xmm0, (%r13,%rcx,8)
incq %rcx
jmp 0x5106
shll $0x2, %edi
incl %esi
jmp 0x50fb
pushq $0x10
popq %rdi
callq 0x2030
movq %rax, %rdi
xorl %ecx, %ecx
movl 0xc(%rsp), %eax
testl %eax, %eax
cmovlel %ecx, %eax
movl %eax, 0xc(%rsp)
testl %ebp, %ebp
cmovlel %ecx, %ebp
pushq $0x1
popq %rsi
cmpq %rbp, %rcx
je 0x51c0
xorl %eax, %eax
cmpq $0x4, %rax
je 0x516b
andl $0x0, (%rdi,%rax,4)
incq %rax
jmp 0x515c
movl 0xc(%rsp), %r8d
movl %esi, %r9d
subl $0x1, %r8d
jb 0x518c
movl %ecx, %eax
xorl %edx, %edx
divl %r9d
andl $0x3, %eax
incl (%rdi,%rax,4)
shll $0x2, %r9d
jmp 0x5173
xorl %eax, %eax
cmpq $0x4, %rax
je 0x51bb
movl (%rdi,%rax,4), %edx
testl %edx, %edx
je 0x51b6
cvtsi2sd %edx, %xmm0
movq 0x60(%rsp), %rdx
movq (%rdx,%rcx,8), %rdx
movsd (%rdx,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rax,8)
incq %rax
jmp 0x518e
incq %rcx
jmp 0x5155
callq 0x2250
xorl %r12d, %r12d
testl %r14d, %r14d
cmovlel %r12d, %r14d
movq %rbp, 0x10(%rsp)
cmpq %rbp, %r12
je 0x52ca
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13,%r12,8)
xorl %ebp, %ebp
pushq $0x1
popq %rbx
cmpl 0xc(%rsp), %ebp
je 0x5267
xorpd %xmm2, %xmm2
xorl %eax, %eax
cmpq %rax, %r14
je 0x5211
movq (%r15,%rax,8), %rcx
addsd (%rcx,%r12,8), %xmm2
incq %rax
jmp 0x51fd
movq 0x60(%rsp), %rax
movq (%rax,%r12,8), %rcx
movl %r12d, %eax
xorl %edx, %edx
divl %ebx
andl $0x3, %eax
movsd (%rcx,%rax,8), %xmm1
xorpd 0x28ef(%rip), %xmm1     # 0x7b20
movsd 0x28d7(%rip), %xmm0     # 0x7b10
mulsd %xmm0, %xmm2
divsd %xmm2, %xmm1
callq 0x2310
movsd 0x1dba(%rip), %xmm1     # 0x7008
subsd %xmm0, %xmm1
mulsd (%r13,%r12,8), %xmm1
movsd %xmm1, (%r13,%r12,8)
shll $0x2, %ebx
incl %ebp
jmp 0x51f1
incq %r12
movq 0x10(%rsp), %rbp
jmp 0x51d4
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
testl %ebp, %ebp
cmovlel %eax, %ebp
cmpq %rbp, %rax
je 0x52ca
andq $0x0, (%r13,%rax,8)
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
cmpq %rcx, %r14
je 0x52ba
movq (%r9,%rcx,8), %rdx
addsd (%rdx,%rax,8), %xmm0
movsd %xmm0, (%r13,%rax,8)
movq (%r15,%rcx,8), %rdx
addsd (%rdx,%rax,8), %xmm1
incq %rcx
jmp 0x5297
divsd %xmm1, %xmm0
movsd %xmm0, (%r13,%rax,8)
incq %rax
jmp 0x5282
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq