// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
movq %rdx, %r14
movsd %xmm2, 0x78(%rsp)
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0xb8(%rsp)
movl %esi, %r13d
movq %rdi, %rbx
movsd 0x265d8(%rip), %xmm1    # 0x2a008
cmpb $0x1, 0x35796(%rip)     # 0x391cd
jne 0x3a56
leaq 0x29d2f(%rip), %rdi     # 0x2d76f
leaq 0x27061(%rip), %rsi     # 0x2aaa8
callq 0x25d5a
movsd 0x265b4(%rip), %xmm1    # 0x2a008
jmp 0x3a58
xorl %eax, %eax
movq %rax, 0xa0(%rsp)
cvttsd2si 0x58(%rsp), %eax
movl %eax, 0x6c(%rsp)
divsd 0x78(%rsp), %xmm1
movslq %r13d, %rax
leal 0x1(%rax), %ecx
movq %rcx, 0x48(%rsp)
movslq %ecx, %rcx
shlq $0x3, %rcx
movq %rcx, 0xb0(%rsp)
cmpl $-0x2, %eax
leaq 0x10(,%rax,8), %rcx
pushq $-0x1
popq %rdx
cmovlq %rdx, %rcx
movq %rcx, 0xa8(%rsp)
leaq 0x8(,%rax,4), %rcx
movq %rcx, 0x98(%rsp)
cmovlq %rdx, %rcx
movq %rcx, 0x90(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xc0(%rsp)
pushq $0x2
popq %r15
movq %r14, 0x88(%rsp)
movl %r13d, 0x3c(%rsp)
movq %rbp, 0x70(%rsp)
mulsd 0x78(%rsp), %xmm1
movsd %xmm1, 0x18(%rsp)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
jne 0x3b08
movq %r14, %rdi
leaq 0x18(%rsp), %rdx
callq 0xb60c
jmp 0x3b14
movsd %xmm1, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r14)
movsd 0x18(%rsp), %xmm0
movsd 0xb8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3b5e
pushq $0x18
popq %rdi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rax, 0xd0(%rsp)
movq %rbp, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x15312
movsd 0x18(%rsp), %xmm1
jmp 0x3ae3
movq 0x3547b(%rip), %r14     # 0x38fe0
movq (%r14), %rdi
cvttsd2si %xmm0, %edx
leaq 0x29c17(%rip), %rsi     # 0x2d78a
movl 0x6c(%rsp), %ecx
xorl %eax, %eax
callq 0x2250
cvttsd2si 0x18(%rsp), %ebp
movl %ebp, %eax
andl $0x80000001, %eax       # imm = 0x80000001
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
subl %ecx, %ebp
movq 0xa8(%rsp), %rdi
callq 0x2040
movq %rax, 0x30(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq 0xb0(%rsp), %rdx
callq 0x20f0
movl %ebp, %eax
cltd
idivl %r15d
testl %r13d, %r13d
jle 0x3e9d
movl $0xf4240, %ecx          # imm = 0xF4240
movl $0xfff0bdc0, %edx       # imm = 0xFFF0BDC0
pushq $0x1
popq %rsi
movq 0x48(%rsp), %r8
movl %ecx, %edi
cvttsd2si (%rbx,%rsi,8), %ecx
cmpl %ecx, %edx
cmovlel %ecx, %edx
cmpl %ecx, %edi
cmovll %edi, %ecx
incq %rsi
cmpq %rsi, %r8
jne 0x3bda
testl %r13d, %r13d
jle 0x3e9d
subl %ecx, %edx
addl $-0x64, %ecx
leal 0xc8(%rdx), %esi
leal 0xca(%rdx), %edi
movslq %edi, %rdi
shlq $0x2, %rdi
cmpl $-0x2, %esi
pushq $-0x1
popq %rsi
cmovlq %rsi, %rdi
movq %rdi, 0x28(%rsp)
addl $0xc9, %edx
movslq %edx, %rdx
shlq $0x2, %rdx
movq %rdx, 0x40(%rsp)
movslq %ecx, %rdx
movq %rdx, 0x80(%rsp)
negq %rdx
movq %rdx, 0x60(%rsp)
cltq
movq %rax, 0x20(%rsp)
movl $0xf4240, %r15d         # imm = 0xF4240
xorl %r14d, %r14d
pushq $0x1
popq %rdi
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %esi, %esi
movq 0x48(%rsp), %r9
movl %esi, %eax
movl %r8d, %ecx
movl %edi, %r8d
movq 0x20(%rsp), %rsi
subl %esi, %r8d
cmpq %rsi, %rdi
pushq $0x1
popq %rdx
cmovlel %edx, %r8d
addl %edi, %esi
cmpl %r13d, %esi
cmovgel %r13d, %esi
movslq %r8d, %r13
movslq %esi, %rbp
testq %r14, %r14
je 0x3d31
cmpl %r8d, %ecx
jge 0x3cb6
movslq %ecx, %rcx
cvttsd2si (%rbx,%rcx,8), %edx
movslq %edx, %rdx
decl (%r14,%rdx,4)
incq %rcx
cmpq %rcx, %r13
jne 0x3ca2
cmpl %esi, %eax
jge 0x3cef
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 %r15d, %xmm1
ucomisd %xmm0, %xmm1
cmoval %ecx, %r15d
cmpq %rbp, %rax
jl 0x3cbc
xorl %r10d, %r10d
movslq %r15d, %r11
cmpl %r12d, %r15d
jg 0x3d16
leaq (%r14,%r11,4), %rax
movl %r12d, %ecx
subl %r11d, %ecx
incl %ecx
xorl %edx, %edx
xorl %r10d, %r10d
addl (%rax,%rdx,4), %r10d
incq %rdx
cmpl %edx, %ecx
jne 0x3d0b
movq %r11, (%rsp)
movl %esi, %r11d
subl %r8d, %r11d
incl %r11d
cmpl %r11d, %r10d
je 0x3db0
jmp 0x460c
movq %rdi, (%rsp)
movq 0x28(%rsp), %rdi
movl %r8d, 0x8(%rsp)
movl %esi, 0x10(%rsp)
callq 0x2040
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x20f0
movl 0x10(%rsp), %esi
movl 0x8(%rsp), %r8d
movq 0x60(%rsp), %rax
leaq (%r14,%rax,4), %r14
cmpl %r8d, %esi
jl 0x3da7
decq %r13
movsd 0x8(%rbx,%r13,8), %xmm0
cvttsd2si %xmm0, %eax
cltq
incl (%r14,%rax,4)
cvtsi2sd %r12d, %xmm1
ucomisd %xmm1, %xmm0
cmoval %eax, %r12d
cvtsi2sd %r15d, %xmm1
ucomisd %xmm0, %xmm1
cmoval %eax, %r15d
incq %r13
cmpq %rbp, %r13
jl 0x3d74
movq 0x48(%rsp), %r9
movq (%rsp), %rdi
cmpl %r12d, %r15d
movl 0x3c(%rsp), %r13d
jle 0x3dc7
xorpd %xmm0, %xmm0
xorl %edx, %edx
xorl %ecx, %ecx
jmp 0x3e69
movq %rdi, %rbp
movl %esi, %r11d
movl %esi, %eax
movl %r8d, %r10d
subl %r8d, %eax
incl %eax
cvtsi2sd %eax, %xmm0
mulsd 0x26275(%rip), %xmm0    # 0x2a058
movslq %r15d, %rax
leal 0x1(%r12), %esi
leaq (%r14,%rax,4), %rdi
xorl %ecx, %ecx
movq %rdi, %r8
movl %r15d, %edx
addl (%r8), %ecx
cvtsi2sd %ecx, %xmm1
ucomisd %xmm0, %xmm1
ja 0x3e14
incl %edx
addq $0x4, %r8
cmpl %edx, %esi
jne 0x3df7
xorpd %xmm0, %xmm0
jmp 0x3e18
cvtsi2sd %edx, %xmm0
xorl %ecx, %ecx
cmpl %r12d, %r15d
movl %r10d, %r8d
jle 0x3e2c
xorl %edx, %edx
movl %r11d, %esi
movq %rbp, %rdi
jmp 0x3e69
movl %r15d, %edx
cmpl $0x0, (%rdi)
jg 0x3e40
incl %edx
addq $0x4, %rdi
cmpl %edx, %esi
jne 0x3e2f
xorl %edx, %edx
cmpl %r12d, %r15d
movq %rbp, %rdi
jg 0x3e66
movslq %r12d, %rsi
incq %rsi
cmpl $0x0, -0x4(%r14,%rsi,4)
jg 0x3e63
decl %r12d
decq %rsi
cmpq %rax, %rsi
jg 0x3e4e
jmp 0x3e66
movl %r12d, %ecx
movl %r11d, %esi
movq 0x30(%rsp), %rax
movsd %xmm0, (%rax,%rdi,8)
incq %rdi
movl %ecx, %r12d
movl %edx, %r15d
cmpq %r9, %rdi
jne 0x3c68
movq 0x80(%rsp), %rax
leaq (%r14,%rax,4), %rdi
callq 0x2220
movq 0x35143(%rip), %r14     # 0x38fe0
cmpb $0x1, 0x35329(%rip)     # 0x391cd
jne 0x3ebb
movq (%r14), %rcx
leaq 0x297d9(%rip), %rdi     # 0x2d689
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, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x90(%rsp), %rdi
callq 0x2040
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq 0x98(%rsp), %rdx
callq 0x20f0
movq 0x30(%rsp), %rdi
movl %r13d, %esi
movq %r15, 0x40(%rsp)
movq %r15, %rdx
movq %r12, %rcx
movq %r14, 0x60(%rsp)
movq %r14, %r8
xorl %r9d, %r9d
callq 0x241dc
movq (%r12), %r14
movq %r12, 0x20(%rsp)
movq 0x8(%r12), %r13
cmpq %r13, %r14
jne 0x4204
movq 0x40(%rsp), %rax
movq (%rax), %r14
movq 0x8(%rax), %r12
cmpq %r12, %r14
movq 0x70(%rsp), %rbp
jne 0x432c
pushq $0x18
popq %rdi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq %rax, 0x50(%rsp)
movq 0x20(%rsp), %r14
movq (%r14), %rax
movq 0x8(%r14), %rdx
cmpb $0x1, 0x3523b(%rip)     # 0x391cd
movq 0xa0(%rsp), %r12
jne 0x3fc2
movq 0x3503d(%rip), %r15     # 0x38fe0
movq (%r15), %rdi
subq %rax, %rdx
shrq $0x3, %rdx
leaq 0x29815(%rip), %rsi     # 0x2d7c9
xorl %eax, %eax
callq 0x2250
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
movl 0x3c(%rsp), %r13d
pushq $0x2
popq %r15
je 0x41ab
shrq $0x3, %rdx
cmpl $0x2, %edx
jl 0x41ab
xorl %r14d, %r14d
callq 0x15644
movq %rax, 0xd0(%rsp)
leaq 0x29702(%rip), %rdi     # 0x2d6fa
callq 0x26db4
movq 0xd0(%rsp), %rcx
movq %rax, (%rcx)
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x4713
movq (%rax,%r14,8), %rcx
movslq (%rcx), %r8
movq 0xd0(%rsp), %rsi
movl %r8d, 0xc(%rsi)
incq %r14
cmpq %r14, %rdx
jbe 0x4713
movq (%rax,%r14,8), %rax
movl (%rax), %ecx
movl %ecx, 0x20(%rsi)
movb $0x2b, 0x8(%rsi)
movsd 0x18(%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 0x25fd8(%rip), %xmm1    # 0x2a058
addsd %xmm0, %xmm1
ucomisd 0xc0(%rsp), %xmm1
movl %r13d, %eax
ja 0x409a
movl %edi, %eax
cltd
idivl %r15d
addl %ecx, %eax
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
cmpl %eax, %esi
jg 0x40c3
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 0x40ae
cmpl %ecx, %r8d
jg 0x40f2
movq 0x30(%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 0x40de
cmpb $0x0, 0x350d4(%rip)     # 0x391cd
je 0x412d
movq %r12, %rdi
leaq 0x296f8(%rip), %rsi     # 0x2d7fd
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 0x414e
movq 0xd0(%rsp), %rdi
pushq $0x50
popq %rsi
callq 0x2190
jmp 0x417c
movq 0x50(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x416f
movq 0xd0(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x417c
leaq 0xd0(%rsp), %rdx
callq 0xb74c
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rax
movq (%rcx), %rcx
subq %rcx, %rax
je 0x41ab
shlq $0x1d, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
cmpq %rax, %r14
jl 0x3fe4
cmpb $0x0, 0x3501b(%rip)     # 0x391cd
je 0x41dc
movq 0x34e25(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rdx
subq (%rax), %rdx
sarq $0x3, %rdx
leaq 0x2963e(%rip), %rsi     # 0x2d813
xorl %eax, %eax
callq 0x2250
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
movq 0x88(%rsp), %r14
jne 0x4462
movq %rbp, %rdi
leaq 0x50(%rsp), %rdx
callq 0x109c0
jmp 0x446f
movq %r13, %r15
subq %r14, %r15
movq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r13, %rsi
leaq 0x1ffa6(%rip), %rcx     # 0x241d1
callq 0x10afb
leaq 0x8(%r14), %r12
cmpq $0x81, %r15
jl 0x44fe
movq %r13, 0x28(%rsp)
movq %r12, (%rsp)
pushq $0x8
popq %rbp
movq %r14, %rax
movq %rax, 0x10(%rsp)
leaq (%r14,%rbp), %r13
movq (%r14,%rbp), %rdi
movq (%r14), %rsi
callq 0x241d1
movq (%r14,%rbp), %r15
testb %al, %al
movq %r13, 0x8(%rsp)
je 0x4286
movq (%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r14, %r13
jmp 0x42bb
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0x241d1
testb %al, %al
je 0x42bb
movq %r12, %rcx
leaq -0x8(%rcx), %r13
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x241d1
movq %r13, %rcx
testb %al, %al
jne 0x429d
movq %r15, (%r13)
addq $0x8, %rbp
addq $0x8, %r12
cmpq $0x80, %rbp
movq 0x8(%rsp), %rax
jne 0x4250
subq $-0x80, %r14
movq 0x28(%rsp), %r13
cmpq %r13, %r14
je 0x3f4b
movq -0x8(%r14), %rsi
movq (%r14), %r15
movq %r15, %rdi
callq 0x241d1
movq %r14, %r12
testb %al, %al
je 0x4322
movq %r14, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x241d1
movq %r12, %rcx
testb %al, %al
jne 0x4304
movq %r15, (%r12)
addq $0x8, %r14
jmp 0x42e2
movq %r12, %r15
subq %r14, %r15
movq %r15, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r12, %rsi
leaq 0x1fe7e(%rip), %rcx     # 0x241d1
callq 0x10afb
leaq 0x8(%r14), %rax
cmpq $0x81, %r15
movq %r12, (%rsp)
jl 0x4585
movq %rax, 0x28(%rsp)
movq %rax, %r13
pushq $0x8
popq %rbp
movq %r14, %rax
movq %rax, 0x10(%rsp)
leaq (%r14,%rbp), %r12
movq (%r14,%rbp), %rdi
movq (%r14), %rsi
callq 0x241d1
movq (%r14,%rbp), %r15
testb %al, %al
movq %r12, 0x8(%rsp)
je 0x43b2
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r14, %r12
jmp 0x43e7
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0x241d1
testb %al, %al
je 0x43e7
movq %r13, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x241d1
movq %r12, %rcx
testb %al, %al
jne 0x43c9
movq %r15, (%r12)
addq $0x8, %rbp
addq $0x8, %r13
cmpq $0x80, %rbp
movq 0x8(%rsp), %rax
jne 0x437b
subq $-0x80, %r14
cmpq (%rsp), %r14
movq 0x70(%rsp), %rbp
je 0x3f65
movq -0x8(%r14), %rsi
movq (%r14), %r15
movq %r15, %rdi
callq 0x241d1
movq %r14, %r12
testb %al, %al
je 0x444f
movq %r14, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x241d1
movq %r12, %rcx
testb %al, %al
jne 0x4431
movq %r15, (%r12)
addq $0x8, %r14
cmpq (%rsp), %r14
jne 0x4418
jmp 0x3f65
movq 0x50(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rbp)
movq 0x20(%rsp), %rdi
callq 0x24d7a
movq 0x40(%rsp), %rdi
callq 0x24d7a
cmpb $0x1, 0x34d43(%rip)     # 0x391cd
jne 0x44bf
movsd 0x18(%rsp), %xmm0
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
leaq 0x29392(%rip), %rsi     # 0x2d836
movb $0x1, %al
callq 0x2080
movq 0x30(%rsp), %rdi
movl %r13d, %esi
movq %r15, %rdx
pushq $0x2
popq %r15
callq 0x1d226
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x30(%rsp), %rdi
callq 0x2220
movsd 0x58(%rsp), %xmm0
ucomisd 0x25bcf(%rip), %xmm0    # 0x2a0b0
movsd 0x18(%rsp), %xmm1
jbe 0x3ae3
ucomisd 0x58(%rsp), %xmm1
jb 0x3ae3
jmp 0x46cd
cmpq %r13, %r12
je 0x3f4b
movq %r14, %rbp
movq %rbp, %r15
movq %r12, %rbp
movq (%r12), %rdi
movq (%r14), %rsi
callq 0x241d1
movq (%r12), %r12
testb %al, %al
je 0x4541
movq %rbp, %rdx
subq %r14, %rdx
subq %rdx, %r15
addq $0x10, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x22b0
movq %r14, %r15
jmp 0x4574
movq (%r15), %rsi
movq %r12, %rdi
callq 0x241d1
movq %rbp, %r15
testb %al, %al
je 0x4574
movq %rbp, %rcx
leaq -0x8(%rcx), %r15
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x241d1
movq %r15, %rcx
testb %al, %al
jne 0x4556
movq %r12, (%r15)
leaq 0x8(%rbp), %r12
cmpq %r13, %r12
jne 0x450a
jmp 0x3f4b
cmpq %r12, %rax
je 0x3f65
movq %r14, %r13
movq %r13, %r15
movq %rax, %r13
movq (%rax), %rdi
movq (%r14), %rsi
callq 0x241d1
movq (%r13), %r12
testb %al, %al
je 0x45c7
movq %r13, %rdx
subq %r14, %rdx
subq %rdx, %r15
addq $0x10, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x22b0
movq %r14, %r15
jmp 0x45fa
movq (%r15), %rsi
movq %r12, %rdi
callq 0x241d1
movq %r13, %r15
testb %al, %al
je 0x45fa
movq %r13, %rcx
leaq -0x8(%rcx), %r15
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r12, %rdi
callq 0x241d1
movq %r15, %rcx
testb %al, %al
jne 0x45dc
movq %r12, (%r15)
leaq 0x8(%r13), %rax
cmpq (%rsp), %rax
jne 0x4591
jmp 0x3f65
movq 0x349cd(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movl %esi, %ecx
leaq 0x290fc(%rip), %rsi     # 0x2d71b
movl %r8d, 0x8(%rsp)
movl %r8d, %edx
movl %ecx, 0x10(%rsp)
movl %r15d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2250
popq %rcx
popq %rdx
cmpl %r12d, %r15d
jg 0x4678
movq (%rsp), %rax
leaq (%r14,%rax,4), %r14
subl %r15d, %r12d
incl %r12d
xorl %r15d, %r15d
movq 0x34985(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movl (%r14,%r15,4), %edx
leaq 0x290e0(%rip), %rsi     # 0x2d749
xorl %eax, %eax
callq 0x2250
incq %r15
cmpl %r15d, %r12d
jne 0x4654
leaq 0x2a0dd(%rip), %rdi     # 0x2e75c
leaq 0x26422(%rip), %rsi     # 0x2aaa8
callq 0x25d5a
movq %rax, %r15
movl 0x10(%rsp), %eax
cmpl 0x8(%rsp), %eax
jl 0x46be
decq %r13
leaq 0x290ab(%rip), %r14     # 0x2d74d
movsd 0x8(%rbx,%r13,8), %xmm0
movq %r15, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x2250
incq %r13
cmpq %rbp, %r13
jl 0x46a2
movq %r15, %rdi
callq 0x2170
xorl %edi, %edi
callq 0x21e0
movq 0x3490c(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
movq 0x8(%r14), %rdx
subq (%r14), %rdx
shrq $0x3, %rdx
leaq 0x2915e(%rip), %rsi     # 0x2d847
xorl %eax, %eax
callq 0x2250
cmpb $0x1, 0x34ad6(%rip)     # 0x391cd
jne 0x4701
movq %r12, %rdi
callq 0x2170
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x281b6(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270