// File: CominLab[P]QCluster/centroid.cc
// Function: d2ast_centroid(int, int, double**, int, double**, double*, double*, double**, double*, double**, double*)
void d2ast_centroid(int N, int row_length, double** freq, int num_nt,
double** freq_1, double* centroid, double* centroid_tilde, 
double **quality, double *expected_qual, double **quality_1,
double *expected_freq)
{
	// compute frequencies from MM
	mm_centroid(N, row_length, freq, num_nt, freq_1, centroid, NULL, 
				quality, expected_qual, quality_1, expected_freq);
	// compute X_tilde
	memset(centroid_tilde, 0, row_length * sizeof(*centroid_tilde));
	
	double total_count = 0;
	for(int n=0; n<N; ++n){
		for(int l=0; l<row_length; ++l){
			centroid_tilde[l] += quality[n][l];
			total_count += freq[n][l];
		}
	}
	
	//Determines what data to use: global(expected_freq) or local(centroid)
	double * exp_freq;
	if (expected_freq==NULL) exp_freq = centroid;
	else exp_freq = expected_freq;
	
	double S = 0;
	for(int l=0; l<row_length; ++l){
		centroid_tilde[l] -= total_count * exp_freq[l] * expected_qual[l];
		centroid_tilde[l] /= sqrt(exp_freq[l] * expected_qual[l]);
		S += centroid_tilde[l] * centroid_tilde[l];
	}
	S = sqrt(S);
	for(int l=0; l<row_length; ++l){
		centroid_tilde[l] /= S;
	}
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x80, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x14(%rbp)
movl %esi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movl -0x14(%rbp), %edi
movl -0x18(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
movq 0x18(%rbp), %rbx
movq 0x20(%rbp), %r11
movq 0x28(%rbp), %r10
movq 0x30(%rbp), %rax
xorl %r14d, %r14d
movq $0x0, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x78f0
movq 0x10(%rbp), %rdi
movslq -0x18(%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 -0x14(%rbp), %eax
jge 0x7bd7
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x7bca
movq 0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x7b71
jmp 0x7bcc
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x7b62
cmpq $0x0, 0x30(%rbp)
jne 0x7be8
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x7bf0
movq 0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x7ccd
movsd -0x40(%rbp), %xmm0
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movq 0x20(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq 0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq %xmm0, %rdx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
xorq %rsi, %rdx
movq %rdx, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x20(%rbp), %rax
movslq -0x5c(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
callq 0x31d0
movaps %xmm0, %xmm1
movq 0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x10(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x7bff
movsd -0x58(%rbp), %xmm0
callq 0x31d0
movsd %xmm0, -0x58(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x7d11
movsd -0x58(%rbp), %xmm1
movq 0x10(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x7ce3
addq $0x80, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax)