// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl %r9d, 0x40(%rsp)
movl %r8d, %ebx
movq %rcx, 0x80(%rsp)
movl %edx, %ebp
movq %rsi, 0x28(%rsp)
movq %rdi, %r15
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x68(%rsp)
movq $0x0, 0x10(%rax)
movq (%r15), %r14
movq %r15, 0x10(%rsp)
movq 0x8(%r15), %r12
movq %r12, %rax
subq %r14, %rax
sarq $0x3, %rax
testl %eax, %eax
movl %ebx, 0x1c(%rsp)
jle 0x1fe21
movslq %ebp, %r13
movslq %ebx, %rbp
movq %rax, 0x50(%rsp)
movl %eax, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorpd %xmm1, %xmm1
xorl %r15d, %r15d
movq 0x28(%rsp), %rsi
cmpq %r14, %r12
je 0x20411
movq (%r14,%r15,8), %rcx
movslq 0xc(%rcx), %rax
movl 0x20(%rcx), %ecx
xorpd %xmm0, %xmm0
cmpl %ecx, %eax
jg 0x1fdff
subl %eax, %ecx
incl %ecx
cmpq %r13, %rax
jge 0x1fde6
addsd (%rsi,%rax,8), %xmm0
cmpq %rbp, %rax
jge 0x1fdf8
movq 0x80(%rsp), %rdx
addsd (%rdx,%rax,8), %xmm0
incq %rax
decl %ecx
jne 0x1fddc
ucomisd %xmm1, %xmm0
jbe 0x1fe17
callq 0x22c0
movq 0x28(%rsp), %rsi
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
incq %r15
cmpq %rbx, %r15
jne 0x1fdbc
jmp 0x1fe25
xorpd %xmm1, %xmm1
cmpb $0x0, 0x1a3a4(%rip)     # 0x3a1d0
jne 0x203ad
addsd 0xdb7e(%rip), %xmm1     # 0x2d9b8
cvttsd2si %xmm1, %edi
callq 0x23bc5
movq %rax, 0xa8(%rsp)
cmpb $0x1, 0x1a37e(%rip)     # 0x3a1d0
movl 0x1c(%rsp), %ebp
movq 0x68(%rsp), %r14
je 0x203da
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebx, %ebx
movq %rax, 0x20(%rsp)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
movq %rax, %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rbx, 0x10(%rax)
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x20398
xorps %xmm0, %xmm0
cvtsi2sdl 0x40(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
xorl %r8d, %r8d
movq %r15, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
cmpq %r8, %rdx
jbe 0x203ed
movq (%rax,%r8,8), %rcx
movl 0x20(%rcx), %eax
cmpl %ebp, %eax
jg 0x20374
movslq 0xc(%rcx), %r15
xorpd %xmm1, %xmm1
cmpl %r15d, %eax
jl 0x1ff2d
movq 0x28(%rsp), %rcx
leaq (%rcx,%r15,8), %rcx
movl %eax, %edx
subl %r15d, %edx
incl %edx
xorl %esi, %esi
addsd (%rcx,%rsi,8), %xmm1
incq %rsi
cmpl %esi, %edx
jne 0x1ff21
subl %r15d, %eax
cltq
imulq $0x66666667, %rax, %r14 # imm = 0x66666667
movq %r14, %rax
shrq $0x3f, %rax
sarq $0x24, %r14
addl %eax, %r14d
cmpl $0x65, %r14d
movl $0x64, %eax
cmovll %eax, %r14d
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
movq 0x28(%rsp), %rdi
jbe 0x203ed
movl %r14d, %ebp
leaq -0x1(%r15), %r9
leaq (%rdi,%r15,8), %r12
leaq (,%rbp,8), %rcx
movq %rcx, 0x88(%rsp)
leaq 0x1(%rbp), %rcx
movq %rcx, 0x90(%rsp)
movq %r8, 0x50(%rsp)
movsd %xmm1, 0x70(%rsp)
movq (%rax,%r8,8), %rax
movslq 0x20(%rax), %rax
cmpl %r15d, %eax
jl 0x201c9
xorpd %xmm2, %xmm2
movq %r9, %rcx
addsd 0x8(%rdi,%rcx,8), %xmm2
incq %rcx
cmpq %rax, %rcx
jl 0x1ffbf
divsd %xmm1, %xmm2
movsd 0xb07f(%rip), %xmm0     # 0x2b058
ucomisd %xmm2, %xmm0
ja 0x201c9
movq %r9, 0x98(%rsp)
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
jbe 0x203ed
xorps %xmm3, %xmm3
cvtsi2sd %r15d, %xmm3
movq 0x90(%rsp), %r13
movl %r15d, %ebx
movsd %xmm2, 0x40(%rsp)
movsd %xmm3, 0xb0(%rsp)
addl %r14d, %ebx
movq (%rax,%r8,8), %rax
cmpl 0x20(%rax), %ebx
jge 0x20189
xorpd %xmm0, %xmm0
cmpl %r15d, %ebx
jl 0x2004d
xorl %eax, %eax
addsd (%r12,%rax,8), %xmm0
incq %rax
cmpl %eax, %r13d
jne 0x2003f
divsd %xmm1, %xmm0
ucomisd 0xafff(%rip), %xmm0     # 0x2b058
jbe 0x2016a
movl $0x0, (%rsp)
movq 0x80(%rsp), %rsi
movl %r15d, %edx
movl %ebx, %ecx
movq 0xa8(%rsp), %r8
movsd 0xa0(%rsp), %xmm0
xorl %r9d, %r9d
callq 0x1eb72
movsd %xmm0, 0x48(%rsp)
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x200af
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x200b9
leaq 0x48(%rsp), %rdx
callq 0xb836
movsd 0xb0(%rsp), %xmm3
movsd %xmm3, 0x78(%rsp)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
movq 0x50(%rsp), %r8
movq 0x30(%rsp), %rdi
movsd 0x70(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
je 0x200fb
movsd %xmm3, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rax)
jmp 0x20120
movq 0x38(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x21e80
movsd 0x40(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
movq 0x30(%rsp), %rdi
movq 0x50(%rsp), %r8
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x20145
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x20160
leaq 0x78(%rsp), %rdx
callq 0x21e80
movsd 0x40(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
movq 0x50(%rsp), %r8
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
addq %rbp, %r13
cmpq %r8, %rdx
ja 0x20024
jmp 0x203ed
movsd 0xaec7(%rip), %xmm0     # 0x2b058
ucomisd %xmm2, %xmm0
movq 0x98(%rsp), %r9
ja 0x201c9
addq %rbp, %r15
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
addq %rbp, %r9
addq 0x88(%rsp), %r12
cmpq %r8, %rdx
ja 0x1ffa7
jmp 0x203ed
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
je 0x2022e
sarq $0x3, %rdx
testl %edx, %edx
movl 0x1c(%rsp), %ebp
movq 0x68(%rsp), %r14
jle 0x20277
movsd (%rax), %xmm0
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0x203fe
movsd (%rax,%rsi,8), %xmm1
ucomisd %xmm1, %xmm0
cmoval %esi, %edi
minsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
cmpq %rsi, %rcx
jne 0x201ff
movslq %edi, %rbx
movq 0x10(%rsp), %rsi
jmp 0x20279
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
movl 0x1c(%rsp), %ebp
movq 0x68(%rsp), %r14
jbe 0x203ed
movq (%rax,%r8,8), %rdi
callq 0x172ac
movq %rax, 0x48(%rsp)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x20327
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x20334
xorl %ebx, %ebx
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r8, %rdx
jbe 0x203ed
movq (%rax,%r8,8), %rdi
callq 0x172ac
movq %rax, 0x48(%rsp)
movq 0x38(%rsp), %r15
movq (%r15), %rcx
movq 0x8(%r15), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x20422
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0xc(%rax)
movq 0x30(%rsp), %rdi
movq (%rdi), %rcx
movq 0x8(%rdi), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x20422
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0x20(%rax)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
movq 0x50(%rsp), %r8
je 0x202ff
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x2031b
movq %r14, %rdi
leaq 0x48(%rsp), %rdx
callq 0xb976
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %r15
movq 0x50(%rsp), %r8
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rcx
jmp 0x2034d
movq %r14, %rdi
leaq 0x48(%rsp), %rdx
callq 0xb976
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0x38(%rsp), %r15
movq 0x30(%rsp), %rdi
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x2035a
movq %rax, 0x8(%rcx)
movq (%r15), %rax
cmpq %rax, 0x8(%r15)
je 0x20367
movq %rax, 0x8(%r15)
movq (%rdi), %rax
cmpq %rax, 0x8(%rdi)
je 0x20374
movq %rax, 0x8(%rdi)
incq %r8
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r8
jl 0x1feea
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x19c2c(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0xee50(%rip), %rsi      # 0x2f20e
movapd %xmm1, %xmm0
movb $0x1, %al
movapd %xmm1, 0x50(%rsp)
callq 0x2250
movapd 0x50(%rsp), %xmm1
jmp 0x1fe32
movq 0x10(%rsp), %rsi
leaq 0x8(%rsi), %rdi
callq 0x5280
jmp 0x1fe61
leaq 0xd4e2(%rip), %rdi      # 0x2d8d6
movq %r8, %rsi
xorl %eax, %eax
callq 0x2270
leal -0x1(%rdx), %esi
cmpq %rsi, %rdx
cmovbq %rdx, %rsi
leaq 0xd4c7(%rip), %rdi      # 0x2d8d6
jmp 0x203f7
leaq 0xd4be(%rip), %rdi      # 0x2d8d6
movq 0x50(%rsp), %rdx
movq %rdx, %rsi
jmp 0x203f7
leaq 0xd4ad(%rip), %rdi      # 0x2d8d6
movq %rbx, %rsi
jmp 0x203f7