// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x888, %rsp            # imm = 0x888
movl %r9d, 0x70(%rsp)
movl %r8d, %r12d
movl %ecx, 0x74(%rsp)
movq %rdx, %r13
movsd %xmm2, 0x80(%rsp)
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0xa8(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
movq 0x8d0(%rsp), %r14
movl $0x18, %edi
callq 0x2180
movsd 0x10(%rsp), %xmm1
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movsd 0x15f7f(%rip), %xmm0    # 0x2a008
divsd 0x80(%rsp), %xmm0
movl %ebp, 0x4(%rsp)
movslq %ebp, %rcx
movq %r14, %rbp
cmpl $-0x2, %ecx
leaq 0x10(,%rcx,8), %rdx
movq $-0x1, %rsi
cmovlq %rsi, %rdx
movq %rdx, 0xa0(%rsp)
leaq 0x8(,%rcx,4), %rdx
movq %rdx, 0x98(%rsp)
cmovlq %rsi, %rdx
movq %rdx, 0x90(%rsp)
incl %ecx
movq %rcx, 0x88(%rsp)
movl $0x0, 0x2c(%rsp)
movl %r12d, 0x64(%rsp)
movq %r13, 0x78(%rsp)
movq %rax, 0x30(%rsp)
mulsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x8(%r13), %rsi
cmpq 0x10(%r13), %rsi
je 0x1411e
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r13)
jmp 0x14136
movq %r13, %rdi
leaq 0x58(%rsp), %rdx
callq 0xb60c
movq 0x30(%rsp), %rax
movsd 0x10(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm2
ucomisd %xmm0, %xmm2
jbe 0x14208
movq $0x0, 0xb0(%rsp)
movq %rax, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1519e
movq 0x8c8(%rsp), %rax
movsd 0x10(%rsp), %xmm1
testb %r12b, %r12b
je 0x141fd
testq %rax, %rax
je 0x141c0
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0xb0(%rsp)
movq 0x8c8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x15312
movsd 0x10(%rsp), %xmm1
testq %rbp, %rbp
je 0x141fd
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0xb0(%rsp)
movq %rbp, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x15312
movsd 0x10(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
jmp 0x14cf3
cmpb $0x1, 0x24fbe(%rip)     # 0x391cd
je 0x14d01
cvttsd2si %xmm0, %ebp
movl %ebp, %eax
andl $0x80000001, %eax       # imm = 0x80000001
xorl %r14d, %r14d
cmpl $0x1, %eax
sete %r15b
movq 0xa0(%rsp), %rdi
callq 0x2040
movq %rax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x1452a
movb %r15b, %r14b
subl %r14d, %ebp
movl %ebp, %eax
shrl $0x1f, %eax
addl %ebp, %eax
sarl %eax
movl $0xf4240, %ecx          # imm = 0xF4240
movl $0xfff0bdc0, %edx       # imm = 0xFFF0BDC0
movl $0x1, %esi
movq 0x88(%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 0x14272
cmpl $0x0, 0x4(%rsp)
jle 0x1452a
subl %ecx, %edx
addl $0xfffffc18, %ecx       # imm = 0xFFFFFC18
leal 0x7d0(%rdx), %esi
leal 0x7d2(%rdx), %edi
movslq %edi, %rdi
shlq $0x2, %rdi
cmpl $-0x2, %esi
movq $-0x1, %rsi
cmovlq %rsi, %rdi
movq %rdi, 0x40(%rsp)
addl $0x7d1, %edx            # imm = 0x7D1
movslq %edx, %rdx
shlq $0x2, %rdx
movq %rdx, 0x20(%rsp)
movslq %ecx, %rdx
movq %rdx, 0x68(%rsp)
negq %rdx
movq %rdx, 0x38(%rsp)
cltq
movq %rax, 0x8(%rsp)
movl $0xf4240, %r14d         # imm = 0xF4240
xorl %r12d, %r12d
movl $0x1, %r15d
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %ecx, %ecx
movl %ecx, %eax
movl %ebp, %esi
movl %r15d, %ebp
movq 0x8(%rsp), %rcx
subl %ecx, %ebp
cmpq %rcx, %r15
movl $0x1, %edx
cmovlel %edx, %ebp
addl %r15d, %ecx
movl 0x4(%rsp), %edx
cmpl %edx, %ecx
cmovgel %edx, %ecx
testq %r12, %r12
je 0x143cb
cmpl %ebp, %esi
jge 0x1434f
movslq %esi, %rdi
movslq %ebp, %rdx
cvttsd2si (%rbx,%rdi,8), %esi
movslq %esi, %rsi
decl (%r12,%rsi,4)
incq %rdi
cmpq %rdi, %rdx
jne 0x1433b
cmpl %ecx, %eax
jge 0x14391
cltq
movslq %ecx, %rsi
movsd 0x8(%rbx,%rax,8), %xmm0
incq %rax
cvttsd2si %xmm0, %edx
movslq %edx, %rdx
incl (%r12,%rdx,4)
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
ucomisd %xmm1, %xmm0
cmoval %edx, %r13d
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
ucomisd %xmm0, %xmm1
cmoval %edx, %r14d
cmpq %rsi, %rax
jl 0x14358
xorl %r10d, %r10d
cmpl %r13d, %r14d
jg 0x143b8
movslq %r14d, %rax
leaq (%r12,%rax,4), %rax
movl %r13d, %esi
subl %r14d, %esi
incl %esi
xorl %edx, %edx
xorl %r10d, %r10d
addl (%rax,%rdx,4), %r10d
incq %rdx
cmpl %edx, %esi
jne 0x143ad
movl %ecx, %r11d
subl %ebp, %r11d
incl %r11d
cmpl %r11d, %r10d
je 0x14442
jmp 0x14f0e
movq 0x40(%rsp), %rdi
movl %ebp, 0x18(%rsp)
movl %ecx, %ebp
callq 0x2040
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
callq 0x20f0
movl %ebp, %ecx
movl 0x18(%rsp), %ebp
movq 0x38(%rsp), %rax
leaq (%r12,%rax,4), %r12
cmpl %ebp, %ecx
jl 0x14442
movslq %ebp, %rax
movslq %ecx, %rsi
decq %rax
movsd 0x8(%rbx,%rax,8), %xmm0
cvttsd2si %xmm0, %edx
movslq %edx, %rdx
incl (%r12,%rdx,4)
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
ucomisd %xmm1, %xmm0
cmoval %edx, %r13d
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
ucomisd %xmm0, %xmm1
cmoval %edx, %r14d
incq %rax
cmpq %rsi, %rax
jl 0x14409
xorpd %xmm0, %xmm0
cmpl %r13d, %r14d
jg 0x14472
movslq %r14d, %rax
leal 0x1(%r13), %r10d
leaq (%r12,%rax,4), %rdx
movl %r10d, %esi
subl %r14d, %esi
xorl %r8d, %r8d
xorl %edi, %edi
addl (%rdx,%r8,4), %edi
incq %r8
cmpl %r8d, %esi
jne 0x14461
cmpl %r13d, %r14d
jle 0x1449a
xorl %esi, %esi
xorl %edi, %edi
movq 0x48(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
movl %edi, %r13d
movl %esi, %r14d
cmpq 0x88(%rsp), %r15
jne 0x14303
jmp 0x14517
movl %edi, %esi
shrl $0x1f, %esi
addl %edi, %esi
sarl %esi
xorl %edi, %edi
movq %rdx, %r9
movl %r14d, %r8d
addl (%r9), %edi
cmpl %esi, %edi
jg 0x144c4
incl %r8d
addq $0x4, %r9
cmpl %r8d, %r10d
jne 0x144ab
xorpd %xmm0, %xmm0
jmp 0x144cc
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
xorl %edi, %edi
cmpl %r13d, %r14d
jle 0x144d7
xorl %esi, %esi
jmp 0x14476
movl %r14d, %esi
cmpl $0x0, (%rdx)
jg 0x144ec
incl %esi
addq $0x4, %rdx
cmpl %esi, %r10d
jne 0x144da
xorl %esi, %esi
cmpl %r13d, %r14d
jg 0x14476
movslq %r13d, %rdx
incq %rdx
cmpl $0x0, -0x4(%r12,%rdx,4)
jg 0x1450f
decl %r13d
decq %rdx
cmpq %rax, %rdx
jg 0x144f7
jmp 0x14476
movl %r13d, %edi
jmp 0x14476
movq 0x68(%rsp), %rax
leaq (%r12,%rax,4), %rdi
callq 0x2220
movl 0x64(%rsp), %r12d
cmpb $0x0, 0x74(%rsp)
movq 0x78(%rsp), %r13
movq 0x8d0(%rsp), %rbp
je 0x145b2
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
leaq 0x186ac(%rip), %rsi     # 0x2cbfc
movq 0x8d8(%rsp), %rdx
movl 0x2c(%rsp), %ecx
xorl %eax, %eax
callq 0x2080
cmpb $0x1, 0x24c63(%rip)     # 0x391cd
je 0x14d24
movq 0x50(%rsp), %rdi
movl 0x4(%rsp), %ebp
movl %ebp, %esi
callq 0x1cdf6
movq %rax, %r14
movq %rax, %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x1d226
testq %r14, %r14
je 0x1459d
movq %r14, %rdi
callq 0x2220
cmpb $0x1, 0x24c29(%rip)     # 0x391cd
movq 0x8d0(%rsp), %rbp
je 0x14d44
testb %r12b, %r12b
je 0x14c84
cmpb $0x1, 0x24c0b(%rip)     # 0x391cd
je 0x14d4e
movl $0x18, %edi
callq 0x2180
movq %rax, %r15
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
xorl %r14d, %r14d
movq %r14, 0x10(%rax)
movl $0x18, %edi
callq 0x2180
movq %rax, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %r14, 0x10(%rax)
movq 0x90(%rsp), %rdi
callq 0x2040
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq 0x98(%rsp), %rdx
callq 0x20f0
movq 0x50(%rsp), %rdi
movl 0x4(%rsp), %esi
movq %r15, 0x8(%rsp)
movq %r15, %rdx
movq %r13, %rcx
movq %r14, 0x68(%rsp)
movq %r14, %r8
xorl %r9d, %r9d
callq 0x241dc
movq (%r13), %r12
movq %r13, 0x18(%rsp)
movq 0x8(%r13), %r15
cmpq %r15, %r12
je 0x14803
movq %r15, %r14
subq %r12, %r14
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %r15, %rsi
leaq 0xfb51(%rip), %rcx      # 0x241d1
callq 0x10afb
leaq 0x8(%r12), %r13
cmpq $0x81, %r14
jl 0x14784
movq %r15, 0x20(%rsp)
movl $0x8, %ebp
movq %r13, 0x38(%rsp)
movq %r12, %rax
movq %rax, 0x40(%rsp)
leaq (%r12,%rbp), %r15
movq (%r12,%rbp), %rdi
movq (%r12), %rsi
callq 0x241d1
movq (%r12,%rbp), %r14
testb %al, %al
movq %r15, 0x48(%rsp)
je 0x146e1
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r12, %r15
jmp 0x14716
movq 0x40(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x241d1
testb %al, %al
je 0x14716
movq %r13, %rcx
leaq -0x8(%rcx), %r15
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x241d1
movq %r15, %rcx
testb %al, %al
jne 0x146f8
movq %r14, (%r15)
addq $0x8, %rbp
addq $0x8, %r13
cmpq $0x80, %rbp
movq 0x48(%rsp), %rax
jne 0x146a9
subq $-0x80, %r12
cmpq 0x20(%rsp), %r12
je 0x14803
movq -0x8(%r12), %rsi
movq (%r12), %r14
movq %r14, %rdi
callq 0x241d1
movq %r12, %r15
testb %al, %al
je 0x1477b
movq %r12, %rcx
leaq -0x8(%rcx), %r15
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x241d1
movq %r15, %rcx
testb %al, %al
jne 0x1475d
movq %r14, (%r15)
addq $0x8, %r12
jmp 0x14737
cmpq %r15, %r13
je 0x14803
movq %r12, %rbp
movq %rbp, %r14
movq %r13, %rbp
movq (%r13), %rdi
movq (%r12), %rsi
callq 0x241d1
movq (%r13), %r13
testb %al, %al
je 0x147c4
movq %rbp, %rdx
subq %r12, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x22b0
movq %r12, %r14
jmp 0x147f7
movq (%r14), %rsi
movq %r13, %rdi
callq 0x241d1
movq %rbp, %r14
testb %al, %al
je 0x147f7
movq %rbp, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x241d1
movq %r14, %rcx
testb %al, %al
jne 0x147d9
movq %r13, (%r14)
leaq 0x8(%rbp), %r13
cmpq %r15, %r13
jne 0x1478c
movq 0x8(%rsp), %rax
movq (%rax), %r12
movq 0x8(%rax), %r15
cmpq %r15, %r12
je 0x149c2
movq %r15, %r14
subq %r12, %r14
movq %r14, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %r15, %rsi
leaq 0xf992(%rip), %rcx      # 0x241d1
callq 0x10afb
leaq 0x8(%r12), %r13
cmpq $0x81, %r14
jl 0x14943
movq %r15, 0x20(%rsp)
movl $0x8, %ebp
movq %r13, 0x38(%rsp)
movq %r12, %rax
movq %rax, 0x40(%rsp)
leaq (%r12,%rbp), %r15
movq (%r12,%rbp), %rdi
movq (%r12), %rsi
callq 0x241d1
movq (%r12,%rbp), %r14
testb %al, %al
movq %r15, 0x48(%rsp)
je 0x148a0
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x22b0
movq %r12, %r15
jmp 0x148d5
movq 0x40(%rsp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x241d1
testb %al, %al
je 0x148d5
movq %r13, %rcx
leaq -0x8(%rcx), %r15
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x241d1
movq %r15, %rcx
testb %al, %al
jne 0x148b7
movq %r14, (%r15)
addq $0x8, %rbp
addq $0x8, %r13
cmpq $0x80, %rbp
movq 0x48(%rsp), %rax
jne 0x14868
subq $-0x80, %r12
cmpq 0x20(%rsp), %r12
je 0x149c2
movq -0x8(%r12), %rsi
movq (%r12), %r14
movq %r14, %rdi
callq 0x241d1
movq %r12, %r15
testb %al, %al
je 0x1493a
movq %r12, %rcx
leaq -0x8(%rcx), %r15
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r14, %rdi
callq 0x241d1
movq %r15, %rcx
testb %al, %al
jne 0x1491c
movq %r14, (%r15)
addq $0x8, %r12
jmp 0x148f6
cmpq %r15, %r13
je 0x149c2
movq %r12, %rbp
movq %rbp, %r14
movq %r13, %rbp
movq (%r13), %rdi
movq (%r12), %rsi
callq 0x241d1
movq (%r13), %r13
testb %al, %al
je 0x14983
movq %rbp, %rdx
subq %r12, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x22b0
movq %r12, %r14
jmp 0x149b6
movq (%r14), %rsi
movq %r13, %rdi
callq 0x241d1
movq %rbp, %r14
testb %al, %al
je 0x149b6
movq %rbp, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %r13, %rdi
callq 0x241d1
movq %r14, %rcx
testb %al, %al
jne 0x14998
movq %r13, (%r14)
leaq 0x8(%rbp), %r13
cmpq %r15, %r13
jne 0x1494b
cmpb $0x0, 0x70(%rsp)
movq 0x78(%rsp), %r13
movq 0x8d0(%rsp), %rbp
jne 0x14d58
cmpq $0x0, 0x8c8(%rsp)
movl 0x64(%rsp), %r12d
je 0x14b28
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0xb0(%rsp)
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
shrq $0x3, %rax
cmpl $0x2, %eax
jl 0x14af7
movl $0x1, %r15d
leaq -0x1(%r15), %r14
callq 0x15644
movq %rax, 0x4a0(%rsp)
leaq 0x18cb3(%rip), %rdi     # 0x2d6fa
callq 0x26db4
movq 0x18(%rsp), %r8
movq 0x4a0(%rsp), %rcx
movq %rax, (%rcx)
movq (%r8), %rax
movq 0x8(%r8), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x14fae
movq -0x8(%rax,%r15,8), %rcx
movl (%rcx), %esi
movq 0x4a0(%rsp), %rcx
movl %esi, 0xc(%rcx)
cmpq %r15, %rdx
jbe 0x14fa2
movq (%rax,%r15,8), %rax
movl (%rax), %eax
incl %eax
movl %eax, 0x20(%rcx)
movb $0x2b, 0x8(%rcx)
movq 0xb0(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x14ab9
movq %rcx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x14acb
leaq 0x4a0(%rsp), %rdx
callq 0xb74c
movq 0x18(%rsp), %r8
movq 0x8(%r8), %rax
subq (%r8), %rax
shlq $0x1d, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
leaq 0x1(%r15), %rcx
cmpq %rax, %r15
movq %rcx, %r15
jl 0x14a2f
movq 0x8c8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x14b1b
movq 0xb0(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x14b28
leaq 0xb0(%rsp), %rdx
callq 0x109c0
testq %rbp, %rbp
je 0x14c66
movl $0x18, %edi
callq 0x2180
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0xb0(%rsp)
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
shrq $0x3, %rax
cmpl $0x2, %eax
jl 0x14c3a
movl $0x1, %r15d
leaq -0x1(%r15), %r14
callq 0x15644
movq %rax, 0x4a0(%rsp)
leaq 0x18b70(%rip), %rdi     # 0x2d6fa
callq 0x26db4
movq 0x8(%rsp), %r8
movq 0x4a0(%rsp), %rcx
movq %rax, (%rcx)
movq (%r8), %rax
movq 0x8(%r8), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x14fae
movq -0x8(%rax,%r15,8), %rcx
movl (%rcx), %esi
movq 0x4a0(%rsp), %rcx
movl %esi, 0xc(%rcx)
cmpq %r15, %rdx
jbe 0x14fa2
movq (%rax,%r15,8), %rax
movl (%rax), %eax
incl %eax
movl %eax, 0x20(%rcx)
movb $0x2b, 0x8(%rcx)
movq 0xb0(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x14bfc
movq %rcx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x14c0e
leaq 0x4a0(%rsp), %rdx
callq 0xb74c
movq 0x8(%rsp), %r8
movq 0x8(%r8), %rax
subq (%r8), %rax
shlq $0x1d, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %rcx, %rax
sarq $0x20, %rax
leaq 0x1(%r15), %rcx
cmpq %rax, %r15
movq %rcx, %r15
jl 0x14b72
movq 0x8(%rbp), %rsi
cmpq 0x10(%rbp), %rsi
je 0x14c56
movq 0xb0(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rbp)
jmp 0x14c66
movq %rbp, %rdi
leaq 0xb0(%rsp), %rdx
callq 0x109c0
movq 0x18(%rsp), %rdi
callq 0x24d7a
movq 0x8(%rsp), %rdi
callq 0x24d7a
movq 0x68(%rsp), %rdi
callq 0x2220
cmpb $0x0, 0x8c0(%rsp)
movsd 0x10(%rsp), %xmm1
je 0x14cb2
movq 0x30(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x14cc3
movq 0x50(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x14cd3
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x14cd3
callq 0x2220
jmp 0x14ccd
leaq 0x50(%rsp), %rdx
callq 0x15426
movsd 0x10(%rsp), %xmm1
ucomisd 0x153d5(%rip), %xmm1    # 0x2a0b0
movsd 0x58(%rsp), %xmm0
jbe 0x14cf3
ucomisd %xmm1, %xmm0
movq 0x30(%rsp), %rax
jb 0x14cf8
jmp 0x14efc
movq 0x30(%rsp), %rax
incl 0x2c(%rsp)
jmp 0x140f7
movq 0x242d8(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x189eb(%rip), %rsi     # 0x2d6fd
movb $0x2, %al
callq 0x2250
movsd 0x58(%rsp), %xmm0
jmp 0x14215
movq 0x242b5(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x1893a(%rip), %rsi     # 0x2d66f
movq %r15, %rdx
xorl %eax, %eax
callq 0x2250
jmp 0x14570
callq 0x4831
jmp 0x145b2
callq 0x4853
jmp 0x145c8
leaq 0xb0(%rsp), %rdi
leaq 0x18940(%rip), %rsi     # 0x2d6a7
movq 0x8d8(%rsp), %r14
movq %r14, %rdx
movl 0x2c(%rsp), %r12d
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2080
leaq 0x4a0(%rsp), %r15
movq %r15, %rdi
leaq 0x18929(%rip), %rsi     # 0x2d6bc
movq %r14, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2080
movq 0x24239(%rip), %rax     # 0x38fe0
movq (%rax), %rcx
movl $0x1d, %esi
movl $0x1, %edx
leaq 0x18917(%rip), %rdi     # 0x2d6d2
callq 0x2340
movq %r15, %rdi
leaq 0x15cde(%rip), %rsi     # 0x2aaa8
callq 0x25d5a
movq %rax, %r15
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpl $0x2, %edx
jl 0x14e43
xorl %r12d, %r12d
cmpq %r12, %rdx
jbe 0x14fbf
movq (%rax,%r12,8), %rax
movl (%rax), %edx
leal 0x1(%rdx), %ecx
movq %r15, %rdi
leaq 0x188e7(%rip), %rsi     # 0x2d6f0
xorl %eax, %eax
callq 0x2250
incq %r12
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shlq $0x1d, %rcx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
addq %rsi, %rcx
sarq $0x20, %rcx
cmpq %rcx, %r12
jl 0x14ded
movq %r15, %rdi
callq 0x2170
leaq 0xb0(%rsp), %rdi
leaq 0x15c4e(%rip), %rsi     # 0x2aaa8
callq 0x25d5a
movq %rax, %r15
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpl $0x2, %edx
jl 0x14eef
movl $0x1, %r12d
leaq -0x1(%r12), %rsi
cmpq %rsi, %rdx
jbe 0x14fcb
cmpq %r12, %rdx
jbe 0x14fbf
movq -0x8(%rax,%r12,8), %rcx
movq (%rax,%r12,8), %rax
movl (%rcx), %edx
movl (%rax), %ecx
incl %ecx
movq %r15, %rdi
leaq 0x18840(%rip), %rsi     # 0x2d6f0
xorl %eax, %eax
callq 0x2250
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shlq $0x1d, %rcx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
addq %rsi, %rcx
sarq $0x20, %rcx
leaq 0x1(%r12), %rsi
cmpq %rcx, %r12
movq %rsi, %r12
jl 0x14e80
movq %r15, %rdi
callq 0x2170
jmp 0x149da
addq $0x888, %rsp            # imm = 0x888
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x240cb(%rip), %rax     # 0x38fe0
movq (%rax), %rdi
leaq 0x187fc(%rip), %rsi     # 0x2d71b
movl %ebp, %edx
movl %ecx, %r15d
movl %r14d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2250
addq $0x10, %rsp
cmpl %r13d, %r14d
jg 0x14f4c
movl %r14d, %edi
movl %r13d, %esi
movq %r12, %rdx
callq 0x47e6
leaq 0x19809(%rip), %rdi     # 0x2e75c
leaq 0x15b4e(%rip), %rsi     # 0x2aaa8
callq 0x25d5a
movq %rax, %r14
cmpl %ebp, %r15d
jl 0x14f93
movslq %ebp, %r12
movslq %r15d, %r13
decq %r12
leaq 0x187d6(%rip), %r15     # 0x2d74d
movsd 0x8(%rbx,%r12,8), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x2250
incq %r12
cmpq %r13, %r12
jl 0x14f77
movq %r14, %rdi
callq 0x2170
xorl %edi, %edi
callq 0x21e0
leaq 0x17927(%rip), %rdi     # 0x2c8d0
movq %r15, %rsi
jmp 0x14fb8
leaq 0x1791b(%rip), %rdi     # 0x2c8d0
movq %r14, %rsi
xorl %eax, %eax
callq 0x2270
leaq 0x1790a(%rip), %rdi     # 0x2c8d0
movq %r12, %rsi
jmp 0x14fb8
leaq 0x178fe(%rip), %rdi     # 0x2c8d0
jmp 0x14fb8