// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: prune_region_ends_per_modified_binomial_p_value_minimization(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, double, double*, int, double*, int, int)
vector<t_annot_region*>* prune_region_ends_per_modified_binomial_p_value_minimization(vector<t_annot_region*>* regions, 
	double flip_prob,
	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 400.
		max_sig = (max_sig > 500)?(500):(max_sig);

		// Start thresholding over all possible thresholds.			
		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.

			// 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);
			double cur_pruned_pvalue = get_modified_binomial_pvalue_per_region(signal_profile, control_profile, flip_prob, 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.

			// 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((int)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, %r14d
movl %r8d, 0x44(%rsp)
movq %rcx, 0x68(%rsp)
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
movsd %xmm0, 0x90(%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, 0x20(%rsp)
movq 0x8(%rbx), %rax
movq %rax, %rbx
subq %r12, %rbx
sarq $0x3, %rbx
testl %ebx, %ebx
movl %r14d, 0x18(%rsp)
jle 0x20fc4
cmpq %r12, %rax
je 0x21564
movslq %ebp, %r13
movslq 0x44(%rsp), %rbp
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
movq 0x10(%rsp), %rsi
movq (%r12,%r14,8), %rcx
movslq 0xc(%rcx), %rax
movl 0x20(%rcx), %ecx
xorpd %xmm0, %xmm0
cmpl %ecx, %eax
jg 0x20fa2
subl %eax, %ecx
incl %ecx
cmpq %r13, %rax
jge 0x20f8c
addsd (%rsi,%rax,8), %xmm0
cmpq %rbp, %rax
jge 0x20f9b
movq 0x68(%rsp), %rdx
addsd (%rdx,%rax,8), %xmm0
incq %rax
decl %ecx
jne 0x20f82
ucomisd %xmm1, %xmm0
jbe 0x20fba
callq 0x22c0
movq 0x10(%rsp), %rsi
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
incq %r14
cmpq %rbx, %r14
jne 0x20f6b
jmp 0x20fc8
xorpd %xmm1, %xmm1
cmpb $0x0, 0x18201(%rip)     # 0x391d0
jne 0x214f5
addsd 0xb9db(%rip), %xmm1     # 0x2c9b8
cvttsd2si %xmm1, %edi
callq 0x236bc
movq %rax, 0x48(%rsp)
cmpb $0x1, 0x181de(%rip)     # 0x391d0
je 0x21522
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %ebx, %ebx
movq %rax, 0x28(%rsp)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rbx, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x50(%rsp)
movq %rbx, 0x10(%rax)
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq 0x10(%rsp), %rsi
jle 0x214d1
xorps %xmm0, %xmm0
cvtsi2sdl 0x18(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
xorl %r12d, %r12d
movq %r15, 0x80(%rsp)
movq %r14, 0x30(%rsp)
cmpq %r12, %rdx
jbe 0x21548
movq (%rax,%r12,8), %rax
movl 0x20(%rax), %r14d
cmpl 0x44(%rsp), %r14d
jg 0x214ac
movslq 0xc(%rax), %rax
cmpl %r14d, %eax
jg 0x212f3
movq %r12, 0x60(%rsp)
leaq (%rsi,%rax,8), %r12
subl %eax, %r14d
incl %r14d
xorl %r13d, %r13d
movl $0xf4240, %ebp          # imm = 0xF4240
xorps %xmm2, %xmm2
xorl %ebx, %ebx
movsd %xmm2, 0x18(%rsp)
movl %ebp, %r15d
movsd (%r12,%r13,8), %xmm0
movsd %xmm0, 0x70(%rsp)
callq 0x22c0
movsd 0x18(%rsp), %xmm2
cvttsd2si %xmm0, %ebp
cmpl %ebp, %ebx
cmovlel %ebp, %ebx
cmpl %ebp, %r15d
cmovll %r15d, %ebp
addsd 0x70(%rsp), %xmm2
incq %r13
cmpl %r13d, %r14d
jne 0x210d0
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %ebx
cmovgel %eax, %ebx
cmpl %eax, %ebp
movq 0x80(%rsp), %r15
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %r12
jg 0x212f3
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x21548
movq (%rax,%r12,8), %rcx
movslq 0xc(%rcx), %rax
movl %eax, %r14d
movl 0x20(%rcx), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
cmpl %r12d, %eax
jg 0x21197
leaq (%rdi,%rax,8), %rcx
movl %r12d, %edx
notl %edx
addq %r14, %rdx
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x2118d
incq %rsi
movl %edx, %edi
addl %esi, %edi
jne 0x21177
jmp 0x21192
addl %esi, %eax
movl %eax, %r14d
movq 0x10(%rsp), %rdi
movslq %r14d, %r13
cmpl %r14d, %r12d
movsd %xmm2, 0x18(%rsp)
jl 0x211c9
movslq %r12d, %rax
incq %rax
movl %r12d, %ecx
movsd -0x8(%rdi,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x211c6
decl %ecx
decq %rax
cmpq %r13, %rax
jg 0x211ae
jmp 0x211c9
movl %ecx, %r12d
movq 0x48(%rsp), %r8
movl $0x0, (%rsp)
movq 0x68(%rsp), %rsi
movsd 0x90(%rsp), %xmm0
movl %r14d, %edx
movl %r12d, %ecx
movsd 0x88(%rsp), %xmm1
xorl %r9d, %r9d
callq 0x1e93c
movsd %xmm0, 0x38(%rsp)
movq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2121d
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x21227
leaq 0x38(%rsp), %rdx
callq 0xb60c
movsd 0x18(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x21258
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rax)
jmp 0x2126d
movq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x2192c
movsd 0x18(%rsp), %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x50(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x21298
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x212a8
leaq 0x58(%rsp), %rdx
callq 0x2192c
movsd 0x18(%rsp), %xmm2
xorpd %xmm0, %xmm0
cmpl %r12d, %r14d
movq 0x10(%rsp), %rdi
jg 0x212cf
leaq (%rdi,%r13,8), %rax
subl %r14d, %r12d
incl %r12d
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
incq %rcx
cmpl %ecx, %r12d
jne 0x212c2
divsd %xmm2, %xmm0
movsd 0x8d7d(%rip), %xmm1     # 0x2a058
ucomisd %xmm0, %xmm1
movq 0x60(%rsp), %r12
jae 0x212f3
leal 0x1(%rbp), %eax
cmpl %ebx, %ebp
movl %eax, %ebp
jl 0x21132
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x213d8
cmpq %rax, %rcx
je 0x21559
movsd (%rax), %xmm0
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %ebx, %ebx
cmpq %rsi, %rdx
je 0x21535
movsd (%rax,%rsi,8), %xmm1
ucomisd %xmm1, %xmm0
cmoval %esi, %ebx
minsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
cmpq %rsi, %rcx
jne 0x2132a
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x21548
movq (%rax,%r12,8), %rdi
callq 0x16f1f
movq %rax, 0x38(%rsp)
movslq %ebx, %rsi
movq 0x30(%rsp), %r14
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x2153f
cvttsd2si (%rcx,%rsi,8), %ecx
movl %ecx, 0xc(%rax)
movq 0x50(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x2153f
cvttsd2si (%rcx,%rsi,8), %ecx
movl %ecx, 0x20(%rax)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x21416
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x21428
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x21548
movq (%rax,%r12,8), %rdi
callq 0x16f1f
movq %rax, 0x38(%rsp)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x21434
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x21441
movq %r15, %rdi
leaq 0x38(%rsp), %rdx
callq 0xb74c
movq 0x30(%rsp), %r14
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rcx
jmp 0x21450
movq %r15, %rdi
leaq 0x38(%rsp), %rdx
callq 0xb74c
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r14
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x2145d
movq %rax, 0x8(%rcx)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x2146a
movq %rax, 0x8(%r14)
movq 0x50(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x21481
movq 0x50(%rsp), %rcx
movq %rax, 0x8(%rcx)
incq %r12
movq 0x20(%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, %r12
jl 0x2108b
jmp 0x214d1
movq 0x17b2d(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movl 0xc(%rax), %edx
leaq 0xcdcb(%rip), %rsi      # 0x2e28b
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2250
movq 0x10(%rsp), %rsi
jmp 0x21481
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x214e0
callq 0x2220
movq %r15, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17ae4(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0xcd08(%rip), %rsi      # 0x2e20e
movapd %xmm1, %xmm0
movb $0x1, %al
movapd %xmm1, 0x70(%rsp)
callq 0x2250
movapd 0x70(%rsp), %xmm1
jmp 0x20fd5
movq 0x20(%rsp), %rsi
leaq 0x8(%rsi), %rdi
callq 0x518c
jmp 0x20ff8
leal -0x1(%rdx), %esi
cmpq %rsi, %rdx
cmovbq %rdx, %rsi
leaq 0xb38a(%rip), %rdi      # 0x2c8d0
jmp 0x21552
leaq 0xb381(%rip), %rdi      # 0x2c8d0
movq %r12, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0xb370(%rip), %rdi      # 0x2c8d0
xorl %esi, %esi
jmp 0x21552
leaq 0xb365(%rip), %rdi      # 0x2c8d0
movq %rbx, %rsi
movq %rbx, %rdx
jmp 0x21552