// File: /workspace/github_cpp_makefile_star2_O2/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, 0x58(%rsp)
movq %r8, 0x48(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movl 0xb8(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0xa0(%rsp), %eax
movl %ecx, %ebx
subl %edx, %ebx
leal 0x1(%rbx), %r13d
cmpl %eax, %ebx
movq %rdx, 0x60(%rsp)
movl %ecx, 0x20(%rsp)
jge 0x19a64
movslq %edx, %r15
movslq %ecx, %rbp
xorpd %xmm2, %xmm2
cmpq %rbp, %r15
jg 0x19b01
movsd (%r12,%r15,8), %xmm0
movsd %xmm2, 0x8(%rsp)
callq 0x22b0
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
incq %r15
jmp 0x19a3b
movq %rax, 0x50(%rsp)
addl %edx, %eax
movslq %edx, %rbx
movslq %eax, %r15
xorpd %xmm1, %xmm1
movq %rbx, %rbp
cmpq %r15, %rbp
movsd %xmm1, 0x8(%rsp)
jge 0x19a9d
movsd (%r12,%rbp,8), %xmm0
callq 0x22b0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %rbp
jmp 0x19a78
movslq 0x50(%rsp), %rax
movslq 0x20(%rsp), %rbp
leaq (%r12,%rax,8), %r15
subq %rax, %rbp
movapd %xmm1, %xmm2
movsd %xmm2, 0x30(%rsp)
movsd (%r12,%rbx,8), %xmm0
callq 0x22b0
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd (%r15,%rbx,8), %xmm0
callq 0x22b0
movsd 0x8(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
maxsd 0x30(%rsp), %xmm2
incq %rbx
cmpq %rbp, %rbx
jl 0x19ab2
movq 0x50(%rsp), %rbx
imull 0x1c(%rsp), %r13d
movq 0x60(%rsp), %rcx
subl %r13d, %ecx
addl 0x20(%rsp), %r13d
cmpl $0x2, %ecx
pushq $0x1
popq %rax
cmovgel %ecx, %eax
movl 0x10(%rsp), %edx
cmpl %edx, %r13d
cmovgel %edx, %r13d
leal (%rbx,%rax), %ecx
xorpd %xmm3, %xmm3
cmpl %edx, %ecx
jle 0x19b46
movapd %xmm3, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %eax
movslq %ecx, %rcx
xorpd %xmm0, %xmm0
movq %rax, %rdx
cmpq %rcx, %rdx
jge 0x19b62
addsd (%r14,%rdx,8), %xmm0
incq %rdx
jmp 0x19b52
movslq %ebx, %rsi
movslq %r13d, %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 0x19b73
movsd 0x58(%rsp), %xmm1
divsd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
callq 0x22b0
movapd %xmm0, 0x30(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x22b0
ucomisd 0xa4e6(%rip), %xmm0     # 0x240b0
xorpd %xmm0, %xmm0
jne 0x19bed
jp 0x19bed
movsd 0xa42e(%rip), %xmm0     # 0x24008
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1f860
movapd 0x30(%rsp), %xmm2
cmpeqsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andnpd 0x20(%rsp), %xmm0
movsd 0xa3fe(%rip), %xmm1     # 0x24008
andpd %xmm2, %xmm1
orpd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebx
movq 0x48(%rsp), %rax
movq %rax, %r14
testq %rax, %rax
jne 0x19c3e
leal 0x3(%rbx), %edi
callq 0x1e201
movq %rax, %r14
xorpd %xmm0, %xmm0
callq 0x1f860
cvtsi2sd %ebx, %xmm1
mulsd 0xe48d(%rip), %xmm1     # 0x280e0
movsd %xmm1, 0x10(%rsp)
movapd %xmm0, %xmm3
movslq %ebx, %rbx
xorl %r15d, %r15d
movq %rbx, %r12
cvtsi2sd %r15d, %xmm0
movapd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm3, 0x8(%rsp)
jb 0x19cce
movsd (%r14,%rbx,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd (%r14,%r15,8), %xmm0
movsd (%r14,%r12,8), %xmm1
callq 0x1f994
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
callq 0x1f9cf
movsd 0x10(%rsp), %xmm1
callq 0x1f994
movapd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
callq 0x1f8b2
movapd %xmm0, %xmm3
incq %r15
decq %r12
jmp 0x19c66
cmpq $0x0, 0x48(%rsp)
jne 0x19b33
testq %r14, %r14
je 0x19b33
movq %r14, %rdi
callq 0x2210
movsd 0x8(%rsp), %xmm3
jmp 0x19b33