// File: /workspace/github_cpp_makefile_star2_O3/CominLab[P]QCluster/centroid.cc
// Function: mm_centroid(int, int, double**, int, double**, double*, double*, double**, double*, double**, double*)
void mm_centroid(int N, int row_length, double** freq, const int num_nt, 
		double** freq_1, double* centroid, double* centroid_tilde, 
		double **quality, double *expected_qual, double **quality_1,
		double *expected_freq)
{
	//If p_method is global (P1G or P2G) we don't need to calculate centroids
	if (expected_freq != NULL) return;
		
	//P2L: It uses a markovian model to compute the expected frequency of words
	double nt_freq[num_nt];
	memset(nt_freq, 0, num_nt * sizeof(nt_freq[0]));
	double S=0;
	// compute frequencies of individual nucleotides
	for (int n=0; n<N; n++){
		for (int l=0; l<num_nt; l++){
			//Number of occurrencies of LETTERS in the cluster:
			nt_freq[l] += freq_1[n][l]; 
		}
	}
	for (int l=0; l<num_nt; l++)
		S += nt_freq[l];	//S = number of letter in the cluster

	for (int l=0; l<num_nt; l++){
		nt_freq[l] /= S;	//Average frequency of letters in the cluster
	}
	// compute frequencies of words using Markov model
	for(int l=0; l<row_length; l++){
		double p = 1;
		for(int M=1; M<row_length; M*=num_nt){
			int digit = l/M % num_nt;
			p *= nt_freq[digit];
		}
		centroid[l] = p;
	}
	return;
	
	/* P1L: Do not use: produces extremely unbalanced clustering*/
	/*int tot_word = 0;
	for (int l=0; l<row_length; l++) {
		centroid[l] = 0;
		for (int n=0; n<N; n++)
			centroid[l] += freq[n][l];
		tot_word += centroid[l];
	}
	
	//Minimum value: 0.01, to avoid division per zero later
	for (int l=0; l<row_length; l++) {
		if (centroid[l] == 0){
			centroid[l] = 0.01;			
		}
		centroid[l] /= tot_word;
	}*/
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpq $0x0, 0x30(%rbp)
jne 0x5563
movq %r8, %r13
movl %ecx, %r14d
movl %esi, %r15d
movq %r9, -0x40(%rbp)
movq %rsp, -0x38(%rbp)
movl %ecx, %ebx
movq %rsp, %r12
leaq 0xf(,%rbx,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movslq %ecx, %rdx
shlq $0x3, %rdx
movl %edi, -0x2c(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
movl -0x2c(%rbp), %eax
testl %eax, %eax
jle 0x54d5
movl %eax, %eax
xorl %ecx, %ecx
testl %r14d, %r14d
jle 0x54cd
movq (%r13,%rcx,8), %rdx
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
addsd (%r12,%rsi,8), %xmm0
movsd %xmm0, (%r12,%rsi,8)
incq %rsi
cmpq %rsi, %rbx
jne 0x54b4
incq %rcx
cmpq %rax, %rcx
jne 0x54a8
testl %r14d, %r14d
movq -0x40(%rbp), %r8
jle 0x5511
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%r12,%rax,8), %xmm0
incq %rax
cmpq %rax, %rbx
jne 0x54e4
testl %r14d, %r14d
jle 0x5511
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x54f9
testl %r15d, %r15d
jle 0x555f
movl %r15d, %esi
xorl %ecx, %ecx
movsd 0x2ae5(%rip), %xmm0     # 0x8008
movapd %xmm0, %xmm1
cmpl $0x1, %r15d
je 0x5551
movl $0x1, %edi
movapd %xmm0, %xmm1
movl %ecx, %eax
cltd
idivl %edi
cltd
idivl %r14d
movslq %edx, %rax
mulsd (%r12,%rax,8), %xmm1
imull %r14d, %edi
cmpl %r15d, %edi
jl 0x5536
movsd %xmm1, (%r8,%rcx,8)
incq %rcx
cmpq %rsi, %rcx
jne 0x5523
movq -0x38(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq