// File: kchan1[P]helloworld_test1/main2.cpp
// Function: main
int main(int argc, char * arg[])

{	
	if (argc == 1){
		std::cout<<"Error: No filename submitted. Please input filename."<<std::endl;
		return 1;
	}

	//for handling the file
	std::string filename = arg[1];
	std::ifstream image_file;
	
	//buffers for reading the header
	unsigned char begin_of_bitmap[4];
	unsigned char width_data[4];
	unsigned char height_data[4];
	unsigned char pixel_bit_data[2];
	unsigned char num_decomps_data;
	
	//secondary buffers for interpretting the header
	int bitmap_offset=0;
	int width =0;
	int image_width = 0;
	int height =0;
	int pixel_bits=0;
	int num_decomps=0;
	char char_reader[1];
	unsigned char uchar_reader[1];
	int total_char;
	int limit_char;
	int quant = 20;
	
	//start reading 10 bytes after the beginning of the file
	image_file.open(filename.c_str(), std::ifstream::in | std::ifstream::binary);
	image_file.seekg(10, std::ifstream::beg);

	//read the first 4 bytes of the file which are interpretted for the header size
	image_file.read(reinterpret_cast<char*>(begin_of_bitmap),4);
	//B0*2^0 + B1*2^8 + B2*2^16 + B3*2^24
	for (int i=0; i<4; i++){
		bitmap_offset+=(int)begin_of_bitmap[i]*pow(2,i*8);
	//	std::cout<<(int)begin_of_bitmap[i]<<" ";
	}
	//std::cout<<std::endl;

	//std::cout<<bitmap_offset<<std::endl;

	//start reading 18 bytes after the beginning of the file
	image_file.seekg(18, std::ifstream::beg);
	//read 4 bytes as the width
	image_file.read(reinterpret_cast<char*>(width_data),4);
	for(int i=0; i<4; i++){
		width+=(int)width_data[i]*pow(2,i*8);
	}
	width = (int)((3*8*width+31)/(32))*4;
	image_width = width;
	width /= 3;

	//read 4 bytes as the height
	image_file.read(reinterpret_cast<char*>(height_data),4);
	for(int i=0; i<4; i++){
		height+=(int)height_data[i]*pow(2,i*8);
	}

	//std::cout<<width<<" "<<height<<std::endl;

	//start reading 28 bytes after the beginning of the file
	image_file.seekg(28, std::ifstream::beg);
	
	//read 2 bytes of data as the number of bits per pixel
	image_file.read(reinterpret_cast<char*>(pixel_bit_data),2);
	for(int i=0; i<2; i++){
		pixel_bits+=(int)pixel_bit_data[i]*pow(2,i*8);
	}

	//we are restricting the program to 24-bit bitmaps
	if(pixel_bits != 24){
		std::cout<<"Image is not a 24-bit bitmap file. Please submit a properly formatted file."<<std::endl;
		return 1;
	}
	
	//holds all the of the header information
	unsigned char* image_header_data= new unsigned char[bitmap_offset];
	//holds the raw input image data BUT also the raw input image output
	unsigned char** imageData=new unsigned char*[height];
	double** image_data = new double*[height];
	//holds the image data separated into colors
	double** blue_data=new double*[height];
	double** green_data=new double*[height];
	double** red_data=new double*[height];

	//further initialize 2D arrays
	for (int i = 0; i<height; i++){
		imageData[i] = new unsigned char[image_width];
		image_data[i] = new double[image_width];
		blue_data[i] = new double[width];
		green_data[i] = new double[width];
		red_data[i] = new double[width];
	}	

	//read the header information(again?) into image_header_data
	image_file.seekg(0, std::ifstream::beg);
	image_file.read(reinterpret_cast<char*>(image_header_data),bitmap_offset);

	//Read in the number of Decompisition Loops:
	image_file.read(reinterpret_cast<char*>(uchar_reader),1);
	num_decomps=(int)uchar_reader[0];
	//cout<<num_decomps<<endl;
	
	image_file.read(reinterpret_cast<char*>(uchar_reader),1);
	quant=(int)uchar_reader[0];
	//cout<<num_decomps<<endl;

	//image_file.seekg(-2, std::ifstream::cur);
	total_char = 0;
	limit_char = width*height;
	int tempi = 0;
	int tempj = 0;
	//int cout_count = 0;

	while(tempi!=height){
		image_file.read(reinterpret_cast<char*>(uchar_reader),1);
		image_file.read(reinterpret_cast<char*>(char_reader),1);
		//if(cout_count <49927){
		//cout<<(int)uchar_reader[0]<<" "<<(int)char_reader[0]<<endl;
		//cout_count++;
		//cout<<cout_count<<endl;
		//}
		for(int k = 0; k<(int)uchar_reader[0]; k++){
			//cout<<"Trying to read..."<<endl;
			blue_data[tempi][tempj] = char_reader[0]*quant;
			total_char++;
			tempj++;
			if(tempj == width){
				tempj = 0;
				tempi++;
				if(tempi == height){
					break;
				}	
			}
			//cout<<tempi<<" "<<tempj<<endl;
		}
				
	}	
	//image_file.seekg(-2, std::ifstream::cur);
	total_char = 0;
	limit_char = width*height;
	tempi = 0;
	tempj = 0;

	while(tempi!=height){
		image_file.read(reinterpret_cast<char*>(uchar_reader),1);
		image_file.read(reinterpret_cast<char*>(char_reader),1);
		//cout<<(int)uchar_reader[0]<<" "<<(int)char_reader[0]<<endl;
		for(int k = 0; k<(int)uchar_reader[0]; k++){
			//cout<<"Trying to read..."<<endl;
			green_data[tempi][tempj] = char_reader[0]*quant;
			total_char++;
			tempj++;
			if(tempj == width){
				tempj = 0;
				tempi++;
				if(tempi == height){
					break;
				}	
			}
			//cout<<tempi<<" "<<tempj<<endl;
		}
				
	}	

	//image_file.seekg(-2, std::ifstream::cur);
	total_char = 0;
	limit_char = width*height;
	tempi = 0;
	tempj = 0;

	while(tempi!=height){
		image_file.read(reinterpret_cast<char*>(uchar_reader),1);
		image_file.read(reinterpret_cast<char*>(char_reader),1);
		//cout<<(int)uchar_reader[0]<<" "<<(int)char_reader[0]<<endl;
		for(int k = 0; k<(int)uchar_reader[0]; k++){
			//cout<<"Trying to read..."<<endl;
			red_data[tempi][tempj] = char_reader[0]*quant;
			//red_data[tempi][tempj] = 0;			
			total_char++;
			tempj++;
			if(tempj == width){
				tempj = 0;
				tempi++;
				if(tempi == height){
					break;
				}	
			}
			//cout<<tempi<<" "<<tempj<<endl;
		}
				
	}	

	//for (int i = 0; i<20; i++){
	//	cout<<red_data[2][i]<<endl;
	//}	

	//read in the raw image data
	//Read in the data from the zero-tree things.
	//reconstruct the zero tree into a matrix of values.

	


//read in the raw image data
	//for (int i=0; i<height; i++){
//image_file.read(reinterpret_cast<char*>(imageData[i]),width*3);
	//}

//	for (int i=0; i<height; i++){
//		std::cout<<imageData[i]<<" ";
//	}
//	std::cout<<std::endl;

/*	for (int i=0; i<10; i++){
		std::cout<<"[ ";
		for(int j = 0; j<10; j++){
			std::cout<<(int)imageData[i][j]<<" ";
		}
		std::cout<<"]"<<std::endl;
	}*/

	/*for(int i = 0; i<height; i++){
		for(int j = 0; j<width*3; j++){
			image_data[i][j]=imageData[i][j];
		}
	}

	//separate imageData into the 3 colors
	for (int i = 0; i<height; i++){
		for(int j = 0; j<width*3; j+=3){
			blue_data[i][j/3] = image_data[i][j];
		}
		for(int j = 1; j<width*3; j+=3){
			green_data[i][j/3] = image_data[i][j];
		}
		for(int j=2;j<width*3; j+=3){
			red_data[i][j/3] = image_data[i][j];
		}
	}*/

	for (int i = 0; i<height; i++){
		for(int j = 0; j<width; j++){
			imageData[i][j]=(unsigned char)0;
		}
	}


	//don't need to read the image anymore
	image_file.close();

/*
 *At this point, the image is properly loaded into several char arrays. They can be 
 numerically manipulated like normal numbers (each individual array entry)
 The arrays are:

 blue_data <- all the blue pixel values.
 green_data <- all the green pixel values.
 red_data <- all the red pixel values.

 height <- original image height.
 width <- original image width.
 
 image_header_data <- the original image header
 *
 *
 *                            Subband Recomposition
 *========================================================================
 */
	int recomp_height= height;
	int recomp_width = width;

	for (int i = num_decomps; i>=1; i--){
		recomp_height = height/pow(2,i-1);
		recomp_width = width/pow(2,i-1);
		subband_recomp(blue_data,recomp_height,recomp_width);
		subband_recomp(green_data,recomp_height,recomp_width);
		subband_recomp(red_data,recomp_height,recomp_width);
	}

	for (int i = 0; i<height; i++){
		for(int j = 0; j<image_width; j+=3){
			image_data[i][j]=blue_data[i][j/3];
		}
		for(int j = 1; j<image_width; j+=3){
			image_data[i][j]=green_data[i][j/3];
		}
		for(int j=2;j<image_width; j+=3){
			image_data[i][j]=red_data[i][j/3];
		}
	}

	for (int i=0; i<height; i++){
		for(int j=0; j<image_width; j++){
			if (image_data[i][j]<0){
				image_data[i][j]=0;
			}
			if (image_data[i][j]>255){
				image_data[i][j]=255;
			}
		}
	}

	for(int i = 0; i<height; i++){
		for(int j = 0; j<image_width; j++){
			imageData[i][j]=image_data[i][j];
		}
	}
	
	reconstruct(imageData,height,image_width,image_header_data,bitmap_offset,filename);


//	reconstruct((imageData),height,width*3,image_header_data,bitmap_offset,filename);


/*
 *	So now imageData has the Subband Decomposition of the oringal image (decomposed twice).
 *	the compression portion hasn't happened yet, that involves choosing the threshold and such.
 *
 *
 *
 *
 *
 *=================================================================================
 */
	//clean up memory
	delete image_header_data;
	for(int i=0;i<height;i++)
	{
	  delete imageData[i];
	  delete blue_data[i];
	  delete green_data[i];
	  delete red_data[i];  
	}
	delete imageData;
	delete blue_data;
	delete green_data;
	delete red_data;
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jne 0x313d
movq 0x3ec2(%rip), %rdi      # 0x6fd8
leaq 0x1f48(%rip), %rsi      # 0x5065
callq 0x2160
movq %rax, %rdi
movq 0x3e9c(%rip), %rsi      # 0x6fc8
callq 0x2180
movl $0x1, -0x4(%rbp)
jmp 0x43ee
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x358(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x350(%rbp)
callq 0x22a0
movq -0x358(%rbp), %rsi
movq -0x350(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x4430
jmp 0x3175
leaq -0x31(%rbp), %rdi
callq 0x21a0
leaq -0x250(%rbp), %rdi
callq 0x21d0
jmp 0x318c
movl $0x0, -0x264(%rbp)
movl $0x0, -0x268(%rbp)
movl $0x0, -0x26c(%rbp)
movl $0x0, -0x270(%rbp)
movl $0x0, -0x274(%rbp)
movl $0x0, -0x278(%rbp)
movl $0x14, -0x288(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x20a0
movq %rax, -0x368(%rbp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x4410
movl %eax, -0x35c(%rbp)
jmp 0x31f9
movl -0x35c(%rbp), %edx
movq -0x368(%rbp), %rsi
leaq -0x250(%rbp), %rdi
callq 0x20f0
jmp 0x3214
leaq -0x250(%rbp), %rdi
movl $0xa, %esi
xorl %edx, %edx
callq 0x2210
jmp 0x3229
leaq -0x250(%rbp), %rdi
leaq -0x254(%rbp), %rsi
movl $0x4, %edx
callq 0x21f0
jmp 0x3243
movl $0x0, -0x28c(%rbp)
cmpl $0x4, -0x28c(%rbp)
jge 0x3305
movslq -0x28c(%rbp), %rax
movl %eax, %esi
movzbl -0x254(%rbp,%rax), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x378(%rbp)
shll $0x3, %esi
movl $0x2, %edi
callq 0x44e0
movsd %xmm0, -0x370(%rbp)
jmp 0x328e
movsd -0x370(%rbp), %xmm2
movsd -0x378(%rbp), %xmm0
cvtsi2sdl -0x264(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x264(%rbp)
movl -0x28c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28c(%rbp)
jmp 0x324d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x21a0
jmp 0x43fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x43e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x43d7
leaq -0x250(%rbp), %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x2210
jmp 0x331a
leaq -0x250(%rbp), %rdi
leaq -0x258(%rbp), %rsi
movl $0x4, %edx
callq 0x21f0
jmp 0x3334
movl $0x0, -0x290(%rbp)
cmpl $0x4, -0x290(%rbp)
jge 0x33b6
movslq -0x290(%rbp), %rax
movl %eax, %esi
movzbl -0x258(%rbp,%rax), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x388(%rbp)
shll $0x3, %esi
movl $0x2, %edi
callq 0x44e0
movsd %xmm0, -0x380(%rbp)
jmp 0x337b
movsd -0x380(%rbp), %xmm2
movsd -0x388(%rbp), %xmm0
cvtsi2sdl -0x268(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x268(%rbp)
movl -0x290(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x290(%rbp)
jmp 0x333e
movl -0x268(%rbp), %eax
shll $0x3, %eax
movl %eax, %ecx
leal 0x1f(%rcx,%rcx,2), %eax
leal 0x3e(%rcx,%rcx,2), %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x5, %eax
shll $0x2, %eax
movl %eax, -0x268(%rbp)
movl -0x268(%rbp), %eax
movl %eax, -0x26c(%rbp)
movslq -0x268(%rbp), %rax
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
movl %eax, -0x268(%rbp)
leaq -0x250(%rbp), %rdi
leaq -0x25c(%rbp), %rsi
movl $0x4, %edx
callq 0x21f0
jmp 0x3421
movl $0x0, -0x294(%rbp)
cmpl $0x4, -0x294(%rbp)
jge 0x34a3
movslq -0x294(%rbp), %rax
movl %eax, %esi
movzbl -0x25c(%rbp,%rax), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x398(%rbp)
shll $0x3, %esi
movl $0x2, %edi
callq 0x44e0
movsd %xmm0, -0x390(%rbp)
jmp 0x3468
movsd -0x390(%rbp), %xmm2
movsd -0x398(%rbp), %xmm0
cvtsi2sdl -0x270(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x270(%rbp)
movl -0x294(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x294(%rbp)
jmp 0x342b
leaq -0x250(%rbp), %rdi
movl $0x1c, %esi
xorl %edx, %edx
callq 0x2210
jmp 0x34b8
leaq -0x250(%rbp), %rdi
leaq -0x25e(%rbp), %rsi
movl $0x2, %edx
callq 0x21f0
jmp 0x34d2
movl $0x0, -0x298(%rbp)
cmpl $0x2, -0x298(%rbp)
jge 0x3554
movslq -0x298(%rbp), %rax
movl %eax, %esi
movzbl -0x25e(%rbp,%rax), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x3a8(%rbp)
shll $0x3, %esi
movl $0x2, %edi
callq 0x44e0
movsd %xmm0, -0x3a0(%rbp)
jmp 0x3519
movsd -0x3a0(%rbp), %xmm2
movsd -0x3a8(%rbp), %xmm0
cvtsi2sdl -0x274(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x274(%rbp)
movl -0x298(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x298(%rbp)
jmp 0x34dc
cmpl $0x18, -0x274(%rbp)
je 0x35a4
movq 0x3a74(%rip), %rdi      # 0x6fd8
leaq 0x1b2f(%rip), %rsi      # 0x509a
callq 0x2160
movq %rax, -0x3b0(%rbp)
jmp 0x3579
movq -0x3b0(%rbp), %rdi
movq 0x3a41(%rip), %rsi      # 0x6fc8
callq 0x2180
jmp 0x358e
movl $0x1, -0x4(%rbp)
movl $0x1, -0x29c(%rbp)
jmp 0x43c0
movslq -0x264(%rbp), %rdi
callq 0x2030
movq %rax, -0x3b8(%rbp)
jmp 0x35b9
movq -0x3b8(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movslq -0x270(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x3c0(%rbp)
jmp 0x35f2
movq -0x3c0(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movslq -0x270(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x3c8(%rbp)
jmp 0x362b
movq -0x3c8(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movslq -0x270(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x3d0(%rbp)
jmp 0x3664
movq -0x3d0(%rbp), %rax
movq %rax, -0x2c0(%rbp)
movslq -0x270(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x3d8(%rbp)
jmp 0x369d
movq -0x3d8(%rbp), %rax
movq %rax, -0x2c8(%rbp)
movslq -0x270(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x3e0(%rbp)
jmp 0x36d6
movq -0x3e0(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movl $0x0, -0x2d4(%rbp)
movl -0x2d4(%rbp), %eax
cmpl -0x270(%rbp), %eax
jge 0x3852
movslq -0x26c(%rbp), %rdi
callq 0x2030
movq %rax, -0x3e8(%rbp)
jmp 0x3715
movq -0x3e8(%rbp), %rdx
movq -0x2b0(%rbp), %rax
movslq -0x2d4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x26c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x3f0(%rbp)
jmp 0x3759
movq -0x3f0(%rbp), %rdx
movq -0x2b8(%rbp), %rax
movslq -0x2d4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x268(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x3f8(%rbp)
jmp 0x379d
movq -0x3f8(%rbp), %rdx
movq -0x2c0(%rbp), %rax
movslq -0x2d4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x268(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x400(%rbp)
jmp 0x37e1
movq -0x400(%rbp), %rdx
movq -0x2c8(%rbp), %rax
movslq -0x2d4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x268(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x408(%rbp)
jmp 0x3825
movq -0x408(%rbp), %rdx
movq -0x2d0(%rbp), %rax
movslq -0x2d4(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x2d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d4(%rbp)
jmp 0x36ee
xorl %edx, %edx
movl %edx, %esi
leaq -0x250(%rbp), %rdi
callq 0x2210
jmp 0x3864
movq -0x2a8(%rbp), %rsi
movslq -0x264(%rbp), %rdx
leaq -0x250(%rbp), %rdi
callq 0x21f0
jmp 0x3880
leaq -0x250(%rbp), %rdi
leaq -0x27a(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x389a
movzbl -0x27a(%rbp), %eax
movl %eax, -0x278(%rbp)
leaq -0x250(%rbp), %rdi
leaq -0x27a(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x38c1
movzbl -0x27a(%rbp), %eax
movl %eax, -0x288(%rbp)
movl $0x0, -0x280(%rbp)
movl -0x268(%rbp), %eax
imull -0x270(%rbp), %eax
movl %eax, -0x284(%rbp)
movl $0x0, -0x2d8(%rbp)
movl $0x0, -0x2dc(%rbp)
movl -0x2d8(%rbp), %eax
cmpl -0x270(%rbp), %eax
je 0x3a06
leaq -0x250(%rbp), %rdi
leaq -0x27a(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x392b
leaq -0x250(%rbp), %rdi
leaq -0x279(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x3945
movl $0x0, -0x2e0(%rbp)
movl -0x2e0(%rbp), %eax
movzbl -0x27a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x3a01
movsbl -0x279(%rbp), %eax
imull -0x288(%rbp), %eax
cvtsi2sd %eax, %xmm0
movq -0x2c0(%rbp), %rax
movslq -0x2d8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2dc(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x280(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x280(%rbp)
movl -0x2dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2dc(%rbp)
movl -0x2dc(%rbp), %eax
cmpl -0x268(%rbp), %eax
jne 0x39eb
movl $0x0, -0x2dc(%rbp)
movl -0x2d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d8(%rbp)
movl -0x2d8(%rbp), %eax
cmpl -0x270(%rbp), %eax
jne 0x39e9
jmp 0x3a01
jmp 0x39eb
jmp 0x39ed
movl -0x2e0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2e0(%rbp)
jmp 0x394f
jmp 0x38ff
movl $0x0, -0x280(%rbp)
movl -0x268(%rbp), %eax
imull -0x270(%rbp), %eax
movl %eax, -0x284(%rbp)
movl $0x0, -0x2d8(%rbp)
movl $0x0, -0x2dc(%rbp)
movl -0x2d8(%rbp), %eax
cmpl -0x270(%rbp), %eax
je 0x3b3e
leaq -0x250(%rbp), %rdi
leaq -0x27a(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x3a63
leaq -0x250(%rbp), %rdi
leaq -0x279(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x3a7d
movl $0x0, -0x2e4(%rbp)
movl -0x2e4(%rbp), %eax
movzbl -0x27a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x3b39
movsbl -0x279(%rbp), %eax
imull -0x288(%rbp), %eax
cvtsi2sd %eax, %xmm0
movq -0x2c8(%rbp), %rax
movslq -0x2d8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2dc(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x280(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x280(%rbp)
movl -0x2dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2dc(%rbp)
movl -0x2dc(%rbp), %eax
cmpl -0x268(%rbp), %eax
jne 0x3b23
movl $0x0, -0x2dc(%rbp)
movl -0x2d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d8(%rbp)
movl -0x2d8(%rbp), %eax
cmpl -0x270(%rbp), %eax
jne 0x3b21
jmp 0x3b39
jmp 0x3b23
jmp 0x3b25
movl -0x2e4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2e4(%rbp)
jmp 0x3a87
jmp 0x3a37
movl $0x0, -0x280(%rbp)
movl -0x268(%rbp), %eax
imull -0x270(%rbp), %eax
movl %eax, -0x284(%rbp)
movl $0x0, -0x2d8(%rbp)
movl $0x0, -0x2dc(%rbp)
movl -0x2d8(%rbp), %eax
cmpl -0x270(%rbp), %eax
je 0x3c76
leaq -0x250(%rbp), %rdi
leaq -0x27a(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x3b9b
leaq -0x250(%rbp), %rdi
leaq -0x279(%rbp), %rsi
movl $0x1, %edx
callq 0x21f0
jmp 0x3bb5
movl $0x0, -0x2e8(%rbp)
movl -0x2e8(%rbp), %eax
movzbl -0x27a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x3c71
movsbl -0x279(%rbp), %eax
imull -0x288(%rbp), %eax
cvtsi2sd %eax, %xmm0
movq -0x2d0(%rbp), %rax
movslq -0x2d8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2dc(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x280(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x280(%rbp)
movl -0x2dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2dc(%rbp)
movl -0x2dc(%rbp), %eax
cmpl -0x268(%rbp), %eax
jne 0x3c5b
movl $0x0, -0x2dc(%rbp)
movl -0x2d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d8(%rbp)
movl -0x2d8(%rbp), %eax
cmpl -0x270(%rbp), %eax
jne 0x3c59
jmp 0x3c71
jmp 0x3c5b
jmp 0x3c5d
movl -0x2e8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2e8(%rbp)
jmp 0x3bbf
jmp 0x3b6f
movl $0x0, -0x2ec(%rbp)
movl -0x2ec(%rbp), %eax
cmpl -0x270(%rbp), %eax
jge 0x3ce7
movl $0x0, -0x2f0(%rbp)
movl -0x2f0(%rbp), %eax
cmpl -0x268(%rbp), %eax
jge 0x3cd4
movq -0x2b0(%rbp), %rax
movslq -0x2ec(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2f0(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x2f0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2f0(%rbp)
jmp 0x3c98
jmp 0x3cd6
movl -0x2ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ec(%rbp)
jmp 0x3c80
leaq -0x250(%rbp), %rdi
callq 0x2060
jmp 0x3cf5
movl -0x270(%rbp), %eax
movl %eax, -0x2f4(%rbp)
movl -0x268(%rbp), %eax
movl %eax, -0x2f8(%rbp)
movl -0x278(%rbp), %eax
movl %eax, -0x2fc(%rbp)
cmpl $0x1, -0x2fc(%rbp)
jl 0x3e1e
cvtsi2sdl -0x270(%rbp), %xmm0
movsd %xmm0, -0x418(%rbp)
movl -0x2fc(%rbp), %esi
decl %esi
movl $0x2, %edi
callq 0x44e0
movsd %xmm0, -0x410(%rbp)
jmp 0x3d52
movsd -0x410(%rbp), %xmm1
movsd -0x418(%rbp), %xmm0
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x2f4(%rbp)
cvtsi2sdl -0x268(%rbp), %xmm0
movsd %xmm0, -0x428(%rbp)
movl -0x2fc(%rbp), %esi
decl %esi
movl $0x2, %edi
callq 0x44e0
movsd %xmm0, -0x420(%rbp)
jmp 0x3d9c
movsd -0x420(%rbp), %xmm1
movsd -0x428(%rbp), %xmm0
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x2f8(%rbp)
movl -0x2f4(%rbp), %esi
movl -0x2f8(%rbp), %edx
leaq -0x2c0(%rbp), %rdi
callq 0x2720
jmp 0x3dd4
movl -0x2f4(%rbp), %esi
movl -0x2f8(%rbp), %edx
leaq -0x2c8(%rbp), %rdi
callq 0x2720
jmp 0x3dee
movl -0x2f4(%rbp), %esi
movl -0x2f8(%rbp), %edx
leaq -0x2d0(%rbp), %rdi
callq 0x2720
jmp 0x3e08
jmp 0x3e0a
movl -0x2fc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2fc(%rbp)
jmp 0x3d19
movl $0x0, -0x300(%rbp)
movl -0x300(%rbp), %eax
cmpl -0x270(%rbp), %eax
jge 0x3fcd
movl $0x0, -0x304(%rbp)
movl -0x304(%rbp), %eax
cmpl -0x26c(%rbp), %eax
jge 0x3eb9
movq -0x2c0(%rbp), %rax
movslq -0x300(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x430(%rbp)
movl -0x304(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x430(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x2b8(%rbp), %rax
movslq -0x300(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x304(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x304(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x304(%rbp)
jmp 0x3e44
movl $0x1, -0x308(%rbp)
movl -0x308(%rbp), %eax
cmpl -0x26c(%rbp), %eax
jge 0x3f38
movq -0x2c8(%rbp), %rax
movslq -0x300(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x438(%rbp)
movl -0x308(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x438(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x2b8(%rbp), %rax
movslq -0x300(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x308(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x308(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x308(%rbp)
jmp 0x3ec3
movl $0x2, -0x30c(%rbp)
movl -0x30c(%rbp), %eax
cmpl -0x26c(%rbp), %eax
jge 0x3fb7
movq -0x2d0(%rbp), %rax
movslq -0x300(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x440(%rbp)
movl -0x30c(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x440(%rbp), %rax
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x2b8(%rbp), %rax
movslq -0x300(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x30c(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x30c(%rbp)
jmp 0x3f42
jmp 0x3fb9
movl -0x300(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x300(%rbp)
jmp 0x3e28
movl $0x0, -0x310(%rbp)
movl -0x310(%rbp), %eax
cmpl -0x270(%rbp), %eax
jge 0x40c7
movl $0x0, -0x314(%rbp)
movl -0x314(%rbp), %eax
cmpl -0x26c(%rbp), %eax
jge 0x40b1
movq -0x2b8(%rbp), %rax
movslq -0x310(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x314(%rbp), %rcx
xorps %xmm0, %xmm0
ucomisd (%rax,%rcx,8), %xmm0
jbe 0x4049
movq -0x2b8(%rbp), %rax
movslq -0x310(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x314(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x2b8(%rbp), %rax
movslq -0x310(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x314(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0xfe9(%rip), %xmm1      # 0x5058
ucomisd %xmm1, %xmm0
jbe 0x409b
movq -0x2b8(%rbp), %rax
movslq -0x310(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x314(%rbp), %rcx
movsd 0xfc2(%rip), %xmm0      # 0x5058
movsd %xmm0, (%rax,%rcx,8)
jmp 0x409d
movl -0x314(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x314(%rbp)
jmp 0x3ff3
jmp 0x40b3
movl -0x310(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x310(%rbp)
jmp 0x3fd7
movl $0x0, -0x318(%rbp)
movl -0x318(%rbp), %eax
cmpl -0x270(%rbp), %eax
jge 0x415a
movl $0x0, -0x31c(%rbp)
movl -0x31c(%rbp), %eax
cmpl -0x26c(%rbp), %eax
jge 0x4144
movq -0x2b8(%rbp), %rax
movslq -0x318(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x31c(%rbp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movb %al, %dl
movq -0x2b0(%rbp), %rax
movslq -0x318(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x31c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x31c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x31c(%rbp)
jmp 0x40e9
jmp 0x4146
movl -0x318(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x318(%rbp)
jmp 0x40d1
movq -0x2b0(%rbp), %rax
movq %rax, -0x460(%rbp)
movl -0x270(%rbp), %eax
movl %eax, -0x458(%rbp)
movl -0x26c(%rbp), %eax
movl %eax, -0x454(%rbp)
movq -0x2a8(%rbp), %rax
movq %rax, -0x450(%rbp)
movl -0x264(%rbp), %eax
movl %eax, -0x444(%rbp)
leaq -0x340(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x20b0
jmp 0x41ac
movl -0x444(%rbp), %r8d
movq -0x450(%rbp), %rcx
movl -0x454(%rbp), %edx
movl -0x458(%rbp), %esi
movq -0x460(%rbp), %rdi
leaq -0x340(%rbp), %r9
callq 0x2fa0
jmp 0x41db
leaq -0x340(%rbp), %rdi
callq 0x20e0
movq -0x2a8(%rbp), %rax
movq %rax, -0x468(%rbp)
cmpq $0x0, %rax
je 0x420c
movq -0x468(%rbp), %rdi
movl $0x1, %esi
callq 0x2170
movl $0x0, -0x344(%rbp)
movl -0x344(%rbp), %eax
cmpl -0x270(%rbp), %eax
jge 0x431b
movq -0x2b0(%rbp), %rax
movslq -0x344(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x470(%rbp)
cmpq $0x0, %rax
je 0x4258
movq -0x470(%rbp), %rdi
movl $0x1, %esi
callq 0x2170
movq -0x2c0(%rbp), %rax
movslq -0x344(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x478(%rbp)
cmpq $0x0, %rax
je 0x4288
movq -0x478(%rbp), %rdi
movl $0x8, %esi
callq 0x2170
movq -0x2c8(%rbp), %rax
movslq -0x344(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x480(%rbp)
cmpq $0x0, %rax
je 0x42b8
movq -0x480(%rbp), %rdi
movl $0x8, %esi
callq 0x2170
movq -0x2d0(%rbp), %rax
movslq -0x344(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x488(%rbp)
cmpq $0x0, %rax
je 0x42e8
movq -0x488(%rbp), %rdi
movl $0x8, %esi
callq 0x2170
jmp 0x42ea
movl -0x344(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x344(%rbp)
jmp 0x4216
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x20e0
jmp 0x43d7
movq -0x2b0(%rbp), %rax
movq %rax, -0x490(%rbp)
cmpq $0x0, %rax
je 0x4340
movq -0x490(%rbp), %rdi
movl $0x8, %esi
callq 0x2170
movq -0x2c0(%rbp), %rax
movq %rax, -0x498(%rbp)
cmpq $0x0, %rax
je 0x4365
movq -0x498(%rbp), %rdi
movl $0x8, %esi
callq 0x2170
movq -0x2c8(%rbp), %rax
movq %rax, -0x4a0(%rbp)
cmpq $0x0, %rax
je 0x438a
movq -0x4a0(%rbp), %rdi
movl $0x8, %esi
callq 0x2170
movq -0x2d0(%rbp), %rax
movq %rax, -0x4a8(%rbp)
cmpq $0x0, %rax
je 0x43af
movq -0x4a8(%rbp), %rdi
movl $0x8, %esi
callq 0x2170
movl $0x0, -0x4(%rbp)
movl $0x1, -0x29c(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x2070
leaq -0x30(%rbp), %rdi
callq 0x20e0
jmp 0x43ee
leaq -0x250(%rbp), %rdi
callq 0x2070
leaq -0x30(%rbp), %rdi
callq 0x20e0
jmp 0x43fa
movl -0x4(%rbp), %eax
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x2290
nopw %cs:(%rax,%rax)
nopl (%rax)