// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: get_binomial_pvalue_per_region_neighborhood_window_control_per_max_profile_signal(double*, double*, int, int, double*, double, bool, int, int, int, int)
double get_binomial_pvalue_per_region_neighborhood_window_control_per_max_profile_signal(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, int l_signal, int l_control, int n_vicinity_wins)
{
	// Get the total profile signal: This is computed by sliding a window over the region.
	double total_profile_signal = 0;
	if(region_end - region_start + 1 > l_norm_win)
	{
		double cur_win_total = 0.0;
		for(int i = region_start; i < region_start + l_norm_win; i++)
		{
			cur_win_total += floor(signal_track_data[i]);
		} // i loop.

		total_profile_signal = cur_win_total;

		// Following loop computes the per window total signals.
		int cur_win_start = region_start;
		while(1)
		{
			// Remove the value that just left the window.
			cur_win_total -= floor(signal_track_data[cur_win_start]);

			// Add the newly entering value to the window.
			cur_win_total += floor(signal_track_data[cur_win_start+l_norm_win]);

			if(cur_win_total > total_profile_signal)
			{
				total_profile_signal = cur_win_total;
			}
			cur_win_start++;

			// Are we at the end of the region?
			if(cur_win_start+l_norm_win >= region_end)
			{
				break;
			}
		} // window loop.
	}
	else
	{
		for(int i = region_start; i <= region_end; i++)
		{
			total_profile_signal += floor(signal_track_data[i]);
		} // i loop.

		// Update the normalization window length, this way there is no underpowering for the shorter regions.
		l_norm_win = region_end - region_start;
	}

	// Get the control signal: Get the total profile signal: This is computed by sliding a window over the vicinity extended region (in terms of length of the whole ER).
	int l_reg = (region_end - region_start + 1);
	int ext_region_start = region_start - n_vicinity_wins * l_reg;
	int ext_region_end = region_end + n_vicinity_wins * l_reg;
	if(ext_region_start < 1)
	{
		ext_region_start = 1;
	}

	if(ext_region_end > l_control)
	{
		ext_region_end = l_control;
	}

	double total_control_signal = 0;

	// If we are past the end of the chromosome, flag this regions as insignificant.
	if(ext_region_start + l_norm_win > l_control)
	{
		return(log(1.0));
	}

	// Must make sure we do not go over the limits.
	double cur_win_total = 0.0;
	for(int i = ext_region_start; i < MIN(l_control, ext_region_start + l_norm_win); i++)
	{
		//cur_win_total += floor(control_track_data[i]);
		cur_win_total += control_track_data[i];
	} // i loop.

	total_control_signal = cur_win_total;

	// Following loop computes the per window total control signals.
	int cur_win_start = ext_region_start;
	while(1)
	{
		// Remove the value that just left the window.
		//cur_win_total -= floor(control_track_data[cur_win_start]);
		cur_win_total -= control_track_data[cur_win_start];

		// Add the newly entering value to the window.
		//cur_win_total += floor(control_track_data[cur_win_start+l_norm_win]);
		cur_win_total += control_track_data[cur_win_start+l_norm_win];

		if(cur_win_total > total_control_signal)
		{
			total_control_signal = cur_win_total;
		}
		cur_win_start++;

		// Are we at the end of the region?
		if(cur_win_start+l_norm_win >= ext_region_end)
		{
			break;
		}
	} // window loop.

	// Scale the control and profile signals.
	total_control_signal /= scaling_factor;
	total_profile_signal /= scaling_factor;

	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, using shortcuts, bypassing the function calls from xlog_math library.
		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 $0x68, %rsp
movsd %xmm0, 0x38(%rsp)
movq %r8, 0x58(%rsp)
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %r12
movl 0xb8(%rsp), %edx
movl 0xb0(%rsp), %eax
movl 0xa0(%rsp), %ebp
movl %ecx, %ebx
subl %r15d, %ebx
leal 0x1(%rbx), %edi
cmpl %ebp, %ebx
jge 0x1e540
xorpd %xmm2, %xmm2
cmpl %r15d, %ecx
jge 0x1e587
jmp 0x1e64d
movl %ecx, 0x20(%rsp)
testl %ebp, %ebp
movl %edi, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl %edx, 0x14(%rsp)
jle 0x1e5cc
leal (%r15,%rbp), %eax
movslq %r15d, %rbx
movslq %eax, %r13
xorpd %xmm2, %xmm2
movsd %xmm2, 0x8(%rsp)
movsd (%r12,%rbx,8), %xmm0
callq 0x22c0
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
incq %rbx
cmpq %r13, %rbx
jl 0x1e562
jmp 0x1e5d0
movl %edx, 0x14(%rsp)
movl %ecx, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movslq %r15d, %rax
leaq (%r12,%rax,8), %r12
movl %edi, 0x1c(%rsp)
movl %edi, %r13d
xorl %ebp, %ebp
movsd %xmm2, 0x8(%rsp)
movsd (%r12,%rbp,8), %xmm0
callq 0x22c0
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
incq %rbp
cmpl %ebp, %r13d
jne 0x1e5a3
movl 0x1c(%rsp), %edi
jmp 0x1e641
xorpd %xmm2, %xmm2
movslq %r15d, %rbx
movq %rbp, 0x60(%rsp)
movslq %ebp, %rax
movslq 0x20(%rsp), %rbp
leaq (%r12,%rax,8), %r13
subq %rax, %rbp
movsd %xmm2, 0x8(%rsp)
movsd %xmm2, 0x40(%rsp)
movsd (%r12,%rbx,8), %xmm0
callq 0x22c0
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd (%r13,%rbx,8), %xmm0
callq 0x22c0
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
maxsd 0x40(%rsp), %xmm2
incq %rbx
cmpq %rbp, %rbx
jl 0x1e5ed
movl 0x1c(%rsp), %edi
movq 0x60(%rsp), %rbx
movl 0x18(%rsp), %eax
movl 0x20(%rsp), %ecx
movl 0x14(%rsp), %edx
imull %edx, %edi
subl %edi, %r15d
addl %ecx, %edi
cmpl $0x2, %r15d
movl $0x1, %ecx
cmovgel %r15d, %ecx
cmpl %eax, %edi
cmovgel %eax, %edi
leal (%rbx,%rcx), %edx
xorpd %xmm3, %xmm3
cmpl %eax, %edx
jle 0x1e685
movapd %xmm3, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, %eax
testl %ebx, %ebx
jle 0x1e6a3
leaq (%r14,%rax,8), %rsi
xorpd %xmm0, %xmm0
addsd (%rsi), %xmm0
addq $0x8, %rsi
incl %ecx
cmpl %ecx, %edx
jg 0x1e693
jmp 0x1e6a7
xorpd %xmm0, %xmm0
movslq %ebx, %rsi
movslq %edi, %rcx
leaq (%r14,%rsi,8), %rdx
subq %rsi, %rcx
movapd %xmm0, %xmm3
movapd %xmm0, %xmm1
subsd (%r14,%rax,8), %xmm3
addsd (%rdx,%rax,8), %xmm3
movapd %xmm3, %xmm0
maxsd %xmm1, %xmm0
incq %rax
cmpq %rcx, %rax
jl 0x1e6b8
movsd 0x38(%rsp), %xmm1
divsd %xmm1, %xmm0
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
movapd %xmm0, 0x40(%rsp)
callq 0x22c0
movapd %xmm0, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x22c0
ucomisd 0xc9a1(%rip), %xmm0     # 0x2b0b0
xorpd %xmm0, %xmm0
jne 0x1e732
jp 0x1e732
movsd 0xc8e9(%rip), %xmm0     # 0x2b008
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x25878
movapd 0x20(%rsp), %xmm2
cmpeqsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andnpd 0x40(%rsp), %xmm0
movsd 0xc8b9(%rip), %xmm1     # 0x2b008
andpd %xmm2, %xmm1
orpd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebx
movq 0x58(%rsp), %rax
movq %rax, %r14
testq %rax, %rax
jne 0x1e783
leal 0x3(%rbx), %edi
callq 0x23bc5
movq %rax, %r14
xorpd %xmm0, %xmm0
callq 0x25878
movapd %xmm0, %xmm3
movapd 0x20(%rsp), %xmm0
ucomisd 0xc912(%rip), %xmm0     # 0x2b0b0
jb 0x1e82b
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd 0x1092d(%rip), %xmm1    # 0x2f0e0
movsd %xmm1, 0x38(%rsp)
movslq %ebx, %rbx
leaq (%r14,%rbx,8), %r15
xorl %r12d, %r12d
movsd %xmm3, 0x8(%rsp)
movsd (%r14,%rbx,8), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd (%r14,%r12,8), %xmm0
movsd (%r15), %xmm1
callq 0x259e1
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
callq 0x25a1c
movsd 0x38(%rsp), %xmm1
callq 0x259e1
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
callq 0x258cc
incq %r12
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movapd %xmm0, %xmm3
addq $-0x8, %r15
movapd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x1e7c3
cmpq $0x0, 0x58(%rsp)
jne 0x1e672
testq %r14, %r14
je 0x1e672
movq %r14, %rdi
movsd %xmm3, 0x8(%rsp)
callq 0x2220
movsd 0x8(%rsp), %xmm3
jmp 0x1e672