// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: get_binomial_pvalue_per_region(double*, double*, int, int, double*, double, bool, int)
double get_binomial_pvalue_per_region(double* signal_track_data, double* control_track_data, int region_start, int region_end, double* _log_factorials, double scaling_factor,
	bool length_normalization, int l_norm_win)
{
	// Get the p-value for the current region.
	double total_profile_signal = 0;
	double total_control_signal = 0;
	for(int i = region_start; i <= region_end; i++)
	{
		total_profile_signal += floor(signal_track_data[i]);
		total_control_signal += (control_track_data[i]);
	} // i loop.

	//// If the region size is smaller than 2000 base pairs, get the control signal from a larger region.
	//if((region_end - region_start+1) <= 2000)
	//{
	//	int eff_reg_start = (region_start+region_end)/2 - 2500;
	//	int eff_reg_end = (region_start+region_end)/2 + 2500;

	//	for(int i = eff_reg_start; i <= eff_reg_end; i++)
	//	{
	//		total_control_signal += floor(control_track_data[i]);
	//	} // i loop.

	//	// Re-normalize the control signal.
	//	total_control_signal = total_control_signal * (region_end - region_start+1) / 5000;
	//}

	total_profile_signal /= scaling_factor;

	//// Get the control signal: Make sure there is enough reads.
	//double total_control_signal = 0.0;
	//int ext_region_start = region_start;
	//int ext_region_end = region_end;
	//while(total_control_signal < MIN_N_CONTROL_SIGNAL/scaling_factor &&
	//	ext_region_start > 0)
	//{
	//	for(int i = ext_region_start; i <= ext_region_end; i++)
	//	{
	//		total_control_signal += floor(control_track_data[i]);	
	//	} // i loop.
	//} // Extension loop.

	//// Region not significant: There is not enough control reads.
	//if(total_control_signal < MIN_N_CONTROL_SIGNAL/scaling_factor)
	//{
	//	//fprintf(stderr, "Could not find enough control signal, returning p_val=1.\n");
	//	return(1.0);
	//}

	// Scale control signal.
	total_control_signal /= scaling_factor;

	// Normalize both signals with respect to normalization region, if they are larger than the region size.
	if(length_normalization && 
		(region_end - region_start+1) > l_norm_win)
	{
		total_profile_signal = total_profile_signal / ((double)(region_end - region_start+1) / l_norm_win);
		total_control_signal = total_control_signal / ((double)(region_end - region_start+1) / l_norm_win);

		if(floor(total_control_signal) == 0)
		{
			total_control_signal = 1.0;
		}

		// The total signal hit 0, return 1.
		if(floor(total_profile_signal) == 0)
		{
			//fprintf(stderr, "Hit 0.\n");
			// Return the most insignificant p-value.
			return(xlog(1.0));
		}
	}
	
	int grand_total_int = (int)(total_profile_signal + total_control_signal);

	// Setup the log factorials.
	double* log_factorials = NULL;
	if(_log_factorials == NULL)
	{
		log_factorials = buffer_log_factorials(grand_total_int+3);
	}
	else
	{
		log_factorials = _log_factorials;
	}

	// Compute the p-values.
	double log_flip = log(.5);
	double cur_region_log_p_val = xlog(0.0);
	for(int i = 0; i <= total_control_signal; i++)
	{
		double log_cur_half_pow = log_flip * grand_total_int;
		double log_cur_perm = 0.0; // = xlog(1.0).

		// Compute the current permutation.
		log_cur_perm = xlog_div(log_factorials[grand_total_int], xlog_mul(log_factorials[i], log_factorials[grand_total_int-i]));
		cur_region_log_p_val = xlog_sum(cur_region_log_p_val, xlog_mul(log_cur_perm, log_cur_half_pow));
	} // i loop.

	// Free memory if it is allocated in the function.
	if(_log_factorials == NULL)
	{
		delete [] log_factorials;
	}

	return(cur_region_log_p_val);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %ebp
movapd %xmm0, 0x20(%rsp)
movq %r8, 0x8(%rsp)
movl %ecx, %r14d
movl 0x80(%rsp), %eax
movl %eax, 0x30(%rsp)
xorpd %xmm2, %xmm2
subl %edx, %r14d
jl 0x1ebe1
movslq %edx, %rax
leaq (%rdi,%rax,8), %r12
leaq (%rsi,%rax,8), %r13
leal 0x1(%r14), %ebx
xorl %r15d, %r15d
movapd %xmm2, 0x10(%rsp)
movsd (%r12,%r15,8), %xmm0
callq 0x22c0
movapd 0x10(%rsp), %xmm2
movhpd (%r13,%r15,8), %xmm0    # xmm0 = xmm0[0],mem[0]
addpd %xmm0, %xmm2
incq %r15
cmpl %r15d, %ebx
jne 0x1ebb7
movapd 0x20(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm2
testb %bpl, %bpl
je 0x1ec71
movl 0x30(%rsp), %eax
cmpl %eax, %r14d
movq 0x8(%rsp), %rbp
jl 0x1ec76
incl %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm2
movapd %xmm2, 0x10(%rsp)
movapd %xmm2, %xmm0
callq 0x22c0
movapd %xmm0, 0x20(%rsp)
movapd 0x10(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0x30(%rsp)
callq 0x22c0
movapd 0x20(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
xorpd %xmm0, %xmm0
cmpeqpd %xmm1, %xmm0
movmskpd %xmm0, %eax
testb $0x2, %al
jne 0x1ed79
movapd 0x30(%rsp), %xmm0
jmp 0x1ed81
movq 0x8(%rsp), %rbp
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
movapd %xmm0, 0x20(%rsp)
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %ebx
movq %rbp, %r14
testq %rbp, %rbp
jne 0x1ec9f
leal 0x3(%rbx), %edi
callq 0x23bc5
movq %rax, %r14
xorpd %xmm0, %xmm0
callq 0x25878
movapd %xmm0, %xmm2
movapd 0x20(%rsp), %xmm0
ucomisd 0xc3f6(%rip), %xmm0     # 0x2b0b0
jb 0x1ed48
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd 0x10411(%rip), %xmm1    # 0x2f0e0
movsd %xmm1, 0x30(%rsp)
movslq %ebx, %r15
leaq (%r14,%r15,8), %r12
xorl %r13d, %r13d
movsd %xmm2, 0x10(%rsp)
movsd (%r14,%r15,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%r14,%r13,8), %xmm0
movsd (%r12), %xmm1
callq 0x259e1
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
callq 0x25a1c
movsd 0x30(%rsp), %xmm1
callq 0x259e1
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x258cc
incq %r13
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
movapd %xmm0, %xmm2
movapd 0x20(%rsp), %xmm0
addq $-0x8, %r12
ucomisd %xmm1, %xmm0
jae 0x1ecdf
testq %rbp, %rbp
jne 0x1ed66
testq %r14, %r14
je 0x1ed66
movq %r14, %rdi
movsd %xmm2, 0x10(%rsp)
callq 0x2220
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0xc287(%rip), %xmm0     # 0x2b008
testb $0x1, %al
movapd 0x10(%rsp), %xmm2
je 0x1eda6
movsd 0xc275(%rip), %xmm0     # 0x2b008
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x25878
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
jmp 0x1ec76