// File: 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:
subq $0x608, %rsp            # imm = 0x608
movq %rdi, 0x600(%rsp)
movl %esi, 0x5fc(%rsp)
movsd %xmm0, 0x5f0(%rsp)
movsd %xmm1, 0x5e8(%rsp)
movsd %xmm2, 0x5e0(%rsp)
movq %rdx, 0x5d8(%rsp)
movq %rcx, 0x5d0(%rsp)
movsd %xmm3, 0x5c8(%rsp)
movsd 0x2ca26(%rip), %xmm0    # 0x52018
divsd 0x5e0(%rsp), %xmm0
movsd %xmm0, 0x5c0(%rsp)
movl $0x0, 0x5bc(%rsp)
movsd 0x2ca21(%rip), %xmm0    # 0x52038
movsd %xmm0, 0x5b0(%rsp)
movq $0x0, 0x5a8(%rsp)
testb $0x1, 0x3fbaa(%rip)     # 0x651dd
je 0x25650
leaq 0x304f3(%rip), %rdi     # 0x55b2f
leaq 0x2d461(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0x5a8(%rsp)
jmp 0x25652
movsd 0x5e0(%rsp), %xmm0
mulsd 0x5c0(%rsp), %xmm0
movsd %xmm0, 0x5c0(%rsp)
movq 0x5d8(%rsp), %rdi
leaq 0x5c0(%rsp), %rsi
callq 0xc850
movsd 0x5c0(%rsp), %xmm1
movsd 0x5f0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x25713
movl 0x5bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5bc(%rsp)
movq 0x5d0(%rsp), %rax
movq %rax, 0x98(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x90(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x90(%rsp), %rdi
callq 0xc670
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rdi
movq %rax, 0x5a0(%rsp)
leaq 0x5a0(%rsp), %rsi
callq 0x26a40
jmp 0x25652
movq 0x3f8c6(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
cvttsd2si 0x5c0(%rsp), %edx
cvttsd2si 0x5e8(%rsp), %ecx
leaq 0x30414(%rip), %rsi     # 0x55b4a
movb $0x0, %al
callq 0x2250
cvttsd2si 0x5c0(%rsp), %eax
movl %eax, 0x59c(%rsp)
movl 0x59c(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x25774
movl 0x59c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x25782
movl 0x59c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x598(%rsp)
movl 0x5fc(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x590(%rsp)
movq 0x590(%rsp), %rdi
movl 0x5fc(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2110
movl $0x0, 0x58c(%rsp)
movl 0x598(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x588(%rsp)
movl $0xfff0bdc0, 0x584(%rsp) # imm = 0xFFF0BDC0
movl $0xf4240, 0x580(%rsp) # imm = 0xF4240
movl $0x1, 0x57c(%rsp)
movl 0x57c(%rsp), %eax
cmpl 0x5fc(%rsp), %eax
jg 0x258cb
movl 0x584(%rsp), %eax
movq 0x600(%rsp), %rcx
movslq 0x57c(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
cmpl %ecx, %eax
jge 0x25877
movq 0x600(%rsp), %rax
movslq 0x57c(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x584(%rsp)
movl 0x580(%rsp), %eax
movq 0x600(%rsp), %rcx
movslq 0x57c(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
cmpl %ecx, %eax
jle 0x258b3
movq 0x600(%rsp), %rax
movslq 0x57c(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x580(%rsp)
jmp 0x258b5
movl 0x57c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x57c(%rsp)
jmp 0x25827
movl 0x584(%rsp), %eax
addl $0x64, %eax
movl %eax, 0x584(%rsp)
movl 0x580(%rsp), %eax
subl $0x64, %eax
movl %eax, 0x580(%rsp)
movq $0x0, 0x570(%rsp)
movl $0x0, 0x56c(%rsp)
movl $0xf4240, 0x568(%rsp) # imm = 0xF4240
movl 0x598(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x560(%rsp)
movl $0x0, 0x55c(%rsp)
movl $0x0, 0x558(%rsp)
movl $0x1, 0x554(%rsp)
movl 0x554(%rsp), %eax
cmpl 0x5fc(%rsp), %eax
jg 0x2606a
movl 0x554(%rsp), %eax
cmpl 0x588(%rsp), %eax
jle 0x259a0
movl 0x554(%rsp), %eax
subl 0x588(%rsp), %eax
movl %eax, 0x88(%rsp)
jmp 0x259ae
movl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x259ae
movl 0x88(%rsp), %eax
movl %eax, 0x550(%rsp)
movl 0x554(%rsp), %eax
addl 0x588(%rsp), %eax
cmpl 0x5fc(%rsp), %eax
jg 0x259ea
movl 0x554(%rsp), %eax
addl 0x588(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x259f8
movl 0x5fc(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x54c(%rsp)
cmpq $0x0, 0x570(%rsp)
jne 0x25b82
movl 0x584(%rsp), %eax
subl 0x580(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x570(%rsp)
movq 0x570(%rsp), %rdi
movl 0x584(%rsp), %eax
subl 0x580(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2110
movl 0x580(%rsp), %ecx
movq 0x570(%rsp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x570(%rsp)
movl 0x550(%rsp), %eax
movl %eax, 0x548(%rsp)
movl 0x548(%rsp), %eax
cmpl 0x54c(%rsp), %eax
jg 0x25b7d
movq 0x570(%rsp), %rax
movq 0x600(%rsp), %rcx
movslq 0x548(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
cvtsi2sdl 0x56c(%rsp), %xmm1
movq 0x600(%rsp), %rax
movslq 0x548(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x25b29
movq 0x600(%rsp), %rax
movslq 0x548(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x56c(%rsp)
cvtsi2sdl 0x568(%rsp), %xmm0
movq 0x600(%rsp), %rax
movslq 0x548(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x25b65
movq 0x600(%rsp), %rax
movslq 0x548(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x568(%rsp)
jmp 0x25b67
movl 0x548(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x548(%rsp)
jmp 0x25aac
jmp 0x25e7b
movl 0x55c(%rsp), %eax
movl %eax, 0x544(%rsp)
movl 0x544(%rsp), %eax
cmpl 0x550(%rsp), %eax
jge 0x25bdc
movq 0x570(%rsp), %rax
movq 0x600(%rsp), %rcx
movslq 0x544(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $-0x1, %edx
movl %edx, (%rax,%rcx,4)
movl 0x544(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x544(%rsp)
jmp 0x25b90
movl 0x558(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x540(%rsp)
movl 0x540(%rsp), %eax
cmpl 0x54c(%rsp), %eax
jg 0x25cbe
movq 0x570(%rsp), %rax
movq 0x600(%rsp), %rcx
movslq 0x540(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
cvtsi2sdl 0x56c(%rsp), %xmm1
movq 0x600(%rsp), %rax
movslq 0x540(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x25c6a
movq 0x600(%rsp), %rax
movslq 0x540(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x56c(%rsp)
cvtsi2sdl 0x568(%rsp), %xmm0
movq 0x600(%rsp), %rax
movslq 0x540(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x25ca6
movq 0x600(%rsp), %rax
movslq 0x540(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x568(%rsp)
jmp 0x25ca8
movl 0x540(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x540(%rsp)
jmp 0x25bed
movl $0x0, 0x53c(%rsp)
movl 0x568(%rsp), %eax
movl %eax, 0x538(%rsp)
movl 0x538(%rsp), %eax
cmpl 0x56c(%rsp), %eax
jg 0x25d1b
movq 0x570(%rsp), %rax
movslq 0x538(%rsp), %rcx
movl (%rax,%rcx,4), %eax
addl 0x53c(%rsp), %eax
movl %eax, 0x53c(%rsp)
movl 0x538(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x538(%rsp)
jmp 0x25cd7
movl 0x53c(%rsp), %eax
movl 0x54c(%rsp), %ecx
subl 0x550(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
je 0x25e79
movq 0x3f29e(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0x550(%rsp), %edx
movl 0x54c(%rsp), %ecx
movl 0x568(%rsp), %r8d
movl 0x56c(%rsp), %r9d
movl 0x53c(%rsp), %r10d
movl 0x54c(%rsp), %eax
subl 0x550(%rsp), %eax
addl $0x1, %eax
leaq 0x2fd58(%rip), %rsi     # 0x55adb
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x2250
movl 0x568(%rsp), %eax
movl %eax, 0x534(%rsp)
movl 0x534(%rsp), %eax
cmpl 0x56c(%rsp), %eax
jg 0x25dee
movq 0x3f229(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0x570(%rsp), %rax
movslq 0x534(%rsp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x2fd35(%rip), %rsi     # 0x55b09
movb $0x0, %al
callq 0x2250
movl 0x534(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x534(%rsp)
jmp 0x25da0
leaq 0x30d02(%rip), %rdi     # 0x56af7
leaq 0x2cca8(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0x528(%rsp)
movl 0x550(%rsp), %eax
movl %eax, 0x524(%rsp)
movl 0x524(%rsp), %eax
cmpl 0x54c(%rsp), %eax
jg 0x25e65
movq 0x528(%rsp), %rdi
movq 0x600(%rsp), %rax
movslq 0x524(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x2fcc2(%rip), %rsi     # 0x55b0d
movb $0x1, %al
callq 0x2250
movl 0x524(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x524(%rsp)
jmp 0x25e17
movq 0x528(%rsp), %rdi
callq 0x2180
xorl %edi, %edi
callq 0x21f0
jmp 0x25e7b
movl $0x0, 0x520(%rsp)
movl $0x0, 0x51c(%rsp)
movl 0x568(%rsp), %eax
movl %eax, 0x518(%rsp)
movl 0x518(%rsp), %eax
cmpl 0x56c(%rsp), %eax
jg 0x25f25
movq 0x570(%rsp), %rax
movslq 0x518(%rsp), %rcx
movl (%rax,%rcx,4), %eax
addl 0x520(%rsp), %eax
movl %eax, 0x520(%rsp)
cvtsi2sdl 0x520(%rsp), %xmm0
movl 0x54c(%rsp), %eax
subl 0x550(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
mulsd 0x5b0(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x25f0d
movl 0x518(%rsp), %eax
movl %eax, 0x51c(%rsp)
jmp 0x25f25
jmp 0x25f0f
movl 0x518(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x518(%rsp)
jmp 0x25e9f
movl $0x0, 0x514(%rsp)
movl 0x568(%rsp), %eax
movl %eax, 0x510(%rsp)
movl 0x510(%rsp), %eax
cmpl 0x56c(%rsp), %eax
jg 0x25f89
movq 0x570(%rsp), %rax
movslq 0x510(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x25f74
movl 0x510(%rsp), %eax
movl %eax, 0x514(%rsp)
jmp 0x25f89
jmp 0x25f76
movl 0x510(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x510(%rsp)
jmp 0x25f3e
movl $0x0, 0x50c(%rsp)
movl 0x56c(%rsp), %eax
movl %eax, 0x508(%rsp)
movl 0x508(%rsp), %eax
cmpl 0x568(%rsp), %eax
jl 0x25fed
movq 0x570(%rsp), %rax
movslq 0x508(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x25fd8
movl 0x508(%rsp), %eax
movl %eax, 0x50c(%rsp)
jmp 0x25fed
jmp 0x25fda
movl 0x508(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x508(%rsp)
jmp 0x25fa2
cvtsi2sdl 0x51c(%rsp), %xmm0
movq 0x590(%rsp), %rax
movslq 0x554(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x550(%rsp), %eax
movl %eax, 0x55c(%rsp)
movl 0x54c(%rsp), %eax
movl %eax, 0x558(%rsp)
movl 0x514(%rsp), %eax
movl %eax, 0x568(%rsp)
movl 0x50c(%rsp), %eax
movl %eax, 0x56c(%rsp)
movl 0x58c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58c(%rsp)
movl 0x554(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x554(%rsp)
jmp 0x25965
movq 0x560(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x26087
movq 0x78(%rsp), %rdi
callq 0x2220
movq 0x570(%rsp), %rax
movslq 0x580(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, %rax
je 0x260b3
movq 0x70(%rsp), %rdi
callq 0x2220
testb $0x1, 0x3f123(%rip)     # 0x651dd
je 0x260d4
movq 0x3ef1d(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x2f97c(%rip), %rsi     # 0x55a49
movb $0x0, %al
callq 0x2250
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x58(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x58(%rsp), %rdi
callq 0x1ae90
movq 0x58(%rsp), %rax
movq %rax, 0x500(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x60(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x60(%rsp), %rdi
callq 0x1ae90
movq 0x60(%rsp), %rax
movq %rax, 0x4f8(%rsp)
movl 0x5fc(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x4f0(%rsp)
movq 0x4f0(%rsp), %rdi
movl 0x5fc(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2110
movq 0x590(%rsp), %rdi
movl 0x5fc(%rsp), %esi
movq 0x500(%rsp), %rdx
movq 0x4f8(%rsp), %rcx
movq 0x4f0(%rsp), %r8
xorl %r9d, %r9d
callq 0x409a0
movq 0x4f8(%rsp), %rdi
callq 0x1af10
movq %rax, 0x4e8(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x1af40
movq %rax, 0x4e0(%rsp)
movq 0x4e8(%rsp), %rdi
movq 0x4e0(%rsp), %rsi
leaq 0x1a75d(%rip), %rdx     # 0x40960
callq 0x1aec0
movq 0x500(%rsp), %rdi
callq 0x1af10
movq %rax, 0x4d8(%rsp)
movq 0x500(%rsp), %rdi
callq 0x1af40
movq %rax, 0x4d0(%rsp)
movq 0x4d8(%rsp), %rdi
movq 0x4d0(%rsp), %rsi
leaq 0x1a717(%rip), %rdx     # 0x40960
callq 0x1aec0
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x68(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x68(%rsp), %rdi
callq 0xc670
movq 0x68(%rsp), %rax
movq %rax, 0x4c8(%rsp)
testb $0x1, 0x3ef53(%rip)     # 0x651dd
je 0x262bd
movq 0x3ed4d(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x1aea0
movq 0x50(%rsp), %rdi
movl %eax, %edx
leaq 0x2f8d3(%rip), %rsi     # 0x55b89
movb $0x0, %al
callq 0x2250
movl $0x0, 0x4c4(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x1aea0
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, 0x4f(%rsp)
jbe 0x2630f
movl 0x4c4(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x4f8(%rsp), %rdi
callq 0x1aea0
movq %rax, %rcx
movl 0x48(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
setl %al
movb %al, 0x4f(%rsp)
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x2631c
jmp 0x2664e
callq 0x27b00
movq %rax, 0x4b8(%rsp)
leaq 0x2f78a(%rip), %rdi     # 0x55aba
callq 0x47b60
movq %rax, %rcx
movq 0x4b8(%rsp), %rax
movq %rcx, (%rax)
movq 0x4f8(%rsp), %rdi
movslq 0x4c4(%rsp), %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %ecx
movq 0x4b8(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x4f8(%rsp), %rdi
movl 0x4c4(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %ecx
movq 0x4b8(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x4b8(%rsp), %rax
movb $0x2b, 0x8(%rax)
movq 0x4b8(%rsp), %rax
movl 0xc(%rax), %ecx
cvttsd2si 0x5c0(%rsp), %eax
movl $0x2, %esi
cltd
idivl %esi
subl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jle 0x263d0
movl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x263fc
movq 0x4b8(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x40(%rsp)
cvttsd2si 0x5c0(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x40(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x4b4(%rsp)
cvtsi2sdl 0x5fc(%rsp), %xmm1
movq 0x4b8(%rsp), %rax
cvtsi2sdl 0x20(%rax), %xmm0
movsd 0x5c0(%rsp), %xmm2
movsd 0x2bc22(%rip), %xmm3    # 0x52050
divsd %xmm3, %xmm2
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x26449
movl 0x5fc(%rsp), %eax
movl %eax, 0x3c(%rsp)
jmp 0x26475
movq 0x4b8(%rsp), %rax
movl 0x20(%rax), %eax
movl %eax, 0x38(%rsp)
cvttsd2si 0x5c0(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x38(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x4b0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x4a8(%rsp)
movl 0x4b4(%rsp), %eax
movl %eax, 0x4a4(%rsp)
movl 0x4a4(%rsp), %eax
cmpl 0x4b0(%rsp), %eax
jg 0x26501
movsd 0x4a8(%rsp), %xmm1
movq 0x600(%rsp), %rax
movslq 0x4a4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x264ec
movq 0x600(%rsp), %rax
movslq 0x4a4(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x4a8(%rsp)
jmp 0x264ee
movl 0x4a4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4a4(%rsp)
jmp 0x2649a
xorps %xmm0, %xmm0
movsd %xmm0, 0x498(%rsp)
movq 0x4b8(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x494(%rsp)
movl 0x494(%rsp), %eax
movq 0x4b8(%rsp), %rcx
cmpl 0x20(%rcx), %eax
jg 0x2658a
movsd 0x498(%rsp), %xmm1
movq 0x590(%rsp), %rax
movslq 0x494(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x26575
movq 0x590(%rsp), %rax
movslq 0x494(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x498(%rsp)
jmp 0x26577
movl 0x494(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x494(%rsp)
jmp 0x2651f
testb $0x1, 0x3ec4c(%rip)     # 0x651dd
je 0x265da
movq 0x5a8(%rsp), %rdi
movq 0x4b8(%rsp), %rax
movl 0xc(%rax), %edx
movq 0x4b8(%rsp), %rax
movl 0x20(%rax), %ecx
movsd 0x498(%rsp), %xmm0
movsd 0x4a8(%rsp), %xmm1
movsd 0x5c0(%rsp), %xmm2
leaq 0x2f5ea(%rip), %rsi     # 0x55bbd
movb $0x3, %al
callq 0x2250
movsd 0x498(%rsp), %xmm0
movsd 0x4a8(%rsp), %xmm1
divsd 0x5c8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x26612
movq 0x4c8(%rsp), %rdi
leaq 0x4b8(%rsp), %rsi
callq 0xc8c0
jmp 0x26636
movq 0x4b8(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x26634
movq 0x30(%rsp), %rdi
movl $0x50, %esi
callq 0x21a0
jmp 0x26636
jmp 0x26638
movl 0x4c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c4(%rsp)
jmp 0x262c8
testb $0x1, 0x3eb88(%rip)     # 0x651dd
je 0x26689
movq 0x3e982(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x4c8(%rsp), %rdi
callq 0xc680
movq 0x28(%rsp), %rdi
movq %rax, %rdx
leaq 0x2f551(%rip), %rsi     # 0x55bd3
movb $0x0, %al
callq 0x2250
movq 0x5d0(%rsp), %rdi
leaq 0x4c8(%rsp), %rsi
callq 0x1adc0
movq 0x4f8(%rsp), %rdi
callq 0x41ae0
movq 0x500(%rsp), %rdi
callq 0x41ae0
testb $0x1, 0x3eb1e(%rip)     # 0x651dd
je 0x266fc
leaq 0xa0(%rsp), %rdi
movsd 0x5c0(%rsp), %xmm0
leaq 0x2f51d(%rip), %rsi     # 0x55bf6
movb $0x1, %al
callq 0x2090
movq 0x590(%rsp), %rdi
movl 0x5fc(%rsp), %esi
leaq 0xa0(%rsp), %rdx
callq 0x35140
movq 0x4f0(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x26719
movq 0x20(%rsp), %rdi
callq 0x2220
movq 0x590(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x26736
movq 0x18(%rsp), %rdi
callq 0x2220
movsd 0x5e8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2675e
movsd 0x5c0(%rsp), %xmm0
ucomisd 0x5e8(%rsp), %xmm0
jb 0x2675e
jmp 0x26774
movl 0x5bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5bc(%rsp)
jmp 0x25652
movq 0x3e865(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x5d8(%rsp), %rdi
callq 0x11c40
movq 0x10(%rsp), %rdi
movl %eax, %edx
leaq 0x2f469(%rip), %rsi     # 0x55c07
movb $0x0, %al
callq 0x2250
testb $0x1, 0x3ea31(%rip)     # 0x651dd
je 0x267bb
movq 0x5a8(%rsp), %rdi
callq 0x2180
addq $0x608, %rsp            # imm = 0x608
retq
nopw %cs:(%rax,%rax)