// File: kchan1[P]helloworld_test1/main.cpp
// Function: compress(double**&, double**&, double**&, int, int, unsigned char*&, int&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int)
void compress(double** &blue_data,double** &green_data, 
	      double** &red_data, int height,int width,
              unsigned char* &image_header_data, int &bitmap_offset,
	      std::string filename , int decomposition_loops, int quant){

	filename+=".205";
	std::ofstream output_file;

	output_file.open(filename.c_str(), std::ofstream::out | std::ofstream::binary);
	output_file.write(reinterpret_cast<const char*>(image_header_data), bitmap_offset);
	
	unsigned char depth[1];
	depth[0] = decomposition_loops;
	unsigned char how_many[1];
	how_many[0] = 0;
	char value[1];
	value[0] = 0;
	double current;
	int saved = 0;
	
	output_file.write(reinterpret_cast<const char*>(depth), 1);

	depth[0] = quant;

	output_file.write(reinterpret_cast<const char*>(depth), 1);

	current = blue_data[0][0];
	how_many[0]=1;
	//int cout_count = 0;
	int count = 0;

	for (int i = 0; i<height; i++){
		for (int j = 0; j<height; j++){
			if (blue_data[i][j]!=current){
				value[0] = current;
				output_file.write(reinterpret_cast<const char*>(how_many), 1);
				output_file.write(reinterpret_cast<const char*>(value),1);
				//if(cout_count<49927){
				//std::cout<<(int)how_many[0]<<" "<<(int)value[0]<<std::endl;
				//cout_count++;
				//std::cout<<cout_count<<std::endl;
				//}
				saved+=how_many[0]-2;
				current = blue_data[i][j];
				count+=how_many[0];
				how_many[0] = 1;
				continue;		
			}
			if (how_many[0] == 255){
				value[0] =  current;
				output_file.write(reinterpret_cast<const char*>(how_many), 1);
				output_file.write(reinterpret_cast<const char*>(value),1);
				//if(cout_count<49927){
				//std::cout<<(int)how_many[0]<<" "<<(int)value[0]<<std::endl;
				//cout_count++;
				//std::cout<<cout_count<<std::endl;
				//}
				saved+=how_many[0]-2;
				count+=how_many[0];
				how_many[0] = 1;
				continue;
			}
			how_many[0]++;
		}
	}
	value[0] =  current;
	output_file.write(reinterpret_cast<const char*>(how_many), 1);
	output_file.write(reinterpret_cast<const char*>(value),1);
	//std::cout<<(int)how_many[0]<<" "<<(int)value[0]<<std::endl;
	count+=how_many[0];
	//std::cout<<count<<std::endl;

	current = green_data[0][0];
	how_many[0]=1;
	count=0;

	for (int i = 0; i<height; i++){
		for (int j = 0; j<height; j++){
			if (green_data[i][j]!=current){
				value[0] = current;
				output_file.write(reinterpret_cast<const char*>(how_many), 1);
				output_file.write(reinterpret_cast<const char*>(value),1);
				//std::cout<<(int)how_many[0]<<" "<<(int)value[0]<<std::endl;
				saved+=how_many[0]-2;
				current = green_data[i][j];
				count+=how_many[0];
				how_many[0] = 1;
				continue;		
			}
			if (how_many[0] == 255){
				value[0] = current;
				output_file.write(reinterpret_cast<const char*>(how_many), 1);
				output_file.write(reinterpret_cast<const char*>(value),1);
				//std::cout<<(int)how_many[0]<<" "<<(int)value[0]<<std::endl;
				saved+=how_many[0]-2;
				count+=how_many[0];
				how_many[0] = 1;
				continue;
			}
			how_many[0]++;
		}
	}
	value[0] =  current;
	output_file.write(reinterpret_cast<const char*>(how_many), 1);
	output_file.write(reinterpret_cast<const char*>(value),1);
	count+=how_many[0];
	//std::cout<<count<<std::endl;

	current = red_data[0][0];
	how_many[0]=1;
	count = 0;

	for (int i = 0; i<height; i++){
		for (int j = 0; j<height; j++){
			if (red_data[i][j]!=current){
				value[0] = current;
				output_file.write(reinterpret_cast<const char*>(how_many),1);
				output_file.write(reinterpret_cast<const char*>(value),1);
				//std::cout<<(int)how_many[0]<<" "<<(int)value[0]<<std::endl;
				saved+=how_many[0]-2;
				count+=how_many[0];
				current = red_data[i][j];
				how_many[0] = 1;
				continue;		
			}
			if (how_many[0] == 255){
				value[0] = current;
				output_file.write(reinterpret_cast<const char*>(how_many), 1);
				output_file.write(reinterpret_cast<const char*>(value),1);
				//std::cout<<(int)how_many[0]<<" "<<(int)value[0]<<std::endl;
				saved+=how_many[0]-2;
				count+=how_many[0];
				how_many[0] = 1;
				continue;
			}
			how_many[0]++;
		}
	}
	value[0] =  current;
	output_file.write(reinterpret_cast<const char*>(how_many), 1);
	output_file.write(reinterpret_cast<const char*>(value),1);
	count+=how_many[0];
	//std::cout<<count<<std::endl;	

	//std::cout<<saved<<std::endl;
	
	output_file.close();
	return;
	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, %rax
movl 0x28(%rbp), %edi
movl 0x20(%rbp), %edi
movq 0x18(%rbp), %rdi
movq %rdi, -0x278(%rbp)
movq 0x10(%rbp), %r10
movq %rax, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movq %r9, -0x28(%rbp)
leaq 0x264c(%rip), %rsi      # 0x70ed
callq 0x2160
leaq -0x228(%rbp), %rdi
callq 0x22c0
movq -0x278(%rbp), %rdi
callq 0x20a0
movq %rax, -0x270(%rbp)
movl $0x10, %edi
movl $0x4, %esi
callq 0x6780
movl %eax, -0x264(%rbp)
jmp 0x4adc
movl -0x264(%rbp), %edx
movq -0x270(%rbp), %rsi
leaq -0x228(%rbp), %rdi
callq 0x2040
jmp 0x4af7
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movq 0x10(%rbp), %rax
movslq (%rax), %rdx
leaq -0x228(%rbp), %rdi
callq 0x2230
jmp 0x4b13
movb 0x20(%rbp), %al
movb %al, -0x235(%rbp)
movb $0x0, -0x236(%rbp)
movb $0x0, -0x237(%rbp)
movl $0x0, -0x244(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x235(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4b4e
movb 0x28(%rbp), %al
movb %al, -0x235(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x235(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4b71
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x240(%rbp)
movb $0x1, -0x236(%rbp)
movl $0x0, -0x248(%rbp)
movl $0x0, -0x24c(%rbp)
movl -0x24c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4d73
movl $0x0, -0x250(%rbp)
movl -0x250(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4d5d
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x24c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x250(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x240(%rbp), %xmm0
jne 0x4bf9
jp 0x4bf9
jmp 0x4cb9
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4c21
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4c3b
movzbl -0x236(%rbp), %eax
subl $0x2, %eax
addl -0x244(%rbp), %eax
movl %eax, -0x244(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x24c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x250(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x240(%rbp)
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movb $0x1, -0x236(%rbp)
jmp 0x4d49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x230(%rbp)
movl %eax, -0x234(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x2270
jmp 0x5255
movzbl -0x236(%rbp), %eax
cmpl $0xff, %eax
jne 0x4d3b
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4cef
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4d09
movzbl -0x236(%rbp), %eax
subl $0x2, %eax
addl -0x244(%rbp), %eax
movl %eax, -0x244(%rbp)
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movb $0x1, -0x236(%rbp)
jmp 0x4d49
movb -0x236(%rbp), %al
addb $0x1, %al
movb %al, -0x236(%rbp)
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x4bbb
jmp 0x4d5f
movl -0x24c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24c(%rbp)
jmp 0x4ba2
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4d9b
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4db5
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x240(%rbp)
movb $0x1, -0x236(%rbp)
movl $0x0, -0x248(%rbp)
movl $0x0, -0x254(%rbp)
movl -0x254(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4fa7
movl $0x0, -0x258(%rbp)
movl -0x258(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4f91
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x254(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x258(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x240(%rbp), %xmm0
jne 0x4e50
jp 0x4e50
jmp 0x4eed
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4e78
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4e92
movzbl -0x236(%rbp), %eax
subl $0x2, %eax
addl -0x244(%rbp), %eax
movl %eax, -0x244(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x254(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x258(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x240(%rbp)
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movb $0x1, -0x236(%rbp)
jmp 0x4f7d
movzbl -0x236(%rbp), %eax
cmpl $0xff, %eax
jne 0x4f6f
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4f23
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4f3d
movzbl -0x236(%rbp), %eax
subl $0x2, %eax
addl -0x244(%rbp), %eax
movl %eax, -0x244(%rbp)
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movb $0x1, -0x236(%rbp)
jmp 0x4f7d
movb -0x236(%rbp), %al
addb $0x1, %al
movb %al, -0x236(%rbp)
movl -0x258(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x258(%rbp)
jmp 0x4e12
jmp 0x4f93
movl -0x254(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x254(%rbp)
jmp 0x4df9
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4fcf
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x4fe9
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x240(%rbp)
movb $0x1, -0x236(%rbp)
movl $0x0, -0x248(%rbp)
movl $0x0, -0x25c(%rbp)
movl -0x25c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x51db
movl $0x0, -0x260(%rbp)
movl -0x260(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x51c5
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x25c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x260(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x240(%rbp), %xmm0
jne 0x5084
jp 0x5084
jmp 0x5121
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x50ac
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x50c6
movzbl -0x236(%rbp), %eax
subl $0x2, %eax
addl -0x244(%rbp), %eax
movl %eax, -0x244(%rbp)
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x25c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x260(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x240(%rbp)
movb $0x1, -0x236(%rbp)
jmp 0x51b1
movzbl -0x236(%rbp), %eax
cmpl $0xff, %eax
jne 0x51a3
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x5157
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x5171
movzbl -0x236(%rbp), %eax
subl $0x2, %eax
addl -0x244(%rbp), %eax
movl %eax, -0x244(%rbp)
movzbl -0x236(%rbp), %eax
addl -0x248(%rbp), %eax
movl %eax, -0x248(%rbp)
movb $0x1, -0x236(%rbp)
jmp 0x51b1
movb -0x236(%rbp), %al
addb $0x1, %al
movb %al, -0x236(%rbp)
movl -0x260(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x260(%rbp)
jmp 0x5046
jmp 0x51c7
movl -0x25c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x25c(%rbp)
jmp 0x502d
cvttsd2si -0x240(%rbp), %eax
movb %al, -0x237(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x236(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x5203
leaq -0x228(%rbp), %rdi
leaq -0x237(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
jmp 0x521d
movzbl -0x236(%rbp), %ecx
movl -0x248(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x248(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x2250
jmp 0x5240
leaq -0x228(%rbp), %rdi
callq 0x2270
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
movq -0x230(%rbp), %rdi
callq 0x22f0
nopw %cs:(%rax,%rax)