// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_min_max_utils.cpp
// Function: prune_extrema_derivative_map(std::vector<t_extrema_node*, std::allocator<t_extrema_node*>>*, std::vector<t_extrema_node*, std::allocator<t_extrema_node*>>*, int*, int, int, std::vector<t_extrema_node*, std::allocator<t_extrema_node*>>*, std::vector<t_extrema_node*, std::allocator<t_extrema_node*>>*, int*)
void prune_extrema_derivative_map(vector<t_extrema_node*>* mins,
	vector<t_extrema_node*>* maxes,
	signed int* derivative_map, 
	int l_smallest_side,
	int l_signal, 
	vector<t_extrema_node*>* pruned_mins,
	vector<t_extrema_node*>* pruned_maxes,
	signed int* pruned_derivative_map)
{
	vector<t_extrema_node*>* extrema = new vector<t_extrema_node*>();
	extrema->insert(extrema->end(), mins->begin(), mins->end());
	extrema->insert(extrema->end(), maxes->begin(), maxes->end());

	sort(extrema->begin(), extrema->end(), sort_extremas_per_posn);

	// Copy pruned derivative map.
	for(int i = 0; i < l_signal; i++)
	{
		pruned_derivative_map[i] = derivative_map[i];
	}	
	
	int i_ext = 0;
	while(i_ext < (int)extrema->size()-1)
	{
		int l_cur_side = extrema->at(i_ext+1)->extrema_posn - extrema->at(i_ext)->extrema_posn;

		if(l_cur_side < l_smallest_side)
		{
			if(extrema->at(i_ext)->extrema_type == EXTREMA_MAX)
			{
				// This is from MAX to MIN: Fix the derivative map, do not add these extrema to the pruned list.
if(__DUMP_MIN_MAX_UTILS_MSGS__)
				fprintf(stderr, "Pruning MAX-MIN transition at %d, %d\n",extrema->at(i_ext)->extrema_posn, extrema->at(i_ext+1)->extrema_posn);
				for(int i = extrema->at(i_ext)->extrema_posn; i <= extrema->at(i_ext+1)->extrema_posn; i++)
				{
					if(derivative_map[i] > 0)
					{
						fprintf(stderr, "The derivative is set to negative from max to min transition.\n");
						exit(0);
					}
					else
					{
						pruned_derivative_map[i] = 1;
					}
				} // i loop.
			}
			else if(extrema->at(i_ext)->extrema_type == EXTREMA_MIN)
			{
if(__DUMP_MIN_MAX_UTILS_MSGS__)
				fprintf(stderr, "Pruning MIN-MAX transition at %d, %d\n",extrema->at(i_ext)->extrema_posn, extrema->at(i_ext+1)->extrema_posn);
				// This is from MIN to MAX: Fix the derivative map, do not add these extrema to the pruned list.
				for(int i = extrema->at(i_ext)->extrema_posn; i <= extrema->at(i_ext+1)->extrema_posn; i++)
				{
					if(derivative_map[i] < 0)
					{
						fprintf(stderr, "The derivative is set to negative from max to min transition.\n");
						exit(0);
					}
					else
					{
						pruned_derivative_map[i] = -1;
					}
				} // i loop.
			}

			// Remove the extrema at i_ext and i_ext+1.
			extrema->erase(extrema->begin()+i_ext);
			extrema->erase(extrema->begin()+i_ext);

			// At this point, we are basically at the next element.
		}
		else
		{
			// This side's length is ok. Move to the next side.
			i_ext++;
		}
	} // i_ext loop.

	// Must check whether the last extrema nodes are removed, then make sure those are added for correct tracing.
	if(pruned_derivative_map[0] != 0)
	{
		fprintf(stderr, "The first extrema is removed, adding it back.\n");
		//getc(stdin);

		t_extrema_node* begin_ext_node = new t_extrema_node();
		begin_ext_node->extrema_posn = 0;
		begin_ext_node->extrema_type = EXTREMA_MIN;
		begin_ext_node->higher_node = NULL;
		begin_ext_node->lower_node = NULL;
		begin_ext_node->scale = extrema->at(0)->scale;

		if(pruned_derivative_map[0] > 0)
		{
			// Add a minima to the beginning.
			begin_ext_node->extrema_type = EXTREMA_MIN;
		}
		else if(pruned_derivative_map[0] < 0)
		{
			// Add a maxima to the beginning.
			begin_ext_node->extrema_type = EXTREMA_MAX;
		}

		pruned_derivative_map[0] = 0;
		extrema->push_back(begin_ext_node);
	}

	if(pruned_derivative_map[l_signal-1] != 0)
	{
		fprintf(stderr, "The last extrema is removed, adding it back.\n");
		//getc(stdin);

		t_extrema_node* end_ext_node = new t_extrema_node();
		end_ext_node->extrema_posn = l_signal-1;
		end_ext_node->extrema_type = EXTREMA_MIN;
		end_ext_node->higher_node = NULL;
		end_ext_node->lower_node = NULL;
		end_ext_node->scale = extrema->back()->scale;

		if(pruned_derivative_map[l_signal-1] > 0)
		{
			end_ext_node->extrema_type = EXTREMA_MAX;
		}
		else if(pruned_derivative_map[l_signal-1] < 0)
		{
			end_ext_node->extrema_type = EXTREMA_MIN;
		}

		pruned_derivative_map[l_signal-1] = 0;
		extrema->push_back(end_ext_node);
	}

	// Sort the extrema.
	sort(extrema->begin(), extrema->end(), sort_extremas_per_posn);

	// Separate the total list into two lists.
	for(i_ext = 0; i_ext < (int)extrema->size(); i_ext++)
	{
		if(extrema->at(i_ext)->extrema_type == EXTREMA_MIN)
		{
			pruned_mins->push_back(extrema->at(i_ext));
		}
		else if(extrema->at(i_ext)->extrema_type == EXTREMA_MAX)
		{
			pruned_maxes->push_back(extrema->at(i_ext));
		}
	} // i_ext loop.

	//vector<t_extrema_node*>* all_pruned_nodes = new vector<t_extrema_node*>();
	//all_pruned_nodes->insert(all_pruned_nodes->begin(), pruned_mins->begin(), pruned_mins->end());
	//all_pruned_nodes->insert(all_pruned_nodes->begin(), pruned_maxes->begin(), pruned_maxes->end());

	//sort(all_pruned_nodes->begin(), all_pruned_nodes->end(), sort_extremas);

	// Set the derivative map.
	//int i_cur_nuc = 0;
	for(int i_ext = 0; i_ext < (int)extrema->size(); i_ext++)
	{
		if(i_ext > 0 &&
			extrema->at(i_ext-1)->extrema_type == extrema->at(i_ext)->extrema_type)
		{
			fprintf(stderr, "Consecutive same extrema: %d, %d: %d\n", 
				extrema->at(i_ext-1)->extrema_posn, 
				extrema->at(i_ext)->extrema_posn, 
				extrema->at(i_ext)->extrema_type);
			exit(0);
		}
	} // i_ext loop.
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x20(%rsp)
movl %r8d, 0x4(%rsp)
movl %ecx, 0x14(%rsp)
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbp
movq 0x78(%rsp), %r14
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %rbx
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq (%rbp), %rdx
movq 0x8(%rbp), %rcx
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
callq 0x1f304
movq 0x8(%rbx), %rsi
movq (%r15), %rdx
movq 0x8(%r15), %rcx
movq %rbx, %rdi
callq 0x1f304
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x49f(%rip), %rdx       # 0x1ea7d
callq 0xd2d0
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
cmpq %r12, %rax
je 0x1e604
movl (%r13,%r12,4), %ecx
movl %ecx, (%r14,%r12,4)
incq %r12
jmp 0x1e5f1
xorl %ecx, %ecx
leal 0x1(%rcx), %r15d
movslq %ecx, %rax
movq %rax, 0x18(%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x28(%rsp)
movq %rcx, 0x30(%rsp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
shrq $0x3, %rax
decl %eax
cmpl %eax, %ecx
jge 0x1e7fa
movq %r14, %rbp
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movl (%rax), %r14d
movq %rbx, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0xcfc8
movq (%rax), %rax
subl (%rax), %r14d
cmpl 0x14(%rsp), %r14d
jge 0x1e7ef
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcfc8
movq (%rax), %rax
cmpl $0x0, 0x4(%rax)
je 0x1e6ec
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcfc8
movq (%rax), %rax
cmpl $0x1, 0x4(%rax)
movq %rbp, %r14
jne 0x1e738
cmpb $0x1, 0x16b27(%rip)     # 0x351cb
movq 0x18(%rsp), %r12
je 0x1e7a9
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcfc8
movq (%rax), %rax
movslq (%rax), %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movslq (%rax), %rax
cmpq %rax, %r12
jg 0x1e738
cmpl $0x0, (%r13,%r12,4)
js 0x1ea05
orl $-0x1, (%r14,%r12,4)
incq %r12
jmp 0x1e6c0
cmpb $0x1, 0x16ad8(%rip)     # 0x351cb
movq %rbp, %r14
je 0x1e763
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcfc8
movq (%rax), %rax
movslq (%rax), %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movslq (%rax), %rax
cmpq %rax, %r12
jg 0x1e738
cmpl $0x0, (%r13,%r12,4)
jg 0x1ea05
movl $0x1, (%r14,%r12,4)
incq %r12
jmp 0x1e709
movq (%rbx), %rsi
movq 0x28(%rsp), %r12
addq %r12, %rsi
movq %rbx, %rdi
callq 0x1f324
movq (%rbx), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x1f324
movq 0x30(%rsp), %rcx
jmp 0x1e624
movq 0x16876(%rip), %rax     # 0x34fe0
movq (%rax), %rbp
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcfc8
movq (%rax), %rax
movl (%rax), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movl (%rax), %ecx
movq %rbp, %rdi
leaq 0x9d66(%rip), %rsi      # 0x28500
movl (%rsp), %edx
xorl %eax, %eax
callq 0x2240
jmp 0x1e6f8
movq 0x16830(%rip), %rax     # 0x34fe0
movq (%rax), %rbp
movq %rbx, %rdi
movq %r12, %rsi
callq 0xcfc8
movq (%rax), %rax
movl (%rax), %eax
movl %eax, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movl (%rax), %ecx
movq %rbp, %rdi
leaq 0x9d85(%rip), %rsi      # 0x28565
movl (%rsp), %edx
xorl %eax, %eax
callq 0x2240
jmp 0x1e6af
movl %r15d, %ecx
movq %rbp, %r14
jmp 0x1e606
cmpl $0x0, (%r14)
movq 0x70(%rsp), %r13
jne 0x1e8fb
movl 0x4(%rsp), %eax
movslq %eax, %r15
cmpl $0x0, -0x4(%r14,%r15,4)
jne 0x1e96e
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x253(%rip), %rdx       # 0x1ea7d
callq 0xd2d0
xorl %r15d, %r15d
movq 0x20(%rsp), %r14
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r15
jge 0x1e89a
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movl 0x4(%rax), %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
cmpl $0x1, %ebp
jne 0x1e876
movq %r14, %rdi
jmp 0x1e88d
movq (%rax), %rax
cmpl $0x0, 0x4(%rax)
jne 0x1e895
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq %r13, %rdi
movq %rax, %rsi
callq 0x1f35e
incq %r15
jmp 0x1e837
xorl %r14d, %r14d
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r14
jge 0x1e8ec
testq %r14, %r14
je 0x1e8e7
leaq -0x1(%r14), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movl 0x4(%rax), %ebp
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcfc8
movq (%rax), %rax
cmpl 0x4(%rax), %ebp
je 0x1ea28
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
incq %r14
jmp 0x1e89d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x166de(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x9c7f(%rip), %rdi      # 0x2858b
pushq $0x2e
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
pushq $0x30
popq %rdi
callq 0x2170
movq %rax, %r15
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movq %rax, 0x8(%rsp)
movl $0x1, 0x4(%rax)
movups %xmm0, 0x10(%rax)
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
callq 0xcfc8
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, 0x8(%r15)
cmpl $0x0, (%r14)
jle 0x1e9e9
pushq $0x1
popq %r12
movl %r12d, 0x4(%r15)
jmp 0x1e9ef
decl %eax
decq %r15
movl %eax, %ebp
movq 0x16664(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x9c34(%rip), %rdi      # 0x285ba
pushq $0x2d
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
pushq $0x30
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movq %rax, 0x8(%rsp)
movl %ebp, (%rax)
movl $0x1, 0x4(%rax)
movups %xmm0, 0x10(%rax)
movq 0x8(%rbx), %rcx
movq -0x8(%rcx), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
cmpl $0x0, (%r14,%r15,4)
jle 0x1e9d2
andl $0x0, 0x4(%rax)
andl $0x0, (%r14,%r15,4)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x1f35e
jmp 0x1e81c
js 0x1e965
andl $0x0, (%r14)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x1f35e
jmp 0x1e809
movq 0x165d4(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x9b10(%rip), %rdi      # 0x28526
pushq $0x3e
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
xorl %edi, %edi
callq 0x21d0
movq 0x165b1(%rip), %rax     # 0x34fe0
movq (%rax), %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcfc8
movq (%rax), %rax
movl (%rax), %ebp
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcfc8
movq (%rax), %rax
movl (%rax), %r15d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcfc8
movq (%rax), %rax
movl 0x4(%rax), %r8d
leaq 0x9b7c(%rip), %rsi      # 0x285e8
movq %r12, %rdi
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2240
jmp 0x1ea21