// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movsd %xmm0, 0x28(%rsp)
movq %r8, 0x50(%rsp)
movq %rsi, %r14
movq %rdi, %r12
movl 0xa8(%rsp), %edi
movl 0xa0(%rsp), %esi
movl 0x90(%rsp), %eax
movl %ecx, %ebp
subl %edx, %ebp
leal 0x1(%rbp), %r13d
cmpl %eax, %ebp
jge 0x1e0d1
xorpd %xmm2, %xmm2
cmpl %edx, %ecx
jge 0x1e126
jmp 0x1e1df
movl %edi, 0x8(%rsp)
movl %esi, 0xc(%rsp)
testl %eax, %eax
movq %rax, 0x48(%rsp)
movq %rdx, 0x20(%rsp)
movl %ecx, 0x10(%rsp)
jle 0x1e167
addl %edx, %eax
movslq %edx, %rbp
movslq %eax, %rbx
xorpd %xmm1, %xmm1
movq %rbp, %r15
movsd %xmm1, (%rsp)
movsd (%r12,%r15,8), %xmm0
callq 0x22c0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
incq %r15
cmpq %rbx, %r15
jl 0x1e0fa
movq 0x48(%rsp), %rax
movl 0x10(%rsp), %ecx
jmp 0x1e16e
movl %edi, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movl %esi, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
movslq %edx, %rax
leaq (%r12,%rax,8), %rbx
movl %r13d, %r15d
xorl %r12d, %r12d
movsd %xmm2, (%rsp)
movsd (%rbx,%r12,8), %xmm0
callq 0x22c0
movsd (%rsp), %xmm2
addsd %xmm0, %xmm2
incq %r12
cmpl %r12d, %r15d
jne 0x1e144
jmp 0x1e1ce
movslq %edx, %rbp
xorpd %xmm1, %xmm1
cltq
movslq %ecx, %rbx
leaq (%r12,%rax,8), %r15
subq %rax, %rbx
movapd %xmm1, %xmm2
movsd %xmm1, (%rsp)
movsd %xmm2, 0x30(%rsp)
movsd (%r12,%rbp,8), %xmm0
callq 0x22c0
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd (%r15,%rbp,8), %xmm0
callq 0x22c0
movsd (%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
maxsd 0x30(%rsp), %xmm2
incq %rbp
cmpq %rbx, %rbp
jl 0x1e183
movq 0x48(%rsp), %rbp
movl 0xc(%rsp), %esi
movq 0x20(%rsp), %rdx
movl 0x10(%rsp), %ecx
movl 0x8(%rsp), %edi
imull %edi, %r13d
subl %r13d, %edx
addl %ecx, %r13d
cmpl $0x2, %edx
movl $0x1, %ecx
cmovgel %edx, %ecx
cmpl %esi, %r13d
cmovgel %esi, %r13d
leal (%rcx,%rbp), %edx
xorpd %xmm3, %xmm3
cmpl %esi, %edx
jle 0x1e219
movapd %xmm3, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, %eax
testl %ebp, %ebp
jle 0x1e237
leaq (%r14,%rax,8), %rsi
xorpd %xmm0, %xmm0
addsd (%rsi), %xmm0
addq $0x8, %rsi
incl %ecx
cmpl %ecx, %edx
jg 0x1e227
jmp 0x1e23b
xorpd %xmm0, %xmm0
movslq %ebp, %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 0x1e24c
movsd 0x28(%rsp), %xmm1
divsd %xmm1, %xmm0
divsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
movapd %xmm0, 0x30(%rsp)
callq 0x22c0
movapd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
callq 0x22c0
ucomisd 0xbe0f(%rip), %xmm0     # 0x2a0b0
xorpd %xmm0, %xmm0
jne 0x1e2c4
jp 0x1e2c4
movsd 0xbd57(%rip), %xmm0     # 0x2a008
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2525c
movapd 0x10(%rsp), %xmm2
cmpeqsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andnpd 0x30(%rsp), %xmm0
movsd 0xbd27(%rip), %xmm1     # 0x2a008
andpd %xmm2, %xmm1
orpd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebx
movq 0x50(%rsp), %rax
movq %rax, %r14
testq %rax, %rax
jne 0x1e314
leal 0x3(%rbx), %edi
callq 0x236bc
movq %rax, %r14
xorpd %xmm0, %xmm0
callq 0x2525c
movapd %xmm0, %xmm3
movapd 0x10(%rsp), %xmm0
ucomisd 0xbd81(%rip), %xmm0     # 0x2a0b0
jb 0x1e3ba
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd 0xfd9c(%rip), %xmm1     # 0x2e0e0
movsd %xmm1, 0x28(%rsp)
movslq %ebx, %rbx
leaq (%r14,%rbx,8), %r15
xorl %r12d, %r12d
movsd %xmm3, (%rsp)
movsd (%r14,%rbx,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd (%r14,%r12,8), %xmm0
movsd (%r15), %xmm1
callq 0x253c5
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
callq 0x25400
movsd 0x28(%rsp), %xmm1
callq 0x253c5
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
callq 0x252b0
incq %r12
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movapd %xmm0, %xmm3
addq $-0x8, %r15
movapd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x1e354
cmpq $0x0, 0x50(%rsp)
jne 0x1e206
testq %r14, %r14
je 0x1e206
movq %r14, %rdi
movsd %xmm3, (%rsp)
callq 0x2220
movsd (%rsp), %xmm3
jmp 0x1e206