// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r9d, 0x50(%rsp)
movl %r8d, %ebx
movq %rcx, 0x68(%rsp)
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r12
pushq $0x18
popq %rdi
callq 0x2170
movl %ebx, 0x5c(%rsp)
movslq %ebx, %r13
movslq %ebp, %rbp
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movq %rax, 0x40(%rsp)
movupd %xmm0, (%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %r14d, %r14d
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jge 0x1aca3
movq %r12, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %rbx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbx
jg 0x1ac7a
cmpq %rbp, %rbx
movsd 0x10(%rsp), %xmm0
jge 0x1ac60
addsd (%r15,%rbx,8), %xmm0
cmpq %r13, %rbx
jge 0x1ac6f
movq 0x68(%rsp), %rax
addsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
incq %rbx
jmp 0x1ac38
movsd 0x10(%rsp), %xmm0
ucomisd 0x20(%rsp), %xmm0
jbe 0x1ac9b
callq 0x22b0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
incq %r14
jmp 0x1ac04
cmpb $0x0, 0x1a51e(%rip)     # 0x351c8
jne 0x1b0b7
movapd 0x20(%rsp), %xmm0
addsd 0xbd0a(%rip), %xmm0     # 0x269c8
cvttsd2si %xmm0, %edi
callq 0x1e201
movq %rax, 0x80(%rsp)
cmpb $0x1, 0x1a4f2(%rip)     # 0x351c8
je 0x1b0da
pushq $0x18
popq %r14
movq %r14, %rdi
callq 0x2170
movq %rax, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x2170
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r14, %rdi
movq %rax, %r14
callq 0x2170
cvtsi2sdl 0x50(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
andq $0x0, 0x10(%rax)
movq %rax, 0x38(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebp, %ebp
movq %r13, 0x60(%rsp)
movq %r14, 0x48(%rsp)
movq 0x8(%r12), %rax
subq (%r12), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jge 0x1b0a0
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x5c(%rsp), %ecx
cmpl %ecx, 0x20(%rax)
jg 0x1b098
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %rbx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbx
jg 0x1adbf
movsd 0x20(%rsp), %xmm0
addsd (%r15,%rbx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
incq %rbx
jmp 0x1ad91
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %ebx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
subl 0xc(%rax), %ebx
movl %ebx, %eax
cltd
pushq $0x28
popq %rcx
idivl %ecx
movl %eax, %r14d
cmpl $0x65, %eax
pushq $0x64
popq %rax
cmovll %eax, %r14d
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0xc(%rax), %r13
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpl %r13d, 0x20(%rax)
jl 0x1af66
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r13, %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbx
jg 0x1ae5b
movsd 0x10(%rsp), %xmm0
addsd (%r15,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
incq %rbx
jmp 0x1ae2d
movsd 0x10(%rsp), %xmm1
divsd 0x20(%rsp), %xmm1
movsd 0x91e9(%rip), %xmm0     # 0x24058
ucomisd %xmm1, %xmm0
ja 0x1af66
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r13, %rbx
movq %r13, 0x50(%rsp)
addq %r14, %rbx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movslq 0x20(%rax), %rax
cmpq %rax, %rbx
jge 0x1af5e
xorpd %xmm0, %xmm0
movq %r13, %rax
cmpq %rbx, %rax
jg 0x1aec1
addsd (%r15,%rax,8), %xmm0
incq %rax
jmp 0x1aeb1
divsd 0x20(%rsp), %xmm0
ucomisd 0x9189(%rip), %xmm0     # 0x24058
jbe 0x1ae8c
andl $0x0, (%rsp)
movq %r15, %rdi
movq 0x68(%rsp), %rsi
movl %r13d, %edx
movl %ebx, %ecx
movq 0x80(%rsp), %r8
movsd 0x78(%rsp), %xmm0
xorl %r9d, %r9d
callq 0x19fac
movsd %xmm0, 0x18(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7426
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x48(%rsp), %rdi
movq %r14, %r13
movq %rbp, %r14
movq %r12, %rbp
leaq 0x70(%rsp), %r12
movq %r12, %rsi
callq 0x1c87e
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %r12
movq %r14, %rbp
movq %r13, %r14
movq 0x50(%rsp), %r13
callq 0x1c87e
jmp 0x1ae8c
addq %r14, %r13
jmp 0x1ae08
movq 0x60(%rsp), %r13
movq 0x8(%r13), %rax
cmpq (%r13), %rax
je 0x1b03a
xorl %ebx, %ebx
movq %r13, %rdi
xorl %esi, %esi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jge 0x1afd9
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7466
movsd 0x10(%rsp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x1afd4
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7466
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movl %ebx, %r14d
incq %rbx
jmp 0x1af92
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x18(%rsp)
movslq %r14d, %rbx
movq 0x48(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x18(%rsp), %rcx
movl %eax, 0xc(%rcx)
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x7466
cvttsd2si (%rax), %eax
movq 0x18(%rsp), %rcx
movl %eax, 0x20(%rcx)
movq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7448
jmp 0x1b066
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7448
movq 0x48(%rsp), %r14
movq (%r13), %rax
cmpq %rax, 0x8(%r13)
je 0x1b074
movq %rax, 0x8(%r13)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x1b081
movq %rax, 0x8(%r14)
movq 0x38(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x1b098
movq 0x38(%rsp), %rcx
movq %rax, 0x8(%rcx)
incq %rbp
jmp 0x1ad42
movq 0x40(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x19f22(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xd146(%rip), %rsi      # 0x2820e
movapd 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x2240
jmp 0x1acb0
movq 0x19eff(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
movq 0x8(%r12), %rdx
subq (%r12), %rdx
shrq $0x3, %rdx
leaq 0xd155(%rip), %rsi      # 0x2824d
xorl %eax, %eax
callq 0x2240
jmp 0x1acdc