// File: /workspace/github_cpp_makefile_star2_O3/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, %ebx
movsd %xmm0, 0x60(%rsp)
movl %ecx, 0x24(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, 0x18(%rsp)
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %eax, %eax
movq %rax, 0x10(%r14)
movl $0x18, %edi
callq 0x2180
movq %rax, %r15
subl %ebx, %ebp
movslq %ebp, %r12
leaq 0x8(,%r12,4), %r13
cmpl $-0x2, %r12d
movq $-0x1, %rdi
cmovgeq %r13, %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 %r13, %rdx
callq 0x20f0
movl %ebx, 0x30(%rsp)
movslq %ebx, %rbx
movq 0x18(%rsp), %rax
leaq (%rax,%rbx,8), %rdi
incl %r12d
movl %r12d, %esi
movq %r14, %rdx
movq %r15, %rcx
movq %rbp, 0x50(%rsp)
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x241dc
movabsq $0x7fffffff8, %rcx      # imm = 0x7FFFFFFF8
movq (%r14), %r13
movq %r14, 0x18(%rsp)
movq 0x8(%r14), %r12
movq %r12, %r14
subq %r13, %r14
testq %rcx, %r14
je 0xfbd7
movq 0x8(%r15), %rax
subq (%r15), %rax
testq %rcx, %rax
movl $0x0, %eax
je 0x100c5
movq %rbx, 0x48(%rsp)
movq %r15, 0x28(%rsp)
cmpq %r12, %r13
je 0xfc5b
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x15287(%rip), %rcx     # 0x24d5e
movq %r13, %rdi
movq %r12, %rsi
callq 0x10afb
leaq 0x8(%r13), %rbx
cmpq $0x81, %r14
jl 0xfbde
movq %r12, 0x8(%rsp)
movl $0x8, %ebp
movq %rbx, 0x10(%rsp)
movq %r13, %r15
movq %r13, %r12
addq %rbp, %r12
movq (%r13,%rbp), %rdi
movq (%r13), %rsi
callq 0x24d5e
movq (%r13,%rbp), %r14
testb %al, %al
movq %r12, (%rsp)
je 0xfb3b
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r13, %r12
jmp 0xfb6b
movq (%r15), %rsi
movq %r14, %rdi
callq 0x24d5e
testb %al, %al
je 0xfb6b
movq %rbx, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x24d5e
movq %r12, %rcx
testb %al, %al
jne 0xfb4d
movq %r14, (%r12)
addq $0x8, %rbp
addq $0x8, %rbx
cmpq $0x80, %rbp
movq (%rsp), %r15
jne 0xfb05
subq $-0x80, %r13
movq 0x8(%rsp), %r15
cmpq %r15, %r13
je 0xfc5b
movq -0x8(%r13), %rsi
movq (%r13), %rbx
movq %rbx, %rdi
callq 0x24d5e
movq %r13, %r14
testb %al, %al
je 0xfbce
movq %r13, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x24d5e
movq %r14, %rcx
testb %al, %al
jne 0xfbb0
movq %rbx, (%r14)
addq $0x8, %r13
jmp 0xfb8d
xorl %eax, %eax
jmp 0x100c5
cmpq %r12, %rbx
je 0xfc5b
movq %r13, %rbp
movq %rbp, %r14
movq %rbx, %rbp
movq (%rbx), %rdi
movq (%r13), %rsi
callq 0x24d5e
movq (%rbx), %rbx
testb %al, %al
je 0xfc1c
movq %rbp, %rdx
subq %r13, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x22b0
movq %r13, %r14
jmp 0xfc4f
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x24d5e
movq %rbp, %r14
testb %al, %al
je 0xfc4f
movq %rbp, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x24d5e
movq %r14, %rcx
testb %al, %al
jne 0xfc31
movq %rbx, (%r14)
leaq 0x8(%rbp), %rbx
cmpq %r12, %rbx
jne 0xfbe6
movq 0x28(%rsp), %rax
movq (%rax), %r12
movq 0x8(%rax), %r13
cmpq %r13, %r12
je 0xfe0f
movq %r13, %r14
subq %r12, %r14
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x150db(%rip), %rcx     # 0x24d6c
movq %r12, %rdi
movq %r13, %rsi
callq 0x10afb
leaq 0x8(%r12), %rax
cmpq $0x81, %r14
jl 0xfd92
movq %r13, 0x8(%rsp)
movl $0x8, %ebp
movq %rax, 0x10(%rsp)
movq %rax, %r15
movq %r12, %rbx
leaq (%r12,%rbp), %r13
movq (%r12,%rbp), %rdi
movq (%r12), %rsi
callq 0x24d6c
movq (%r12,%rbp), %r14
testb %al, %al
movq %r13, (%rsp)
je 0xfcf5
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r12, %r13
jmp 0xfd25
movq (%rbx), %rsi
movq %r14, %rdi
callq 0x24d6c
testb %al, %al
je 0xfd25
movq %r15, %rcx
leaq -0x8(%rcx), %r13
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x24d6c
movq %r13, %rcx
testb %al, %al
jne 0xfd07
movq %r14, (%r13)
addq $0x8, %rbp
addq $0x8, %r15
cmpq $0x80, %rbp
movq (%rsp), %rbx
jne 0xfcc3
subq $-0x80, %r12
movq 0x8(%rsp), %r15
cmpq %r15, %r12
je 0xfe0f
movq -0x8(%r12), %rsi
movq (%r12), %rbx
movq %rbx, %rdi
callq 0x24d6c
movq %r12, %r14
testb %al, %al
je 0xfd89
movq %r12, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x24d6c
movq %r14, %rcx
testb %al, %al
jne 0xfd6b
movq %rbx, (%r14)
addq $0x8, %r12
jmp 0xfd47
cmpq %r13, %rax
je 0xfe0f
movq %r12, %r15
movq %r15, %r14
movq %rax, %r15
movq (%rax), %rdi
movq (%r12), %rsi
callq 0x24d6c
movq (%r15), %rbx
testb %al, %al
je 0xfdd0
movq %r15, %rdx
subq %r12, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x22b0
movq %r12, %r14
jmp 0xfe03
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x24d6c
movq %r15, %r14
testb %al, %al
je 0xfe03
movq %r15, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x24d6c
movq %r14, %rcx
testb %al, %al
jne 0xfde5
movq %rbx, (%r14)
leaq 0x8(%r15), %rax
cmpq %r13, %rax
jne 0xfd9a
movl $0x18, %edi
callq 0x2180
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
xorl %ebx, %ebx
testl %edx, %edx
jle 0x1003a
movq %rax, %rdi
movl 0xa0(%rsp), %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movl %esi, 0x8(%rsp)
decl 0x24(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x40(%rsp), %rax
movq 0x48(%rsp), %rsi
leaq (%rax,%rsi,8), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movq %rdi, (%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %r9
movq 0x8(%rax), %rax
subq %r9, %rax
sarq $0x3, %rax
testl %eax, %eax
jle 0xffbf
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 0xfee9
cmpq %rsi, %rax
jbe 0x100e0
cmpq %r13, %rdx
jbe 0x100d4
movq (%r9,%rsi,8), %r15
movl (%r15), %ebp
movq (%rcx,%r13,8), %r12
cmpl (%r12), %ebp
cmovgeq %rbx, %r15
cmovll %esi, %r14d
testq %r11, %r11
jne 0xff17
cmpq %rsi, %rax
jbe 0x100e0
cmpq %r13, %rdx
jbe 0x100d4
movq (%r9,%rsi,8), %r11
movl (%r11), %ebp
movq (%rcx,%r13,8), %r12
cmpl (%r12), %ebp
cmovleq %rbx, %r11
cmovgl %esi, %r8d
incq %rsi
cmpq %rsi, %r10
jne 0xfebb
testq %r15, %r15
movq 0x28(%rsp), %r15
movq (%rsp), %rdi
movq 0x10(%rsp), %r10
je 0xffbf
testq %r11, %r11
je 0xffbf
cmpq $0x0, 0x40(%rsp)
je 0xffdb
cmpq %r13, %rdx
jbe 0x100d4
movq (%rcx,%r13,8), %rcx
movl (%rcx), %esi
movl %esi, %ecx
movl 0x8(%rsp), %r9d
subl %r9d, %ecx
cmovll %ebx, %ecx
addl %r9d, %esi
movl 0x24(%rsp), %r9d
cmpl %esi, %r9d
cmovll %r9d, %esi
xorpd %xmm0, %xmm0
cmpl %esi, %ecx
jge 0xff92
movslq %ecx, %rcx
movslq %esi, %rsi
addsd (%r10,%rcx,8), %xmm0
incq %rcx
cmpq %rsi, %rcx
jl 0xff84
divsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq 0x8(%rdi), %rsi
movq 0x10(%rdi), %rcx
ja 0xffe3
leal (%rdx,%rax,2), %eax
movl %eax, 0x34(%rsp)
cmpq %rcx, %rsi
jne 0xffd0
leaq 0x34(%rsp), %rdx
jmp 0x10005
leal (%rdx,%rax,2), %eax
movl %eax, 0x3c(%rsp)
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0xffd4
movl %eax, (%rsi)
jmp 0xfff6
leaq 0x3c(%rsp), %rdx
jmp 0x10005
movq 0x8(%rdi), %rsi
movq 0x10(%rdi), %rcx
addl %r13d, %r14d
addl %r8d, %r14d
movl %r14d, 0x38(%rsp)
cmpq %rcx, %rsi
je 0x10000
movl %r14d, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x1000e
leaq 0x38(%rsp), %rdx
callq 0xae72
movq (%rsp), %rdi
incq %r13
movq (%r15), %rcx
movq 0x8(%r15), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r13
jl 0xfe88
movq (%rdi), %rbx
movq 0x8(%rdi), %rax
jmp 0x1003c
xorl %eax, %eax
subq %rbx, %rax
sarq $0x2, %rax
cmpq %rdx, %rax
jne 0x100f1
testl %edx, %edx
jle 0x10096
testq %rdx, %rdx
je 0x100ff
movl %edx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
subq (%rdi), %rsi
shrq $0x2, %rsi
andl $-0x2, %esi
addl %edx, %esi
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rbx,%rdi,4), %r9d
cmpl %r9d, %esi
cmovgel %r9d, %esi
cmovgl %edi, %r8d
incq %rdi
cmpq %rdi, %rax
jne 0x1007a
movslq %r8d, %rsi
jmp 0x10098
xorl %esi, %esi
cmpq %rsi, %rdx
jbe 0x100f6
movq (%rcx,%rsi,8), %rax
movl 0x30(%rsp), %ebx
addl (%rax), %ebx
movq %r15, %rdi
callq 0x24d7a
movq 0x18(%rsp), %rdi
callq 0x24d7a
movq 0x50(%rsp), %rdi
callq 0x2220
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c7f5(%rip), %rdi     # 0x2c8d0
movq %r13, %rsi
jmp 0x100ea
leaq 0x1c7e9(%rip), %rdi     # 0x2c8d0
movq %rax, %rdx
xorl %eax, %eax
callq 0x2270
callq 0x37c0
leaq 0x1c7d3(%rip), %rdi     # 0x2c8d0
jmp 0x100ea
leaq 0x1c7ca(%rip), %rdi     # 0x2c8d0
movq %rdx, %rsi
jmp 0x100ea