// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: prune_region_ends_per_p_value_minimization(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(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));

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.
		int prev_pr_s = -1;
		int prev_pr_e = -1;
		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.

            if(prev_pr_s == pruned_start && prev_pr_e == pruned_end)
            {
                    continue;
            }

            prev_pr_s = pruned_start;
            prev_pr_e = pruned_end;

			// Get the binomial p-value for current region.
			double cur_pruned_pvalue = get_binomial_pvalue_per_region(signal_profile, control_profile, pruned_start, pruned_end, log_factorials, l_fragment, false, 0);

			// 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.

			// TODO::Following should actually be removed since it creates issues with overextended ER ends.
			// 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.

	delete [] log_factorials;

	// 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 $0x98, %rsp
movl %r9d, 0x28(%rsp)
movl %r8d, %r14d
movq %rcx, 0x58(%rsp)
movl %edx, %ebp
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
movl $0x18, %edi
callq 0x2180
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%rbx), %r12
movq %rbx, 0x38(%rsp)
movq 0x8(%rbx), %rax
movq %rax, %rbx
subq %r12, %rbx
sarq $0x3, %rbx
testl %ebx, %ebx
movl %r14d, 0x54(%rsp)
jle 0x20039
cmpq %r12, %rax
je 0x2061b
movslq %ebp, %r13
movslq %r14d, %rbp
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
movq 0x58(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq (%r12,%r14,8), %rcx
movslq 0xc(%rcx), %rax
movl 0x20(%rcx), %ecx
xorpd %xmm0, %xmm0
cmpl %ecx, %eax
jg 0x20012
subl %eax, %ecx
incl %ecx
cmpq %r13, %rax
jge 0x20001
addsd (%rsi,%rax,8), %xmm0
cmpq %rbp, %rax
jge 0x2000b
addsd (%rdx,%rax,8), %xmm0
incq %rax
decl %ecx
jne 0x1fff7
ucomisd %xmm1, %xmm0
jbe 0x2002f
callq 0x22c0
movq 0x20(%rsp), %rsi
movq 0x58(%rsp), %rdx
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
incq %r14
cmpq %rbx, %r14
jne 0x1ffe0
jmp 0x2003d
xorpd %xmm1, %xmm1
cmpb $0x0, 0x1918c(%rip)     # 0x391d0
jne 0x205ab
addsd 0xc966(%rip), %xmm1     # 0x2c9b8
cvttsd2si %xmm1, %edi
callq 0x236bc
movq %rax, 0x68(%rsp)
cmpb $0x1, 0x19169(%rip)     # 0x391d0
movl 0x54(%rsp), %r14d
je 0x205d8
movl $0x18, %edi
callq 0x2180
movq %rax, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebx, %ebx
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x40(%rsp)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x18(%rsp)
movq %rbx, 0x10(%rax)
movq 0x38(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x20587
xorps %xmm0, %xmm0
cvtsi2sdl 0x28(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
xorl %r11d, %r11d
movq %r15, 0x88(%rsp)
movq %r12, 0x30(%rsp)
cmpq %r11, %rdx
jbe 0x205fe
movq (%rax,%r11,8), %rax
movl 0x20(%rax), %ebx
cmpl %r14d, %ebx
movq 0x20(%rsp), %rcx
movq %r11, 0x10(%rsp)
jg 0x20563
movslq 0xc(%rax), %rax
cmpl %ebx, %eax
jg 0x203a0
leaq (%rcx,%rax,8), %r12
subl %eax, %ebx
incl %ebx
xorl %r14d, %r14d
movl $0xf4240, %ebp          # imm = 0xF4240
xorps %xmm2, %xmm2
xorl %r13d, %r13d
movsd %xmm2, 0x28(%rsp)
movl %ebp, %r15d
movsd (%r12,%r14,8), %xmm0
movsd %xmm0, 0x70(%rsp)
callq 0x22c0
movsd 0x28(%rsp), %xmm2
cvttsd2si %xmm0, %ebp
cmpl %ebp, %r13d
cmovlel %ebp, %r13d
cmpl %ebp, %r15d
cmovll %r15d, %ebp
addsd 0x70(%rsp), %xmm2
incq %r14
cmpl %r14d, %ebx
jne 0x20145
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %r13d
cmovgel %eax, %r13d
cmpl %eax, %ebp
movq 0x88(%rsp), %r15
movl 0x54(%rsp), %r14d
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r11
jg 0x203a0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x38(%rsp), %rdx
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
cmpq %r11, %rdx
jbe 0x205fe
movq (%rsi,%r11,8), %rsi
movslq 0xc(%rsi), %rdx
movl %edx, %ebx
movl 0x20(%rsi), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
cmpl %r12d, %edx
jg 0x2021e
leaq (%rdi,%rdx,8), %rsi
movl %r12d, %r10d
notl %r10d
addq %rbx, %r10
xorl %r8d, %r8d
movsd (%rsi,%r8,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x20219
incq %r8
movl %r10d, %r9d
addl %r8d, %r9d
jne 0x20200
jmp 0x2021e
addl %r8d, %edx
movl %edx, %ebx
cmpl %ebx, %r12d
jl 0x2024b
movslq %r12d, %rdx
movslq %ebx, %rsi
incq %rdx
movl %r12d, %r8d
movsd -0x8(%rdi,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x20248
decl %r8d
decq %rdx
cmpq %rsi, %rdx
jg 0x2022f
jmp 0x2024b
movl %r8d, %r12d
cmpl %ebx, %ecx
jne 0x20258
cmpl %r12d, %eax
je 0x20392
movsd %xmm2, 0x28(%rsp)
movl $0x0, (%rsp)
movq 0x58(%rsp), %rsi
movl %ebx, %edx
movl %r12d, %ecx
movq 0x68(%rsp), %r8
movsd 0x90(%rsp), %xmm0
xorl %r9d, %r9d
callq 0x1e6ff
movsd %xmm0, 0x48(%rsp)
movq 0x30(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x202a8
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x202b2
leaq 0x48(%rsp), %rdx
callq 0xb60c
movq 0x18(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %r11
movsd 0x28(%rsp), %xmm2
je 0x202f1
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rax)
jmp 0x20313
movq %rax, %rdi
leaq 0x60(%rsp), %rdx
callq 0x2192c
movsd 0x28(%rsp), %xmm2
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %rdi
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x8(%rcx), %rsi
cmpq 0x10(%rcx), %rsi
je 0x20339
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rcx)
jmp 0x20356
movq %rcx, %rdi
leaq 0x60(%rsp), %rdx
callq 0x2192c
movsd 0x28(%rsp), %xmm2
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %rdi
xorpd %xmm0, %xmm0
cmpl %r12d, %ebx
jg 0x2037b
movslq %ebx, %rax
leaq (%rdi,%rax,8), %rax
movl %r12d, %ecx
subl %ebx, %ecx
incl %ecx
xorl %edx, %edx
addsd (%rax,%rdx,8), %xmm0
incq %rdx
cmpl %edx, %ecx
jne 0x2036f
divsd %xmm2, %xmm0
movsd 0x9cd1(%rip), %xmm1     # 0x2a058
ucomisd %xmm0, %xmm1
movl %ebx, %ecx
movl %r12d, %eax
jae 0x203a0
leal 0x1(%rbp), %edx
cmpl %r13d, %ebp
movl %edx, %ebp
jne 0x201ba
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
je 0x203f3
sarq $0x3, %rdx
testl %edx, %edx
jle 0x20438
movsd (%rax), %xmm0
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0x205eb
movsd (%rax,%rsi,8), %xmm1
ucomisd %xmm1, %xmm0
cmoval %esi, %edi
minsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
cmpq %rsi, %rcx
jne 0x203c9
movslq %edi, %rbx
jmp 0x2043a
movq 0x38(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r11, %rdx
jbe 0x205fe
movq (%rax,%r11,8), %rdi
callq 0x16f1f
movq %rax, 0x48(%rsp)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x204ed
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x204fa
xorl %ebx, %ebx
movq 0x38(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r11, %rdx
jbe 0x205fe
movq (%rax,%r11,8), %rdi
callq 0x16f1f
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x2060f
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0xc(%rax)
movq 0x18(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
movq 0x30(%rsp), %r12
jbe 0x2060f
cvttsd2si (%rcx,%rbx,8), %ecx
movl %ecx, 0x20(%rax)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
movq 0x10(%rsp), %r11
je 0x204ca
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x204e1
movq %r15, %rdi
leaq 0x48(%rsp), %rdx
callq 0xb74c
movq 0x10(%rsp), %r11
movq 0x30(%rsp), %r12
movq 0x40(%rsp), %rcx
movq 0x18(%rsp), %rdx
jmp 0x2050e
movq %r15, %rdi
leaq 0x48(%rsp), %rdx
callq 0xb74c
movq 0x30(%rsp), %r12
movq 0x40(%rsp), %rcx
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r11
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x2051e
movq %rax, 0x8(%r12)
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x2052b
movq %rax, 0x8(%rcx)
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
je 0x20538
movq %rax, 0x8(%rdx)
incq %r11
movq 0x38(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r11
jl 0x20100
jmp 0x20587
movq 0x18a76(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movl 0xc(%rax), %edx
leaq 0xdd14(%rip), %rsi      # 0x2e28b
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2250
movq 0x10(%rsp), %r11
jmp 0x20538
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x20596
callq 0x2220
movq %r15, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18a2e(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0xdc52(%rip), %rsi      # 0x2e20e
movapd %xmm1, %xmm0
movb $0x1, %al
movapd %xmm1, 0x70(%rsp)
callq 0x2250
movapd 0x70(%rsp), %xmm1
jmp 0x2004a
movq 0x38(%rsp), %rsi
leaq 0x8(%rsi), %rdi
callq 0x5142
jmp 0x20072
leal -0x1(%rdx), %esi
cmpq %rsi, %rdx
cmovbq %rdx, %rsi
leaq 0xc2d4(%rip), %rdi      # 0x2c8d0
jmp 0x20608
leaq 0xc2cb(%rip), %rdi      # 0x2c8d0
movq %r11, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0xc2ba(%rip), %rdi      # 0x2c8d0
movq %rbx, %rsi
jmp 0x20608
leaq 0xc2ae(%rip), %rdi      # 0x2c8d0
movq %rbx, %rsi
movq %rbx, %rdx
jmp 0x20608