// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %r9, %rbx
movl %ecx, %ebp
movq %rdx, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r15
movq 0x268(%rsp), %r13
leaq 0x18a0(%rip), %rsi      # 0x50ed
movq %r13, %rdi
callq 0x2220
leaq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x21e0
movq (%r13), %rsi
pushq $0x14
popq %rdx
movq %r12, %rdi
callq 0x2040
movq 0x260(%rsp), %rax
movq (%rbx), %rsi
movslq (%rax), %rdx
leaq 0x28(%rsp), %rdi
callq 0x2170
movl 0x270(%rsp), %eax
leaq 0x7(%rsp), %rsi
movb %al, (%rsi)
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x2170
movl 0x278(%rsp), %eax
leaq 0x7(%rsp), %rsi
movb %al, (%rsi)
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x2170
movq (%r15), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x5(%rsp), %rax
movb $0x1, (%rax)
xorl %r12d, %r12d
testl %ebp, %ebp
movl $0x0, %eax
cmovgl %ebp, %eax
movq %rax, 0x10(%rsp)
movl %ebp, %ebx
movb $0x1, %al
leaq 0x28(%rsp), %r13
pushq $0x1
popq %rbp
cmpq 0x10(%rsp), %r12
je 0x39a8
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x39a0
movq (%r15), %rcx
movq (%rcx,%r12,8), %rcx
movsd (%rcx,%r14,8), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
cvttsd2si %xmm1, %ecx
jne 0x392b
jnp 0x396b
movb %cl, 0x6(%rsp)
movq %r13, %rdi
leaq 0x5(%rsp), %rsi
movq %rbp, %rdx
callq 0x2170
movq %r13, %rdi
leaq 0x6(%rsp), %rsi
movq %rbp, %rdx
callq 0x2170
movq (%r15), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movb $0x1, 0x5(%rsp)
movb $0x1, %al
jmp 0x3975
cmpb $-0x1, %al
je 0x397a
incb %al
movb %al, 0x5(%rsp)
incq %r14
jmp 0x3903
movb %cl, 0x6(%rsp)
movq %r13, %rdi
leaq 0x5(%rsp), %rsi
movq %rbp, %rdx
callq 0x2170
movq %r13, %rdi
leaq 0x6(%rsp), %rsi
movq %rbp, %rdx
callq 0x2170
jmp 0x3962
incq %r12
jmp 0x38f5
cvttsd2si 0x8(%rsp), %eax
movb %al, 0x6(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x5(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2170
leaq 0x28(%rsp), %rdi
leaq 0x6(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2170
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x5(%rsp), %rax
movb $0x1, (%rax)
movb $0x1, %al
xorl %r15d, %r15d
leaq 0x28(%rsp), %r12
pushq $0x1
popq %r13
leaq 0x6(%rsp), %rbp
cmpq 0x10(%rsp), %r15
je 0x3abf
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x3ab7
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%r15,8), %rcx
movsd (%rcx,%r14,8), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
cvttsd2si %xmm1, %ecx
jne 0x3a41
jnp 0x3a84
movb %cl, 0x6(%rsp)
movq %r12, %rdi
leaq 0x5(%rsp), %rsi
movq %r13, %rdx
callq 0x2170
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2170
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movb $0x1, 0x5(%rsp)
movb $0x1, %al
jmp 0x3a8e
cmpb $-0x1, %al
je 0x3a93
incb %al
movb %al, 0x5(%rsp)
incq %r14
jmp 0x3a14
movb %cl, 0x6(%rsp)
movq %r12, %rdi
leaq 0x5(%rsp), %rsi
movq %r13, %rdx
callq 0x2170
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2170
jmp 0x3a7b
incq %r15
jmp 0x3a06
cvttsd2si 0x8(%rsp), %eax
movb %al, 0x6(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x5(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2170
leaq 0x28(%rsp), %rdi
leaq 0x6(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2170
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x5(%rsp), %rax
movb $0x1, (%rax)
movb $0x1, %al
xorl %ebp, %ebp
leaq 0x28(%rsp), %r15
pushq $0x1
popq %r12
leaq 0x6(%rsp), %r13
cmpq 0x10(%rsp), %rbp
je 0x3bd5
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x3bcd
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq (%rcx,%rbp,8), %rcx
movsd (%rcx,%r14,8), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
cvttsd2si %xmm1, %ecx
jne 0x3b57
jnp 0x3b9a
movb %cl, 0x6(%rsp)
movq %r15, %rdi
leaq 0x5(%rsp), %rsi
movq %r12, %rdx
callq 0x2170
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x2170
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movb $0x1, 0x5(%rsp)
movb $0x1, %al
jmp 0x3ba4
cmpb $-0x1, %al
je 0x3ba9
incb %al
movb %al, 0x5(%rsp)
incq %r14
jmp 0x3b2a
movb %cl, 0x6(%rsp)
movq %r15, %rdi
leaq 0x5(%rsp), %rsi
movq %r12, %rdx
callq 0x2170
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x2170
jmp 0x3b91
incq %rbp
jmp 0x3b1c
cvttsd2si 0x8(%rsp), %eax
movb %al, 0x6(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x5(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2170
leaq 0x28(%rsp), %rdi
leaq 0x6(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x2170
leaq 0x28(%rsp), %rdi
callq 0x21a0
leaq 0x28(%rsp), %rdi
callq 0x21b0
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c33
jmp 0x3c33
jmp 0x3c33
jmp 0x3c33
jmp 0x3c33
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x21b0
movq %rbx, %rdi
callq 0x2200