// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movl %ecx, %r15d
movl %edx, 0xc(%rsp)
movl %esi, 0x8(%rsp)
movq 0x68(%rsp), %r12
cmpl $0x2, %edi
je 0x5f3e
cmpl $0x3, %edi
jne 0x6096
movq 0x58(%rsp), %r14
movq 0x50(%rsp), %rbx
movl 0x8(%rsp), %r13d
xorl %ebp, %ebp
cmpl $0x0, 0x8(%rsp)
jle 0x5e8e
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x2cab(%rip), %xmm0     # 0x8b10
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 0x5e65
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
mulsd %xmm0, %xmm2
jmp 0x5e9a
xorpd %xmm2, %xmm2
movsd 0x2c76(%rip), %xmm0     # 0x8b10
movsd (%r14,%rbp,8), %xmm1
xorpd 0x2c78(%rip), %xmm1     # 0x8b20
divsd %xmm2, %xmm1
callq 0x2320
movsd 0x214f(%rip), %xmm1     # 0x8008
subsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rbp,8)
incq %rbp
cmpq $0x4, %rbp
jne 0x5e52
testl %r15d, %r15d
jle 0x5eec
movl %r15d, %eax
xorl %ecx, %ecx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x5ee0
cmpl $0x0, 0xc(%rsp)
jle 0x60fd
movl %r15d, %esi
movl $0x1, %edi
xorl %r8d, %r8d
testl %r15d, %r15d
jle 0x5f2c
xorl %ecx, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %edi
andl $0x3, %eax
movsd (%r14,%rax,8), %xmm0
mulsd (%r12,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x5f09
shll $0x2, %edi
incl %r8d
cmpl 0xc(%rsp), %r8d
jne 0x5f02
jmp 0x60fd
movl $0x10, %edi
callq 0x2030
movq %rax, %rdi
testl %r15d, %r15d
jle 0x5fbd
movl %r15d, %esi
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
cmpl $0x0, 0xc(%rsp)
jle 0x5f85
movl $0x1, %r8d
movl 0xc(%rsp), %r9d
movl %ecx, %eax
xorl %edx, %edx
divl %r8d
andl $0x3, %eax
incl (%rdi,%rax,4)
shll $0x2, %r8d
decl %r9d
jne 0x5f6f
xorl %eax, %eax
movl (%rdi,%rax,4), %edx
testl %edx, %edx
je 0x5fac
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movq 0x60(%rsp), %rdx
movq (%rdx,%rcx,8), %rdx
movsd (%rdx,%rax,8), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, (%rdx,%rax,8)
incq %rax
cmpq $0x4, %rax
jne 0x5f87
incq %rcx
cmpq %rsi, %rcx
jne 0x5f59
callq 0x2270
testl %r15d, %r15d
jle 0x60fd
movl %r15d, %eax
movq %rax, 0x10(%rsp)
movl 0x8(%rsp), %ebp
xorl %r15d, %r15d
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r12,%r15,8)
cmpl $0x0, 0xc(%rsp)
jle 0x6086
movl $0x1, %ebx
xorl %r13d, %r13d
cmpl $0x0, 0x8(%rsp)
jle 0x6028
xorpd %xmm2, %xmm2
xorl %eax, %eax
movsd 0x2b00(%rip), %xmm0     # 0x8b10
movq (%r14,%rax,8), %rcx
addsd (%rcx,%r15,8), %xmm2
incq %rax
cmpq %rax, %rbp
jne 0x6010
mulsd %xmm0, %xmm2
jmp 0x6034
xorpd %xmm2, %xmm2
movsd 0x2adc(%rip), %xmm0     # 0x8b10
movq 0x60(%rsp), %rax
movq (%rax,%r15,8), %rcx
movl %r15d, %eax
xorl %edx, %edx
divl %ebx
andl $0x3, %eax
movsd (%rcx,%rax,8), %xmm1
xorpd 0x2acc(%rip), %xmm1     # 0x8b20
divsd %xmm2, %xmm1
callq 0x2320
movsd 0x1fa3(%rip), %xmm1     # 0x8008
subsd %xmm0, %xmm1
mulsd (%r12,%r15,8), %xmm1
movsd %xmm1, (%r12,%r15,8)
shll $0x2, %ebx
incl %r13d
cmpl 0xc(%rsp), %r13d
jne 0x5ffb
incq %r15
cmpq 0x10(%rsp), %r15
jne 0x5fda
jmp 0x60fd
testl %r15d, %r15d
jle 0x60fd
movl %r15d, %eax
movl 0x8(%rsp), %ecx
xorl %edx, %edx
movq $0x0, (%r12,%rdx,8)
cmpl $0x0, 0x8(%rsp)
jle 0x60e1
movsd (%r12,%rdx,8), %xmm1
xorpd %xmm0, %xmm0
xorl %esi, %esi
movq (%r9,%rsi,8), %rdi
addsd (%rdi,%rdx,8), %xmm1
movsd %xmm1, (%r12,%rdx,8)
movq (%r14,%rsi,8), %rdi
addsd (%rdi,%rdx,8), %xmm0
incq %rsi
cmpq %rsi, %rcx
jne 0x60bf
jmp 0x60e5
xorpd %xmm0, %xmm0
movsd (%r12,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rdx,8)
incq %rdx
cmpq %rax, %rdx
jne 0x60a4
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq