// File: /workspace/github_cpp_makefile_star2_O1/gersteinlab[P]MUSIC/src/ms_filter_utils.cpp
// Function: get_filtered_maxima_regions_multiscale_filtered_data(double*, int, double, double, double, std::vector<double, std::allocator<double>>*, std::vector<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::allocator<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*>>*, double)
void get_filtered_maxima_regions_multiscale_filtered_data(double* track_data, 
														int l_track_data, 
														double scale_start, double scale_end, double log_scale_step, 
														vector<double>* scales_per_i_scale,
														vector<vector<t_annot_region*>*>* per_scale_minima_regions,
														double min_allowed_filtered_value_scale)
{
	// Start from the first scale, go over all the scales and filter the data.
	double scale = 1.0 / log_scale_step;
	int i_scale = 0;

	// Define where which quantile we set as the signal.
	double quantile = 0.5;

	FILE* f_feature_signal_vals = NULL;

if(__DUMP_FILTER_MSGS__)
	f_feature_signal_vals = open_f("features_signal_values.bed", "w");

	while(1)
	{
		scale *= log_scale_step;

		scales_per_i_scale->push_back(scale);

		// Are we going to process a scale within the requested limits? If not, we will skip this computation to save time.
		if(scale < scale_start)
		{
			i_scale++;
			per_scale_minima_regions->push_back(new vector<t_annot_region*>());
			continue;
		}
		else
		{
//if(__DUMP_FILTER_MSGS__)
//{
			//fprintf(stderr, "Processing scale %.2f (%.2f)\n", scale, scale_end);
			fprintf(stderr, "Generating ERs @ scale with %d/%d bins.                      \r", (int)scale, (int)scale_end);
//}

			// Get the filter for the current scale: The gaussian filter.
			int int_scale = (int)(scale);
			int l_averaging_win = ((int_scale % 2) == 1)?(int_scale-1):(int_scale);

			// Do copying from start to end.
			double* cur_filtered_track = new double[l_track_data + 2];
			memset(cur_filtered_track, 0, (l_track_data+1)*sizeof(double));

			int n_signal_wins = 0;
			int half_l_averaging = l_averaging_win / 2;

			// Set the maximum value for the histogram.
			int MAX_VAL = -1000*1000;
			int MIN_VAL = 1000*1000;
			for(int i_sig = 1; i_sig <= l_track_data; i_sig++)
			{
				if(MAX_VAL < (int)(track_data[i_sig]))
				{
					MAX_VAL = (int)(track_data[i_sig]);
				}

				if(MIN_VAL > (int)(track_data[i_sig]))
				{
					MIN_VAL = (int)(track_data[i_sig]);
				}
			} // i_sig loop.

			// Make sure that this is the maximum.
			MAX_VAL += 100;
			MIN_VAL -= 100;
		
			// Initialize the current pdf.
			int* cur_win_pdf = NULL;
			int cur_win_max = 0;
			int cur_win_min = 1000*1000;
			double* cur_win_vals = new double[l_averaging_win + 2];
			int prev_avg_start = 0;
			int prev_avg_end = 0;

			// Go over all the positions as the middle of the filtering window: This must be 1 based.
			for(int cur_win_mid = 1; cur_win_mid <= l_track_data; cur_win_mid++)
			{
				int cur_avg_start = (cur_win_mid > half_l_averaging)?(cur_win_mid - half_l_averaging):(1);
				int cur_avg_end = (cur_win_mid + half_l_averaging <= l_track_data)?(cur_win_mid + half_l_averaging):(l_track_data);
				if(cur_win_pdf == NULL)
				{
					cur_win_pdf = new int[MAX_VAL - MIN_VAL+ 2];
					memset(cur_win_pdf, 0, sizeof(int) * (MAX_VAL - MIN_VAL + 1));
					cur_win_pdf -= MIN_VAL;

					// Generate the pdf, get the minimum and maximum in the current window.
					for(int i = cur_avg_start; i <= cur_avg_end; i++)
					{
						cur_win_pdf[(int)(track_data[i])]++;

						if(cur_win_max < track_data[i])
						{
							cur_win_max = (int)track_data[i];
						}
					
						if(cur_win_min > track_data[i])
						{
							cur_win_min = (int)track_data[i];
						}
					} // i loop.
				} // cur_win_pdf is NULL check.
				else
				{
					// Remove the old values from the pdf, add the new values.
					for(int i = prev_avg_start; i < cur_avg_start; i++)
					{
						cur_win_pdf[(int)(track_data[i])]--;
					} // i loop.

					// Update the min and max only for the values that are new in this window.
					for(int i = prev_avg_end+1; i <= cur_avg_end; i++)
					{
						cur_win_pdf[(int)(track_data[i])]++;

						if(cur_win_max < track_data[i])
						{
							cur_win_max = (int)track_data[i];
						}
					
						if(cur_win_min > track_data[i])
						{
							cur_win_min = (int)track_data[i];
						}
					}

					// Sanity Check: The total # of points must be equal to the window length.
					int n_total_pts = 0;
					for(int i = cur_win_min; i <= cur_win_max; i++)
					{
						n_total_pts += cur_win_pdf[i];
					} // i loop.

					// Sanity check on the number of points to be processed in the current window.
					if(n_total_pts != (cur_avg_end - cur_avg_start + 1))
					{
						fprintf(stderr, "Sanity check failed at %d-%d (%d-%d): %d, %d\n", cur_avg_start, cur_avg_end, cur_win_min, cur_win_max, n_total_pts, (cur_avg_end - cur_avg_start + 1));

						for(int i = cur_win_min; i <= cur_win_max; i++)
						{
							fprintf(stderr, "%d ", cur_win_pdf[i]);
						} // i loop.

						FILE* f_op = open_f("op.txt", "w");
						for(int i_sig = cur_avg_start; i_sig <= cur_avg_end; i_sig++)
						{
							fprintf(f_op, "%lf ", track_data[i_sig]);
						} // i_sig loop.
						fclose(f_op);

						exit(0);
					}
				} // cur_win_pdf is NULL check.

				// At this point, the histogram is updated, now must find the median for the histogram.
				int n_total = 0;
				int cur_win_median = 0;
				for(int i = cur_win_min; i <= cur_win_max; i++)
				{
					n_total += cur_win_pdf[i];

					if(n_total > (cur_avg_end - cur_avg_start + 1)*quantile)
					{
						// We found the median, can break out of the loop.
						cur_win_median = i;
						break;
					}
				} // i loop.

				// Track the minimum and maximum from the histogram to update them for the current window.
				int updated_win_min = 0;			
				for(int i = cur_win_min; i <= cur_win_max; i++)
				{
					if(cur_win_pdf[i] > 0)
					{
						updated_win_min = i;
						break;
					}
				} // i loop.

				int updated_win_max = 0;
				for(int i = cur_win_max; i >= cur_win_min; i--)
				{
					if(cur_win_pdf[i] > 0)
					{
						updated_win_max = i;
						break;
					}
				} // i loop.

				// Set the median.
				//int median_per_pdf = cur_win_median;
				cur_filtered_track[cur_win_mid] = cur_win_median;

				// Update the previous averaging window limits.
				prev_avg_start = cur_avg_start;
				prev_avg_end = cur_avg_end;
				cur_win_min = updated_win_min;
				cur_win_max = updated_win_max;

#undef _QSORT_CHECK_
#ifdef _QSORT_CHECK_
				// Get the median via qsort and compare as a sanity check.
				for(int i = cur_avg_start; i <= cur_avg_end; i++)
				{
					cur_win_vals[i-cur_avg_start] = track_data[i];
				} // i loop.
				qsort(cur_win_vals, l_averaging_win, sizeof(double), sort_doubles_descending);
				//per_win_profile[n_signal_wins] = cur_win_vals[l_averaging_win/2];

				if(cur_win_vals[l_averaging_win/2] != median_per_pdf)
				{
					fprintf(stderr, "Medians do not match: %d, %d:\n", (int)cur_win_vals[l_averaging_win/2], median_per_pdf);
					for(int i = cur_avg_start; i <= cur_avg_end; i++)
					{
						fprintf(stderr, "%lf ", track_data[i]);
					} // i loop.
					fprintf(stderr, "\n");
					getc(stdin);
				}
#endif // _QSORT_CHECK_

				n_signal_wins++;
			} // main signal filtering loop.

			// Free pdf memory.
			delete [] cur_win_vals;
			delete [] (cur_win_pdf+MIN_VAL);

if(__DUMP_FILTER_MSGS__)
{
			fprintf(stderr, "Getting the extrema regions.\n");
}

			// Get the extrema regions for the current filtered regions.
			vector<t_extrema_node*>* maxima = new vector<t_extrema_node*>();
			vector<t_extrema_node*>* minima = new vector<t_extrema_node*>();
			int* derivative_map = new int[l_track_data+2];
			memset(derivative_map, 0, sizeof(int) * (l_track_data+2));
			get_extrema_per_plateaus(cur_filtered_track, l_track_data, maxima, minima, derivative_map, 0);

			// Sort the extremas.
			sort(minima->begin(), minima->end(), sort_extremas_per_posn);
			sort(maxima->begin(), maxima->end(), sort_extremas_per_posn);

			// Get the minima regions.
			vector<t_annot_region*>* cur_scale_minima_regions = new vector<t_annot_region*>();

if(__DUMP_FILTER_MSGS__)
{
			fprintf(stderr, "Generating filtered minima regions from %d minima.\n", (int)minima->size());
}

			for(int i_m = 0; 
				minima->size() > 0 &&
				i_m < (int)minima->size()-1; 
				i_m++)
			{
				t_annot_region* new_minima = get_empty_region();
				new_minima->chrom = t_string::copy_me_str("XX");
				//new_minima->start = translate_coord(minima->at(i_m)->extrema_posn+1, CODEBASE_COORDS::start_base, BED_COORDS::start_base);
				//new_minima->end = translate_coord(minima->at(i_m+1)->extrema_posn+1, CODEBASE_COORDS::end_base, BED_COORDS::end_base);
				new_minima->start = minima->at(i_m)->extrema_posn;
				new_minima->end = minima->at(i_m+1)->extrema_posn;
				new_minima->strand = '+';

				// The maximum in the original signal is found within the effective data that contributes to the filtering process.
				int i_eff_data_start = (1 > new_minima->start-(int)scale/2)?(1):(new_minima->start-(int)scale/2);
				int i_eff_data_end = (l_track_data < new_minima->end+scale/2)?(l_track_data):(new_minima->end+(int)scale/2);
				double original_max = 0.0;
				for(int i = i_eff_data_start; i <= i_eff_data_end; i++)
				{
					if(original_max < track_data[i])
					{
						original_max = track_data[i];
					}
				}

				double filtered_max = 0.0;
				for(int i = new_minima->start; i <= new_minima->end; i++)
				{
					if(filtered_max < cur_filtered_track[i])
					{
						filtered_max = cur_filtered_track[i];
					}
				} // i loop.

if(__DUMP_FILTER_MSGS__)
{
				fprintf(f_feature_signal_vals, "XX\t%d\t%d\t%lf\t%lf\t%lf\n", new_minima->start, new_minima->end, filtered_max, original_max, scale);
}

				// This is where the filtering is done.
				if(filtered_max >= original_max / min_allowed_filtered_value_scale)
				{
					cur_scale_minima_regions->push_back(new_minima);
				}
				else
				{					
					delete new_minima;
				}
			} // i_min loop.

if(__DUMP_FILTER_MSGS__)
{
			fprintf(stderr, "%ld minima regions are generated.\n", cur_scale_minima_regions->size());
}

			per_scale_minima_regions->push_back(cur_scale_minima_regions);

			delete_extrema_nodes(minima);
			delete_extrema_nodes(maxima);

if(__DUMP_FILTER_MSGS__)
{
			// Dump the current filtered track.
			char cur_filtered_data_op_fp[1000];
			sprintf(cur_filtered_data_op_fp, "filtered_%lf.bin", scale);
			dump_per_nucleotide_binary_profile(cur_filtered_track, l_track_data, cur_filtered_data_op_fp);
}

			// Free the current decomposition.
			delete [] derivative_map;
			delete [] cur_filtered_track;

			// Was the last processed value equal to or larger than what was requested?
			if(scale_end > 0 && 
				scale >= scale_end)
			{
				break;
			}

			i_scale++;
		} // scale computation check.
	} // scale loop.
	fprintf(stderr, "Generated SSERs for %d scales.                                \n", (int)scales_per_i_scale->size());

if(__DUMP_FILTER_MSGS__)
		fclose(f_feature_signal_vals);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movsd %xmm3, 0xc8(%rsp)
movq %rcx, 0x78(%rsp)
movq %rdx, %r12
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0xb0(%rsp)
movl %esi, 0x18(%rsp)
movq %rdi, %rbx
movsd 0x274ba(%rip), %xmm0    # 0x2b008
movsd %xmm2, 0xb8(%rsp)
divsd %xmm2, %xmm0
movsd %xmm0, 0x20(%rsp)
cmpb $0x1, 0x36665(%rip)     # 0x3a1cd
jne 0x3b82
leaq 0x2abfe(%rip), %rdi     # 0x2e76f
leaq 0x27f36(%rip), %rsi     # 0x2baae
callq 0x26387
movq %rax, %rbp
jmp 0x3b84
xorl %ebp, %ebp
cvttsd2si 0x58(%rsp), %eax
movl %eax, 0x6c(%rsp)
movslq 0x18(%rsp), %rax
leal 0x1(%rax), %ecx
movq %rcx, 0x48(%rsp)
movslq %ecx, %rcx
shlq $0x3, %rcx
movq %rcx, 0xa0(%rsp)
cmpl $-0x2, %eax
leaq 0x10(,%rax,8), %rcx
pushq $-0x1
popq %rdx
cmovlq %rdx, %rcx
movq %rcx, 0x98(%rsp)
leaq 0x8(,%rax,4), %rcx
movq %rcx, 0x88(%rsp)
cmovlq %rdx, %rcx
movq %rcx, 0x80(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0x363ec(%rip), %r14     # 0x39fe0
movq %r12, 0x90(%rsp)
movq %rbp, 0x70(%rsp)
movsd 0x20(%rsp), %xmm0
mulsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
jne 0x3c31
movq %r12, %rdi
leaq 0x20(%rsp), %rdx
callq 0xb836
jmp 0x3c3e
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r12)
movsd 0x20(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3c87
pushq $0x18
popq %rdi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rax, 0xd0(%rsp)
movq 0x78(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x15668
jmp 0x3c01
movq (%r14), %rdi
cvttsd2si %xmm0, %edx
leaq 0x2aaf5(%rip), %rsi     # 0x2e78a
movl 0x6c(%rsp), %ecx
xorl %eax, %eax
callq 0x2250
cvttsd2si 0x20(%rsp), %ebp
movl %ebp, %eax
andl $0x80000001, %eax       # imm = 0x80000001
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
subl %ecx, %ebp
movq 0x98(%rsp), %rdi
callq 0x2040
movq %rax, 0x38(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq 0xa0(%rsp), %rdx
callq 0x20f0
movl %ebp, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
cmpl $0x0, 0x18(%rsp)
jg 0x3cfb
movl $0xfff0bdc0, %ecx       # imm = 0xFFF0BDC0
movl $0xf4240, %edx          # imm = 0xF4240
movq 0x48(%rsp), %r9
jmp 0x3d26
movl $0xf4240, %edi          # imm = 0xF4240
movl $0xfff0bdc0, %ecx       # imm = 0xFFF0BDC0
pushq $0x1
popq %rsi
movq 0x48(%rsp), %r9
cvttsd2si (%rbx,%rsi,8), %edx
cmpl %edx, %ecx
cmovlel %edx, %ecx
cmpl %edx, %edi
cmovll %edi, %edx
incq %rsi
movl %edx, %edi
cmpq %rsi, %r9
jne 0x3d0d
leal -0x64(%rdx), %esi
movslq %esi, %rdi
cmpl $0x0, 0x18(%rsp)
movq %rdi, 0xa8(%rsp)
jg 0x3d43
xorl %r14d, %r14d
jmp 0x3fab
subl %edx, %ecx
leal 0xc8(%rcx), %edx
leal 0xca(%rcx), %esi
movslq %esi, %rsi
shlq $0x2, %rsi
cmpl $-0x2, %edx
pushq $-0x1
popq %rdx
cmovlq %rdx, %rsi
movq %rsi, 0x30(%rsp)
addl $0xc9, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
movq %rcx, 0x40(%rsp)
negq %rdi
movq %rdi, 0x60(%rsp)
cltq
movq %rax, 0x28(%rsp)
movl $0xf4240, %r13d         # imm = 0xF4240
xorl %r14d, %r14d
pushq $0x1
popq %r15
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %r11d, %r11d
movl %r8d, %ecx
movl %r15d, %r8d
movq 0x28(%rsp), %rsi
subl %esi, %r8d
cmpq %rsi, %r15
pushq $0x1
popq %rdx
cmovlel %edx, %r8d
movq %r15, 0x10(%rsp)
addl %r15d, %esi
movl 0x18(%rsp), %edx
cmpl %edx, %esi
cmovgel %edx, %esi
movslq %r8d, %r15
movslq %esi, %rbp
testq %r14, %r14
movl %esi, 0x8(%rsp)
je 0x3e74
movl %r11d, %eax
cmpl %r8d, %ecx
jge 0x3df9
movslq %ecx, %rcx
cvttsd2si (%rbx,%rcx,8), %edx
movslq %edx, %rdx
decl (%r14,%rdx,4)
incq %rcx
cmpq %rcx, %r15
jne 0x3de5
cmpl %esi, %eax
jge 0x3e32
cltq
movsd 0x8(%rbx,%rax,8), %xmm0
incq %rax
cvttsd2si %xmm0, %ecx
movslq %ecx, %rcx
incl (%r14,%rcx,4)
cvtsi2sd %r12d, %xmm1
ucomisd %xmm1, %xmm0
cmoval %ecx, %r12d
cvtsi2sd %r13d, %xmm1
ucomisd %xmm0, %xmm1
cmoval %ecx, %r13d
cmpq %rbp, %rax
jl 0x3dff
xorl %r10d, %r10d
movslq %r13d, %rdi
cmpl %r12d, %r13d
jg 0x3e58
leaq (%r14,%rdi,4), %rax
movl %r12d, %ecx
subl %edi, %ecx
incl %ecx
xorl %edx, %edx
xorl %r10d, %r10d
addl (%rax,%rdx,4), %r10d
incq %rdx
cmpl %edx, %ecx
jne 0x3e4d
movl %esi, %r11d
subl %r8d, %r11d
incl %r11d
cmpl %r11d, %r10d
jne 0x4732
movl %r13d, %ecx
movl 0x8(%rsp), %r11d
jmp 0x3ee9
movq 0x30(%rsp), %rdi
movl %r8d, (%rsp)
callq 0x2040
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x20f0
movl 0x8(%rsp), %r11d
movl (%rsp), %r8d
movq 0x60(%rsp), %rax
leaq (%r14,%rax,4), %r14
cmpl %r8d, %r11d
jl 0x3ee1
decq %r15
movsd 0x8(%rbx,%r15,8), %xmm0
cvttsd2si %xmm0, %eax
cltq
incl (%r14,%rax,4)
cvtsi2sd %r12d, %xmm1
ucomisd %xmm1, %xmm0
cmoval %eax, %r12d
cvtsi2sd %r13d, %xmm1
ucomisd %xmm0, %xmm1
cmoval %eax, %r13d
incq %r15
cmpq %rbp, %r15
jl 0x3eae
movl %r13d, %ecx
movq 0x48(%rsp), %r9
xorpd %xmm0, %xmm0
movslq %ecx, %rax
cmpl %r12d, %ecx
movq 0x10(%rsp), %r15
jg 0x3f41
movl %r11d, %edx
movl %r8d, %r10d
subl %r8d, %edx
incl %edx
cvtsi2sd %edx, %xmm1
mulsd 0x27147(%rip), %xmm1    # 0x2b058
leal 0x1(%r12), %edx
leaq (%r14,%rax,4), %rsi
xorl %edi, %edi
movl %ecx, %r8d
addl (%rsi), %edi
cvtsi2sd %edi, %xmm2
ucomisd %xmm1, %xmm2
ja 0x3f39
incl %r8d
addq $0x4, %rsi
cmpl %r8d, %edx
jne 0x3f1f
jmp 0x3f3e
cvtsi2sd %r8d, %xmm0
movl %r10d, %r8d
xorl %edx, %edx
movl $0x0, %r13d
cmpl %r12d, %ecx
jg 0x3f6e
leal 0x1(%r12), %esi
leaq (%r14,%rax,4), %rdi
movl %ecx, %r13d
cmpl $0x0, (%rdi)
jg 0x3f6e
incl %r13d
addq $0x4, %rdi
cmpl %r13d, %esi
jne 0x3f5a
xorl %r13d, %r13d
cmpl %ecx, %r12d
jl 0x3f91
movslq %r12d, %rcx
incq %rcx
cmpl $0x0, -0x4(%r14,%rcx,4)
jg 0x3f8e
decl %r12d
decq %rcx
cmpq %rax, %rcx
jg 0x3f79
jmp 0x3f91
movl %r12d, %edx
movq 0x38(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
movl %edx, %r12d
cmpq %r9, %r15
jne 0x3d9e
testq %r14, %r14
je 0x3fc1
movq 0xa8(%rsp), %rax
leaq (%r14,%rax,4), %rdi
callq 0x2220
cmpb $0x1, 0x36205(%rip)     # 0x3a1cd
jne 0x3fe6
movq 0x3600f(%rip), %rax     # 0x39fe0
movq (%rax), %rcx
leaq 0x2a6ae(%rip), %rdi     # 0x2e689
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
callq 0x2340
pushq $0x18
popq %r14
movq %r14, %rdi
callq 0x2180
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %r14, %rdi
callq 0x2180
movq %rax, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x80(%rsp), %rdi
callq 0x2040
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq 0x88(%rsp), %rdx
callq 0x20f0
movq 0x38(%rsp), %rdi
movl 0x18(%rsp), %esi
movq %r15, 0x40(%rsp)
movq %r15, %rdx
movq %r13, %rcx
movq %r14, 0x60(%rsp)
movq %r14, %r8
xorl %r9d, %r9d
callq 0x24787
movq (%r13), %r15
movq 0x8(%r13), %r12
cmpq %r12, %r15
movq %r13, 0x28(%rsp)
jne 0x4313
movq 0x40(%rsp), %rax
movq (%rax), %r15
movq 0x8(%rax), %r12
cmpq %r12, %r15
movq 0x70(%rsp), %rbp
jne 0x4447
pushq $0x18
popq %rdi
callq 0x2180
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %rax, 0x50(%rsp)
cmpb $0x1, 0x3611c(%rip)     # 0x3a1cd
movq 0x35f28(%rip), %r14     # 0x39fe0
jne 0x40d7
movq (%r14), %rdi
movq 0x8(%r13), %rdx
subq (%r13), %rdx
shrq $0x3, %rdx
leaq 0x2a6f9(%rip), %rsi     # 0x2e7c9
xorl %eax, %eax
callq 0x2250
movq 0x8(%r13), %rax
movq (%r13), %rcx
subq %rcx, %rax
movq 0x90(%rsp), %r12
je 0x42c7
shrq $0x3, %rax
cmpl $0x2, %eax
jl 0x42c7
xorl %r15d, %r15d
callq 0x15990
movq %rax, 0xd0(%rsp)
leaq 0x2a5e6(%rip), %rdi     # 0x2e6fa
callq 0x273e0
movq 0xd0(%rsp), %rcx
movq %rax, (%rcx)
movq (%r13), %rax
movq 0x8(%r13), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x483b
movq (%rax,%r15,8), %rcx
movslq (%rcx), %r8
movq 0xd0(%rsp), %rsi
movl %r8d, 0xc(%rsi)
incq %r15
cmpq %r15, %rdx
jbe 0x483b
movq (%rax,%r15,8), %rax
movl (%rax), %ecx
movl %ecx, 0x20(%rsi)
movb $0x2b, 0x8(%rsi)
movsd 0x20(%rsp), %xmm2
cvttsd2si %xmm2, %edi
movl %edi, %eax
cltd
pushq $-0x2
popq %rsi
idivl %esi
movl %eax, %esi
addl %r8d, %esi
cmpl $0x2, %esi
pushq $0x1
popq %rax
cmovll %eax, %esi
cvtsi2sd %ecx, %xmm0
movapd %xmm2, %xmm1
mulsd 0x26ec0(%rip), %xmm1    # 0x2b058
addsd %xmm0, %xmm1
ucomisd 0xc0(%rsp), %xmm1
movl 0x18(%rsp), %eax
ja 0x41b5
movl %edi, %eax
cltd
pushq $0x2
popq %rdi
idivl %edi
addl %ecx, %eax
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
cmpl %eax, %esi
jg 0x41de
movl %esi, %edx
incl %eax
xorpd %xmm3, %xmm3
movsd (%rbx,%rdx,8), %xmm1
maxsd %xmm3, %xmm1
incq %rdx
movapd %xmm1, %xmm3
cmpq %rdx, %rax
jne 0x41c9
cmpl %ecx, %r8d
jg 0x420d
movq 0x38(%rsp), %rax
leaq (%rax,%r8,8), %rax
movl %ecx, %edx
subl %r8d, %edx
incl %edx
xorpd %xmm3, %xmm3
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm0
maxsd %xmm3, %xmm0
incq %rsi
movapd %xmm0, %xmm3
cmpl %esi, %edx
jne 0x41f9
cmpb $0x0, 0x35fb9(%rip)     # 0x3a1cd
je 0x4248
movq %rbp, %rdi
leaq 0x2a5dd(%rip), %rsi     # 0x2e7fd
movl %r8d, %edx
movsd %xmm1, 0x8(%rsp)
movsd 0x8(%rsp), %xmm1
movb $0x3, %al
movsd %xmm0, 0x10(%rsp)
callq 0x2250
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
divsd 0xc8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x426e
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x429c
pushq $0x50
popq %rsi
callq 0x2190
jmp 0x429c
movq 0x50(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x428f
movq 0xd0(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x429c
leaq 0xd0(%rsp), %rdx
callq 0xb976
movq 0x8(%r13), %rax
movq (%r13), %rcx
subq %rcx, %rax
je 0x42c7
shlq $0x1d, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
cmpq %rax, %r15
jl 0x4100
cmpb $0x0, 0x35eff(%rip)     # 0x3a1cd
je 0x42f1
movq (%r14), %rdi
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rdx
subq (%rax), %rdx
sarq $0x3, %rdx
leaq 0x2a529(%rip), %rsi     # 0x2e813
xorl %eax, %eax
callq 0x2250
movq 0x78(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
jne 0x4581
leaq 0x50(%rsp), %rdx
callq 0x10c8a
jmp 0x458e
movq %r12, %r14
subq %r15, %r14
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r12, %rsi
leaq 0x20442(%rip), %rcx     # 0x2477c
callq 0x10dc5
leaq 0x8(%r15), %rax
cmpq $0x81, %r14
movq %r12, (%rsp)
jl 0x4618
movq %rax, 0x30(%rsp)
movq %rax, %r13
pushq $0x8
popq %rbp
movq %r15, %rax
movq %rax, 0x10(%rsp)
leaq (%r15,%rbp), %r14
movq (%r15,%rbp), %rdi
movq (%r15), %rsi
callq 0x2477c
movq (%r15,%rbp), %r12
testb %al, %al
movq %r14, 0x8(%rsp)
je 0x4399
movq 0x30(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r15, %r14
jmp 0x43ce
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0x2477c
testb %al, %al
je 0x43ce
movq %r13, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x2477c
movq %r14, %rcx
testb %al, %al
jne 0x43b0
movq %r12, (%r14)
addq $0x8, %rbp
addq $0x8, %r13
cmpq $0x80, %rbp
movq 0x8(%rsp), %rax
jne 0x4362
subq $-0x80, %r15
cmpq (%rsp), %r15
movq 0x28(%rsp), %r13
je 0x4076
movq -0x8(%r15), %rsi
movq (%r15), %r12
movq %r12, %rdi
callq 0x2477c
movq %r15, %r14
testb %al, %al
je 0x4435
movq %r15, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x2477c
movq %r14, %rcx
testb %al, %al
jne 0x4417
movq %r12, (%r14)
addq $0x8, %r15
cmpq (%rsp), %r15
jne 0x43fe
jmp 0x4076
movq %r12, %r14
subq %r15, %r14
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r12, %rsi
leaq 0x2030e(%rip), %rcx     # 0x2477c
callq 0x10dc5
leaq 0x8(%r15), %rax
cmpq $0x81, %r14
movq %r12, (%rsp)
jl 0x46a5
movq %rax, 0x30(%rsp)
movq %rax, %r14
pushq $0x8
popq %rbp
movq %r15, %rax
movq %rax, 0x10(%rsp)
leaq (%r15,%rbp), %r13
movq (%r15,%rbp), %rdi
movq (%r15), %rsi
callq 0x2477c
movq (%r15,%rbp), %r12
testb %al, %al
movq %r13, 0x8(%rsp)
je 0x44cd
movq 0x30(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r15, %r13
jmp 0x4502
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0x2477c
testb %al, %al
je 0x4502
movq %r14, %rcx
leaq -0x8(%rcx), %r13
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x2477c
movq %r13, %rcx
testb %al, %al
jne 0x44e4
movq %r12, (%r13)
addq $0x8, %rbp
addq $0x8, %r14
cmpq $0x80, %rbp
movq 0x8(%rsp), %rax
jne 0x4496
subq $-0x80, %r15
cmpq (%rsp), %r15
movq 0x70(%rsp), %rbp
movq 0x28(%rsp), %r13
je 0x4090
movq -0x8(%r15), %rsi
movq (%r15), %r12
movq %r12, %rdi
callq 0x2477c
movq %r15, %r14
testb %al, %al
je 0x456f
movq %r15, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x2477c
movq %r14, %rcx
testb %al, %al
jne 0x4551
movq %r12, (%r14)
addq $0x8, %r15
cmpq (%rsp), %r15
jne 0x4538
jmp 0x4090
movq 0x50(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
movq %r13, %rdi
callq 0x25358
movq 0x40(%rsp), %rdi
callq 0x25358
cmpb $0x1, 0x35c26(%rip)     # 0x3a1cd
jne 0x45d9
movsd 0x20(%rsp), %xmm0
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
leaq 0x2a275(%rip), %rsi     # 0x2e836
movb $0x1, %al
callq 0x2080
movq 0x38(%rsp), %rdi
movl 0x18(%rsp), %esi
movq %r15, %rdx
callq 0x1d6be
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rdi
callq 0x2220
movsd 0x58(%rsp), %xmm0
ucomisd 0x26ab5(%rip), %xmm0    # 0x2b0b0
jbe 0x3c01
movsd 0x20(%rsp), %xmm0
ucomisd 0x58(%rsp), %xmm0
jb 0x3c01
jmp 0x47fa
cmpq %r12, %rax
je 0x4076
movq %r15, %rbp
movq %rbp, %r12
movq %rax, %rbp
movq (%rax), %rdi
movq (%r15), %rsi
callq 0x2477c
movq (%rbp), %r13
testb %al, %al
je 0x465a
movq %rbp, %rdx
subq %r15, %rdx
subq %rdx, %r12
addq $0x10, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
movq %r15, %r14
jmp 0x468e
movq (%r12), %rsi
movq %r13, %rdi
callq 0x2477c
movq %rbp, %r14
testb %al, %al
je 0x468e
movq %rbp, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x2477c
movq %r14, %rcx
testb %al, %al
jne 0x4670
movq %r13, (%r14)
leaq 0x8(%rbp), %rax
cmpq (%rsp), %rax
movq 0x28(%rsp), %r13
jne 0x4624
jmp 0x4076
cmpq %r12, %rax
je 0x4090
movq %r15, %r14
movq %r14, %r12
movq %rax, %r14
movq (%rax), %rdi
movq (%r15), %rsi
callq 0x2477c
movq (%r14), %r13
testb %al, %al
je 0x46e6
movq %r14, %rdx
subq %r15, %rdx
subq %rdx, %r12
addq $0x10, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x22b0
movq %r15, %r12
jmp 0x471a
movq (%r12), %rsi
movq %r13, %rdi
callq 0x2477c
movq %r14, %r12
testb %al, %al
je 0x471a
movq %r14, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x2477c
movq %r12, %rcx
testb %al, %al
jne 0x46fc
movq %r13, (%r12)
leaq 0x8(%r14), %rax
cmpq (%rsp), %rax
movq 0x28(%rsp), %r13
jne 0x46b1
jmp 0x4090
movq %rdi, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x3589d(%rip), %rbx     # 0x39fe0
movq (%rbx), %rdi
leaq 0x29fce(%rip), %rsi     # 0x2e71b
movl %r8d, (%rsp)
movl %r8d, %edx
movl 0x8(%rsp), %ecx
movl %r13d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2250
addq $0x10, %rsp
cmpl %r12d, %r13d
jg 0x47a1
movq 0x10(%rsp), %rax
leaq (%r14,%rax,4), %r14
subl %r13d, %r12d
incl %r12d
xorl %r13d, %r13d
movq (%rbx), %rdi
movl (%r14,%r13,4), %edx
leaq 0x29fb7(%rip), %rsi     # 0x2e749
xorl %eax, %eax
callq 0x2250
incq %r13
cmpl %r13d, %r12d
jne 0x4784
leaq 0x2afb4(%rip), %rdi     # 0x2f75c
leaq 0x272ff(%rip), %rsi     # 0x2baae
callq 0x26387
movq %rax, %r12
movl 0x8(%rsp), %eax
cmpl (%rsp), %eax
movq 0x18(%rsp), %rbx
jl 0x47eb
decq %r15
leaq 0x29f7e(%rip), %r14     # 0x2e74d
movsd 0x8(%rbx,%r15,8), %xmm0
movq %r12, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x2250
incq %r15
cmpq %rbp, %r15
jl 0x47cf
movq %r12, %rdi
callq 0x2170
xorl %edi, %edi
callq 0x21e0
movq (%r14), %rdi
movq 0x8(%r12), %rdx
subq (%r12), %rdx
shrq $0x3, %rdx
leaq 0x2a036(%rip), %rsi     # 0x2e847
xorl %eax, %eax
callq 0x2250
cmpb $0x1, 0x359ae(%rip)     # 0x3a1cd
jne 0x4829
movq %rbp, %rdi
callq 0x2170
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x29094(%rip), %rdi     # 0x2d8d6
movq %r15, %rsi
xorl %eax, %eax
callq 0x2270