// File: gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: prune_region_ends_per_p_value_minimization_per_window(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_per_window(vector<t_annot_region*>* regions, double* signal_track_data, int l_profile_signal, 
	double* control_track_data, int l_control_signal,
	int l_fragment)
{
	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_signal)
			{
				current_grand_total += signal_track_data[i];
			}

			if(i < l_control_signal)
			{
				current_grand_total += control_track_data[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));

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_signal)
		{
			//fprintf(stderr, "Skipping pruning %d-%d\n", regions->at(i_reg)->start, regions->at(i_reg)->end);
			continue;
		}

		double total_signal = 0;
		for(int i = regions->at(i_reg)->start; i <= regions->at(i_reg)->end; i++)
		{
			total_signal += signal_track_data[i];
		} // i loop.

		int l_win = (regions->at(i_reg)->end - regions->at(i_reg)->start) / 40;
		if(l_win < 100)
		{
			l_win = 100;
		}

		// Start thresholding over all possible thresholds.			
		//for(int sig_thresh = min_sig; sig_thresh <= max_sig; sig_thresh++)
		for(int cur_start = regions->at(i_reg)->start; cur_start <= regions->at(i_reg)->end; cur_start += l_win)
		{
			double cur_total_sig_till_end = 0.0;
			for(int i_sig = cur_start; i_sig <= regions->at(i_reg)->end; i_sig++)
			{
				cur_total_sig_till_end += signal_track_data[i_sig];
			} // i_sig.

			// If we do not have enough signal till the end, just break.
			if(cur_total_sig_till_end / total_signal < 0.5)
			{
				break;
			}

			for(int cur_end = cur_start + l_win; cur_end < regions->at(i_reg)->end; cur_end += l_win)
			{
				// Add the current p-value.
				double total_pruned_sig = 0;
				for(int i_sig = cur_start; i_sig <= cur_end; i_sig++)
				{
					total_pruned_sig += signal_track_data[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)
				{
					// Get the binomial p-value for current region.
					double cur_pruned_pvalue = get_binomial_pvalue_per_region(signal_track_data, control_track_data, cur_start, cur_end, log_factorials, l_fragment, false, 0);

					p_values_per_threshold->push_back(cur_pruned_pvalue);
					pruned_starts_per_thresholds->push_back(cur_start);
					pruned_ends_per_thresholds->push_back(cur_end);
				}
			} // cur_end loop.
		} // 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.

	return(pruned_regions);
}

// Assembly:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x140(%rsp)
movq %rsi, 0x138(%rsp)
movl %edx, 0x134(%rsp)
movq %rcx, 0x128(%rsp)
movl %r8d, 0x124(%rsp)
movl %r9d, 0x120(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x58(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x58(%rsp), %rdi
callq 0xc670
movq 0x58(%rsp), %rax
movq %rax, 0x118(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x110(%rsp)
movl $0x0, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x140(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x3874f
xorps %xmm0, %xmm0
movsd %xmm0, 0x100(%rsp)
movq 0x140(%rsp), %rdi
movslq 0x10c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x140(%rsp), %rdi
movslq 0x10c(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x50(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x38700
movl 0xfc(%rsp), %eax
cmpl 0x134(%rsp), %eax
jge 0x386b1
movq 0x138(%rsp), %rax
movslq 0xfc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x100(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
movl 0xfc(%rsp), %eax
cmpl 0x124(%rsp), %eax
jge 0x386e8
movq 0x128(%rsp), %rax
movslq 0xfc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x100(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
jmp 0x386ea
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x38647
movsd 0x110(%rsp), %xmm1
movsd 0x100(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x38737
movsd 0x100(%rsp), %xmm0
callq 0x22e0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x110(%rsp)
jmp 0x38739
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x385f2
testb $0x1, 0x2ca8a(%rip)     # 0x651e0
je 0x38779
movq 0x2c881(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movsd 0x110(%rsp), %xmm0
leaq 0x1de48(%rip), %rsi     # 0x565ba
movb $0x1, %al
callq 0x2250
movsd 0x1c5d7(%rip), %xmm0    # 0x54d58
addsd 0x110(%rsp), %xmm0
cvttsd2si %xmm0, %edi
callq 0x3f420
movq %rax, 0xf0(%rsp)
testb $0x1, 0x2ca3e(%rip)     # 0x651e0
je 0x387d5
movq 0x2c835(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x140(%rsp), %rdi
callq 0xc680
movq 0x48(%rsp), %rdi
movl %eax, %edx
leaq 0x1de2b(%rip), %rsi     # 0x565f9
movb $0x0, %al
callq 0x2250
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x30(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x30(%rsp), %rdi
callq 0xc840
movq 0x30(%rsp), %rax
movq %rax, 0xe8(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x38(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x38(%rsp), %rdi
callq 0xc840
movq 0x38(%rsp), %rax
movq %rax, 0xe0(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x40(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x40(%rsp), %rdi
callq 0xc840
movq 0x40(%rsp), %rax
movq %rax, 0xd8(%rsp)
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x140(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x38e1c
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
cmpl 0x124(%rsp), %eax
jle 0x388cf
jmp 0x38e06
xorps %xmm0, %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x28(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x38966
movq 0x138(%rsp), %rax
movslq 0xc4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x388fd
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x24(%rsp)
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x24(%rsp), %eax
movq (%rcx), %rcx
subl 0xc(%rcx), %eax
movl $0x28, %ecx
cltd
idivl %ecx
movl %eax, 0xc0(%rsp)
cmpl $0x64, 0xc0(%rsp)
jge 0x389cb
movl $0x64, 0xc0(%rsp)
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x20(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x38c7a
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x1c(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jg 0x38aa3
movq 0x138(%rsp), %rax
movslq 0xac(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x38a3a
movsd 0xb0(%rsp), %xmm1
divsd 0xc8(%rsp), %xmm1
movsd 0x1957b(%rip), %xmm0    # 0x52038
ucomisd %xmm1, %xmm0
jbe 0x38ac8
jmp 0x38c7a
movl 0xbc(%rsp), %eax
addl 0xc0(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq %rax, %rcx
movl 0x18(%rsp), %eax
movq (%rcx), %rcx
cmpl 0x20(%rcx), %eax
jge 0x38c5e
xorps %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0xa8(%rsp), %eax
jg 0x38b74
movq 0x138(%rsp), %rax
movslq 0x9c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x38b2a
movsd 0xa0(%rsp), %xmm0
divsd 0xc8(%rsp), %xmm0
movsd 0x194aa(%rip), %xmm1    # 0x52038
ucomisd %xmm1, %xmm0
jbe 0x38c42
movq 0x138(%rsp), %rdi
movq 0x128(%rsp), %rsi
movl 0xbc(%rsp), %edx
movl 0xa8(%rsp), %ecx
movq 0xf0(%rsp), %r8
cvtsi2sdl 0x120(%rsp), %xmm0
xorl %r9d, %r9d
movl $0x0, (%rsp)
callq 0x370e0
movsd %xmm0, 0x90(%rsp)
movq 0xe8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0xc850
movq 0xe0(%rsp), %rdi
cvtsi2sdl 0xbc(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
leaq 0x88(%rsp), %rsi
callq 0x3ae20
movq 0xd8(%rsp), %rdi
cvtsi2sdl 0xa8(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0x80(%rsp), %rsi
callq 0x3ae20
jmp 0x38c44
movl 0xc0(%rsp), %eax
addl 0xa8(%rsp), %eax
movl %eax, 0xa8(%rsp)
jmp 0x38add
jmp 0x38c60
movl 0xc0(%rsp), %eax
addl 0xbc(%rsp), %eax
movl %eax, 0xbc(%rsp)
jmp 0x389ed
movq 0xe8(%rsp), %rdi
callq 0x11c40
cmpq $0x0, %rax
jbe 0x38dab
movq 0xe8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xc930
movsd (%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
movl $0x0, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x11c40
movq %rax, %rcx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x38d39
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xe8(%rsp), %rdi
movslq 0x70(%rsp), %rsi
callq 0xc930
movsd 0x8(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x38d2a
movq 0xe8(%rsp), %rdi
movslq 0x70(%rsp), %rsi
callq 0xc930
movsd (%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x38d2c
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x38cbc
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x68(%rsp)
movq 0xe0(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc930
cvttsd2si (%rax), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0xd8(%rsp), %rdi
movslq 0x74(%rsp), %rsi
callq 0xc930
cvttsd2si (%rax), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x118(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xc8c0
jmp 0x38ddf
movq 0x140(%rsp), %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x60(%rsp)
movq 0x118(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xc8c0
movq 0xe8(%rsp), %rdi
callq 0x3ae00
movq 0xe0(%rsp), %rdi
callq 0x3ae00
movq 0xd8(%rsp), %rdi
callq 0x3ae00
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x3887f
movq 0x118(%rsp), %rax
addq $0x148, %rsp            # imm = 0x148
retq
nopl (%rax)