// File: /workspace/github_cpp_makefile_star2_O1/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, 0x34(%rsp)
movsd %xmm0, 0x38(%rsp)
movl %edx, %ebx
movq %rsi, %r14
movq (%rdi), %r13
movq %rdi, 0x28(%rsp)
movq 0x8(%rdi), %r12
cmpq %r12, %r13
je 0x1f61c
movq %r12, %r15
subq %r13, %r15
movq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0x81c8(%rip), %rcx     # 0x17291
movq %r13, %rdi
movq %r12, %rsi
callq 0x1c869
leaq 0x8(%r13), %rax
cmpq $0x81, %r15
jl 0x1f579
movq %r12, 0x8(%rsp)
movl $0x8, %ebp
movq %rax, 0x20(%rsp)
movq %rax, %r15
movq %r13, %rax
movq %r13, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %r13, %rax
addq %rbp, %rax
movq %rax, (%rsp)
movq (%r13,%rbp), %rdi
movq (%r13), %rsi
callq 0x17291
movq %r13, %r12
movq (%r13,%rbp), %r13
testb %al, %al
je 0x1f4ca
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x22b0
jmp 0x1f503
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq %r13, %rdi
callq 0x17291
movq (%rsp), %r12
testb %al, %al
je 0x1f503
movq %r15, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x17291
movq %r12, %rcx
testb %al, %al
jne 0x1f4e5
movq %r13, (%r12)
addq $0x8, %rbp
addq $0x8, %r15
cmpq $0x80, %rbp
movq 0x10(%rsp), %r13
movq (%rsp), %rax
jne 0x1f48f
subq $-0x80, %r13
movq 0x8(%rsp), %rbp
cmpq %rbp, %r13
je 0x1f61c
movq -0x8(%r13), %rsi
movq (%r13), %r15
movq %r15, %rdi
callq 0x17291
movq %r13, %r12
testb %al, %al
je 0x1f56f
movq %r13, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x17291
movq %r12, %rcx
testb %al, %al
jne 0x1f551
movq %r15, (%r12)
addq $0x8, %r13
jmp 0x1f52e
cmpq %r12, %rax
je 0x1f61c
movq %r13, %rcx
movq %r12, 0x8(%rsp)
movq %r13, %rbp
movq %rcx, %r13
movq %rax, %r15
movq (%rax), %rdi
movq (%rbp), %rsi
callq 0x17291
movq %r15, %rdx
movq (%r15), %r15
testb %al, %al
movq %rdx, (%rsp)
je 0x1f5ca
subq %rbp, %rdx
subq %rdx, %r13
addq $0x10, %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x22b0
movq %rbp, %r12
movq %rbp, %r13
jmp 0x1f602
movq (%r13), %rsi
movq %r15, %rdi
movq %rdx, %r12
callq 0x17291
testb %al, %al
movq %rbp, %r13
je 0x1f602
movq (%rsp), %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x17291
movq %r12, %rcx
testb %al, %al
jne 0x1f5e4
movq %r15, (%r12)
movq (%rsp), %rcx
leaq 0x8(%rcx), %rax
movq 0x8(%rsp), %r12
cmpq %r12, %rax
jne 0x1f58a
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x10(%rsp)
movq $0x0, 0x10(%rax)
cmpl $0x2, %ebx
jl 0x1f819
movslq 0x34(%rsp), %rax
movq %rax, 0x20(%rsp)
movl $0x1, %ebp
movq 0x20(%rsp), %rax
leaq (%rax,%rbp), %rcx
movl %ebx, %edx
subl %ebp, %edx
movq %rcx, 0x8(%rsp)
cmpl %ebx, %ecx
cmovlel %eax, %edx
leal (%rdx,%rbp), %ecx
cmpl %ebx, %ecx
movl %ebx, %eax
movl %ecx, (%rsp)
cmovll %ecx, %eax
movl $0x0, %r9d
cmpl %ebp, %eax
jle 0x1f6ad
cltq
xorl %esi, %esi
movq %rbp, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movsd (%r14,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
cmovbel %esi, %r9d
incq %rcx
movl %r9d, %esi
cmpq %rax, %rcx
jl 0x1f688
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %ecx
movsd 0x38(%rsp), %xmm0
movl $0x1, %r8d
callq 0x1e321
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq 0x28(%rsp), %r9
movl (%rsp), %r10d
jbe 0x1f80c
xorl %r13d, %r13d
movsd %xmm0, 0x18(%rsp)
movq (%r9), %rax
movq 0x8(%r9), %rdx
subq %rax, %rdx
movq %rdx, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jge 0x1f80c
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0x1f82d
movq (%rax,%r13,8), %rcx
movl 0xc(%rcx), %r12d
cmpl %ebp, %r12d
jl 0x1f7ff
cmpl %r10d, %r12d
jge 0x1f7ff
movl 0x20(%rcx), %r15d
cmpl %r15d, %r12d
setg %cl
jg 0x1f76f
leal 0x1(%r15), %edx
movl %r12d, %esi
cmpl %ebx, %esi
setge %dil
testl %esi, %esi
setle %r8b
orb %dil, %r8b
jne 0x1f75b
movl %esi, %edi
movsd (%r14,%rdi,8), %xmm2
ucomisd %xmm0, %xmm2
ja 0x1f76c
cmpl %r15d, %esi
leal 0x1(%rsi), %edi
setge %cl
movl %edi, %esi
cmpl %edi, %edx
jne 0x1f73c
jmp 0x1f76f
movl %esi, %r12d
cmpl %r12d, %r15d
jl 0x1f7a5
movl %r15d, %edx
cmpl %ebx, %edx
setge %sil
testl %edx, %edx
setle %dil
orb %sil, %dil
jne 0x1f796
movl %edx, %esi
movsd (%r14,%rsi,8), %xmm2
ucomisd %xmm0, %xmm2
ja 0x1f7a2
leal -0x1(%rdx), %esi
cmpl %r12d, %edx
movl %esi, %edx
jg 0x1f777
jmp 0x1f7a5
movl %edx, %r15d
testb $0x1, %cl
jne 0x1f7ff
cmpl %r15d, %r12d
jg 0x1f83e
movq (%rax,%r13,8), %rdi
callq 0x172ac
movq %rax, 0x40(%rsp)
movl %r12d, 0xc(%rax)
movl %r15d, 0x20(%rax)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1f7e2
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1f7ec
leaq 0x40(%rsp), %rdx
callq 0xb976
movq 0x28(%rsp), %r9
xorpd %xmm1, %xmm1
movl (%rsp), %r10d
movsd 0x18(%rsp), %xmm0
incq %r13
ucomisd %xmm1, %xmm0
ja 0x1f6e5
movq 0x8(%rsp), %rbp
cmpl %ebx, %ebp
jl 0x1f653
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe0a2(%rip), %rdi      # 0x2d8d6
movq %r13, %rsi
xorl %eax, %eax
callq 0x2270
movl %r12d, %edi
movl %r15d, %esi
callq 0x5223