// File: /workspace/github_cpp_makefile_star2_O3/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, 0x28(%rsp)
movl %r8d, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movl $0x18, %edi
callq 0x2180
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq (%r15), %rdx
movq 0x8(%r15), %rcx
movq %rax, %rdi
xorl %esi, %esi
callq 0x24e26
movq 0x8(%r12), %rsi
movq (%r14), %rdx
movq 0x8(%r14), %rcx
movq %r12, %rdi
callq 0x24e26
movq (%r12), %r14
movq 0x8(%r12), %r15
cmpq %r15, %r14
movq %r12, 0x10(%rsp)
je 0x23c3b
movq %r15, %rbx
subq %r14, %rbx
movq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x6c2(%rip), %rcx       # 0x241d1
movq %r14, %rdi
movq %r15, %rsi
callq 0x10afb
leaq 0x8(%r14), %r13
cmpq $0x81, %rbx
jl 0x23bdb
movq %r15, 0x20(%rsp)
movl $0x8, %ebp
movq %r13, %r15
movq %r14, %rbx
movq %rbx, %rcx
leaq (%r14,%rbp), %rbx
movq (%r14,%rbp), %r12
movq (%r14), %rdx
movl (%r12), %eax
cmpl (%rdx), %eax
jge 0x23b64
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r14, %rcx
jmp 0x23b80
movq (%rcx), %rdx
movq %rbx, %rcx
cmpl (%rdx), %eax
jge 0x23b80
movq %r15, %rcx
movq %rdx, (%rcx)
movq -0x10(%rcx), %rdx
addq $-0x8, %rcx
cmpl (%rdx), %eax
jl 0x23b71
movq %r12, (%rcx)
addq $0x8, %rbp
addq $0x8, %r15
cmpq $0x80, %rbp
jne 0x23b3b
subq $-0x80, %r14
movq 0x20(%rsp), %rdi
cmpq %rdi, %r14
movq 0x10(%rsp), %r12
je 0x23c3b
movq -0x8(%r14), %rcx
movq (%r14), %rax
movl (%rax), %edx
movq %r14, %rsi
cmpl (%rcx), %edx
jge 0x23bcd
movq %r14, %rsi
movq %rcx, (%rsi)
movq -0x10(%rsi), %rcx
addq $-0x8, %rsi
cmpl (%rcx), %edx
jl 0x23bbe
movq %rax, (%rsi)
addq $0x8, %r14
cmpq %rdi, %r14
jne 0x23bab
jmp 0x23c3b
cmpq %r15, %r13
je 0x23c3b
movq %r15, %rbp
movq %r14, %r15
movq %r15, %rdi
movq %r13, %r15
movq (%r13), %rbx
movq (%r14), %rcx
movl (%rbx), %eax
cmpl (%rcx), %eax
jge 0x23c13
movq %r15, %rdx
subq %r14, %rdx
subq %rdx, %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x22b0
movq %r14, %rcx
jmp 0x23c2f
movq (%rdi), %rdx
movq %r15, %rcx
cmpl (%rdx), %eax
jge 0x23c2f
movq %r15, %rcx
movq %rdx, (%rcx)
movq -0x10(%rcx), %rdx
addq $-0x8, %rcx
cmpl (%rdx), %eax
jl 0x23c20
movq %rbx, (%rcx)
leaq 0x8(%r15), %r13
cmpq %rbp, %r13
jne 0x23be6
movq 0x78(%rsp), %r13
movl 0x18(%rsp), %eax
testl %eax, %eax
movq 0x8(%rsp), %r10
jle 0x23c62
movl %eax, %eax
xorl %ecx, %ecx
movl (%r10,%rcx,4), %edx
movl %edx, (%r13,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x23c51
movq %r12, %rbx
addq $0x8, %rbx
movq (%r12), %rdi
movq 0x8(%r12), %rdx
movq %rdx, %rax
subq %rdi, %rax
sarq $0x3, %rax
cmpl $0x2, %eax
movl 0x1c(%rsp), %r12d
jl 0x23e67
xorl %ebp, %ebp
leal 0x1(%rbp), %esi
movslq %esi, %r15
cmpq %r15, %rax
jbe 0x2419d
movslq %ebp, %r14
cmpq %r14, %rax
jbe 0x24191
movq (%rdi,%r15,8), %rax
movl (%rax), %ecx
movq (%rdi,%r14,8), %r8
movl (%r8), %eax
movl %ecx, %r9d
subl %eax, %r9d
cmpl %r12d, %r9d
jge 0x23d31
movl 0x4(%r8), %esi
cmpl $0x1, %esi
je 0x23d38
testl %esi, %esi
jne 0x23d93
cmpb $0x1, 0x154fc(%rip)     # 0x391d3
je 0x23e07
movq %rdx, %rax
subq %rdi, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x24191
cmpq %r15, %rax
jbe 0x2419d
movq (%rdi,%r14,8), %rax
movslq (%rax), %rax
movq (%rdi,%r15,8), %rcx
cmpl (%rcx), %eax
jg 0x23d93
decq %rax
cmpl $0x0, 0x4(%r10,%rax,4)
jg 0x24187
movl $0x1, 0x4(%r13,%rax,4)
movslq (%rcx), %rsi
incq %rax
cmpq %rsi, %rax
jl 0x23d0f
jmp 0x23d93
movl %esi, %ebp
jmp 0x23df0
cmpb $0x1, 0x15494(%rip)     # 0x391d3
je 0x23e37
movq %rdx, %rax
subq %rdi, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x24191
cmpq %r15, %rax
jbe 0x2419d
movq (%rdi,%r14,8), %rax
movslq (%rax), %rax
movq (%rdi,%r15,8), %rcx
cmpl (%rcx), %eax
jg 0x23d93
decq %rax
cmpl $0x0, 0x4(%r10,%rax,4)
js 0x2418c
movl $0xffffffff, 0x4(%r13,%rax,4) # imm = 0xFFFFFFFF
movslq (%rcx), %rsi
incq %rax
cmpq %rsi, %rax
jl 0x23d73
leaq (%rdi,%r14,8), %rsi
addq $0x8, %rsi
cmpq %rdx, %rsi
je 0x23db8
leaq (%rdi,%r14,8), %rdi
subq %rsi, %rdx
callq 0x22b0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rdx
addq $-0x8, %rdx
movq %rdx, (%rbx)
leaq (%rdi,%r14,8), %rsi
addq $0x8, %rsi
cmpq %rdx, %rsi
je 0x23de4
leaq (%rdi,%r14,8), %rdi
subq %rsi, %rdx
callq 0x22b0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rdx
movq 0x8(%rsp), %r10
addq $-0x8, %rdx
movq %rdx, (%rbx)
movq %rdx, %rax
subq %rdi, %rax
sarq $0x3, %rax
leal -0x1(%rax), %ecx
cmpl %ecx, %ebp
jl 0x23c8c
jmp 0x23e67
movq 0x151d2(%rip), %rdx     # 0x38fe0
movq (%rdx), %rdi
leaq 0xa6e8(%rip), %rsi      # 0x2e500
movl %eax, %edx
xorl %eax, %eax
callq 0x2250
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rdx
jmp 0x23cdd
movq 0x151a2(%rip), %rdx     # 0x38fe0
movq (%rdx), %rdi
leaq 0xa71d(%rip), %rsi      # 0x2e565
movl %eax, %edx
xorl %eax, %eax
callq 0x2250
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rdx
jmp 0x23d45
cmpl $0x0, (%r13)
movq 0x10(%rsp), %rbp
jne 0x240da
movl 0x18(%rsp), %r14d
movslq %r14d, %r15
cmpl $0x0, -0x4(%r13,%r15,4)
jne 0x240ed
movq (%rbp), %r14
movq 0x8(%rbp), %r15
cmpq %r15, %r14
je 0x23fe6
movq %r15, %rbx
subq %r14, %rbx
movq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x314(%rip), %rcx       # 0x241d1
movq %r14, %rdi
movq %r15, %rsi
callq 0x10afb
leaq 0x8(%r14), %r12
cmpq $0x81, %rbx
jl 0x23f89
movq %r15, 0x8(%rsp)
movl $0x8, %r13d
movq %r12, %rbp
movq %r14, %rbx
movq %rbx, %rcx
leaq (%r14,%r13), %rbx
movq (%r14,%r13), %r15
movq (%r14), %rdx
movl (%r15), %eax
cmpl (%rdx), %eax
jge 0x23f12
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x22b0
movq %r14, %rcx
jmp 0x23f2e
movq (%rcx), %rdx
movq %rbx, %rcx
cmpl (%rdx), %eax
jge 0x23f2e
movq %rbp, %rcx
movq %rdx, (%rcx)
movq -0x10(%rcx), %rdx
addq $-0x8, %rcx
cmpl (%rdx), %eax
jl 0x23f1f
movq %r15, (%rcx)
addq $0x8, %r13
addq $0x8, %rbp
cmpq $0x80, %r13
jne 0x23eea
subq $-0x80, %r14
movq 0x8(%rsp), %rdi
cmpq %rdi, %r14
movq 0x10(%rsp), %rbp
je 0x23fe6
movq -0x8(%r14), %rcx
movq (%r14), %rax
movl (%rax), %edx
movq %r14, %rsi
cmpl (%rcx), %edx
jge 0x23f7b
movq %r14, %rsi
movq %rcx, (%rsi)
movq -0x10(%rsi), %rcx
addq $-0x8, %rsi
cmpl (%rcx), %edx
jl 0x23f6c
movq %rax, (%rsi)
addq $0x8, %r14
cmpq %rdi, %r14
jne 0x23f59
jmp 0x23fe6
cmpq %r15, %r12
je 0x23fe6
movq %r14, %r13
movq %r13, %rdi
movq %r12, %r13
movq (%r12), %rbx
movq (%r14), %rcx
movl (%rbx), %eax
cmpl (%rcx), %eax
jge 0x23fbe
movq %r13, %rdx
subq %r14, %rdx
subq %rdx, %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x22b0
movq %r14, %rcx
jmp 0x23fda
movq (%rdi), %rdx
movq %r13, %rcx
cmpl (%rdx), %eax
jge 0x23fda
movq %r13, %rcx
movq %rdx, (%rcx)
movq -0x10(%rcx), %rdx
addq $-0x8, %rcx
cmpl (%rdx), %eax
jl 0x23fcb
movq %rbx, (%rcx)
leaq 0x8(%r13), %r12
cmpq %r15, %r12
jne 0x23f91
movq (%rbp), %rdx
movq 0x8(%rbp), %rax
subq %rdx, %rax
sarq $0x3, %rax
testl %eax, %eax
jle 0x240cb
movq 0x70(%rsp), %r15
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x28(%rsp), %rbx
cmpq %r14, %rax
jbe 0x24191
movq (%rdx,%r12), %rax
addq %r12, %rdx
movl 0x4(%rax), %ecx
testl %ecx, %ecx
je 0x2403d
cmpl $0x1, %ecx
jne 0x2405e
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x24051
movq %rax, (%rsi)
addq $0x8, 0x8(%rbx)
jmp 0x2405e
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x24056
movq %rax, (%rsi)
addq $0x8, 0x8(%r15)
jmp 0x2405e
movq %rbx, %rdi
jmp 0x24059
movq %r15, %rdi
callq 0x25042
incq %r14
movq (%rbp), %rdx
movq 0x8(%rbp), %rcx
subq %rdx, %rcx
movq %rcx, %rax
sarq $0x3, %rax
shrq $0x3, %rcx
movslq %ecx, %rcx
addq $0x8, %r12
cmpq %rcx, %r14
jl 0x2400d
testl %eax, %eax
jle 0x240cb
movl %eax, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x240c3
leaq -0x1(%rcx), %rsi
cmpq %rsi, %rax
jbe 0x241b5
cmpq %rcx, %rax
jbe 0x241a9
movq -0x8(%rdx,%rcx,8), %r8
movq (%rdx,%rcx,8), %r9
movl 0x4(%r8), %r8d
cmpl 0x4(%r9), %r8d
je 0x241c6
incq %rcx
cmpq %rcx, %rdi
jne 0x24091
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x545f
jmp 0x23e77
decl %r14d
decq %r15
movq 0x14ee6(%rip), %rax     # 0x38fe0
movq (%rax), %rcx
leaq 0xa4b6(%rip), %rdi      # 0x2e5ba
movl $0x2d, %esi
movl $0x1, %edx
callq 0x2340
movl $0x30, %edi
callq 0x2180
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movq %rax, 0x30(%rsp)
movl %r14d, (%rax)
movl $0x1, 0x4(%rax)
movups %xmm0, 0x10(%rax)
movq (%rbx), %rsi
movq -0x8(%rsi), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
cmpl $0x0, (%r13,%r15,4)
jle 0x2415a
movl $0x0, 0x4(%rax)
movl $0x0, (%r13,%r15,4)
cmpq 0x10(%rbp), %rsi
je 0x24175
movq %rax, (%rsi)
addq $0x8, (%rbx)
jmp 0x23e8b
leaq 0x30(%rsp), %rdx
movq %rbp, %rdi
callq 0x25042
jmp 0x23e8b
callq 0x543b
callq 0x5417
leaq 0x8738(%rip), %rdi      # 0x2c8d0
movq %r14, %rsi
jmp 0x241bc
leaq 0x872c(%rip), %rdi      # 0x2c8d0
movq %r15, %rsi
jmp 0x241bc
leaq 0x8720(%rip), %rdi      # 0x2c8d0
movq %rcx, %rsi
jmp 0x241bc
leaq 0x8714(%rip), %rdi      # 0x2c8d0
movq %rax, %rdx
xorl %eax, %eax
callq 0x2270
movq %rbp, %rdi
movq %rcx, %rdx
callq 0x5518