// File: gersteinlab[P]MUSIC/src/ms_profile_normalization.cpp
// Function: PeakSeq_slope(double*, double*, int)
double PeakSeq_slope(double* prof1_total_sigs_per_win, double* prof2_total_sigs_per_win, int n_processed_wins)
{
    // Compute the slope from 2 DOF (slope) LLS regression.
    double num = 0.0f;
    double den = 0.0f;

    double sx = 0.0f;
    double sy = 0.0f;
    double sxx = 0.0f;
    double sxy = 0.0f;

    for(int i = 0; i < n_processed_wins; i++)
    {
		sx += prof1_total_sigs_per_win[i];
		sy += prof2_total_sigs_per_win[i];
		sxx += prof1_total_sigs_per_win[i] * prof1_total_sigs_per_win[i];
		sxy += prof1_total_sigs_per_win[i] * prof2_total_sigs_per_win[i];

		//sx += control_frag_cnt->at(i);
		//sy += chip_seq_frag_cnt->at(i);
		//sxx += control_frag_cnt->at(i) * control_frag_cnt->at(i);
		//sxy += control_frag_cnt->at(i) * chip_seq_frag_cnt->at(i);
    } // i loop

    num = n_processed_wins * sxy - sx * sy;
    den = n_processed_wins * sxx - sx * sx;
	
	if(num == 0 || den == 0)
	{
		fprintf(stderr, "Normalization(1): Very low signal mapped.\n");
		return(1.0);
	}

	return(num / den);
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %edx, 0x3c(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0x3e838
movq 0x48(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x40(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x48(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x48(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd 0x10(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x40(%rsp), %rax
movslq 0x4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd 0x8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x3e780
cvtsi2sdl 0x3c(%rsp), %xmm0
movsd 0x8(%rsp), %xmm2
movsd 0x20(%rsp), %xmm1
mulsd 0x18(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
cvtsi2sdl 0x3c(%rsp), %xmm0
movsd 0x10(%rsp), %xmm2
movsd 0x20(%rsp), %xmm1
mulsd 0x20(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x30(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3e8c5
jp 0x3e8c5
jmp 0x3e8d6
movsd 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3e8fe
jp 0x3e8fe
movq 0x26703(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x17f3c(%rip), %rsi     # 0x56823
movb $0x0, %al
callq 0x2250
movsd 0x13722(%rip), %xmm0    # 0x52018
movsd %xmm0, 0x50(%rsp)
jmp 0x3e910
movsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
addq $0x58, %rsp
retq
nopl (%rax,%rax)