// File: /workspace/github_cpp_makefile_star2_O3/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 0x1f1b3
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 -0x80ec(%rip), %rcx     # 0x16f04
movq %r13, %rdi
movq %r12, %rsi
callq 0x1c455
leaq 0x8(%r13), %rax
cmpq $0x81, %r15
jl 0x1f110
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 0x16f04
movq %r13, %r12
movq (%r13,%rbp), %r13
testb %al, %al
je 0x1f061
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x22b0
jmp 0x1f09a
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq %r13, %rdi
callq 0x16f04
movq (%rsp), %r12
testb %al, %al
je 0x1f09a
movq %r15, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x16f04
movq %r12, %rcx
testb %al, %al
jne 0x1f07c
movq %r13, (%r12)
addq $0x8, %rbp
addq $0x8, %r15
cmpq $0x80, %rbp
movq 0x10(%rsp), %r13
movq (%rsp), %rax
jne 0x1f026
subq $-0x80, %r13
movq 0x8(%rsp), %rbp
cmpq %rbp, %r13
je 0x1f1b3
movq -0x8(%r13), %rsi
movq (%r13), %r15
movq %r15, %rdi
callq 0x16f04
movq %r13, %r12
testb %al, %al
je 0x1f106
movq %r13, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x16f04
movq %r12, %rcx
testb %al, %al
jne 0x1f0e8
movq %r15, (%r12)
addq $0x8, %r13
jmp 0x1f0c5
cmpq %r12, %rax
je 0x1f1b3
movq %r13, %rcx
movq %r12, 0x8(%rsp)
movq %r13, %rbp
movq %rcx, %r13
movq %rax, %r15
movq (%rax), %rdi
movq (%rbp), %rsi
callq 0x16f04
movq %r15, %rdx
movq (%r15), %r15
testb %al, %al
movq %rdx, (%rsp)
je 0x1f161
subq %rbp, %rdx
subq %rdx, %r13
addq $0x10, %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x22b0
movq %rbp, %r12
movq %rbp, %r13
jmp 0x1f199
movq (%r13), %rsi
movq %r15, %rdi
movq %rdx, %r12
callq 0x16f04
testb %al, %al
movq %rbp, %r13
je 0x1f199
movq (%rsp), %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x16f04
movq %r12, %rcx
testb %al, %al
jne 0x1f17b
movq %r15, (%r12)
movq (%rsp), %rcx
leaq 0x8(%rcx), %rax
movq 0x8(%rsp), %r12
cmpq %r12, %rax
jne 0x1f121
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x10(%rsp)
movq $0x0, 0x10(%rax)
cmpl $0x2, %ebx
jl 0x1f3c1
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 0x1f244
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 0x1f21f
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %ecx
movsd 0x38(%rsp), %xmm0
movl $0x1, %r8d
callq 0x1de93
ucomisd 0xae4c(%rip), %xmm0     # 0x2a0b0
movq 0x28(%rsp), %r9
movl (%rsp), %r10d
jbe 0x1f3b4
movq (%r9), %rax
movq 0x8(%r9), %rdx
subq %rax, %rdx
movq %rdx, %rcx
shlq $0x1d, %rcx
movabsq $0x100000000, %rsi      # imm = 0x100000000
cmpq %rsi, %rcx
jl 0x1f3b4
xorl %r13d, %r13d
movsd %xmm0, 0x18(%rsp)
sarq $0x3, %rdx
cmpq %r13, %rdx
jbe 0x1f3d5
movq (%rax,%r13,8), %rdi
movl 0xc(%rdi), %r12d
cmpl %ebp, %r12d
jl 0x1f394
cmpl %r10d, %r12d
jge 0x1f394
movl 0x20(%rdi), %r15d
movb $0x1, %al
cmpl %r15d, %r12d
jg 0x1f30e
leal 0x1(%r15), %ecx
xorl %eax, %eax
movl %r12d, %edx
cmpl %ebx, %edx
setge %sil
testl %edx, %edx
setle %r8b
orb %sil, %r8b
jne 0x1f2fa
movl %edx, %esi
movsd (%r14,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1f30b
cmpl %r15d, %edx
leal 0x1(%rdx), %esi
setge %al
movl %esi, %edx
cmpl %esi, %ecx
jne 0x1f2db
jmp 0x1f30e
movl %edx, %r12d
cmpl %r12d, %r15d
jl 0x1f343
movl %r15d, %ecx
cmpl %ebx, %ecx
setge %dl
testl %ecx, %ecx
setle %sil
orb %dl, %sil
jne 0x1f334
movl %ecx, %edx
movsd (%r14,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x1f340
leal -0x1(%rcx), %edx
cmpl %r12d, %ecx
movl %edx, %ecx
jg 0x1f316
jmp 0x1f343
movl %ecx, %r15d
testb $0x1, %al
jne 0x1f394
cmpl %r15d, %r12d
jg 0x1f3e6
callq 0x16f1f
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 0x1f37b
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x1f385
leaq 0x40(%rsp), %rdx
callq 0xb74c
movq 0x28(%rsp), %r9
movl (%rsp), %r10d
movsd 0x18(%rsp), %xmm0
incq %r13
movq (%r9), %rax
movq 0x8(%r9), %rdx
subq %rax, %rdx
movq %rdx, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jl 0x1f2a0
movq 0x8(%rsp), %rbp
cmpl %ebx, %ebp
jl 0x1f1ea
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd4f4(%rip), %rdi      # 0x2c8d0
movq %r13, %rsi
xorl %eax, %eax
callq 0x2270
movl %r12d, %edi
movl %r15d, %esi
callq 0x50c0