// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movl %ecx, %ebx
movq %rdx, 0x20(%rsp)
movq %rsi, %r14
movq %rdi, %r15
movq 0x268(%rsp), %r13
leaq 0x160d(%rip), %rsi      # 0x506d
movq %r13, %rdi
callq 0x2230
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
callq 0x21f0
movq (%r13), %rsi
movq %rbp, %rdi
movl $0x14, %edx
callq 0x2050
movq 0x260(%rsp), %rax
movq (%r12), %rsi
movslq (%rax), %rdx
leaq 0x28(%rsp), %rdi
callq 0x2190
movl 0x270(%rsp), %eax
leaq 0xf(%rsp), %rsi
movb %al, (%rsi)
xorl %eax, %eax
movb %al, 0xd(%rsp)
movb %al, 0xe(%rsp)
leaq 0x28(%rsp), %rdi
movl $0x1, %edx
callq 0x2190
movl 0x278(%rsp), %eax
leaq 0xf(%rsp), %rsi
movb %al, (%rsi)
leaq 0x28(%rsp), %rdi
movl $0x1, %edx
callq 0x2190
movq (%r15), %rax
movq (%rax), %rax
movsd (%rax), %xmm1
movb $0x1, 0xd(%rsp)
movl %ebx, %r13d
movl %ebx, 0x1c(%rsp)
testl %ebx, %ebx
jle 0x3bc2
xorl %ebp, %ebp
leaq 0x28(%rsp), %r12
xorl %ebx, %ebx
movq (%r15), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x3b1e
jnp 0x3b5d
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
movl $0x1, %edx
movq %r12, %rdi
leaq 0xd(%rsp), %rsi
callq 0x2190
movl $0x1, %edx
movq %r12, %rdi
leaq 0xe(%rsp), %rsi
callq 0x2190
movq (%r15), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm1
movb $0x1, 0xd(%rsp)
jmp 0x3baa
movb 0xd(%rsp), %al
cmpb $-0x1, %al
je 0x3b6d
incb %al
movb %al, 0xd(%rsp)
jmp 0x3baa
movsd %xmm1, 0x10(%rsp)
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
movl $0x1, %edx
movq %r12, %rdi
leaq 0xd(%rsp), %rsi
callq 0x2190
movl $0x1, %edx
movq %r12, %rdi
leaq 0xe(%rsp), %rsi
callq 0x2190
movb $0x1, 0xd(%rsp)
movsd 0x10(%rsp), %xmm1
incq %rbx
cmpq %rbx, %r13
jne 0x3b0a
incq %rbp
cmpq %r13, %rbp
jne 0x3b08
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0xd(%rsp), %rsi
movl $0x1, %edx
callq 0x2190
leaq 0x28(%rsp), %rdi
leaq 0xe(%rsp), %rsi
movl $0x1, %edx
callq 0x2190
movq (%r14), %rax
movq (%rax), %rax
movsd (%rax), %xmm1
movb $0x1, 0xd(%rsp)
cmpl $0x0, 0x1c(%rsp)
jle 0x3ccf
xorl %r12d, %r12d
leaq 0x28(%rsp), %r15
leaq 0xe(%rsp), %rbp
xorl %ebx, %ebx
movq (%r14), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x3c2f
jnp 0x3c6c
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0xd(%rsp), %rsi
callq 0x2190
movl $0x1, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2190
movq (%r14), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm1
movb $0x1, 0xd(%rsp)
jmp 0x3cb7
movb 0xd(%rsp), %al
cmpb $-0x1, %al
je 0x3c7c
incb %al
movb %al, 0xd(%rsp)
jmp 0x3cb7
movsd %xmm1, 0x10(%rsp)
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
movl $0x1, %edx
movq %r15, %rdi
leaq 0xd(%rsp), %rsi
callq 0x2190
movl $0x1, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2190
movb $0x1, 0xd(%rsp)
movsd 0x10(%rsp), %xmm1
incq %rbx
cmpq %rbx, %r13
jne 0x3c1b
incq %r12
cmpq %r13, %r12
jne 0x3c19
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0xd(%rsp), %rsi
movl $0x1, %edx
callq 0x2190
leaq 0x28(%rsp), %rdi
leaq 0xe(%rsp), %rsi
movl $0x1, %edx
callq 0x2190
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movsd (%rax), %xmm1
movb $0x1, 0xd(%rsp)
cmpl $0x0, 0x1c(%rsp)
jle 0x3deb
xorl %ebp, %ebp
leaq 0x28(%rsp), %r14
leaq 0xd(%rsp), %r15
leaq 0xe(%rsp), %r12
xorl %ebx, %ebx
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x3d4a
jnp 0x3d8a
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2190
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2190
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm1
movb $0x1, 0xd(%rsp)
jmp 0x3dd3
movb 0xd(%rsp), %al
cmpb $-0x1, %al
je 0x3d9a
incb %al
movb %al, 0xd(%rsp)
jmp 0x3dd3
movsd %xmm1, 0x10(%rsp)
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2190
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2190
movb $0x1, 0xd(%rsp)
movsd 0x10(%rsp), %xmm1
incq %rbx
cmpq %rbx, %r13
jne 0x3d31
incq %rbp
cmpq %r13, %rbp
jne 0x3d2f
cvttsd2si %xmm1, %eax
movb %al, 0xe(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0xd(%rsp), %rsi
movl $0x1, %edx
callq 0x2190
leaq 0x28(%rsp), %rdi
leaq 0xe(%rsp), %rsi
movl $0x1, %edx
callq 0x2190
leaq 0x28(%rsp), %rdi
callq 0x21b0
movq 0x31a4(%rip), %rax      # 0x6fd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x30(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x28(%rsp,%rcx)
callq 0x20f0
leaq 0x120(%rsp), %rdi
callq 0x2080
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3e73
jmp 0x3e73
jmp 0x3e73
jmp 0x3e73
jmp 0x3e73
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x21c0
movq %rbx, %rdi
callq 0x2210