// File: gersteinlab[P]MUSIC/src/ms_signal_track_tools.cpp
// Function: get_peaks_per_per_nucleotide_signal_profile(double*, char*, int, double, double)
vector<t_annot_region*>** get_peaks_per_per_nucleotide_signal_profile(double* signal_profile, char* chrom, int l_data, double min_thresh, double max_thresh)
{
	// Allocate the states of peaks at each position.
	vector<t_annot_region*>** peaks_per_threshes = new vector<t_annot_region*>*[(int)max_thresh-(int)min_thresh+1];
	int* peak_starts = new int[(int)max_thresh-(int)min_thresh+1];
	for(int thresh = (int)min_thresh; thresh <= (int)max_thresh; thresh++)
	{
		peaks_per_threshes[thresh-(int)min_thresh] = new vector<t_annot_region*>();
		peak_starts[thresh-(int)min_thresh] = 0;
	} // thresh loop.

	for(int i = 1; i <= l_data; i++)
	{
		for(int thresh = (int)min_thresh; thresh <= (int)max_thresh; thresh++)
		{
			if(signal_profile[i] >= thresh)
			{
				// The signal is below the threshold.
				if(peak_starts[thresh-(int)min_thresh] == 0)
				{
					// Set a new peak start.
					peak_starts[thresh-(int)min_thresh] = i;
				}
				else
				{
					// This was a peak already.
				}
			}
			else
			{
				// The signal is below the threshold.
				if(peak_starts[thresh-(int)min_thresh] != 0)
				{
					// Add this as a peak.
					t_annot_region* new_peak = get_empty_region();
					new_peak->chrom = t_string::copy_me_str(chrom);
					new_peak->start = peak_starts[thresh-(int)min_thresh];
					new_peak->end = i-1;
					new_peak->strand = '+';

					// Set the current start at this threshold to 0; no peak.
					peak_starts[thresh-(int)min_thresh] = 0;

					// Add the new peak.
					peaks_per_threshes[thresh-(int)min_thresh]->push_back(new_peak);
				}
				else
				{
				// This was not a peak already.
				}
			}
		} // thresh loop.
	} // i loop.

	delete [] peak_starts;

	return(peaks_per_threshes);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movsd %xmm0, 0x48(%rsp)
movsd %xmm1, 0x40(%rsp)
cvttsd2si 0x40(%rsp), %eax
cvttsd2si 0x48(%rsp), %ecx
subl %ecx, %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x38(%rsp)
cvttsd2si 0x40(%rsp), %eax
cvttsd2si 0x48(%rsp), %ecx
subl %ecx, %eax
addl $0x1, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x30(%rsp)
cvttsd2si 0x48(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cvttsd2si 0x40(%rsp), %ecx
cmpl %ecx, %eax
jg 0x35715
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x10(%rsp), %rdi
callq 0xc670
movq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rax
movl 0x2c(%rsp), %ecx
cvttsd2si 0x48(%rsp), %esi
subl %esi, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x30(%rsp), %rax
movl 0x2c(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x3569a
movl $0x1, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x54(%rsp), %eax
jg 0x3587b
cvttsd2si 0x48(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cvttsd2si 0x40(%rsp), %ecx
cmpl %ecx, %eax
jg 0x35869
movq 0x60(%rsp), %rax
movslq 0x28(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl 0x24(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x357a0
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x35799
movl 0x28(%rsp), %edx
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %esi
subl %esi, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x3579b
jmp 0x3579b
jmp 0x35857
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x35853
callq 0x27b00
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rdi
callq 0x47b60
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x28(%rsp), %ecx
subl $0x1, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x18(%rsp), %rax
movb $0x2b, 0x8(%rax)
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
movl $0x0, (%rax,%rcx,4)
movq 0x38(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc8c0
jmp 0x35855
jmp 0x35855
jmp 0x35857
jmp 0x35859
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x35735
jmp 0x3586b
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x3571d
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x35895
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rax
addq $0x68, %rsp
retq
nop