// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_signal_enrichment_utils.cpp
// Function: prune_region_ends_per_window_average(std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, double*, int, double*, int, double, int)
vector<t_annot_region*>* prune_region_ends_per_window_average(vector<t_annot_region*>* regions, double* signal_profile, int l_profile, double* control_profile, int l_control,
	double fdr, int l_win)
{
		// Go over all the windows.
		int cur_win_start = 1;
		int cur_l_win = l_win;

		// Sort the regions.
		sort(regions->begin(), regions->end(), sort_regions);

		// List of pruned regions.
		vector<t_annot_region*>* pruned_regions = new vector<t_annot_region*>();

		// The windowing schedule matches up with the ms_decomposition.
		while(cur_win_start < l_profile)
		{
			// Set the length of the window 
			if(cur_win_start+l_win > l_profile)
			{
				cur_l_win = l_profile - cur_win_start;
			}
			else
			{
				cur_l_win = l_win;
			}

			//fprintf(stderr, "Processing %d-%d\n", cur_win_start, cur_win_start+cur_l_win);

			// Get the minimum and maximum in the window.
			int max_thresh = 0;
			for(int i = cur_win_start; i < cur_win_start+cur_l_win && i < l_profile; i++)
			{
				if(max_thresh < signal_profile[i])
				{
					max_thresh = (int)(signal_profile[i]);
				}
			} // i loop.


//#define __POISSON_BCKGRND__
//#undef __GEOMETRIC_BCKGRND__
#ifdef __POISSON_BCKGRND__
			double cur_win_thresh = get_poisson_thresholds_per_win(signal_profile,
											l_profile,
											cur_l_win,
											cur_win_start,
											fdr,
											1,
											max_thresh);
#elif defined __GEOMETRIC_BCKGRND__
			double cur_win_thresh = get_geometric_thresholds_per_win(signal_profile,
											l_profile,
											cur_l_win,
											cur_win_start,
											fdr,
											1,
											max_thresh);
#else
			#error "Did not select a background model."
#endif // Bckgrnd selection

			//fprintf(stderr, "Window: %d-%d: Threshold: %lf\n", cur_win_start, cur_win_start + cur_l_win, cur_win_thresh);

			// Go over all the regions in the current window and prune the ends with respect to the threshold.
			for(int i_reg = 0; cur_win_thresh > 0 && i_reg < (int)regions->size(); i_reg++)
			{
				// Process the current region if its start is in the current window.
				if((regions->at(i_reg)->start >= cur_win_start &&
					regions->at(i_reg)->start < cur_win_start + cur_l_win))
				{
					// For the current region, update 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(i_pr < l_profile &&
							i_pr > 0 &&
							signal_profile[i_pr] > cur_win_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(i_pr < l_profile &&
							i_pr > 0 &&
							signal_profile[i_pr] > cur_win_thresh)
						{
							pruned_end = i_pr;
							break;
						}
					} // i_pr loop.

					// If this peak passed the threshold, add it to the list of all peaks.
					if(passed_threshold)
					{
						if(pruned_start <= pruned_end)
						{
							t_annot_region* pruned_peak = duplicate_region(regions->at(i_reg));
							pruned_peak->start = pruned_start;
							pruned_peak->end = pruned_end;

							pruned_regions->push_back(pruned_peak);
						}
						else
						{
							fprintf(stderr, "Pruning error: %d-%d\n", pruned_start, pruned_end);
							exit(0);
						}
					}
				} // Check if this is a region in the current window.
				else
				{
				}
			} // i_reg loop.

			// Update window.
			cur_win_start += l_win;
		} // window start check.

	//	// Dump the bed file.
	//	dump_BED(op_bed_fp, pruned_regions);
	//} // -prune_region_ends_per_window_average

	return(pruned_regions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %ebp
movsd %xmm0, 0x28(%rsp)
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq (%rdi), %rdi
movq 0x8(%r15), %rsi
leaq -0x7299(%rip), %rdx     # 0x13497
callq 0x17bcf
pushq $0x18
popq %rdi
callq 0x2170
movslq %ebp, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x10(%rsp)
andq $0x0, 0x10(%rax)
pushq $0x1
popq %rax
pushq $0x1
popq %rbp
movq %r12, 0x20(%rsp)
movl %ebx, %edx
subl %eax, %edx
jle 0x1a900
movl %eax, %r13d
leaq (%rax,%r12), %rcx
movq %rcx, 0x18(%rsp)
cmpl %ebx, %ecx
cmovlel %r12d, %edx
leal (%rdx,%r13), %esi
cmpl %ebx, %esi
movl %ebx, %ecx
movl %esi, 0xc(%rsp)
cmovll %esi, %ecx
movslq %ecx, %rcx
xorl %r9d, %r9d
cmpq %rcx, %rax
jge 0x1a7af
cvtsi2sd %r9d, %xmm0
movsd (%r14,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
cvttsd2si %xmm1, %esi
cmoval %esi, %r9d
incq %rax
jmp 0x1a78e
movq %r14, %rdi
movl %ebx, %esi
movl %r13d, %ecx
movsd 0x28(%rsp), %xmm0
movl %ebp, %r8d
callq 0x19847
ucomisd 0x98e3(%rip), %xmm0     # 0x240b0
movq 0x18(%rsp), %rax
jbe 0x1a75d
movq %r13, 0x38(%rsp)
xorl %r13d, %r13d
movsd %xmm0, 0x30(%rsp)
movq 0x8(%r15), %rax
subq (%r15), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jge 0x1a8ee
movq %r15, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x38(%rsp), %rcx
cmpl %ecx, 0xc(%rax)
jl 0x1a8e6
movq %r15, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rsp), %ecx
cmpl %ecx, 0xc(%rax)
jge 0x1a8e6
movq %r15, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r12d
movq %r15, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %r12d, %ecx
movsd 0x30(%rsp), %xmm1
cmpl %eax, %ecx
jg 0x1a884
cmpl %ebx, %ecx
setge %dl
testl %ecx, %ecx
setle %sil
orb %dl, %sil
jne 0x1a87d
movl %ecx, %edx
movsd (%r14,%rdx,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1a881
incl %ecx
jmp 0x1a85b
movl %ecx, %r12d
movl %eax, %ebp
cmpl %r12d, %ebp
jl 0x1a8ad
cmpl %ebx, %ebp
setge %dl
testl %ebp, %ebp
setle %sil
orb %dl, %sil
jne 0x1a8a9
movl %ebp, %edx
movsd (%r14,%rdx,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1a8af
decl %ebp
jmp 0x1a886
movl %eax, %ebp
cmpl %eax, %ecx
jg 0x1a8e6
cmpl %ebp, %r12d
jg 0x1a914
movq %r15, %rdi
movq %r13, %rsi
callq 0x7404
movq (%rax), %rdi
callq 0x134b2
movq %rax, 0x40(%rsp)
movl %r12d, 0xc(%rax)
movl %ebp, 0x20(%rax)
movq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x7448
incq %r13
jmp 0x1a7e2
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %r12
pushq $0x1
popq %rbp
jmp 0x1a75d
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1a6c5(%rip), %rax     # 0x34fe0
movq (%rax), %rdi
leaq 0xd8b7(%rip), %rsi      # 0x281dc
movl %r12d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x2240
xorl %edi, %edi
callq 0x21d0