// 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(double*, double*, int, int, double*, double, bool, int, int, int, int)
double get_binomial_pvalue_per_region_neighborhood_window_control(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.
	double total_profile_signal = 0;
	for(int i = region_start; i <= region_end; i++)
	{
		total_profile_signal += floor(signal_track_data[i]);
	} // i loop.

	// If the region size is smaller than 2000 base pairs, get the control signal from a larger region.
	//double total_control_signal = 0;
	int eff_control_region_start = region_start;
	int eff_control_region_end = region_end;
	if((eff_control_region_end - eff_control_region_start+1) <= l_norm_win)
	{
		eff_control_region_start = (region_start+region_end)/2 - l_norm_win;
		eff_control_region_end = (region_start+region_end)/2 + l_norm_win;
	}

	if(eff_control_region_start < 0)
	{
		eff_control_region_start = 1;
	}

	if(eff_control_region_end > l_control)
	{
		eff_control_region_end = l_control;
	}

	if(eff_control_region_end ==eff_control_region_start)
	{
		return(0.0);
		//eff_control_region_end++;
	}

	double total_control_signal = 0;
	for(int cur_start = MAX(1, eff_control_region_start-n_vicinity_wins*(eff_control_region_end-eff_control_region_start)); 
		cur_start < MIN(l_control, eff_control_region_end + n_vicinity_wins*(eff_control_region_end-eff_control_region_start));
		cur_start += (eff_control_region_end-eff_control_region_start))
	{
		double cur_total_control_signal = 0;
		for(int i = cur_start; i <= MIN(l_control, cur_start+(eff_control_region_end-eff_control_region_start)); i++)
		{
			double cur_control_value = control_track_data[i];
			//cur_total_control_signal += floor(cur_control_value);
			cur_total_control_signal += (cur_control_value);
		} // i loop.

		total_control_signal = MAX(total_control_signal, cur_total_control_signal);
	} // i loop.

	// Finally, normalize the control signal from effective window size to the original window size.
	total_control_signal = total_control_signal * (double)(region_end - region_start+1) / (double)(eff_control_region_end - eff_control_region_start+1);

	// Scale the control and profile signals.
	total_control_signal /= scaling_factor;
	total_profile_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, 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 $0x78, %rsp
movl %r9d, 0x2c(%rsp)
movapd %xmm0, 0x60(%rsp)
movq %r8, 0x58(%rsp)
movl %edx, %r12d
movq %rsi, %r14
movl 0xc8(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0xc0(%rsp), %ebp
movl 0xb0(%rsp), %eax
movl %eax, 0x30(%rsp)
xorpd %xmm3, %xmm3
movl %ecx, 0xc(%rsp)
movl %ecx, %eax
subl %edx, %eax
movq %rax, 0x40(%rsp)
jl 0x1e8e3
movslq %r12d, %rax
leaq (%rdi,%rax,8), %r13
movq 0x40(%rsp), %rax
leal 0x1(%rax), %ebx
xorl %r15d, %r15d
movapd %xmm3, 0x10(%rsp)
movsd (%r13,%r15,8), %xmm0
callq 0x22c0
movapd 0x10(%rsp), %xmm3
addsd %xmm0, %xmm3
incq %r15
cmpl %r15d, %ebx
jne 0x1e8bf
movl 0x30(%rsp), %r10d
movq 0x40(%rsp), %rbx
cmpl %r10d, %ebx
jl 0x1e8f8
movl 0xc(%rsp), %esi
jmp 0x1e913
movl 0xc(%rsp), %ecx
addl %r12d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, %r12d
subl %r10d, %r12d
addl %r10d, %eax
movl %eax, %esi
testl %r12d, %r12d
movl $0x1, %ecx
cmovsl %ecx, %r12d
cmpl %ebp, %esi
cmovgel %ebp, %esi
xorpd %xmm2, %xmm2
movl %esi, %eax
subl %r12d, %eax
movl 0x20(%rsp), %r11d
je 0x1eb5f
leal 0x1(%rbx), %edx
imull %eax, %r11d
subl %r11d, %r12d
cmpl $0x2, %r12d
cmovgel %r12d, %ecx
addl %esi, %r11d
cmpl %ebp, %r11d
cmovgel %ebp, %r11d
xorpd %xmm0, %xmm0
cmpl %r11d, %ecx
movq 0x58(%rsp), %r13
jge 0x1e9a4
movl %ecx, %esi
movslq %eax, %rdi
decq %rsi
movl %ecx, %r9d
leal (%r9,%rax), %ecx
cmpl %ebp, %ecx
movl %ebp, %r8d
cmovll %ecx, %r8d
xorpd %xmm1, %xmm1
cmpl %r8d, %r9d
jg 0x1e998
movslq %r8d, %r8
movq %rsi, %r9
addsd 0x8(%r14,%r9,8), %xmm1
incq %r9
cmpq %r8, %r9
jl 0x1e989
maxsd %xmm1, %xmm0
addq %rdi, %rsi
cmpl %r11d, %ecx
jl 0x1e96a
cmpl %r10d, %ebx
setge %cl
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
mulsd %xmm1, %xmm0
incl %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm0
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
movapd 0x60(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm3
testb %cl, 0x2c(%rsp)
je 0x1ea70
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
divsd %xmm0, %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
divpd %xmm1, %xmm3
movapd %xmm3, 0x10(%rsp)
movapd %xmm3, %xmm0
callq 0x22c0
movapd %xmm0, 0x40(%rsp)
movapd 0x10(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0x30(%rsp)
callq 0x22c0
movapd 0x40(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
xorpd %xmm0, %xmm0
cmpeqpd %xmm1, %xmm0
movmskpd %xmm0, %eax
testb $0x2, %al
jne 0x1ea3f
movapd 0x30(%rsp), %xmm0
jmp 0x1ea47
movsd 0xc5c1(%rip), %xmm0     # 0x2b008
testb $0x1, %al
movapd 0x10(%rsp), %xmm3
je 0x1ea6c
movsd 0xc5af(%rip), %xmm0     # 0x2b008
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x25878
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
movapd %xmm0, 0x30(%rsp)
addsd %xmm3, %xmm0
cvttsd2si %xmm0, %ebx
movq %r13, %r14
testq %r13, %r13
jne 0x1ea99
leal 0x3(%rbx), %edi
callq 0x23bc5
movq %rax, %r14
xorpd %xmm0, %xmm0
callq 0x25878
movapd %xmm0, %xmm2
movapd 0x30(%rsp), %xmm0
ucomisd 0xc5fc(%rip), %xmm0     # 0x2b0b0
jb 0x1eb41
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
mulsd 0x10617(%rip), %xmm1    # 0x2f0e0
movsd %xmm1, 0x20(%rsp)
movslq %ebx, %rbx
leaq (%r14,%rbx,8), %r15
xorl %r12d, %r12d
movsd %xmm2, 0x10(%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 0x20(%rsp), %xmm1
callq 0x259e1
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x258cc
incq %r12
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movapd %xmm0, %xmm2
movapd 0x30(%rsp), %xmm0
addq $-0x8, %r15
ucomisd %xmm1, %xmm0
jae 0x1ead9
testq %r13, %r13
jne 0x1eb5f
testq %r14, %r14
je 0x1eb5f
movq %r14, %rdi
movsd %xmm2, 0x10(%rsp)
callq 0x2220
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq