// File: gersteinlab[P]MUSIC/src/ms_peak_calling_utils.cpp
// Function: write_decomposition_WAV(char*, int, char*, int, double, double, double, int, double)
void write_decomposition_WAV(char* chip_reads_dir,
		int l_fragment,
		char* mapability_signal_dir,
		int l_read_mapability_signal,
		double base_scale_l_win,
		double end_scale_l_win,
		double log_step,
		int l_mapability_filtering_win,
		double max_normalized_mapability_signal)
{
	char chr_ids_fp[1000];
	sprintf(chr_ids_fp, "%s/chr_ids.txt", chip_reads_dir);
	vector<char*>* chr_ids = buffer_file(chr_ids_fp);
	if(chr_ids == NULL)
	{
		fprintf(stderr, "Could not load the chromosome id's for the chip data.\n");
		exit(0);
	}

	// Process each chromosome.
	for(int i_chr = 0; i_chr < (int)chr_ids->size(); i_chr++)
	{
		fprintf(stderr, "Writing %s..", chr_ids->at(i_chr));

		// Skip chromosome M for now.
		if(strcmp(chr_ids->at(i_chr), "M") == 0)
		{
			continue;
		}

		// Generate the current signal profile.
		int l_buffer = 300*1000*1000;
		int l_profile = 0;
		//int l_read = l_read_mapability_signal;
		double* buffered_signal_profile = new double[l_buffer + 2];	
		char cur_chr_chip_reads_fp[1000];
		sprintf(cur_chr_chip_reads_fp, "%s/%s_mapped_reads.txt", chip_reads_dir, chr_ids->at(i_chr));
		buffer_per_nucleotide_profile_no_buffer(cur_chr_chip_reads_fp, l_fragment, 
			buffered_signal_profile, NULL, NULL,
			l_buffer, l_profile);

		double* signal_profile = new double[l_profile + 2];	
		for(int i = 1; i <= l_profile; i++)
		{
			signal_profile[i] = buffered_signal_profile[i];
		} // i loop.
		delete [] buffered_signal_profile;

		//// Generate the current control profile.
		//l_buffer = 300*1000*1000;
		//int l_control = 0;
		//double* buffered_control_profile = new double[l_buffer + 2];
		//char cur_chr_control_reads_fp[1000];
		//sprintf(cur_chr_control_reads_fp, "%s/%s_mapped_reads.txt", control_reads_dir, chr_ids->at(i_chr));
		//buffer_per_nucleotide_profile_no_buffer(cur_chr_control_reads_fp, l_fragment, 
		//	buffered_control_profile, NULL, NULL, 
		//	l_buffer, l_control);


		//double* control_profile = new double[l_control + 2];
		//for(int i = 1; i <= l_control; i++)
		//{
		//	control_profile[i] = buffered_control_profile[i];
		//} // i loop.
		//delete [] buffered_control_profile;
		
		// Load and process the mapability profile.
		char mapability_signal_profile_fp[1000];
		sprintf(mapability_signal_profile_fp, "%s/%s.bin", mapability_signal_dir, chr_ids->at(i_chr));

		double* mapability_aware_smoothed_signal_profile = NULL;
		int l_mapability_profile = 0;
		if(check_file(mapability_signal_profile_fp))
		{
			double* mapability_signal_profile = NULL;

#define __UCHAR_MAPPABILITY__
#ifdef __DOUBLE_MAPPABILITY__
			// Load the mapability map signal profile, do filtering.
			mapability_signal_profile = load_per_nucleotide_binary_profile(mapability_signal_profile_fp, l_mapability_profile);

			// Do mapability aware median filtering on the current signal profile.
if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
			fprintf(stderr, "Scaling the mapability map with %d.\n", l_read_mapability_signal * 2);

			int mapability_scaling = l_read * 2;
			for(int i = 1; i <= l_mapability_profile; i++)
			{
				mapability_signal_profile[i] /= mapability_scaling;
			} // i loop.
#elif defined(__UCHAR_MAPPABILITY__)
			// Following loads the mappability signal profile from the char version of the multi-mappability profile.
			// Load the mapability map signal profile, do filtering.
			unsigned char* mapability_signal_char_profile = load_per_nucleotide_binary_uchar_profile(mapability_signal_profile_fp, l_mapability_profile);
			mapability_signal_profile = new double[l_mapability_profile + 2];
			for(int i = 1; i <= l_mapability_profile; i++)
			{
				unsigned char unsigned_char_val = (unsigned char)(mapability_signal_char_profile[i]);
				mapability_signal_profile[i] = (double)(unsigned_char_val);
				mapability_signal_profile[i] /= 100;

				if(mapability_signal_profile[i] < 0)
				{
					fprintf(stderr, "Sanity check failed.\n");
					exit(0);
				}
			} // i loop.
			delete [] mapability_signal_char_profile;
#else
			#error "Must define the type of mappability."
#endif

if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
			fprintf(stderr, "Smoothing the signal profile.\n");
			mapability_aware_smoothed_signal_profile = mapability_aware_median_filter(signal_profile, l_profile,
																					mapability_signal_profile, l_mapability_profile,
																					max_normalized_mapability_signal,
																					l_mapability_filtering_win);

			delete [] mapability_signal_profile;
		} // mapability based filtering check.
		else
		{
			fprintf(stderr, "Could not find the mapability map profile, skipping it.");
			mapability_aware_smoothed_signal_profile = new double[l_profile + 2];
			for(int i = 1; i <= l_profile; i++)
			{
				mapability_aware_smoothed_signal_profile[i] = signal_profile[i];
			} // i loop.
		}

//		// Scale the control with respect to signal.
//		double per_win_2DOF_lls_scaling_factor = 0;
//		double per_win_1DOF_lls_scaling_factor = 0;
//		double total_sig_scaling_factor = 0;
//		get_per_window_scaling_factors_for_profile1_per_profile2(control_profile, l_control, signal_profile, l_profile, 10000,
//																	per_win_2DOF_lls_scaling_factor,
//																	per_win_1DOF_lls_scaling_factor,
//																	total_sig_scaling_factor);
//
//		double scaling_factor = per_win_1DOF_lls_scaling_factor;
//
//if(__DUMP_PEAK_CALLING_UTILS_MSGS__)
//		fprintf(stderr, "Scaling control with factor of %lf\n", scaling_factor);
//
//		// Go over the whole control signal.
//		for(int i = 0; i < l_control; i++)
//		{
//			control_profile[i] *= scaling_factor;
//		} // i loop.

		// Now, do the multiscale decomposition, 
		vector<double>* scales_per_i_scale = new vector<double>();
		vector<double*>* filtered_tracks = multiscale_median_filter_data(mapability_aware_smoothed_signal_profile, l_profile, 
			base_scale_l_win, end_scale_l_win, log_step, 
			scales_per_i_scale,
			true,
			false, 
			false, 
			false, 
			NULL, 
			NULL,
			"temp");

		// Dump the tracks.
		short int* merged_waveform_buffer = new short int[l_profile + 2];
		memset(merged_waveform_buffer, 0, sizeof(short int) * l_profile);
		for(int i_scale = 0; i_scale < (int)scales_per_i_scale->size(); i_scale++)
		{
			char cur_decomp_fp[1000];
			sprintf(cur_decomp_fp, "%s_scale_%d.bin", "temp", i_scale);

			if(check_file(cur_decomp_fp))
			{
				int l_cur_decomp;
				double* cur_filtered_track = load_per_nucleotide_binary_profile(cur_decomp_fp, l_cur_decomp);

				int l_cur_last_index = MIN(l_cur_decomp, l_profile);
				for(int i = 1; i < l_cur_last_index; i++)
				{
					merged_waveform_buffer[i] += (short int)(cur_filtered_track[i]);
				} // i loop.

				// Delete the temp file.
				remove(cur_decomp_fp);

				//char cur_bgr_fp[1000];
				//sprintf(cur_bgr_fp, "chr%s_scale_%d.bgr", chr_ids->at(i_chr), (int)(floor(scales_per_i_scale->at(i_scale))));
				//dump_bedGraph_per_per_nucleotide_binary_profile(cur_filtered_track, l_cur_decomp, chr_ids->at(i_chr), cur_bgr_fp);
				delete [] cur_filtered_track;
			}
		} // i_scale loop.
		delete filtered_tracks;

		fprintf(stderr, "Dumping WAV file.\n");

		// Amplify to get 
		short max_val = 0;
		short min_val = 60*1000;
		for(int i = 0; i < l_profile; i++)
		{
			if(max_val < merged_waveform_buffer[i])
			{
				max_val = merged_waveform_buffer[i];
			}

			if(min_val > merged_waveform_buffer[i])
			{
				min_val = merged_waveform_buffer[i];
			}
		} // i_loop.

		double norm_val = ((double)64000 / (double)(max_val - min_val));
		double half_val = (max_val + min_val) / 2;
		for(int i = 0; i < l_profile; i++)
		{
			double fraction_posn_per_cur_val = fabs((merged_waveform_buffer[i] - half_val)/half_val);
			double a = (1 - fraction_posn_per_cur_val);
			double warping_factor = pow((1 - a*a),.5) / (a+.05);
			//if(warping_factor > 1 || warping_factor < 0)
			//{
			//	fprintf(stderr, "Sanity check failed: %lf\n", warping_factor);
			//	exit(0);
			//}
			merged_waveform_buffer[i] *= (short)(warping_factor * norm_val * (merged_waveform_buffer[i] - half_val));
		} // i loop.

		char cur_chr_wav_fp[1000];
		sprintf(cur_chr_wav_fp, "%s.wav", chr_ids->at(i_chr));
		FILE * f_wave = wavfile_open(cur_chr_wav_fp);
		fprintf(stderr, "Opened WAV file.\n");
		if(f_wave == NULL) 
		{
			fprintf(stderr, "couldn't open %s for writing.", cur_chr_wav_fp);
			exit(0);
		}

		fprintf(stderr, "Writing WAV file.\n");
		wavfile_write(f_wave,merged_waveform_buffer,l_profile);
		fprintf(stderr, "Closing WAV file.\n");
		wavfile_close(f_wave);
		fprintf(stderr, "Done!\n");

		delete [] merged_waveform_buffer;
		delete scales_per_i_scale;
		delete [] signal_profile;
		//delete [] control_profile;
		delete [] mapability_aware_smoothed_signal_profile;
	} // i_chr loop.
}

// Assembly:
subq $0x1588, %rsp           # imm = 0x1588
movq %rdi, 0x1580(%rsp)
movl %esi, 0x157c(%rsp)
movq %rdx, 0x1570(%rsp)
movl %ecx, 0x156c(%rsp)
movsd %xmm0, 0x1560(%rsp)
movsd %xmm1, 0x1558(%rsp)
movsd %xmm2, 0x1550(%rsp)
movl %r8d, 0x154c(%rsp)
movsd %xmm3, 0x1540(%rsp)
leaq 0x1150(%rsp), %rdi
movq 0x1580(%rsp), %rdx
leaq 0x3ef62(%rip), %rsi     # 0x529da
movb $0x0, %al
callq 0x2090
leaq 0x1150(%rsp), %rdi
callq 0x452c0
movq %rax, 0x1148(%rsp)
cmpq $0x0, 0x1148(%rsp)
jne 0x13abe
movq 0x5153a(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x41472(%rip), %rsi     # 0x54f22
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl $0x0, 0x1144(%rsp)
movl 0x1144(%rsp), %eax
movl %eax, 0xb4(%rsp)
movq 0x1148(%rsp), %rdi
callq 0xc220
movq %rax, %rcx
movl 0xb4(%rsp), %eax
cmpl %ecx, %eax
jge 0x1466b
movq 0x514e3(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0x1148(%rsp), %rdi
movslq 0x1144(%rsp), %rsi
callq 0xc240
movq 0xa8(%rsp), %rdi
movq (%rax), %rdx
leaq 0x4142a(%rip), %rsi     # 0x54f59
movb $0x0, %al
callq 0x2250
movq 0x1148(%rsp), %rdi
movslq 0x1144(%rsp), %rsi
callq 0xc240
movq (%rax), %rdi
leaq 0x3ed50(%rip), %rsi     # 0x528a5
callq 0x2240
cmpl $0x0, %eax
jne 0x13b64
jmp 0x14655
movl $0x11e1a300, 0x1140(%rsp) # imm = 0x11E1A300
movl $0x0, 0x113c(%rsp)
movl 0x1140(%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, 0x1130(%rsp)
leaq 0xd40(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x1580(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x1148(%rsp), %rdi
movslq 0x1144(%rsp), %rsi
callq 0xc240
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rdx
movq (%rax), %rcx
leaq 0x3ee36(%rip), %rsi     # 0x52a34
movb $0x0, %al
callq 0x2090
leaq 0xd40(%rsp), %rdi
movl 0x157c(%rsp), %esi
movq 0x1130(%rsp), %rdx
movl 0x1140(%rsp), %r9d
xorl %eax, %eax
movl %eax, %r8d
leaq 0x113c(%rsp), %rax
movq %r8, %rcx
movq %rax, (%rsp)
callq 0x1edf0
movl 0x113c(%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, 0xd38(%rsp)
movl $0x1, 0xd34(%rsp)
movl 0xd34(%rsp), %eax
cmpl 0x113c(%rsp), %eax
jg 0x13cca
movq 0x1130(%rsp), %rax
movslq 0xd34(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xd38(%rsp), %rax
movslq 0xd34(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0xd34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd34(%rsp)
jmp 0x13c7d
movq 0x1130(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, %rax
je 0x13ced
movq 0x90(%rsp), %rdi
callq 0x2220
leaq 0x940(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x1570(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x1148(%rsp), %rdi
movslq 0x1144(%rsp), %rsi
callq 0xc240
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rdx
movq (%rax), %rcx
leaq 0x3f253(%rip), %rsi     # 0x52f8f
movb $0x0, %al
callq 0x2090
movq $0x0, 0x938(%rsp)
movl $0x0, 0x934(%rsp)
leaq 0x940(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x13d70
jmp 0x13f3b
movq $0x0, 0x928(%rsp)
leaq 0x940(%rsp), %rdi
leaq 0x934(%rsp), %rsi
callq 0x34400
movq %rax, 0x920(%rsp)
movl 0x934(%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, 0x928(%rsp)
movl $0x1, 0x91c(%rsp)
movl 0x91c(%rsp), %eax
cmpl 0x934(%rsp), %eax
jg 0x13e9f
movq 0x920(%rsp), %rax
movslq 0x91c(%rsp), %rcx
movb (%rax,%rcx), %al
movb %al, 0x91b(%rsp)
movzbl 0x91b(%rsp), %eax
cvtsi2sd %eax, %xmm0
movq 0x928(%rsp), %rax
movslq 0x91c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq 0x928(%rsp), %rax
movslq 0x91c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x3e1e3(%rip), %xmm1    # 0x52028
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x928(%rsp), %rax
movslq 0x91c(%rsp), %rcx
xorps %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x13e87
movq 0x51171(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x41093(%rip), %rsi     # 0x54f0c
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
jmp 0x13e89
movl 0x91c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x91c(%rsp)
jmp 0x13dd9
movq 0x920(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x13ebc
movq 0x78(%rsp), %rdi
callq 0x2220
testb $0x1, 0x51317(%rip)     # 0x651da
je 0x13edd
movq 0x51114(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x41090(%rip), %rsi     # 0x54f66
movb $0x0, %al
callq 0x2250
movq 0xd38(%rsp), %rdi
movl 0x113c(%rsp), %esi
movq 0x928(%rsp), %rdx
movl 0x934(%rsp), %ecx
movsd 0x1540(%rsp), %xmm0
movl 0x154c(%rsp), %r8d
callq 0x25380
movq %rax, 0x938(%rsp)
movq 0x928(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, %rax
je 0x13f36
movq 0x70(%rsp), %rdi
callq 0x2220
jmp 0x13fe2
movq 0x5109e(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x41039(%rip), %rsi     # 0x54f85
movb $0x0, %al
callq 0x2250
movl 0x113c(%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, 0x938(%rsp)
movl $0x1, 0x914(%rsp)
movl 0x914(%rsp), %eax
cmpl 0x113c(%rsp), %eax
jg 0x13fe0
movq 0xd38(%rsp), %rax
movslq 0x914(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x938(%rsp), %rax
movslq 0x914(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x914(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x914(%rsp)
jmp 0x13f93
jmp 0x13fe2
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 0xc840
movq 0x68(%rsp), %rax
movq %rax, 0x908(%rsp)
movq 0x938(%rsp), %rdi
movl 0x113c(%rsp), %esi
movsd 0x1560(%rsp), %xmm0
movsd 0x1558(%rsp), %xmm1
movsd 0x1550(%rsp), %xmm2
movq 0x908(%rsp), %rdx
movl $0x1, %ecx
xorl %r9d, %r9d
xorl %eax, %eax
leaq 0x40f63(%rip), %rax     # 0x54fbd
movl %r9d, %r8d
movl $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x23eb0
movq %rax, 0x900(%rsp)
movl 0x113c(%rsp), %eax
addl $0x2, %eax
cltq
movl $0x2, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, 0x8f8(%rsp)
movq 0x8f8(%rsp), %rdi
movslq 0x113c(%rsp), %rdx
shlq %rdx
xorl %esi, %esi
callq 0x2110
movl $0x0, 0x8f4(%rsp)
movl 0x8f4(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x908(%rsp), %rdi
callq 0x11c40
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x1423b
leaq 0x500(%rsp), %rdi
movl 0x8f4(%rsp), %ecx
leaq 0x40ea3(%rip), %rsi     # 0x54fc2
leaq 0x40e97(%rip), %rdx     # 0x54fbd
movb $0x0, %al
callq 0x2090
leaq 0x500(%rsp), %rdi
callq 0x455a0
testb $0x1, %al
jne 0x14143
jmp 0x14223
leaq 0x500(%rsp), %rdi
leaq 0x4fc(%rsp), %rsi
callq 0x351c0
movq %rax, 0x4f0(%rsp)
movl 0x4fc(%rsp), %eax
cmpl 0x113c(%rsp), %eax
jle 0x1417d
movl 0x113c(%rsp), %eax
movl %eax, 0x60(%rsp)
jmp 0x14188
movl 0x4fc(%rsp), %eax
movl %eax, 0x60(%rsp)
movl 0x60(%rsp), %eax
movl %eax, 0x4ec(%rsp)
movl $0x1, 0x4e8(%rsp)
movl 0x4e8(%rsp), %eax
cmpl 0x4ec(%rsp), %eax
jge 0x141f7
movq 0x4f0(%rsp), %rax
movslq 0x4e8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvttsd2si %xmm0, %eax
movswl %ax, %esi
movq 0x8f8(%rsp), %rax
movslq 0x4e8(%rsp), %rcx
movswl (%rax,%rcx,2), %edx
addl %esi, %edx
movw %dx, (%rax,%rcx,2)
movl 0x4e8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4e8(%rsp)
jmp 0x1419e
leaq 0x500(%rsp), %rdi
callq 0x2330
movq 0x4f0(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x14221
movq 0x58(%rsp), %rdi
callq 0x2220
jmp 0x14223
jmp 0x14225
movl 0x8f4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8f4(%rsp)
jmp 0x140e2
movq 0x900(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x14267
movq 0x50(%rsp), %rdi
callq 0x1acf0
movq 0x50(%rsp), %rdi
movl $0x18, %esi
callq 0x21a0
movq 0x50d72(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x40d5a(%rip), %rsi     # 0x54fd2
movb $0x0, %al
callq 0x2250
movw $0x0, 0x4e6(%rsp)
movw $0xea60, 0x4e4(%rsp)    # imm = 0xEA60
movl $0x0, 0x4e0(%rsp)
movl 0x4e0(%rsp), %eax
cmpl 0x113c(%rsp), %eax
jge 0x14342
movswl 0x4e6(%rsp), %eax
movq 0x8f8(%rsp), %rcx
movslq 0x4e0(%rsp), %rdx
movswl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jge 0x142ee
movq 0x8f8(%rsp), %rax
movslq 0x4e0(%rsp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, 0x4e6(%rsp)
movswl 0x4e4(%rsp), %eax
movq 0x8f8(%rsp), %rcx
movslq 0x4e0(%rsp), %rdx
movswl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jle 0x1432a
movq 0x8f8(%rsp), %rax
movslq 0x4e0(%rsp), %rcx
movw (%rax,%rcx,2), %ax
movw %ax, 0x4e4(%rsp)
jmp 0x1432c
movl 0x4e0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4e0(%rsp)
jmp 0x1429e
movswl 0x4e6(%rsp), %eax
movswl 0x4e4(%rsp), %ecx
subl %ecx, %eax
cvtsi2sd %eax, %xmm1
movsd 0x40a08(%rip), %xmm0    # 0x54d68
divsd %xmm1, %xmm0
movsd %xmm0, 0x4d8(%rsp)
movswl 0x4e6(%rsp), %eax
movswl 0x4e4(%rsp), %ecx
addl %ecx, %eax
movl $0x2, %ecx
cltd
idivl %ecx
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x4d0(%rsp)
movl $0x0, 0x4cc(%rsp)
movl 0x4cc(%rsp), %eax
cmpl 0x113c(%rsp), %eax
jge 0x144c8
movq 0x8f8(%rsp), %rax
movslq 0x4cc(%rsp), %rcx
movswl (%rax,%rcx,2), %eax
cvtsi2sd %eax, %xmm0
movsd 0x4d0(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd %xmm1, %xmm0
movaps 0x409ad(%rip), %xmm1    # 0x54d90
pand %xmm1, %xmm0
movlpd %xmm0, 0x4c0(%rsp)
movsd 0x4c0(%rsp), %xmm2
movsd 0x3dc17(%rip), %xmm0    # 0x52018
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
movsd %xmm1, 0x4b8(%rsp)
movsd 0x4b8(%rsp), %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm0
movsd 0x3dc0e(%rip), %xmm1    # 0x52038
callq 0x22f0
movsd 0x4b8(%rsp), %xmm1
movsd 0x3dbf0(%rip), %xmm2    # 0x52030
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x4b0(%rsp)
movsd 0x4b0(%rsp), %xmm0
movsd 0x4d8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movq 0x8f8(%rsp), %rax
movslq 0x4cc(%rsp), %rcx
movswl (%rax,%rcx,2), %eax
cvtsi2sd %eax, %xmm1
movsd 0x4d0(%rsp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movswl %ax, %esi
movq 0x8f8(%rsp), %rax
movslq 0x4cc(%rsp), %rcx
movswl (%rax,%rcx,2), %edx
imull %esi, %edx
movw %dx, (%rax,%rcx,2)
movl 0x4cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4cc(%rsp)
jmp 0x1439f
leaq 0xc0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x1148(%rsp), %rdi
movslq 0x1144(%rsp), %rsi
callq 0xc240
movq 0x48(%rsp), %rdi
movq (%rax), %rdx
leaq 0x40aec(%rip), %rsi     # 0x54fe5
movb $0x0, %al
callq 0x2090
leaq 0xc0(%rsp), %rdi
callq 0x51630
movq %rax, 0xb8(%rsp)
movq 0x50ac4(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x40ac6(%rip), %rsi     # 0x54fec
movb $0x0, %al
callq 0x2250
cmpq $0x0, 0xb8(%rsp)
jne 0x1455f
movq 0x50aa1(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0xc0(%rsp), %rdx
leaq 0x40aad(%rip), %rsi     # 0x54ffe
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movq 0x50a7a(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x40aac(%rip), %rsi     # 0x5501c
movb $0x0, %al
callq 0x2250
movq 0xb8(%rsp), %rdi
movq 0x8f8(%rsp), %rsi
movl 0x113c(%rsp), %edx
callq 0x51780
movq 0x50a46(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x40a8b(%rip), %rsi     # 0x5502f
movb $0x0, %al
callq 0x2250
movq 0xb8(%rsp), %rdi
callq 0x517b0
movq 0x50a21(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x40a79(%rip), %rsi     # 0x55042
movb $0x0, %al
callq 0x2250
movq 0x8f8(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x145ed
movq 0x40(%rsp), %rdi
callq 0x2220
movq 0x908(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x14619
movq 0x38(%rsp), %rdi
callq 0x1ad50
movq 0x38(%rsp), %rdi
movl $0x18, %esi
callq 0x21a0
movq 0xd38(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x14636
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x938(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x14653
movq 0x28(%rsp), %rdi
callq 0x2220
jmp 0x14655
movl 0x1144(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1144(%rsp)
jmp 0x13ac9
addq $0x1588, %rsp           # imm = 0x1588
retq
nopw %cs:(%rax,%rax)