// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movl %r9d, %r12d
movl %r8d, 0x34(%rsp)
movq %rcx, 0x70(%rsp)
movl %edx, %ebp
movq %rsi, 0x40(%rsp)
movq %rdi, %rbx
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x48(%rsp)
movq $0x0, 0x10(%rax)
movq (%rbx), %r14
movq %rbx, 0x38(%rsp)
movq 0x8(%rbx), %rax
movq %rax, %rbx
subq %r14, %rbx
sarq $0x3, %rbx
testl %ebx, %ebx
jle 0x1f9c1
cmpq %r14, %rax
je 0x1ff47
movslq %ebp, %r15
movslq 0x34(%rsp), %r13
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
movq (%r14,%rbp,8), %rcx
movslq 0xc(%rcx), %rax
movl 0x20(%rcx), %ecx
xorpd %xmm0, %xmm0
cmpl %ecx, %eax
jg 0x1f9a4
subl %eax, %ecx
incl %ecx
cmpq %r15, %rax
jge 0x1f98e
movq 0x40(%rsp), %rdx
addsd (%rdx,%rax,8), %xmm0
cmpq %r13, %rax
jge 0x1f99d
movq 0x70(%rsp), %rdx
addsd (%rdx,%rax,8), %xmm0
incq %rax
decl %ecx
jne 0x1f97f
ucomisd %xmm1, %xmm0
jbe 0x1f9b7
callq 0x22c0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
incq %rbp
cmpq %rbx, %rbp
jne 0x1f968
jmp 0x1f9c5
xorpd %xmm1, %xmm1
cmpb $0x0, 0x19804(%rip)     # 0x391d0
jne 0x1fec8
addsd 0xcfde(%rip), %xmm1     # 0x2c9b8
cvttsd2si %xmm1, %edi
callq 0x236bc
movq %rax, 0x98(%rsp)
cmpb $0x1, 0x197de(%rip)     # 0x391d0
movq 0x40(%rsp), %r14
je 0x1fef5
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebx, %ebx
movq %rax, 0x18(%rsp)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x10(%rsp)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x20(%rsp)
movq %rbx, 0x10(%rax)
movq 0x38(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rdx
movq %rdx, %rax
subq %rcx, %rax
sarq $0x3, %rax
testl %eax, %eax
jle 0x1feb1
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x90(%rsp)
xorl %r13d, %r13d
cmpq %r13, %rax
jbe 0x1ff08
movq (%rcx,%r13,8), %rsi
movl 0x20(%rsi), %eax
cmpl 0x34(%rsp), %eax
jg 0x1fe85
movslq 0xc(%rsi), %r12
xorpd %xmm2, %xmm2
cmpl %r12d, %eax
jl 0x1fac5
leaq (%r14,%r12,8), %rsi
movl %eax, %edi
subl %r12d, %edi
incl %edi
xorl %r8d, %r8d
addsd (%rsi,%r8,8), %xmm2
incq %r8
cmpl %r8d, %edi
jne 0x1fab7
subl %r12d, %eax
cltq
imulq $0x66666667, %rax, %rsi # imm = 0x66666667
movq %rsi, %rax
shrq $0x3f, %rax
sarq $0x24, %rsi
addl %eax, %esi
cmpl $0x65, %esi
movl $0x64, %eax
cmovll %eax, %esi
movq %rsi, 0x60(%rsp)
movq %rdx, %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x1ff08
movl 0x60(%rsp), %ebp
leaq -0x1(%r12), %rdi
leaq (%r14,%r12,8), %r15
leaq (,%rbp,8), %rax
movq %rax, 0x80(%rsp)
leaq 0x1(%rbp), %rax
movq %rax, 0x78(%rsp)
movsd %xmm2, 0x50(%rsp)
movq (%rcx,%r13,8), %rax
movslq 0x20(%rax), %rax
cmpl %r12d, %eax
jl 0x1fd22
xorpd %xmm0, %xmm0
movq %rdi, %rsi
addsd 0x8(%r14,%rsi,8), %xmm0
incq %rsi
cmpq %rax, %rsi
jl 0x1fb45
divsd %xmm2, %xmm0
movsd 0xa4f8(%rip), %xmm1     # 0x2a058
ucomisd %xmm0, %xmm1
ja 0x1fd22
movq %rdi, 0x88(%rsp)
movq %rdx, %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x1ff08
movq 0x60(%rsp), %rax
leal (%rax,%r12), %ebx
movq (%rcx,%r13,8), %rax
cmpl 0x20(%rax), %ebx
jge 0x1fcef
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
movq 0x78(%rsp), %r14
movsd %xmm1, 0xa0(%rsp)
xorpd %xmm0, %xmm0
cmpl %r12d, %ebx
jl 0x1fbca
xorl %eax, %eax
addsd (%r15,%rax,8), %xmm0
incq %rax
cmpl %eax, %r14d
jne 0x1fbbc
divsd %xmm2, %xmm0
ucomisd 0xa482(%rip), %xmm0     # 0x2a058
jbe 0x1fcc8
movl $0x0, (%rsp)
movq 0x40(%rsp), %rdi
movq 0x70(%rsp), %rsi
movl %r12d, %edx
movl %ebx, %ecx
movq 0x98(%rsp), %r8
movsd 0x90(%rsp), %xmm0
xorl %r9d, %r9d
callq 0x1e6ff
movsd %xmm0, 0x28(%rsp)
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1fc2e
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x1fc38
leaq 0x28(%rsp), %rdx
callq 0xb60c
movsd 0xa0(%rsp), %xmm1
movsd %xmm1, 0x58(%rsp)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
movq 0x20(%rsp), %rax
movsd 0x50(%rsp), %xmm2
je 0x1fc6f
movsd %xmm1, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x1fc84
leaq 0x58(%rsp), %rdx
callq 0x2192c
movsd 0x50(%rsp), %xmm2
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x1fca9
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rax)
jmp 0x1fcbc
movq %rax, %rdi
leaq 0x58(%rsp), %rdx
callq 0x2192c
movsd 0x50(%rsp), %xmm2
movq 0x38(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rdx
movq %rdx, %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x1ff08
addl 0x60(%rsp), %ebx
movq (%rcx,%r13,8), %rax
addq %rbp, %r14
cmpl 0x20(%rax), %ebx
jl 0x1fbb1
addq %rbp, %r12
movq %rdx, %rax
subq %rcx, %rax
sarq $0x3, %rax
movq 0x88(%rsp), %rdi
addq %rbp, %rdi
addq 0x80(%rsp), %r15
cmpq %r13, %rax
movq 0x40(%rsp), %r14
ja 0x1fb2d
jmp 0x1ff08
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rax
subq %rsi, %rax
je 0x1fd7e
sarq $0x3, %rax
testl %eax, %eax
movq 0x48(%rsp), %r15
jle 0x1fdbc
movsd (%rsi), %xmm0
movl %eax, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r8, %rax
je 0x1ff1c
movsd (%rsi,%r8,8), %xmm1
ucomisd %xmm1, %xmm0
cmoval %r8d, %r9d
minsd %xmm0, %xmm1
incq %r8
movapd %xmm1, %xmm0
cmpq %r8, %rdi
jne 0x1fd52
movslq %r9d, %rbx
jmp 0x1fdbe
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
movq 0x48(%rsp), %rbx
jbe 0x1ff3b
movq (%rcx,%r13,8), %rdi
callq 0x16f1f
movq %rax, 0x28(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x1fe3d
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x1fe4a
xorl %ebx, %ebx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0x1ff3b
movq (%rcx,%r13,8), %rdi
callq 0x16f1f
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x1ff2f
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0xc(%rax)
movq 0x20(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x1ff2f
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0x20(%rax)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x1fe38
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x1fe4a
movq %r15, %rdi
jmp 0x1fe40
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
callq 0xb74c
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x1fe61
movq %rax, 0x8(%rcx)
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
je 0x1fe6e
movq %rax, 0x8(%rdx)
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x1fe85
movq 0x20(%rsp), %rcx
movq %rax, 0x8(%rcx)
incq %r13
movq 0x38(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
movq %rsi, %rax
sarq $0x3, %rax
shrq $0x3, %rsi
movslq %esi, %rsi
cmpq %rsi, %r13
jl 0x1fa82
movq 0x48(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x19111(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0xe335(%rip), %rsi      # 0x2e20e
movapd %xmm1, %xmm0
movb $0x1, %al
movapd %xmm1, 0x60(%rsp)
callq 0x2250
movapd 0x60(%rsp), %xmm1
jmp 0x1f9d2
movq 0x38(%rsp), %rsi
leaq 0x8(%rsi), %rdi
callq 0x511d
jmp 0x1f9fd
leaq 0xc9c1(%rip), %rdi      # 0x2c8d0
movq %r13, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2270
leal -0x1(%rax), %esi
cmpq %rsi, %rax
cmovbq %rax, %rsi
leaq 0xc9a3(%rip), %rdi      # 0x2c8d0
jmp 0x1ff12
leaq 0xc99a(%rip), %rdi      # 0x2c8d0
movq %rbx, %rsi
jmp 0x1ff15
leaq 0xc98e(%rip), %rdi      # 0x2c8d0
movq %r13, %rsi
jmp 0x1ff15
leaq 0xc982(%rip), %rdi      # 0x2c8d0
movq %rbx, %rsi
movq %rbx, %rdx
jmp 0x1ff15