// File: gersteinlab[P]MUSIC/src/ms_signal_track_tools.cpp
// Function: get_valleys_per_per_nucleotide_signal_profile(double*, char*, int, double, double)
vector<t_annot_region*>** get_valleys_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*>** valleys_per_threshes = new vector<t_annot_region*>*[(int)max_thresh-(int)min_thresh+1];
	int* valley_starts = new int[(int)max_thresh-(int)min_thresh+1];
	for(int thresh = (int)min_thresh; thresh <= (int)max_thresh; thresh++)
	{
		valleys_per_threshes[thresh-(int)min_thresh] = new vector<t_annot_region*>();
		valley_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(valley_starts[thresh-(int)min_thresh] == 0)
				{
					// Set a new peak start.
					valley_starts[thresh-(int)min_thresh] = i;
				}
				else
				{
					// This was a peak already.
				}
			}
			else
			{
				// The signal is above the threshold.
				if(valley_starts[thresh-(int)min_thresh] != 0)
				{
					// Add this as a valley.
					t_annot_region* new_valley = get_empty_region();
					new_valley->chrom = t_string::copy_me_str(chrom);
					new_valley->start = valley_starts[thresh-(int)min_thresh];
					new_valley->end = i-1;
					new_valley->strand = '+';

					// Set the current start at this threshold to 0; no peak.
					valley_starts[thresh-(int)min_thresh] = 0;

					// Add the new peak.
					valleys_per_threshes[thresh-(int)min_thresh]->push_back(new_valley);
				}
				else
				{
					// This was not a valley already.
				}
			}
		} // thresh loop.
	} // i loop.

	delete [] valley_starts;

	return(valleys_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 0x359b5
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 0x3593a
movl $0x1, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x54(%rsp), %eax
jg 0x35b1b
cvttsd2si 0x48(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cvttsd2si 0x40(%rsp), %ecx
cmpl %ecx, %eax
jg 0x35b09
movq 0x60(%rsp), %rax
movslq 0x28(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
cvtsi2sdl 0x24(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x35a40
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x35a39
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 0x35a3b
jmp 0x35a3b
jmp 0x35af7
movq 0x30(%rsp), %rax
movl 0x24(%rsp), %ecx
cvttsd2si 0x48(%rsp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x35af3
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 0x35af5
jmp 0x35af5
jmp 0x35af7
jmp 0x35af9
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x359d5
jmp 0x35b0b
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x359bd
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x35b35
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rax
addq $0x68, %rsp
retq
nop