// File: 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:
subq $0xe8, %rsp
movb %r9b, %al
movl 0x108(%rsp), %r9d
movl 0x100(%rsp), %r9d
movl 0xf8(%rsp), %r9d
movl 0xf0(%rsp), %r9d
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movl %edx, 0xcc(%rsp)
movl %ecx, 0xc8(%rsp)
movq %r8, 0xc0(%rsp)
movsd %xmm0, 0xb8(%rsp)
andb $0x1, %al
movb %al, 0xb7(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jg 0x36afb
movq 0xd8(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
addsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x36aac
movl 0xcc(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0xc8(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
subl 0xa0(%rsp), %eax
addl $0x1, %eax
cmpl 0xf0(%rsp), %eax
jg 0x36b79
movl 0xcc(%rsp), %eax
addl 0xc8(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
subl 0xf0(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0xcc(%rsp), %eax
addl 0xc8(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl 0xf0(%rsp), %eax
movl %eax, 0x9c(%rsp)
cmpl $0x0, 0xa0(%rsp)
jge 0x36b8e
movl $0x1, 0xa0(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x100(%rsp), %eax
jle 0x36bac
movl 0x100(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jne 0x36bcd
xorps %xmm0, %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x370c4
xorps %xmm0, %xmm0
movsd %xmm0, 0x90(%rsp)
movl 0xa0(%rsp), %ecx
movl 0x108(%rsp), %eax
movl 0x9c(%rsp), %edx
subl 0xa0(%rsp), %edx
imull %edx, %eax
subl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jle 0x36c0e
movl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x36c33
movl 0xa0(%rsp), %eax
movl 0x108(%rsp), %ecx
movl 0x9c(%rsp), %edx
subl 0xa0(%rsp), %edx
imull %edx, %ecx
subl %ecx, %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x100(%rsp), %eax
movl 0x9c(%rsp), %ecx
movl 0x108(%rsp), %edx
movl 0x9c(%rsp), %esi
subl 0xa0(%rsp), %esi
imull %esi, %edx
addl %edx, %ecx
cmpl %ecx, %eax
jle 0x36c9c
movl 0x9c(%rsp), %eax
movl 0x108(%rsp), %ecx
movl 0x9c(%rsp), %edx
subl 0xa0(%rsp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, 0x2c(%rsp)
jmp 0x36ca7
movl 0x100(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x30(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x36dd0
xorps %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x100(%rsp), %eax
movl 0x8c(%rsp), %ecx
movl 0x9c(%rsp), %edx
subl 0xa0(%rsp), %edx
addl %edx, %ecx
cmpl %ecx, %eax
jle 0x36d15
movl 0x8c(%rsp), %eax
movl 0x9c(%rsp), %ecx
subl 0xa0(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x24(%rsp)
jmp 0x36d20
movl 0x100(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x28(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jg 0x36d6c
movq 0xd0(%rsp), %rax
movslq 0x7c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x36cce
movsd 0x90(%rsp), %xmm0
ucomisd 0x80(%rsp), %xmm0
jbe 0x36d91
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
jmp 0x36da0
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movl 0x9c(%rsp), %eax
subl 0xa0(%rsp), %eax
addl 0x8c(%rsp), %eax
movl %eax, 0x8c(%rsp)
jmp 0x36c3e
movsd 0x90(%rsp), %xmm0
movl 0xc8(%rsp), %eax
subl 0xcc(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
movl 0x9c(%rsp), %eax
subl 0xa0(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0xb8(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0xb8(%rsp), %xmm1
movsd 0xa8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
testb $0x1, 0xb7(%rsp)
je 0x36f4e
movl 0xc8(%rsp), %eax
subl 0xcc(%rsp), %eax
addl $0x1, %eax
cmpl 0xf0(%rsp), %eax
jle 0x36f4e
movsd 0xa8(%rsp), %xmm0
movl 0xc8(%rsp), %eax
subl 0xcc(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl 0xf0(%rsp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0x90(%rsp), %xmm0
movl 0xc8(%rsp), %eax
subl 0xcc(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl 0xf0(%rsp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x36f18
jp 0x36f18
movsd 0x1b109(%rip), %xmm0    # 0x52018
movsd %xmm0, 0x90(%rsp)
movsd 0xa8(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x36f4c
jp 0x36f4c
movsd 0x1b0df(%rip), %xmm0    # 0x52018
callq 0x441e0
movsd %xmm0, 0xe0(%rsp)
jmp 0x370c4
jmp 0x36f4e
movsd 0xa8(%rsp), %xmm0
addsd 0x90(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x6c(%rsp)
movq $0x0, 0x60(%rsp)
cmpq $0x0, 0xc0(%rsp)
jne 0x36f8f
movl 0x6c(%rsp), %edi
addl $0x3, %edi
callq 0x3f420
movq %rax, 0x60(%rsp)
jmp 0x36f9c
movq 0xc0(%rsp), %rax
movq %rax, 0x60(%rsp)
movsd 0x1b094(%rip), %xmm0    # 0x52038
callq 0x2140
movsd %xmm0, 0x58(%rsp)
xorps %xmm0, %xmm0
callq 0x441e0
movsd %xmm0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
cvtsi2sdl 0x4c(%rsp), %xmm1
movsd 0x90(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x37090
movsd 0x58(%rsp), %xmm0
cvtsi2sdl 0x6c(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x60(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x60(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x60(%rsp), %rax
movl 0x6c(%rsp), %ecx
subl 0x4c(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
callq 0x445a0
movsd %xmm0, 0x38(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x44310
movsd %xmm0, 0x50(%rsp)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x36fc5
cmpq $0x0, 0xc0(%rsp)
jne 0x370b5
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x370b3
movq (%rsp), %rdi
callq 0x2220
jmp 0x370b5
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0xe0(%rsp), %xmm0
addq $0xe8, %rsp
retq
nopw %cs:(%rax,%rax)