// File: /workspace/github_cpp_makefile_star2_O1/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, 0x60(%rsp)
movl %r8d, %ebx
movq %rcx, 0x78(%rsp)
movl %edx, %ebp
movq %rsi, %r15
movsd %xmm0, 0x88(%rsp)
movq %rdi, %r14
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x50(%rsp)
movq $0x0, 0x10(%rax)
movq (%r14), %r8
movq %r14, 0x10(%rsp)
movq 0x8(%r14), %rsi
movq %rsi, %rax
subq %r8, %rax
sarq $0x3, %rax
testl %eax, %eax
movl %ebx, 0xc(%rsp)
jle 0x2151e
movslq %ebp, %rbp
movslq %ebx, %r14
movq %rax, 0x18(%rsp)
movl %eax, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
movq %r8, 0x30(%rsp)
cmpq %r8, %rsi
je 0x21a88
movq (%r8,%r13,8), %rcx
movslq 0xc(%rcx), %rax
movl 0x20(%rcx), %ecx
xorpd %xmm0, %xmm0
cmpl %ecx, %eax
jg 0x214f6
subl %eax, %ecx
incl %ecx
cmpq %rbp, %rax
jge 0x214e0
addsd (%r15,%rax,8), %xmm0
cmpq %r14, %rax
jge 0x214ef
movq 0x78(%rsp), %rdx
addsd (%rdx,%rax,8), %xmm0
incq %rax
decl %ecx
jne 0x214d5
ucomisd %xmm1, %xmm0
jbe 0x21514
movq %rsi, %rbx
callq 0x22c0
movq 0x30(%rsp), %r8
movq %rbx, %rsi
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
incq %r13
cmpq %r12, %r13
jne 0x214b5
jmp 0x21522
xorpd %xmm1, %xmm1
cmpb $0x0, 0x18ca7(%rip)     # 0x3a1d0
jne 0x21a24
addsd 0xc481(%rip), %xmm1     # 0x2d9b8
cvttsd2si %xmm1, %edi
callq 0x23bc5
movq %rax, 0x48(%rsp)
cmpb $0x1, 0x18c84(%rip)     # 0x3a1d0
movl 0xc(%rsp), %ebp
je 0x21a51
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
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x58(%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 0x10(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq 0x50(%rsp), %rbx
jle 0x21a00
xorps %xmm0, %xmm0
cvtsi2sdl 0x60(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
xorl %r12d, %r12d
cmpq %r12, %rdx
jbe 0x21a7c
movq (%rax,%r12,8), %rax
movl 0x20(%rax), %r14d
cmpl %ebp, %r14d
jg 0x219e0
movq %r12, 0x70(%rsp)
movslq 0xc(%rax), %rax
cmpl %r14d, %eax
jle 0x21613
xorpd %xmm1, %xmm1
movl $0xf4240, %ebx          # imm = 0xF4240
xorl %ebp, %ebp
jmp 0x2167a
leaq (%r15,%rax,8), %rcx
movq %rcx, 0x90(%rsp)
subl %eax, %r14d
incl %r14d
xorl %r13d, %r13d
movl $0xf4240, %r12d         # imm = 0xF4240
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
movsd %xmm1, 0x30(%rsp)
movq 0x90(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x60(%rsp)
callq 0x22c0
movsd 0x30(%rsp), %xmm1
cvttsd2si %xmm0, %ebx
cmpl %ebx, %ebp
cmovlel %ebx, %ebp
cmpl %ebx, %r12d
cmovll %r12d, %ebx
addsd 0x60(%rsp), %xmm1
incq %r13
movl %ebx, %r12d
cmpl %r13d, %r14d
jne 0x21634
movsd %xmm1, 0x30(%rsp)
movl $0x1f4, %eax            # imm = 0x1F4
cmpl %eax, %ebp
cmovgel %eax, %ebp
cmpl %ebp, %ebx
jg 0x2184a
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x70(%rsp), %rsi
cmpq %rsi, %rdx
jbe 0x21a6e
movq (%rax,%rsi,8), %rcx
movslq 0xc(%rcx), %rax
movl %eax, %r14d
movl 0x20(%rcx), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
cmpl %r12d, %eax
jg 0x216f7
leaq (%r15,%rax,8), %rcx
movl %r12d, %edx
notl %edx
addq %r14, %rdx
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x216f2
incq %rsi
movl %edx, %edi
addl %esi, %edi
jne 0x216dc
jmp 0x216f7
addl %esi, %eax
movl %eax, %r14d
movslq %r14d, %r13
cmpl %r14d, %r12d
jl 0x21724
movslq %r12d, %rax
incq %rax
movl %r12d, %ecx
movsd -0x8(%r15,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x21721
decl %ecx
decq %rax
cmpq %r13, %rax
jg 0x21708
jmp 0x21724
movl %ecx, %r12d
movq 0x48(%rsp), %r8
movl $0x0, (%rsp)
movq %r15, %rdi
movq 0x78(%rsp), %rsi
movsd 0x88(%rsp), %xmm0
movl %r14d, %edx
movl %r12d, %ecx
movsd 0x80(%rsp), %xmm1
xorl %r9d, %r9d
callq 0x1edaf
movsd %xmm0, 0x28(%rsp)
movq 0x20(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x2177b
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x21785
leaq 0x28(%rsp), %rdx
callq 0xb836
movsd 0x30(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x58(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
movq 0x18(%rsp), %rax
je 0x217bb
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x217d0
leaq 0x68(%rsp), %rdx
callq 0x21e80
movsd 0x30(%rsp), %xmm1
movq 0x18(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x8(%rax), %rsi
cmpq 0x10(%rax), %rsi
je 0x217f6
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rax)
jmp 0x21809
movq %rax, %rdi
leaq 0x68(%rsp), %rdx
callq 0x21e80
movsd 0x30(%rsp), %xmm1
xorpd %xmm0, %xmm0
cmpl %r12d, %r14d
jg 0x2182b
leaq (%r15,%r13,8), %rax
subl %r14d, %r12d
incl %r12d
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
incq %rcx
cmpl %ecx, %r12d
jne 0x2181e
divsd %xmm1, %xmm0
movsd 0x9821(%rip), %xmm1     # 0x2b058
ucomisd %xmm0, %xmm1
jae 0x2184a
leal 0x1(%rbx), %eax
cmpl %ebp, %ebx
movl %eax, %ebx
jl 0x21692
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq 0x70(%rsp), %r12
jle 0x2192b
cmpq %rax, %rcx
movl 0xc(%rsp), %ebp
je 0x21a99
movsd (%rax), %xmm0
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %ebx, %ebx
cmpq %rsi, %rdx
je 0x21a64
movsd (%rax,%rsi,8), %xmm1
ucomisd %xmm1, %xmm0
cmoval %esi, %ebx
minsd %xmm0, %xmm1
incq %rsi
movapd %xmm1, %xmm0
cmpq %rsi, %rcx
jne 0x2188a
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
jbe 0x21a7c
movq (%rax,%r12,8), %rdi
callq 0x172ac
movq %rax, 0x28(%rsp)
movslq %ebx, %rsi
movq 0x58(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x21a6e
cvttsd2si (%rcx,%rsi,8), %ecx
movl %ecx, 0xc(%rax)
movq 0x18(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
movq 0x50(%rsp), %rbx
jbe 0x21a6e
cvttsd2si (%rcx,%rsi,8), %ecx
movl %ecx, 0x20(%rax)
jmp 0x2195e
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
movq 0x50(%rsp), %rbx
movl 0xc(%rsp), %ebp
jbe 0x21a7c
movq (%rax,%r12,8), %rdi
callq 0x172ac
movq %rax, 0x28(%rsp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x21972
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x2197f
movq %rbx, %rdi
leaq 0x28(%rsp), %rdx
callq 0xb976
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x21991
movq %rax, 0x8(%rcx)
movq 0x58(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x219a3
movq %rax, 0x8(%rcx)
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x219b5
movq %rax, 0x8(%rcx)
incq %r12
movq 0x10(%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 0x215de
jmp 0x21a00
movq 0x185f9(%rip), %rcx     # 0x39fe0
movq (%rcx), %rdi
movl 0xc(%rax), %edx
leaq 0xd897(%rip), %rsi      # 0x2f28b
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2250
jmp 0x219b5
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x21a0f
callq 0x2220
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x185b5(%rip), %rax     # 0x39fe0
movq (%rax), %rdi
leaq 0xd7d9(%rip), %rsi      # 0x2f20e
movapd %xmm1, %xmm0
movb $0x1, %al
movapd %xmm1, 0x30(%rsp)
callq 0x2250
movapd 0x30(%rsp), %xmm1
jmp 0x2152f
movq 0x10(%rsp), %rsi
leaq 0x8(%rsi), %rdi
callq 0x52ef
jmp 0x21556
leal -0x1(%rdx), %esi
cmpq %rsi, %rdx
cmovbq %rdx, %rsi
leaq 0xbe61(%rip), %rdi      # 0x2d8d6
xorl %eax, %eax
callq 0x2270
leaq 0xbe53(%rip), %rdi      # 0x2d8d6
movq %r12, %rsi
jmp 0x21a75
leaq 0xbe47(%rip), %rdi      # 0x2d8d6
movq 0x18(%rsp), %rdx
movq %rdx, %rsi
jmp 0x21a75
leaq 0xbe36(%rip), %rdi      # 0x2d8d6
xorl %esi, %esi
jmp 0x21a75