// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: prune_region_ends_per_p_value_minimization_buffered_binomial_p_vals(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, double*, int, double*, int, int)
vector<t_annot_region*>* prune_region_ends_per_p_value_minimization_buffered_binomial_p_vals(vector<t_annot_region*>* regions, 
																							double* signal_profile, int l_profile, 
																							double* control_profile, int l_control,
																							int l_fragment)
{	
	// List of pruned regions.
	vector<t_annot_region*>* pruned_regions = new vector<t_annot_region*>();

	double max_grand_total_int = 0.0;
	for(int i_reg = 0; i_reg < (int)regions->size(); i_reg++)
	{
		// Get the extrema signal values.
		double current_grand_total = 0.0;
		for(int i = regions->at(i_reg)->start; i <= regions->at(i_reg)->end; i++)
		{
			if(i < l_profile)
			{
				current_grand_total += signal_profile[i];
			}

			if(i < l_control)
			{
				current_grand_total += control_profile[i];
			}
		} // i loop.

		if(max_grand_total_int < current_grand_total)
		{
			max_grand_total_int = (int)(floor(current_grand_total));
		} 
	} // i_reg loop.

if(__DUMP_SIGNAL_ENRICHMENT_MSGS__)
{
	fprintf(stderr, "Setting up the log factorials with maximum grand total of %lf\n", max_grand_total_int);
}

	double* log_factorials = buffer_log_factorials((int)(max_grand_total_int+20));

	int buffered_binomial_bin_size = 500;
	double** buffered_binomial_p_vals = buffer_binomial_pvalues((max_grand_total_int / l_fragment) + 100, log_factorials, buffered_binomial_bin_size);

if(__DUMP_SIGNAL_ENRICHMENT_MSGS__)
{
	fprintf(stderr, "Pruning region ends for %d regions via p-value minimization.\n", (int)regions->size());
}

	// Go over all the regions.
	vector<double>* p_values_per_threshold = new vector<double>();
	vector<double>* pruned_starts_per_thresholds = new vector<double>();
	vector<double>* pruned_ends_per_thresholds = new vector<double>();
	for(int i_reg = 0; i_reg < (int)regions->size(); i_reg++)
	{
		//if(i_reg % 1000 == 0)
		//{
		//	fprintf(stderr, "Processing %d.(%d) region: %d-%d\n", i_reg, (int)regions->size(), regions->at(i_reg)->start, regions->at(i_reg)->end);
		//}

		// Skip the region if the signal is out of the control signal range.
		if(regions->at(i_reg)->end > l_control)
		{
			fprintf(stderr, "Skipping pruning %d-%d\n", regions->at(i_reg)->start, regions->at(i_reg)->end);
			continue;
		}

		// Get the extrema signal values.
		int max_sig = 0;
		int min_sig = 1000*1000;
		double total_signal = 0;
		for(int i = regions->at(i_reg)->start; i <= regions->at(i_reg)->end; i++)
		{
			int cur_floorized_val = (int)(floor(signal_profile[i]));
			if(max_sig < cur_floorized_val)
			{
				max_sig = cur_floorized_val;
			}

			if(min_sig > cur_floorized_val)
			{
				min_sig = cur_floorized_val;
			}

			total_signal += signal_profile[i];
		} // i loop.

		// Limit the maximum at 500.
		max_sig = (max_sig > 500)?(500):(max_sig);

		// Start thresholding over all possible thresholds.			
		for(int sig_thresh = min_sig; sig_thresh <= max_sig; sig_thresh++)
		{
			// Threshold the ends.
			int pruned_start = regions->at(i_reg)->start;
			int pruned_end = regions->at(i_reg)->end;

			//bool passed_threshold = false;
			for(int i_pr = pruned_start; i_pr <= pruned_end; i_pr++)
			{
				if(signal_profile[i_pr] > sig_thresh)
				{
					//passed_threshold = true;
					pruned_start = i_pr;
					break;
				}
			} // i_pr loop.

			for(int i_pr = pruned_end; i_pr >= pruned_start; i_pr--)
			{
				if(signal_profile[i_pr] > sig_thresh)
				{
					pruned_end = i_pr;
					break;
				}
			} // i_pr loop.

			// Get the binomial p-value for current region.
			//double test_cur_pruned_pvalue = get_binomial_pvalue_per_region(signal_profile, control_profile, pruned_start, pruned_end, log_factorials, l_fragment, false, 0);

			// Get the p-value for the current region.
			double total_profile_signal = 0;
			double total_control_signal = 0;
			for(int i = pruned_start; i <= pruned_end; i++)
			{
				total_profile_signal += floor(signal_profile[i]);
				total_control_signal += (control_profile[i]);
			} // i loop.

			total_profile_signal /= l_fragment;
			total_control_signal /= l_fragment;
	
			int grand_total_int = (int)(total_profile_signal + total_control_signal);

			double log_flip = log(.5);

			int buffered_base_val = ((int)(total_control_signal / buffered_binomial_bin_size)) * buffered_binomial_bin_size;
			double cur_pruned_pvalue = buffered_binomial_p_vals[grand_total_int][buffered_base_val];
			for(int i = buffered_base_val+1; 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.
				log_cur_perm = xlog_div(log_factorials[grand_total_int], xlog_mul(log_factorials[i], log_factorials[grand_total_int-i]));
				cur_pruned_pvalue = xlog_sum(cur_pruned_pvalue, xlog_mul(log_cur_perm, log_cur_half_pow));
			} // i loop.

			//fprintf(stderr, "p-vals: %lf, %lf\n", test_cur_pruned_pvalue, cur_pruned_pvalue);

			// Add the current p-value.
			p_values_per_threshold->push_back(cur_pruned_pvalue);
			pruned_starts_per_thresholds->push_back(pruned_start);
			pruned_ends_per_thresholds->push_back(pruned_end);

			double total_pruned_sig = 0;
			for(int i_sig = pruned_start; i_sig <= pruned_end; i_sig++)
			{
				total_pruned_sig += signal_profile[i_sig];
			} // i_sig.

			// If we are below 50% of the total probability, stop since most of the signal is lost already.
			if(total_pruned_sig/total_signal <= 0.5)
			{
				break;
			}
		} // sig thresh loop.

		// If the condition is satisfied, we are done.
		if(p_values_per_threshold->size() > 0)
		{
			double min_p_val = p_values_per_threshold->at(0);
			int min_p_val_i = 0;
			for(int i_th = 0; i_th < (int)p_values_per_threshold->size(); i_th++)
			{
				if(min_p_val > p_values_per_threshold->at(i_th))
				{
					min_p_val = p_values_per_threshold->at(i_th);
					min_p_val_i = i_th;
				}
			} // total mass fraction check.

			t_annot_region* pruned_peak = duplicate_region(regions->at(i_reg));
			pruned_peak->start = (int)(pruned_starts_per_thresholds->at(min_p_val_i));
			pruned_peak->end = (int)(pruned_ends_per_thresholds->at(min_p_val_i));

			pruned_regions->push_back(pruned_peak);
		}
		else
		{
			t_annot_region* pruned_peak = duplicate_region(regions->at(i_reg));
			pruned_regions->push_back(pruned_peak);
		}

		// Clear the values.
		p_values_per_threshold->clear();
		pruned_starts_per_thresholds->clear();
		pruned_ends_per_thresholds->clear();
	} // i_reg loop.

	// Free buffered value memory.
	delete [] log_factorials;
	delete_buffered_binomial_pvalues(buffered_binomial_p_vals, max_grand_total_int, buffered_binomial_bin_size);

	// Dump the bed file.
	//dump_BED(op_bed_fp, pruned_regions);

	return(pruned_regions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %r9d, 0xb0(%rsp)
movl %r8d, %r14d
movq %rcx, %r15
movl %edx, %ebp
movq %rsi, 0x50(%rsp)
movq %rdi, 0x38(%rsp)
pushq $0x18
popq %rdi
callq 0x2170
movl %r14d, 0x64(%rsp)
movslq %r14d, %r12
movslq %ebp, %rbp
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movq %rax, 0x40(%rsp)
movupd %xmm0, (%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
xorl %r13d, %r13d
movq 0x38(%rsp), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0x1b7c7
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %r14
jg 0x1b79e
cmpq %rbp, %r14
movsd 0x10(%rsp), %xmm0
jge 0x1b788
movq 0x50(%rsp), %rax
addsd (%rax,%r14,8), %xmm0
cmpq %r12, %r14
jge 0x1b793
addsd (%r15,%r14,8), %xmm0
movsd %xmm0, 0x10(%rsp)
incq %r14
jmp 0x1b759
movsd 0x10(%rsp), %xmm0
ucomisd 0x70(%rsp), %xmm0
jbe 0x1b7bf
callq 0x22b0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
incq %r13
jmp 0x1b725
cmpb $0x0, 0x199fa(%rip)     # 0x351c8
jne 0x1be1b
movsd 0xb1ec(%rip), %xmm0     # 0x269c8
addsd 0x70(%rsp), %xmm0
cvttsd2si %xmm0, %edi
callq 0x1e201
movq %rax, %rbx
cvtsi2sdl 0xb0(%rsp), %xmm1
movapd 0x70(%rsp), %xmm0
movapd %xmm1, 0xa0(%rsp)
divsd %xmm1, %xmm0
addsd 0x880e(%rip), %xmm0     # 0x24020
cvttsd2si %xmm0, %edi
movq %rax, %rsi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x1e08d
movq %rax, 0x80(%rsp)
cmpb $0x1, 0x19996(%rip)     # 0x351c8
movq 0x50(%rsp), %r14
movq 0x38(%rsp), %r12
je 0x1be3e
pushq $0x18
popq %rbp
movq %rbp, %rdi
callq 0x2170
movq %rax, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rbp, %rdi
callq 0x2170
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
pushq $0x18
popq %rdi
callq 0x2170
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x30(%rsp)
andq $0x0, 0x10(%rax)
movapd 0xa0(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
leaq -0x8(%rbx), %rax
movq %rax, 0xd0(%rsp)
movq %rbx, 0x90(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0xc8(%rsp)
xorl %ebx, %ebx
movq %r15, 0xd8(%rsp)
movapd %xmm0, 0xa0(%rsp)
movq %r13, 0x88(%rsp)
movq %rbp, 0x48(%rsp)
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x1bdda
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x64(%rsp), %ecx
cmpl %ecx, 0x20(%rax)
movq %rbx, 0x20(%rsp)
jg 0x1bd8b
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %rbx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movl $0xf4240, %ebp          # imm = 0xF4240
xorl %r13d, %r13d
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbx
jg 0x1b989
movsd (%r14,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x22b0
cvttsd2si %xmm0, %eax
cmpl %eax, %r13d
cmovlel %eax, %r13d
cmpl %eax, %ebp
cmovgel %eax, %ebp
movsd 0x58(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
incq %rbx
jmp 0x1b938
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %r13d
cmovgel %eax, %r13d
movl %r13d, 0x68(%rsp)
movq 0x20(%rsp), %rbx
movq 0x88(%rsp), %r13
cmpl 0x68(%rsp), %ebp
jg 0x1bc50
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r13d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %edi
movl %ebp, 0x6c(%rsp)
cvtsi2sd %ebp, %xmm0
movl %r13d, 0x2c(%rsp)
movslq %r13d, %rbp
movslq %edi, %r12
movq %rbp, %rax
shlq $0x20, %rax
movq %rbp, %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
movabsq $0x100000000, %rsi      # imm = 0x100000000
cmpq %r12, %rcx
jg 0x1ba29
movsd (%r14,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1ba1e
incq %rcx
addq %rsi, %rax
jmp 0x1ba05
sarq $0x20, %rax
movq %rax, %rbp
movl %ecx, 0x2c(%rsp)
movq %r12, %rax
shlq $0x20, %rax
movq %r12, %rcx
cmpq %rbp, %rcx
jl 0x1ba4c
movsd (%r14,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1ba52
decq %rcx
addq %rdx, %rax
jmp 0x1ba33
movl %edi, 0x28(%rsp)
jmp 0x1ba5d
sarq $0x20, %rax
movq %rax, %r12
movl %ecx, 0x28(%rsp)
xorpd %xmm2, %xmm2
movq %rbp, %rbx
movapd 0xa0(%rsp), %xmm0
cmpq %r12, %rbx
jg 0x1baa1
movsd (%r14,%rbx,8), %xmm0
movapd %xmm2, 0x10(%rsp)
callq 0x22b0
movapd 0x10(%rsp), %xmm2
movhpd (%r15,%rbx,8), %xmm0    # xmm0 = xmm0[0],mem[0]
addpd %xmm0, %xmm2
movapd 0xa0(%rsp), %xmm0
incq %rbx
jmp 0x1ba6d
divpd %xmm0, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
movapd %xmm1, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movapd %xmm1, 0xb0(%rsp)
divsd 0xc61e(%rip), %xmm1     # 0x280e8
cvttsd2si %xmm1, %ecx
imull $0x1f4, %ecx, %ecx      # imm = 0x1F4
movslq %eax, %rbx
movq 0x80(%rsp), %rax
movq (%rax,%rbx,8), %rax
movslq %ecx, %r14
movsd (%rax,%r14,8), %xmm1
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movsd %xmm1, 0x8(%rsp)
mulsd 0xc5de(%rip), %xmm0     # 0x280e0
movapd %xmm0, 0xe0(%rsp)
leaq (,%rbx,8), %r13
leaq (,%r14,8), %rax
subq %rax, %r13
addq 0xd0(%rsp), %r13
movq 0xc8(%rsp), %rax
leaq (%rax,%r14,8), %r15
incl %r14d
cvtsi2sd %r14d, %xmm0
movapd 0xb0(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x1bbb2
movq 0x90(%rsp), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd (%r15), %xmm0
movsd (%r13), %xmm1
callq 0x1f994
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x1f9cf
movsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
movaps 0xe0(%rsp), %xmm1
callq 0x1f994
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
callq 0x1f8b2
movsd %xmm0, 0x8(%rsp)
addq $-0x8, %r13
addq $0x8, %r15
jmp 0x1bb32
movq 0x88(%rsp), %r13
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
callq 0x7426
cvtsi2sdl 0x2c(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x98(%rsp), %rbx
movq %rbx, %rsi
callq 0x1c87e
cvtsi2sdl 0x28(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x1c87e
xorpd %xmm0, %xmm0
movq 0xd8(%rsp), %r15
movq 0x50(%rsp), %r14
movq 0x20(%rsp), %rbx
cmpq %r12, %rbp
jg 0x1bc2d
addsd (%r14,%rbp,8), %xmm0
incq %rbp
jmp 0x1bc1d
divsd 0x58(%rsp), %xmm0
movl 0x6c(%rsp), %ebp
incl %ebp
movsd 0x8417(%rip), %xmm1     # 0x24058
ucomisd %xmm0, %xmm1
movq 0x38(%rsp), %r12
jb 0x1b9a7
movq 0x8(%r13), %rax
cmpq (%r13), %rax
je 0x1bd1f
xorl %ebx, %ebx
movq %r13, %rdi
xorl %esi, %esi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ebp, %ebp
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x1bcbc
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7466
movsd 0x10(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x1bcb7
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movl %ebx, %ebp
incq %rbx
jmp 0x1bc76
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x8(%rsp)
movslq %ebp, %rbx
movq 0x48(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x8(%rsp), %rcx
movl %eax, 0xc(%rcx)
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x8(%rsp), %rcx
movl %eax, 0x20(%rcx)
movq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
jmp 0x1bd4b
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x8(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7448
movq 0x48(%rsp), %rbp
movq (%r13), %rax
cmpq %rax, 0x8(%r13)
je 0x1bd59
movq %rax, 0x8(%r13)
movq (%rbp), %rax
cmpq %rax, 0x8(%rbp)
movq 0x20(%rsp), %rbx
je 0x1bd6c
movq %rax, 0x8(%rbp)
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x1bd83
movq 0x30(%rsp), %rcx
movq %rax, 0x8(%rcx)
incq %rbx
jmp 0x1b8dc
movq 0x1924e(%rip), %rax     # 0x34fe0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %ebx
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ecx
movq 0x10(%rsp), %rdi
leaq 0xc4c1(%rip), %rsi      # 0x2828b
movl %ebx, %edx
movq 0x20(%rsp), %rbx
xorl %eax, %eax
callq 0x2240
jmp 0x1bd83
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1bdec
callq 0x2210
cvttsd2si 0x70(%rsp), %esi
movq 0x80(%rsp), %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x1e1c3
movq 0x40(%rsp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x191be(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xc3e2(%rip), %rsi      # 0x2820e
movapd 0x70(%rsp), %xmm0
movb $0x1, %al
callq 0x2240
jmp 0x1b7d4
movq 0x1919b(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
movq 0x8(%r12), %rdx
subq (%r12), %rdx
shrq $0x3, %rdx
leaq 0xc3f1(%rip), %rsi      # 0x2824d
xorl %eax, %eax
callq 0x2240
jmp 0x1b842