// File: 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
subq $0x70, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
cmpq $0x0, 0x30(%rbp)
je 0x792d
jmp 0x7aae
movl -0x14(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x70(%rbp)
movq %rdi, %rsp
movq %rax, -0x38(%rbp)
movslq -0x14(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x3150
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x79c8
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x79bb
movq -0x70(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0x44(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x48(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x48(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x7981
jmp 0x79bd
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x7972
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x79f9
movq -0x70(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x79cf
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x7a2e
movq -0x70(%rbp), %rax
movsd -0x40(%rbp), %xmm1
movslq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x7a00
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x7aa7
movsd 0x35c3(%rip), %xmm0     # 0xb008
movsd %xmm0, -0x60(%rbp)
movl $0x1, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x7a8a
movl -0x54(%rbp), %eax
cltd
idivl -0x64(%rbp)
cltd
idivl -0x14(%rbp)
movq -0x70(%rbp), %rax
movl %edx, -0x68(%rbp)
movslq -0x68(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
mulsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x14(%rbp), %eax
imull -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x7a51
movsd -0x60(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x7a35
movq -0x30(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)