// File: gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: get_modified_binomial_pvalue_per_region(double*, double*, double, int, int, double*, double, bool, int)
double get_modified_binomial_pvalue_per_region(double* signal_track_data, double* control_track_data, 
	double flip_prob, 
	int region_start, int region_end, 
	double* _log_factorials, 
	double scaling_factor,
	bool length_normalization, int l_norm_win)
{
	// Get the p-value for the current region.
	double total_profile_signal = 0;
	double total_control_signal = 0;
	for(int i = region_start; i <= region_end; i++)
	{
		total_profile_signal += floor(signal_track_data[i]);
		total_control_signal += floor(control_track_data[i]);	
	} // i loop.

	total_profile_signal /= scaling_factor;

	// Scale control signal.
	total_control_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(flip_prob);
	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 $0xa8, %rsp
movb %r9b, %al
movl 0xb0(%rsp), %r9d
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movsd %xmm0, 0x88(%rsp)
movl %edx, 0x84(%rsp)
movl %ecx, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movsd %xmm1, 0x70(%rsp)
andb $0x1, %al
movb %al, 0x6f(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x80(%rsp), %eax
jg 0x374c7
movq 0x98(%rsp), %rax
movslq 0x54(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
addsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x90(%rsp), %rax
movslq 0x54(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x22e0
addsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x37467
movsd 0x70(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x70(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
testb $0x1, 0x6f(%rsp)
je 0x375d7
movl 0x80(%rsp), %eax
subl 0x84(%rsp), %eax
addl $0x1, %eax
cmpl 0xb0(%rsp), %eax
jle 0x375d7
movsd 0x60(%rsp), %xmm0
movl 0x80(%rsp), %eax
subl 0x84(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl 0xb0(%rsp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x58(%rsp), %xmm0
movl 0x80(%rsp), %eax
subl 0x84(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
cvtsi2sdl 0xb0(%rsp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x375a4
jp 0x375a4
movsd 0x1aa7a(%rip), %xmm0    # 0x52018
movsd %xmm0, 0x58(%rsp)
movsd 0x60(%rsp), %xmm0
callq 0x22e0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x375d5
jp 0x375d5
movsd 0x1aa56(%rip), %xmm0    # 0x52018
callq 0x441e0
movsd %xmm0, 0xa0(%rsp)
jmp 0x3773e
jmp 0x375d7
movsd 0x60(%rsp), %xmm0
addsd 0x58(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x3760f
movl 0x50(%rsp), %edi
addl $0x3, %edi
callq 0x3f420
movq %rax, 0x48(%rsp)
jmp 0x37619
movq 0x78(%rsp), %rax
movq %rax, 0x48(%rsp)
movsd 0x88(%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
callq 0x441e0
movsd %xmm0, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
cvtsi2sdl 0x34(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3770b
movsd 0x40(%rsp), %xmm0
cvtsi2sdl 0x50(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x48(%rsp), %rax
movslq 0x50(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rax
movslq 0x34(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x48(%rsp), %rax
movl 0x50(%rsp), %ecx
subl 0x34(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x445a0
movsd %xmm0, 0x20(%rsp)
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
callq 0x44510
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
callq 0x44310
movsd %xmm0, 0x38(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x37643
cmpq $0x0, 0x78(%rsp)
jne 0x3772f
movq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x3772d
movq 0x8(%rsp), %rdi
callq 0x2220
jmp 0x3772f
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
addq $0xa8, %rsp
retq
nop