// File: CominLab[P]QCluster/dists.cc
// Function: d2ast_distance(double*, double*, int, double*, double*, double*, double*)
double d2ast_distance(double *centroid, double *p, int L, double* centroid_tilde,
					  double *quality, double *expected_qual, double *expected_freq)
{
	//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 x[L];
	memcpy(x, quality, L*sizeof(*quality));
	double total_count = 0;
	for(int i=0; i<L; i++){
		total_count += p[i];
	}
	double S = 0;
	for(int l=0; l<L; ++l){
		x[l] -= total_count * exp_freq[l] * expected_qual[l];
		x[l] /= sqrt(exp_freq[l] * expected_qual[l]);
		S += x[l] * x[l];
	}
	S = sqrt(S);
	for(int l=0; l<L; ++l){
		x[l] /= S;
	}	
	S = 0;
	for(int l=0; l<L; ++l){
		S += x[l] * centroid_tilde[l];
	}
	return (1-S)/2;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
cmpq $0x0, 0x10(%rbp)
jne 0x73a7
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x73af
movq 0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x14(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x78(%rbp)
movq %rdi, %rsp
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rsi
movslq -0x14(%rbp), %rdx
shlq $0x3, %rdx
callq 0x3200
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x7420
movq -0x10(%rbp), %rax
movslq -0x54(%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 0x73f6
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x74f5
movq -0x78(%rbp), %rax
movsd -0x50(%rbp), %xmm0
movq -0x38(%rbp), %rcx
movslq -0x64(%rbp), %rdx
mulsd (%rcx,%rdx,8), %xmm0
movq -0x30(%rbp), %rcx
movslq -0x64(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movslq -0x64(%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 -0x38(%rbp), %rax
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0x64(%rbp), %rcx
mulsd (%rax,%rcx,8), %xmm0
callq 0x31d0
movq -0x78(%rbp), %rax
movaps %xmm0, %xmm1
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x64(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x60(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x742f
movsd -0x60(%rbp), %xmm0
callq 0x31d0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x7539
movq -0x78(%rbp), %rax
movsd -0x60(%rbp), %xmm1
movslq -0x68(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x750b
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x7587
movq -0x78(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd -0x60(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x7548
movsd 0x3a79(%rip), %xmm0     # 0xb008
subsd -0x60(%rbp), %xmm0
movsd 0x474c(%rip), %xmm1     # 0xbce8
divsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)