// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
cmpq $0x0, 0x30(%rbp)
jne 0x491d
movq %r8, %r13
movl %ecx, %r14d
movl %esi, %r15d
movq %r9, -0x48(%rbp)
movq %rsp, -0x40(%rbp)
movl %ecx, %eax
movq %rsp, %r12
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movslq %ecx, %rax
movq %rax, -0x30(%rbp)
leaq (,%rax,8), %rdx
xorl %ebx, %ebx
movq %rdi, -0x38(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
movq -0x38(%rbp), %rsi
cmpl $0x0, -0x30(%rbp)
movl $0x0, %eax
cmovgl %r14d, %eax
testl %esi, %esi
cmovlel %ebx, %esi
cmpq %rsi, %rbx
je 0x4895
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x4890
movq (%r13,%rbx,8), %rdx
movsd (%rdx,%rcx,8), %xmm0
addsd (%r12,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
jmp 0x4870
incq %rbx
jmp 0x4869
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq -0x48(%rbp), %r9
cmpq %rcx, %rax
je 0x48af
addsd (%r12,%rcx,8), %xmm0
incq %rcx
jmp 0x489f
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x48cb
movsd (%r12,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rcx,8)
incq %rcx
jmp 0x48b1
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %esi
cmovgl %r15d, %esi
pushq $0x1
popq %rdi
movsd 0x2724(%rip), %xmm0     # 0x7008
cmpq %rsi, %rcx
je 0x4919
movapd %xmm0, %xmm1
movl %edi, %r8d
cmpl %r15d, %r8d
jge 0x490e
movl %ecx, %eax
cltd
idivl %r8d
cltd
idivl %r14d
movslq %edx, %rax
mulsd (%r12,%rax,8), %xmm1
imull %r14d, %r8d
jmp 0x48f0
movsd %xmm1, (%r9,%rcx,8)
incq %rcx
jmp 0x48e4
movq -0x40(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq