// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: get_trough_posn_per_ER(double*, int, double*, int, double, int, int, int)
int get_trough_posn_per_ER(double* signal_profile, int l_profile, 
							double* multi_mapp_signal, int l_multi_map_signal, double max_multi_mapp_val,
							int ER_start, int ER_end,
							int l_trough_win)
{
	//vector<int>* maxima_posns = new vector<int>();
	//double min_max_val = 0.0;

	vector<t_extrema_node*>* maxima_nodes = new vector<t_extrema_node*>();
	vector<t_extrema_node*>* minima_nodes = new vector<t_extrema_node*>();
	int* derivative_map = new int[ER_end - ER_start + 2];
	memset(derivative_map, 0, sizeof(int) * (ER_end - ER_start + 2));
	get_extrema_per_plateaus(&signal_profile[ER_start], ER_end-ER_start+1, maxima_nodes, minima_nodes, derivative_map, 0);

	if((int)maxima_nodes->size() == 0 ||
		(int)minima_nodes->size() == 0)
	{
		return(0);
	}

	sort(maxima_nodes->begin(), maxima_nodes->end(), sort_extremas_per_decreasing_height);

	// Sort the minima with increasing height then take the top minima.
	sort(minima_nodes->begin(), minima_nodes->end(), sort_extremas_per_height);

	// Start from the top minimum and check if it is around one of the top maxima.
	vector<int>* rank_sum_per_minimum = new vector<int>();
	for(int i_min = 0; i_min < (int)minima_nodes->size(); i_min++)
	{
		t_extrema_node* prev_max = NULL;
		t_extrema_node* next_max = NULL;
		int prev_max_rank = 0;
		int next_max_rank = 0;

		for(int i_max = 0; i_max < (int)maxima_nodes->size(); i_max++)
		{
			if(prev_max == NULL &&
				maxima_nodes->at(i_max)->extrema_posn < minima_nodes->at(i_min)->extrema_posn)
			{
				prev_max_rank = i_max;
				prev_max = maxima_nodes->at(i_max);
			}

			if(next_max == NULL &&
				maxima_nodes->at(i_max)->extrema_posn > minima_nodes->at(i_min)->extrema_posn)
			{
				next_max_rank = i_max;
				next_max = maxima_nodes->at(i_max);
			}
		} // i_max loop.

		if(prev_max == NULL ||
			next_max == NULL)
		{
			rank_sum_per_minimum->push_back((int)maxima_nodes->size() + (int)maxima_nodes->size() + (int)minima_nodes->size());
			continue;
		}

		double total_multi_mapp_signal = 0;
		if(multi_mapp_signal != NULL)
		{
            for(int i = MAX(0, minima_nodes->at(i_min)->extrema_posn-l_trough_win/2); 
				i < MIN(l_multi_map_signal-1, minima_nodes->at(i_min)->extrema_posn+l_trough_win/2); i++)
            {
                total_multi_mapp_signal += multi_mapp_signal[ER_start+i];
            } // i loop.

			//for(int i = prev_max->extrema_posn; i <= next_max->extrema_posn; i++)
			//{
			//	total_multi_mapp_signal += multi_mapp_signal[ER_start+i];
			//} // i loop.
		}

		// Make sure that the region in this trough is mappable.
		// total_multi_mapp_signal / (next_max->extrema_posn - prev_max->extrema_posn+1) < max_multi_mapp_val)
		if(multi_mapp_signal == NULL ||
			total_multi_mapp_signal / l_trough_win < max_multi_mapp_val)
		{
			rank_sum_per_minimum->push_back(i_min + prev_max_rank + next_max_rank);
		}
		else
		{
			rank_sum_per_minimum->push_back((int)maxima_nodes->size() + (int)maxima_nodes->size() + (int)minima_nodes->size());
		}
	} // i_min loop.

	// Check on the number of rank sums that are generated.
	if(rank_sum_per_minimum->size() != minima_nodes->size())
	{
		fprintf(stderr, "The sanity check failed, per minimum rank sum count does not match the minima positions.\n");
		exit(0);
	}

	// Find the minimum with smallest rank-sum statistic.
	int smallest_rank_sum_stat = (int)maxima_nodes->size() + (int)maxima_nodes->size() + (int)minima_nodes->size();
	int smallest_rank_sum_min_i = 0;
	for(int i_min = 0; i_min < (int)rank_sum_per_minimum->size(); i_min++)
	{
		if(smallest_rank_sum_stat > rank_sum_per_minimum->at(i_min))
		{
			smallest_rank_sum_stat = rank_sum_per_minimum->at(i_min);
			smallest_rank_sum_min_i = i_min;
		}
	} // i_min loop.

	// This position is with respect to the beginning of the ER. Since extrema are with respect to begining of the ER, translate this coordinate.
	int trough_posn = minima_nodes->at(smallest_rank_sum_min_i)->extrema_posn + ER_start;

	delete_extrema_nodes(minima_nodes);
	delete_extrema_nodes(maxima_nodes);

	// Free the current decomposition.
	delete [] derivative_map;

	return(trough_posn);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, %ebp
movl %r8d, %r13d
movsd %xmm0, 0x60(%rsp)
movl %ecx, 0x24(%rsp)
movq %rdx, 0x30(%rsp)
movq %rdi, 0x18(%rsp)
movl $0x18, %edi
callq 0x2180
movq %rax, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %eax, %eax
movq %rax, 0x10(%rbx)
movl $0x18, %edi
callq 0x2180
movq %rax, %r15
subl %r13d, %ebp
movslq %ebp, %r14
leaq 0x8(,%r14,4), %r12
cmpl $-0x2, %r14d
movq $-0x1, %rdi
cmovgeq %r12, %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %eax, %eax
movq %rax, 0x10(%r15)
callq 0x2040
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x20f0
movl %r13d, 0x3c(%rsp)
movslq %r13d, %rax
movq %rax, 0x50(%rsp)
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rdi
incl %r14d
movl %r14d, %esi
movq %rbx, %rdx
movq %r15, %rcx
movq %rbp, 0x58(%rsp)
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x24787
movabsq $0x7fffffff8, %rcx      # imm = 0x7FFFFFFF8
movq (%rbx), %r12
movq %rbx, 0x18(%rsp)
movq 0x8(%rbx), %rbx
movq %rbx, %r14
subq %r12, %r14
testq %rcx, %r14
je 0xfe9b
movq 0x8(%r15), %rax
subq (%r15), %rax
testq %rcx, %rax
movl $0x0, %eax
je 0x103a9
movq %r15, 0x28(%rsp)
cmpq %rbx, %r12
je 0xff2c
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x155a2(%rip), %rcx     # 0x2533c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10dc5
leaq 0x8(%r12), %rax
cmpq $0x81, %r14
jl 0xfea2
movq %rbx, (%rsp)
movl $0x8, %r13d
movq %rax, 0x10(%rsp)
movq %rax, %rbp
movq %r12, %r15
leaq (%r12,%r13), %rbx
movq (%r12,%r13), %rdi
movq (%r12), %rsi
callq 0x2533c
movq (%r12,%r13), %r14
testb %al, %al
movq %rbx, 0x8(%rsp)
je 0xfdff
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x22b0
movq %r12, %rbx
jmp 0xfe2f
movq (%r15), %rsi
movq %r14, %rdi
callq 0x2533c
testb %al, %al
je 0xfe2f
movq %rbp, %rcx
leaq -0x8(%rcx), %rbx
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x2533c
movq %rbx, %rcx
testb %al, %al
jne 0xfe11
movq %r14, (%rbx)
addq $0x8, %r13
addq $0x8, %rbp
cmpq $0x80, %r13
movq 0x8(%rsp), %r15
jne 0xfdcc
subq $-0x80, %r12
movq (%rsp), %r15
cmpq %r15, %r12
je 0xff2c
movq -0x8(%r12), %rsi
movq (%r12), %r14
movq %r14, %rdi
callq 0x2533c
movq %r12, %rbx
testb %al, %al
je 0xfe92
movq %r12, %rcx
leaq -0x8(%rcx), %rbx
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x2533c
movq %rbx, %rcx
testb %al, %al
jne 0xfe74
movq %r14, (%rbx)
addq $0x8, %r12
jmp 0xfe50
xorl %eax, %eax
jmp 0x103a9
cmpq %rbx, %rax
je 0xff2c
movq %r12, %rbp
movq %rbx, (%rsp)
movq %rbp, %r14
movq %rax, %rbp
movq (%rax), %rdi
movq (%r12), %rsi
callq 0x2533c
movq (%rbp), %r13
testb %al, %al
je 0xfee9
movq %rbp, %rdx
subq %r12, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x22b0
movq %r12, %rbx
jmp 0xff1c
movq (%r14), %rsi
movq %r13, %rdi
callq 0x2533c
movq %rbp, %rbx
testb %al, %al
je 0xff1c
movq %rbp, %rcx
leaq -0x8(%rcx), %rbx
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x2533c
movq %rbx, %rcx
testb %al, %al
jne 0xfefe
movq %r13, (%rbx)
leaq 0x8(%rbp), %rax
movq (%rsp), %rbx
cmpq %rbx, %rax
jne 0xfeb2
movq 0x28(%rsp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r12
cmpq %r12, %rbx
je 0x100dd
movq %r12, %r14
subq %rbx, %r14
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x153e8(%rip), %rcx     # 0x2534a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x10dc5
leaq 0x8(%rbx), %rax
cmpq $0x81, %r14
jl 0x10061
movq %r12, 0x10(%rsp)
movl $0x8, %r13d
movq %rax, (%rsp)
movq %rax, %r15
movq %rbx, %rbp
leaq (%rbx,%r13), %r12
movq (%rbx,%r13), %rdi
movq (%rbx), %rsi
callq 0x2534a
movq (%rbx,%r13), %r14
testb %al, %al
movq %r12, 0x8(%rsp)
je 0xffc4
movq (%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x22b0
movq %rbx, %r12
jmp 0xfff5
movq (%rbp), %rsi
movq %r14, %rdi
callq 0x2534a
testb %al, %al
je 0xfff5
movq %r15, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x2534a
movq %r12, %rcx
testb %al, %al
jne 0xffd7
movq %r14, (%r12)
addq $0x8, %r13
addq $0x8, %r15
cmpq $0x80, %r13
movq 0x8(%rsp), %rbp
jne 0xff93
subq $-0x80, %rbx
movq 0x10(%rsp), %r12
cmpq %r12, %rbx
je 0x100dd
movq -0x8(%rbx), %rsi
movq (%rbx), %r14
movq %r14, %rdi
callq 0x2534a
movq %rbx, %r15
testb %al, %al
je 0x10058
movq %rbx, %rcx
leaq -0x8(%rcx), %r15
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x2534a
movq %r15, %rcx
testb %al, %al
jne 0x1003a
movq %r14, (%r15)
addq $0x8, %rbx
jmp 0x10018
cmpq %r12, %rax
je 0x100dd
movq %rbx, %r15
movq %r15, %r14
movq %rax, %r15
movq (%rax), %rdi
movq (%rbx), %rsi
callq 0x2534a
movq (%r15), %r13
testb %al, %al
je 0x1009e
movq %r15, %rdx
subq %rbx, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22b0
movq %rbx, %r14
jmp 0x100d1
movq (%r14), %rsi
movq %r13, %rdi
callq 0x2534a
movq %r15, %r14
testb %al, %al
je 0x100d1
movq %r15, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x2534a
movq %r14, %rcx
testb %al, %al
jne 0x100b3
movq %r13, (%r14)
leaq 0x8(%r15), %rax
cmpq %r12, %rax
jne 0x10069
movl $0x18, %edi
callq 0x2180
movq %rax, %rdi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x28(%rsp), %r15
movq (%r15), %rcx
movq 0x8(%r15), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x10319
movl 0xa0(%rsp), %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movl %esi, 0x40(%rsp)
decl 0x24(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x30(%rsp), %rax
movq 0x50(%rsp), %rsi
leaq (%rax,%rsi,8), %r10
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %rdi, 0x8(%rsp)
movq %r10, (%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %r9
movq 0x8(%rax), %rax
subq %r9, %rax
sarq $0x3, %rax
testl %eax, %eax
jle 0x1020e
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r14d, %r14d
xorl %r11d, %r11d
xorl %r15d, %r15d
testq %r15, %r15
jne 0x101b7
cmpq %rsi, %rax
jbe 0x103c4
cmpq %rbx, %rdx
jbe 0x103b8
movq (%r9,%rsi,8), %r15
movl (%r15), %ebp
movq (%rcx,%rbx,8), %r12
cmpl (%r12), %ebp
cmovgeq %r13, %r15
cmovll %esi, %r14d
testq %r11, %r11
jne 0x101e5
cmpq %rsi, %rax
jbe 0x103c4
cmpq %rbx, %rdx
jbe 0x103b8
movq (%r9,%rsi,8), %r11
movl (%r11), %ebp
movq (%rcx,%rbx,8), %r12
cmpl (%r12), %ebp
cmovleq %r13, %r11
cmovgl %esi, %r8d
incq %rsi
cmpq %rsi, %r10
jne 0x10189
testq %r15, %r15
sete %r9b
testq %r11, %r11
sete %sil
orb %r9b, %sil
movq 0x28(%rsp), %r15
movq 0x8(%rsp), %rdi
movq (%rsp), %r10
jmp 0x10217
movb $0x1, %sil
xorl %r14d, %r14d
xorl %r8d, %r8d
testb %sil, %sil
je 0x1023b
leal (%rdx,%rax,2), %eax
movl %eax, 0x4c(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
jne 0x102b6
leaq 0x4c(%rsp), %rdx
jmp 0x102e8
xorpd %xmm0, %xmm0
cmpq $0x0, 0x30(%rsp)
je 0x1028b
cmpq %rbx, %rdx
jbe 0x103b8
movq (%rcx,%rbx,8), %rcx
movl (%rcx), %esi
movl %esi, %ecx
movl 0x40(%rsp), %r9d
subl %r9d, %ecx
cmovll %r13d, %ecx
addl %r9d, %esi
movl 0x24(%rsp), %r9d
cmpl %esi, %r9d
cmovll %r9d, %esi
cmpl %esi, %ecx
jge 0x1028b
movslq %ecx, %rcx
movslq %esi, %rsi
addsd (%r10,%rcx,8), %xmm0
incq %rcx
cmpq %rsi, %rcx
jl 0x1027d
cmpq $0x0, 0x30(%rsp)
je 0x102ba
divsd 0x10(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x102ba
leal (%rdx,%rax,2), %eax
movl %eax, 0x44(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x102e3
movl %eax, (%rsi)
jmp 0x102d2
addl %ebx, %r14d
addl %r8d, %r14d
movl %r14d, 0x48(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x102dc
movl %r14d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x102f6
leaq 0x48(%rsp), %rdx
jmp 0x102e8
leaq 0x44(%rsp), %rdx
callq 0xb048
movq (%rsp), %r10
movq 0x8(%rsp), %rdi
incq %rbx
movq (%r15), %rcx
movq 0x8(%r15), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jl 0x10156
movq (%rdi), %r14
movq 0x8(%rdi), %r8
movq %r8, %rax
subq %r14, %rax
sarq $0x2, %rax
cmpq %rdx, %rax
jne 0x103e1
xorl %esi, %esi
testl %eax, %eax
jle 0x1037c
movq 0x18(%rsp), %r10
movq 0x8(%r10), %r9
subq (%r10), %r9
shrq $0x2, %r9
andl $-0x2, %r9d
addl %edx, %r9d
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
xorl %r11d, %r11d
cmpq %r14, %r8
je 0x103d5
movl (%r14,%r11,4), %ebx
cmpl %ebx, %r9d
cmovgel %ebx, %r9d
cmovgl %r11d, %esi
incq %r11
cmpq %r11, %r10
jne 0x1035d
movslq %esi, %rsi
cmpq %rsi, %rdx
jbe 0x103e6
movq (%rcx,%rsi,8), %rax
movl 0x3c(%rsp), %ebx
addl (%rax), %ebx
movq %r15, %rdi
callq 0x25358
movq 0x18(%rsp), %rdi
callq 0x25358
movq 0x58(%rsp), %rdi
callq 0x2220
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d517(%rip), %rdi     # 0x2d8d6
movq %rbx, %rsi
jmp 0x103ce
leaq 0x1d50b(%rip), %rdi     # 0x2d8d6
movq %rax, %rdx
xorl %eax, %eax
callq 0x2270
leaq 0x1d4fa(%rip), %rdi     # 0x2d8d6
movq %rax, %rsi
jmp 0x103cb
callq 0x38e1
leaq 0x1d4e9(%rip), %rdi     # 0x2d8d6
jmp 0x103ce