// File: 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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movl %edx, 0x84(%rsp)
movq %rcx, 0x78(%rsp)
movl %r8d, 0x74(%rsp)
movsd %xmm0, 0x68(%rsp)
movl %r9d, 0x64(%rsp)
movl $0x1, 0x60(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x90(%rsp), %rdi
callq 0xc7d0
movq %rax, 0x50(%rsp)
movq 0x90(%rsp), %rdi
callq 0xc760
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rsi
leaq -0xd998(%rip), %rdx     # 0x2a3a0
callq 0x31d00
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x8(%rsp), %rdi
callq 0xc670
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x84(%rsp), %eax
jge 0x38096
movl 0x60(%rsp), %eax
addl 0x64(%rsp), %eax
cmpl 0x84(%rsp), %eax
jle 0x37da2
movl 0x84(%rsp), %eax
subl 0x60(%rsp), %eax
movl %eax, 0x5c(%rsp)
jmp 0x37daa
movl 0x64(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl $0x0, 0x3c(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %ecx
movl 0x60(%rsp), %edx
addl 0x5c(%rsp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, 0x7(%rsp)
jge 0x37de2
movl 0x38(%rsp), %eax
cmpl 0x84(%rsp), %eax
setl %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x37dec
jmp 0x37e2f
cvtsi2sdl 0x3c(%rsp), %xmm1
movq 0x88(%rsp), %rax
movslq 0x38(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x37e20
movq 0x88(%rsp), %rax
movslq 0x38(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x3c(%rsp)
jmp 0x37e22
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x37dba
movq 0x88(%rsp), %rdi
movl 0x84(%rsp), %esi
movl 0x5c(%rsp), %edx
movl 0x60(%rsp), %ecx
movsd 0x68(%rsp), %xmm0
movl 0x3c(%rsp), %r9d
movl $0x1, %r8d
callq 0x36100
movsd %xmm0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movsd 0x30(%rsp), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movb %al, 0x6(%rsp)
jbe 0x37ea2
movl 0x2c(%rsp), %eax
movl %eax, (%rsp)
movq 0x90(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl (%rsp), %eax
cmpl %ecx, %eax
setl %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %al
testb $0x1, %al
jne 0x37eaf
jmp 0x38085
movq 0x90(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
cmpl 0x60(%rsp), %eax
jl 0x38071
movq 0x90(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl 0x60(%rsp), %ecx
addl 0x5c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x38071
movq 0x90(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x28(%rsp)
movq 0x90(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x24(%rsp)
movb $0x0, 0x23(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x24(%rsp), %eax
jg 0x37f94
movl 0x1c(%rsp), %eax
cmpl 0x84(%rsp), %eax
jge 0x37f85
cmpl $0x0, 0x1c(%rsp)
jle 0x37f85
movq 0x88(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x37f85
movb $0x1, 0x23(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x28(%rsp)
jmp 0x37f94
jmp 0x37f87
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x37f3e
movl 0x24(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x28(%rsp), %eax
jl 0x37fed
movl 0x18(%rsp), %eax
cmpl 0x84(%rsp), %eax
jge 0x37fde
cmpl $0x0, 0x18(%rsp)
jle 0x37fde
movq 0x88(%rsp), %rax
movslq 0x18(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x37fde
movl 0x18(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x37fed
jmp 0x37fe0
movl 0x18(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x37f9c
testb $0x1, 0x23(%rsp)
je 0x3806f
movl 0x28(%rsp), %eax
cmpl 0x24(%rsp), %eax
jg 0x38046
movq 0x90(%rsp), %rdi
movslq 0x2c(%rsp), %rsi
callq 0xc800
movq (%rax), %rdi
callq 0x2a410
movq %rax, 0x10(%rsp)
movl 0x28(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x24(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc8c0
jmp 0x3806d
movq 0x2cf93(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0x28(%rsp), %edx
movl 0x24(%rsp), %ecx
leaq 0x1e529(%rip), %rsi     # 0x56588
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
jmp 0x3806f
jmp 0x38073
jmp 0x38073
jmp 0x38075
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x37e6a
movl 0x64(%rsp), %eax
addl 0x60(%rsp), %eax
movl %eax, 0x60(%rsp)
jmp 0x37d6f
movq 0x40(%rsp), %rax
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)