// File: gersteinlab[P]MUSIC/src/ms_profile_normalization.cpp
// Function: pearson_correlation(double*, double*, int)
double pearson_correlation(double* prof1_total_sigs_per_win, double* prof2_total_sigs_per_win, int n_processed_wins)
{
	double num = 0.0f;
	//double den = 0.0f;

	double prof1_mean = 0.0f;
	double prof2_mean = 0.0f;
	for(int i_win = 0; i_win < n_processed_wins; i_win++)
	{
		//control_mean += control_frag_cnt->at(i_frag);
		prof1_mean += prof1_total_sigs_per_win[i_win];
		prof2_mean += prof2_total_sigs_per_win[i_win];
	} // i_win loop.
	prof1_mean /= n_processed_wins;
	prof2_mean /= n_processed_wins;

    //for(int i_frag = 0; i_frag < chip_seq_frag_cnt->size(); i_frag++)
	for(int i_win = 0; i_win < n_processed_wins; i_win++)
    {
		num += (prof1_total_sigs_per_win[i_win] - prof1_mean) * (prof2_total_sigs_per_win[i_win] - prof2_mean);
	} // i_win loop.
	
	double prof1_var = 0.0f;
    for(int i_win = 0; i_win < n_processed_wins; i_win++)
    {
		prof1_var += (prof1_total_sigs_per_win[i_win] - prof1_mean) * (prof1_total_sigs_per_win[i_win] - prof1_mean);
	}	
	prof1_var = pow(prof1_var, .5);

	double prof2_var = 0.0f;
    for(int i_win = 0; i_win < n_processed_wins; i_win++)
    {
		prof2_var += (prof2_total_sigs_per_win[i_win] - prof2_mean) * (prof2_total_sigs_per_win[i_win] - prof2_mean);
	}
	prof2_var = pow(prof2_var, .5);

	double r = num / (prof1_var * prof2_var);

	return(r);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x3eab2
movq 0x60(%rsp), %rax
movslq 0x34(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x58(%rsp), %rax
movslq 0x34(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x3ea65
cvtsi2sdl 0x54(%rsp), %xmm1
movsd 0x40(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
cvtsi2sdl 0x54(%rsp), %xmm1
movsd 0x38(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x3eb3b
movq 0x60(%rsp), %rax
movslq 0x30(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd 0x40(%rsp), %xmm0
movq 0x58(%rsp), %rax
movslq 0x30(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
subsd 0x38(%rsp), %xmm2
movsd 0x48(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x3eae6
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x3eba1
movq 0x60(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd 0x40(%rsp), %xmm0
movq 0x60(%rsp), %rax
movslq 0x24(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
subsd 0x40(%rsp), %xmm2
movsd 0x28(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x3eb4c
movsd 0x28(%rsp), %xmm0
movsd 0x13489(%rip), %xmm1    # 0x52038
callq 0x22f0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x3ec20
movq 0x58(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
subsd 0x38(%rsp), %xmm0
movq 0x58(%rsp), %rax
movslq 0x14(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
subsd 0x38(%rsp), %xmm2
movsd 0x18(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x3ebcb
movsd 0x18(%rsp), %xmm0
movsd 0x1340a(%rip), %xmm1    # 0x52038
callq 0x22f0
movsd %xmm0, 0x18(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
mulsd 0x18(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
addq $0x68, %rsp
retq