// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movl %ecx, -0x10(%rbp)
movq %r8, -0x18(%rbp)
movq %r9, -0x20(%rbp)
movl -0x4(%rbp), %eax
movl %eax, -0x88(%rbp)
subl $0x1, %eax
je 0x8ba3
jmp 0x87b3
movl -0x88(%rbp), %eax
subl $0x2, %eax
je 0x87d4
jmp 0x87c0
movl -0x88(%rbp), %eax
subl $0x3, %eax
je 0x8a29
jmp 0x8ba5
movl $0x10, %edi
callq 0x3030
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x88d6
movl $0x0, -0x30(%rbp)
cmpl $0x4, -0x30(%rbp)
jge 0x881c
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x87fc
movl $0x1, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x8873
movq -0x28(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x2c(%rbp), %eax
cltd
idivl -0x34(%rbp)
movl $0x4, %ecx
cltd
idivl %ecx
movq -0x90(%rbp), %rax
movslq %edx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x882a
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jge 0x88c6
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x88b9
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
movq 0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x3c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x88bb
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x887a
jmp 0x88c8
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x87e9
movq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x88f3
movq -0x98(%rbp), %rdi
callq 0x3350
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x8a24
movl $0x1, -0x44(%rbp)
movq 0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
movsd 0x26eb(%rip), %xmm0     # 0xb008
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x8a14
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x8976
movq -0x18(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x8944
movq 0x20(%rbp), %rax
movslq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa0(%rbp)
movl -0x40(%rbp), %eax
cltd
idivl -0x44(%rbp)
movl $0x4, %ecx
cltd
idivl %ecx
movq -0xa0(%rbp), %rax
movslq %edx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movsd 0x31ba(%rip), %xmm0     # 0xbb80
mulsd -0x50(%rbp), %xmm0
divsd %xmm0, %xmm1
movsd 0x31a9(%rip), %xmm0     # 0xbb80
callq 0x3410
movaps %xmm0, %xmm1
movsd 0x2621(%rip), %xmm0     # 0xb008
subsd %xmm1, %xmm0
movq 0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x8929
jmp 0x8a16
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x88fa
jmp 0x8c6d
movl $0x0, -0x58(%rbp)
cmpl $0x4, -0x58(%rbp)
jge 0x8af0
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x8a80
movq 0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
cvtsi2sdl -0x5c(%rbp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x5c(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x8a48
movq 0x18(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
cvtsi2sdl -0x5c(%rbp), %xmm2
movsd 0x30cf(%rip), %xmm0     # 0xbb80
mulsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd 0x30bf(%rip), %xmm0     # 0xbb80
callq 0x3410
movaps %xmm0, %xmm1
movsd 0x2537(%rip), %xmm0     # 0xb008
subsd %xmm1, %xmm0
movq 0x18(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x8a30
movl $0x1, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x8b26
movq 0x28(%rbp), %rax
movslq -0x68(%rbp), %rcx
movsd 0x24f2(%rip), %xmm0     # 0xb008
movsd %xmm0, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x8afe
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x8b9e
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x8b8a
movq 0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl -0x70(%rbp), %eax
cltd
idivl -0x64(%rbp)
movl $0x4, %ecx
cltd
idivl %ecx
movq -0xa8(%rbp), %rax
movslq %edx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x28(%rbp), %rax
movslq -0x70(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x8b3c
movl -0x64(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x64(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x8b2d
jmp 0x8c6d
jmp 0x8ba5
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x8c6b
movq 0x28(%rbp), %rax
movslq -0x74(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x8c42
movq -0x20(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x74(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x28(%rbp), %rax
movslq -0x74(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x74(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x8bda
movsd -0x80(%rbp), %xmm1
movq 0x28(%rbp), %rax
movslq -0x74(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x8bac
jmp 0x8c6d
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)