// File: gersteinlab[P]MUSIC/src/ms_filter_utils.cpp
// Function: multiscale_median_filter_data(double*, int, double, double, double, std::vector<double, std::allocator<double>>*, bool, bool, bool, bool, std::vector<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::allocator<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*>>*, std::vector<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*, std::allocator<std::vector<t_annot_region*, std::allocator<t_annot_region*>>*>>*, char const*)
vector<double*>* multiscale_median_filter_data(double* track_data, 
	int l_track_data, 
	double scale_start, double scale_end, double log_scale_step, 
	vector<double>* scales_per_i_scale,
	bool dump_decomposition,
	bool compute_extrema_regions,
	bool dump_extrema_regions,
	bool return_filtered_tracks,
	vector<vector<t_annot_region*>*>* per_scale_minima_regions, 
	vector<vector<t_annot_region*>*>* per_scale_maxima_regions,
	const char* op_file_prefix)
{
	vector<double*>* decomps = new vector<double*>();

	// 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;
	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)
		{
			decomps->push_back(NULL);

			if(compute_extrema_regions)
			{
				if(per_scale_minima_regions != NULL)
				{
					per_scale_minima_regions->push_back(new vector<t_annot_region*>());
				}

				if(per_scale_maxima_regions != NULL)
				{
					per_scale_maxima_regions->push_back(new vector<t_annot_region*>());
				}
			}

			i_scale++;
			continue;
		}
		else
		{
if(__DUMP_FILTER_MSGS__)
			fprintf(stderr, "Processing scale %.2f (%.2f)\n", scale, 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];

			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 += 1000;
			MIN_VAL -= 1000;
		
			// 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.
			//for(int cur_win_mid = 0; cur_win_mid < l_track_data; cur_win_mid++)
			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;
				for(int i = cur_win_min; i <= cur_win_max; i++)
				{
					n_total += cur_win_pdf[i];
				} // i loop.

				// At this point, the histogram is updated, now must find the median for the histogram.
				int cur_n_total = 0;
				int cur_win_median = 0;
				for(int i = cur_win_min; i <= cur_win_max; i++)
				{
					cur_n_total += cur_win_pdf[i];
					if(cur_n_total > n_total/2)
					{
						// 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);

			// Copy the data.
			if(dump_decomposition)
			{
				// Dump and free memory.
				char cur_decomp_fp[1000];
				//sprintf(cur_decomp_fp, "decomp_%d_%d_%d.bin", i_scale, cur_win_start, cur_win_start + cur_l_win);
				sprintf(cur_decomp_fp, "%s_scale_%d.bin", op_file_prefix, i_scale);

if(__DUMP_FILTER_MSGS__)
				fprintf(stderr, "Dumping %s\n", cur_decomp_fp);

				double* _cur_filtered_track = get_one_indexed_per_zero_indexed_data(cur_filtered_track, l_track_data);
				dump_per_nucleotide_binary_profile(_cur_filtered_track, l_track_data, cur_decomp_fp);
				delete [] _cur_filtered_track;

if(__DUMP_FILTER_MSGS__)
				fprintf(stderr, "Closing file\n");
			}
			
			if(compute_extrema_regions)
			{
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);

				if(dump_extrema_regions)
				{
					// Get the minima and maxima, then dump.
					char cur_minima_regs_fp[1000];
					char cur_maxima_regs_fp[1000];
					sprintf(cur_minima_regs_fp, "%s_scale_%d_mins.bed", op_file_prefix, i_scale);
					sprintf(cur_maxima_regs_fp, "%s_scale_%d_maxes.bed", op_file_prefix, i_scale);

					fprintf(stderr, "Dumping the extrema regions.\n");
					FILE* f_maxes = open_f(cur_maxima_regs_fp, "w");
					for(int i_m = 0; i_m < (int)maxima->size()-1; i_m++)
					{
						fprintf(f_maxes, "XX\t%d\t%d\n", translate_coord(maxima->at(i_m)->extrema_posn+1, CODEBASE_COORDS::start_base, BED_COORDS::start_base),
							translate_coord(maxima->at(i_m)->extrema_posn+1, CODEBASE_COORDS::end_base, BED_COORDS::end_base));
					} // i_m loop.
					fclose(f_maxes);

					FILE* f_mins = open_f(cur_minima_regs_fp, "w");
					for(int i_m = 0; i_m < (int)minima->size()-1; i_m++)
					{
						fprintf(f_mins, "XX\t%d\t%d\n", translate_coord(minima->at(i_m)->extrema_posn+1, CODEBASE_COORDS::start_base, BED_COORDS::start_base),
							translate_coord(minima->at(i_m+1)->extrema_posn+1, CODEBASE_COORDS::end_base, BED_COORDS::end_base));
					} // i_m loop.
					fclose(f_mins);
				}

				if(per_scale_minima_regions != NULL)
				{
					vector<t_annot_region*>* cur_scale_minima_regions = new vector<t_annot_region*>();
					for(int i_m = 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->strand = '+';

						cur_scale_minima_regions->push_back(new_minima);
					} // i_min loop.

					per_scale_minima_regions->push_back(cur_scale_minima_regions);
				}
			
				if(per_scale_maxima_regions != NULL)
				{
					vector<t_annot_region*>* cur_scale_maxima_regions = new vector<t_annot_region*>();
					for(int i_m = 0; i_m < (int)maxima->size()-1; i_m++)
					{
						t_annot_region* new_maxima = get_empty_region();
						new_maxima->chrom = t_string::copy_me_str("XX");
						new_maxima->start = translate_coord(maxima->at(i_m)->extrema_posn+1, CODEBASE_COORDS::start_base, BED_COORDS::start_base);
						new_maxima->end = translate_coord(maxima->at(i_m+1)->extrema_posn+1, CODEBASE_COORDS::end_base, BED_COORDS::end_base);
						new_maxima->strand = '+';

						cur_scale_maxima_regions->push_back(new_maxima);
					} // i_m loop.

					per_scale_maxima_regions->push_back(cur_scale_maxima_regions);
				}

				delete_extrema_nodes(minima);
				delete_extrema_nodes(maxima);
			
				// Free the current decomposition.
				delete [] derivative_map;
			}
			
			// Do we need the filtered track back?
			if(return_filtered_tracks)
			{
				// Store the decompositions.
				decomps->push_back(cur_filtered_track);
			}
			else
			{
				// Clean track memory.
				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.

	return(decomps);
}

// Assembly:
subq $0xe38, %rsp            # imm = 0xE38
movl %r9d, 0xb8(%rsp)
movl %r8d, 0xbc(%rsp)
movl %ecx, %eax
movl 0xb8(%rsp), %ecx
movq %rdx, %r8
movl 0xbc(%rsp), %edx
movq %r8, 0xc0(%rsp)
movl %esi, %r8d
movq %rdi, %r9
movq 0xc0(%rsp), %rdi
movb %al, %sil
movq 0xe58(%rsp), %rax
movq 0xe50(%rsp), %rax
movq 0xe48(%rsp), %rax
movb 0xe40(%rsp), %al
movq %r9, 0xe30(%rsp)
movl %r8d, 0xe2c(%rsp)
movsd %xmm0, 0xe20(%rsp)
movsd %xmm1, 0xe18(%rsp)
movsd %xmm2, 0xe10(%rsp)
movq %rdi, 0xe08(%rsp)
andb $0x1, %sil
movb %sil, 0xe07(%rsp)
andb $0x1, %dl
movb %dl, 0xe06(%rsp)
andb $0x1, %cl
movb %cl, 0xe05(%rsp)
andb $0x1, %al
movb %al, 0xe04(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0xc8(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0xc8(%rsp), %rdi
callq 0x26a00
movq 0xc8(%rsp), %rax
movq %rax, 0xdf8(%rsp)
movsd 0x2e064(%rip), %xmm0    # 0x52018
divsd 0xe10(%rsp), %xmm0
movsd %xmm0, 0xdf0(%rsp)
movl $0x0, 0xdec(%rsp)
movsd 0xe10(%rsp), %xmm0
mulsd 0xdf0(%rsp), %xmm0
movsd %xmm0, 0xdf0(%rsp)
movq 0xe08(%rsp), %rdi
leaq 0xdf0(%rsp), %rsi
callq 0xc850
movsd 0xdf0(%rsp), %xmm1
movsd 0xe20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x24140
movq 0xdf8(%rsp), %rdi
movq $0x0, 0xde0(%rsp)
leaq 0xde0(%rsp), %rsi
callq 0x26a10
testb $0x1, 0xe06(%rsp)
je 0x2412a
cmpq $0x0, 0xe48(%rsp)
je 0x240ba
movq 0xe48(%rsp), %rax
movq %rax, 0xb0(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0xa8(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0xa8(%rsp), %rdi
callq 0xc670
movq 0xa8(%rsp), %rax
movq 0xb0(%rsp), %rdi
movq %rax, 0xdd8(%rsp)
leaq 0xdd8(%rsp), %rsi
callq 0x26a40
cmpq $0x0, 0xe50(%rsp)
je 0x24128
movq 0xe50(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x98(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x98(%rsp), %rdi
callq 0xc670
movq 0x98(%rsp), %rax
movq 0xa0(%rsp), %rdi
movq %rax, 0xdd0(%rsp)
leaq 0xdd0(%rsp), %rsi
callq 0x26a40
jmp 0x2412a
movl 0xdec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdec(%rsp)
jmp 0x23fd1
testb $0x1, 0x41096(%rip)     # 0x651dd
je 0x24173
movq 0x40e90(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movsd 0xdf0(%rsp), %xmm0
movsd 0xe18(%rsp), %xmm1
leaq 0x31951(%rip), %rsi     # 0x55abd
movb $0x2, %al
callq 0x2250
cvttsd2si 0xdf0(%rsp), %eax
movl %eax, 0xdcc(%rsp)
movl 0xdcc(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x1, %edx
jne 0x241aa
movl 0xdcc(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x241b8
movl 0xdcc(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0xdc8(%rsp)
movl 0xe2c(%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, 0xdc0(%rsp)
movl $0x0, 0xdbc(%rsp)
movl 0xdc8(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0xdb8(%rsp)
movl $0xfff0bdc0, 0xdb4(%rsp) # imm = 0xFFF0BDC0
movl $0xf4240, 0xdb0(%rsp) # imm = 0xF4240
movl $0x1, 0xdac(%rsp)
movl 0xdac(%rsp), %eax
cmpl 0xe2c(%rsp), %eax
jg 0x242e1
movl 0xdb4(%rsp), %eax
movq 0xe30(%rsp), %rcx
movslq 0xdac(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
cmpl %ecx, %eax
jge 0x2428d
movq 0xe30(%rsp), %rax
movslq 0xdac(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0xdb4(%rsp)
movl 0xdb0(%rsp), %eax
movq 0xe30(%rsp), %rcx
movslq 0xdac(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
cmpl %ecx, %eax
jle 0x242c9
movq 0xe30(%rsp), %rax
movslq 0xdac(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0xdb0(%rsp)
jmp 0x242cb
movl 0xdac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdac(%rsp)
jmp 0x2423d
movl 0xdb4(%rsp), %eax
addl $0x3e8, %eax            # imm = 0x3E8
movl %eax, 0xdb4(%rsp)
movl 0xdb0(%rsp), %eax
subl $0x3e8, %eax            # imm = 0x3E8
movl %eax, 0xdb0(%rsp)
movq $0x0, 0xda0(%rsp)
movl $0x0, 0xd9c(%rsp)
movl $0xf4240, 0xd98(%rsp) # imm = 0xF4240
movl 0xdc8(%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, 0xd90(%rsp)
movl $0x0, 0xd8c(%rsp)
movl $0x0, 0xd88(%rsp)
movl $0x1, 0xd84(%rsp)
movl 0xd84(%rsp), %eax
cmpl 0xe2c(%rsp), %eax
jg 0x24adb
movl 0xd84(%rsp), %eax
cmpl 0xdb8(%rsp), %eax
jle 0x243ba
movl 0xd84(%rsp), %eax
subl 0xdb8(%rsp), %eax
movl %eax, 0x90(%rsp)
jmp 0x243c8
movl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x243c8
movl 0x90(%rsp), %eax
movl %eax, 0xd80(%rsp)
movl 0xd84(%rsp), %eax
addl 0xdb8(%rsp), %eax
cmpl 0xe2c(%rsp), %eax
jg 0x24404
movl 0xd84(%rsp), %eax
addl 0xdb8(%rsp), %eax
movl %eax, 0x8c(%rsp)
jmp 0x24412
movl 0xe2c(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0xd7c(%rsp)
cmpq $0x0, 0xda0(%rsp)
jne 0x2459c
movl 0xdb4(%rsp), %eax
subl 0xdb0(%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, 0xda0(%rsp)
movq 0xda0(%rsp), %rdi
movl 0xdb4(%rsp), %eax
subl 0xdb0(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2110
movl 0xdb0(%rsp), %ecx
movq 0xda0(%rsp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0xda0(%rsp)
movl 0xd80(%rsp), %eax
movl %eax, 0xd78(%rsp)
movl 0xd78(%rsp), %eax
cmpl 0xd7c(%rsp), %eax
jg 0x24597
movq 0xda0(%rsp), %rax
movq 0xe30(%rsp), %rcx
movslq 0xd78(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
cvtsi2sdl 0xd9c(%rsp), %xmm1
movq 0xe30(%rsp), %rax
movslq 0xd78(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x24543
movq 0xe30(%rsp), %rax
movslq 0xd78(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0xd9c(%rsp)
cvtsi2sdl 0xd98(%rsp), %xmm0
movq 0xe30(%rsp), %rax
movslq 0xd78(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x2457f
movq 0xe30(%rsp), %rax
movslq 0xd78(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0xd98(%rsp)
jmp 0x24581
movl 0xd78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd78(%rsp)
jmp 0x244c6
jmp 0x24895
movl 0xd8c(%rsp), %eax
movl %eax, 0xd74(%rsp)
movl 0xd74(%rsp), %eax
cmpl 0xd80(%rsp), %eax
jge 0x245f6
movq 0xda0(%rsp), %rax
movq 0xe30(%rsp), %rcx
movslq 0xd74(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $-0x1, %edx
movl %edx, (%rax,%rcx,4)
movl 0xd74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd74(%rsp)
jmp 0x245aa
movl 0xd88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd70(%rsp)
movl 0xd70(%rsp), %eax
cmpl 0xd7c(%rsp), %eax
jg 0x246d8
movq 0xda0(%rsp), %rax
movq 0xe30(%rsp), %rcx
movslq 0xd70(%rsp), %rdx
cvttsd2si (%rcx,%rdx,8), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
cvtsi2sdl 0xd9c(%rsp), %xmm1
movq 0xe30(%rsp), %rax
movslq 0xd70(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x24684
movq 0xe30(%rsp), %rax
movslq 0xd70(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0xd9c(%rsp)
cvtsi2sdl 0xd98(%rsp), %xmm0
movq 0xe30(%rsp), %rax
movslq 0xd70(%rsp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x246c0
movq 0xe30(%rsp), %rax
movslq 0xd70(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0xd98(%rsp)
jmp 0x246c2
movl 0xd70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd70(%rsp)
jmp 0x24607
movl $0x0, 0xd6c(%rsp)
movl 0xd98(%rsp), %eax
movl %eax, 0xd68(%rsp)
movl 0xd68(%rsp), %eax
cmpl 0xd9c(%rsp), %eax
jg 0x24735
movq 0xda0(%rsp), %rax
movslq 0xd68(%rsp), %rcx
movl (%rax,%rcx,4), %eax
addl 0xd6c(%rsp), %eax
movl %eax, 0xd6c(%rsp)
movl 0xd68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd68(%rsp)
jmp 0x246f1
movl 0xd6c(%rsp), %eax
movl 0xd7c(%rsp), %ecx
subl 0xd80(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
je 0x24893
movq 0x40884(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movl 0xd80(%rsp), %edx
movl 0xd7c(%rsp), %ecx
movl 0xd98(%rsp), %r8d
movl 0xd9c(%rsp), %r9d
movl 0xd6c(%rsp), %r10d
movl 0xd7c(%rsp), %eax
subl 0xd80(%rsp), %eax
addl $0x1, %eax
leaq 0x3133e(%rip), %rsi     # 0x55adb
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x2250
movl 0xd98(%rsp), %eax
movl %eax, 0xd64(%rsp)
movl 0xd64(%rsp), %eax
cmpl 0xd9c(%rsp), %eax
jg 0x24808
movq 0x4080f(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
movq 0xda0(%rsp), %rax
movslq 0xd64(%rsp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x3131b(%rip), %rsi     # 0x55b09
movb $0x0, %al
callq 0x2250
movl 0xd64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd64(%rsp)
jmp 0x247ba
leaq 0x322e8(%rip), %rdi     # 0x56af7
leaq 0x2e28e(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0xd58(%rsp)
movl 0xd80(%rsp), %eax
movl %eax, 0xd54(%rsp)
movl 0xd54(%rsp), %eax
cmpl 0xd7c(%rsp), %eax
jg 0x2487f
movq 0xd58(%rsp), %rdi
movq 0xe30(%rsp), %rax
movslq 0xd54(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x312a8(%rip), %rsi     # 0x55b0d
movb $0x1, %al
callq 0x2250
movl 0xd54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd54(%rsp)
jmp 0x24831
movq 0xd58(%rsp), %rdi
callq 0x2180
xorl %edi, %edi
callq 0x21f0
jmp 0x24895
movl $0x0, 0xd50(%rsp)
movl 0xd98(%rsp), %eax
movl %eax, 0xd4c(%rsp)
movl 0xd4c(%rsp), %eax
cmpl 0xd9c(%rsp), %eax
jg 0x248f2
movq 0xda0(%rsp), %rax
movslq 0xd4c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
addl 0xd50(%rsp), %eax
movl %eax, 0xd50(%rsp)
movl 0xd4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4c(%rsp)
jmp 0x248ae
movl $0x0, 0xd48(%rsp)
movl $0x0, 0xd44(%rsp)
movl 0xd98(%rsp), %eax
movl %eax, 0xd40(%rsp)
movl 0xd40(%rsp), %eax
cmpl 0xd9c(%rsp), %eax
jg 0x24996
movq 0xda0(%rsp), %rax
movslq 0xd40(%rsp), %rcx
movl (%rax,%rcx,4), %eax
addl 0xd48(%rsp), %eax
movl %eax, 0xd48(%rsp)
movl 0xd48(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0xd50(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x88(%rsp), %eax
cmpl %ecx, %eax
jle 0x24981
movl 0xd40(%rsp), %eax
movl %eax, 0xd44(%rsp)
jmp 0x24996
jmp 0x24983
movl 0xd40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd40(%rsp)
jmp 0x24916
movl $0x0, 0xd3c(%rsp)
movl 0xd98(%rsp), %eax
movl %eax, 0xd38(%rsp)
movl 0xd38(%rsp), %eax
cmpl 0xd9c(%rsp), %eax
jg 0x249fa
movq 0xda0(%rsp), %rax
movslq 0xd38(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x249e5
movl 0xd38(%rsp), %eax
movl %eax, 0xd3c(%rsp)
jmp 0x249fa
jmp 0x249e7
movl 0xd38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd38(%rsp)
jmp 0x249af
movl $0x0, 0xd34(%rsp)
movl 0xd9c(%rsp), %eax
movl %eax, 0xd30(%rsp)
movl 0xd30(%rsp), %eax
cmpl 0xd98(%rsp), %eax
jl 0x24a5e
movq 0xda0(%rsp), %rax
movslq 0xd30(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x24a49
movl 0xd30(%rsp), %eax
movl %eax, 0xd34(%rsp)
jmp 0x24a5e
jmp 0x24a4b
movl 0xd30(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xd30(%rsp)
jmp 0x24a13
cvtsi2sdl 0xd44(%rsp), %xmm0
movq 0xdc0(%rsp), %rax
movslq 0xd84(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0xd80(%rsp), %eax
movl %eax, 0xd8c(%rsp)
movl 0xd7c(%rsp), %eax
movl %eax, 0xd88(%rsp)
movl 0xd3c(%rsp), %eax
movl %eax, 0xd98(%rsp)
movl 0xd34(%rsp), %eax
movl %eax, 0xd9c(%rsp)
movl 0xdbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdbc(%rsp)
movl 0xd84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd84(%rsp)
jmp 0x2437f
movq 0xd90(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, %rax
je 0x24afe
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0xda0(%rsp), %rax
movslq 0xdb0(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x24b2a
movq 0x78(%rsp), %rdi
callq 0x2220
testb $0x1, 0xe07(%rsp)
je 0x24bfe
leaq 0x940(%rsp), %rdi
movq 0xe58(%rsp), %rdx
movl 0xdec(%rsp), %ecx
leaq 0x3046c(%rip), %rsi     # 0x54fc2
movb $0x0, %al
callq 0x2090
testb $0x1, 0x40679(%rip)     # 0x651dd
je 0x24b86
movq 0x40473(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x940(%rsp), %rdx
leaq 0x30eb0(%rip), %rsi     # 0x55a2f
movb $0x0, %al
callq 0x2250
movq 0xdc0(%rsp), %rdi
movl 0xe2c(%rsp), %esi
callq 0x34630
movq %rax, 0x938(%rsp)
movq 0x938(%rsp), %rdi
movl 0xe2c(%rsp), %esi
leaq 0x940(%rsp), %rdx
callq 0x35140
movq 0x938(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, %rax
je 0x24bdb
movq 0x70(%rsp), %rdi
callq 0x2220
testb $0x1, 0x405fb(%rip)     # 0x651dd
je 0x24bfc
movq 0x403f5(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x30e46(%rip), %rsi     # 0x55a3b
movb $0x0, %al
callq 0x2250
jmp 0x24bfe
testb $0x1, 0xe06(%rsp)
je 0x252ef
testb $0x1, 0x405ca(%rip)     # 0x651dd
je 0x24c2d
movq 0x403c4(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x30e23(%rip), %rsi     # 0x55a49
movb $0x0, %al
callq 0x2250
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, 0x930(%rsp)
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 0x1ae90
movq 0x68(%rsp), %rax
movq %rax, 0x928(%rsp)
movl 0xe2c(%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, 0x920(%rsp)
movq 0x920(%rsp), %rdi
movl 0xe2c(%rsp), %eax
addl $0x2, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2110
movq 0xdc0(%rsp), %rdi
movl 0xe2c(%rsp), %esi
movq 0x930(%rsp), %rdx
movq 0x928(%rsp), %rcx
movq 0x920(%rsp), %r8
xorl %r9d, %r9d
callq 0x409a0
movq 0x928(%rsp), %rdi
callq 0x1af10
movq %rax, 0x918(%rsp)
movq 0x928(%rsp), %rdi
callq 0x1af40
movq %rax, 0x910(%rsp)
movq 0x918(%rsp), %rdi
movq 0x910(%rsp), %rsi
leaq 0x1bc04(%rip), %rdx     # 0x40960
callq 0x1aec0
movq 0x930(%rsp), %rdi
callq 0x1af10
movq %rax, 0x908(%rsp)
movq 0x930(%rsp), %rdi
callq 0x1af40
movq %rax, 0x900(%rsp)
movq 0x908(%rsp), %rdi
movq 0x900(%rsp), %rsi
leaq 0x1bbbe(%rip), %rdx     # 0x40960
callq 0x1aec0
testb $0x1, 0xe05(%rsp)
je 0x24ffc
leaq 0x510(%rsp), %rdi
movq 0xe58(%rsp), %rdx
movl 0xdec(%rsp), %ecx
leaq 0x30c94(%rip), %rsi     # 0x55a67
movb $0x0, %al
callq 0x2090
leaq 0x120(%rsp), %rdi
movq 0xe58(%rsp), %rdx
movl 0xdec(%rsp), %ecx
leaq 0x30c84(%rip), %rsi     # 0x55a7c
movb $0x0, %al
callq 0x2090
movq 0x401da(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x30c82(%rip), %rsi     # 0x55a92
movb $0x0, %al
callq 0x2250
leaq 0x120(%rsp), %rdi
leaq 0x2dc7e(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0x118(%rsp)
movl $0x0, 0x114(%rsp)
movl 0x114(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x930(%rsp), %rdi
callq 0x1aea0
movq %rax, %rcx
movl 0x5c(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x24efa
movq 0x118(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x930(%rsp), %rdi
movslq 0x114(%rsp), %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1fd20
movl %eax, 0x58(%rsp)
movq 0x930(%rsp), %rdi
movslq 0x114(%rsp), %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %edx
movl %edx, %esi
callq 0x1fd20
movq 0x50(%rsp), %rdi
movl 0x58(%rsp), %edx
movl %eax, %ecx
leaq 0x30bd3(%rip), %rsi     # 0x55ab0
movb $0x0, %al
callq 0x2250
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x24e3e
movq 0x118(%rsp), %rdi
callq 0x2180
leaq 0x510(%rsp), %rdi
leaq 0x2db8e(%rip), %rsi     # 0x52aa4
callq 0x455f0
movq %rax, 0x108(%rsp)
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x928(%rsp), %rdi
callq 0x1aea0
movq %rax, %rcx
movl 0x4c(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x24fef
movq 0x108(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x928(%rsp), %rdi
movslq 0x104(%rsp), %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1fd20
movl %eax, 0x48(%rsp)
movq 0x928(%rsp), %rdi
movl 0x104(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %edx
movl %edx, %esi
callq 0x1fd20
movq 0x40(%rsp), %rdi
movl 0x48(%rsp), %edx
movl %eax, %ecx
leaq 0x30ade(%rip), %rsi     # 0x55ab0
movb $0x0, %al
callq 0x2250
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x24f2e
movq 0x108(%rsp), %rdi
callq 0x2180
cmpq $0x0, 0xe48(%rsp)
je 0x25159
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x38(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x38(%rsp), %rdi
callq 0xc670
movq 0x38(%rsp), %rax
movq %rax, 0xf8(%rsp)
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x928(%rsp), %rdi
callq 0x1aea0
movq %rax, %rcx
movl 0x34(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x25144
callq 0x27b00
movq %rax, 0xe8(%rsp)
leaq 0x30a31(%rip), %rdi     # 0x55aba
callq 0x47b60
movq %rax, %rcx
movq 0xe8(%rsp), %rax
movq %rcx, (%rax)
movq 0x928(%rsp), %rdi
movslq 0xf4(%rsp), %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1fd20
movl %eax, %ecx
movq 0xe8(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x928(%rsp), %rdi
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %edx
movl %edx, %esi
callq 0x1fd20
movl %eax, %ecx
movq 0xe8(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0xe8(%rsp), %rax
movb $0x2b, 0x8(%rax)
movq 0xf8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0xc8c0
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x2504b
movq 0xe48(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1adc0
cmpq $0x0, 0xe50(%rsp)
je 0x252b6
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x28(%rsp), %rdi
callq 0xc670
movq 0x28(%rsp), %rax
movq %rax, 0xe0(%rsp)
movl $0x0, 0xdc(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x930(%rsp), %rdi
callq 0x1aea0
movq %rax, %rcx
movl 0x24(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x252a1
callq 0x27b00
movq %rax, 0xd0(%rsp)
leaq 0x308d4(%rip), %rdi     # 0x55aba
callq 0x47b60
movq %rax, %rcx
movq 0xd0(%rsp), %rax
movq %rcx, (%rax)
movq 0x930(%rsp), %rdi
movslq 0xdc(%rsp), %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1fd20
movl %eax, %ecx
movq 0xd0(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x930(%rsp), %rdi
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x1af70
movq (%rax), %rax
movl (%rax), %edi
addl $0x1, %edi
movl $0x1, %edx
movl %edx, %esi
callq 0x1fd20
movl %eax, %ecx
movq 0xd0(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0xd0(%rsp), %rax
movb $0x2b, 0x8(%rax)
movq 0xe0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0xc8c0
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x251a8
movq 0xe50(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x1adc0
movq 0x928(%rsp), %rdi
callq 0x41ae0
movq 0x930(%rsp), %rdi
callq 0x41ae0
movq 0x920(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x252ed
movq 0x18(%rsp), %rdi
callq 0x2220
jmp 0x252ef
testb $0x1, 0xe04(%rsp)
je 0x25310
movq 0xdf8(%rsp), %rdi
leaq 0xdc0(%rsp), %rsi
callq 0x26a70
jmp 0x2532f
movq 0xdc0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x2532d
movq 0x10(%rsp), %rdi
callq 0x2220
jmp 0x2532f
movsd 0xe18(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x25357
movsd 0xdf0(%rsp), %xmm0
ucomisd 0xe18(%rsp), %xmm0
jb 0x25357
jmp 0x2536d
movl 0xdec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdec(%rsp)
jmp 0x23fd1
movq 0xdf8(%rsp), %rax
addq $0xe38, %rsp            # imm = 0xE38
retq
nopl (%rax)