// File: /workspace/github_cpp_makefile_star2_O3/irakr[P]Minimal-File-Compression-Tool/src/compression-driver.cpp
// Function: CompressionDriver::compress(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void CompressionDriver :: compress(std::string &ifilename) {
	Log(this, "Started...");
	if(!m_Compressor) {
		Log(this, "[Fatal Error] No compression algorithm registered.");
		return; // FIXME... Throw an exception from here
	}

	std::cout << "Input File: " << ifilename << std::endl;

	// Output filename
	// (XXX) For now this is the auto generated output file name
	std::string ofilename = ifilename + ".compressed";

	// TODO ...
	// Open file named 'file'
	// Pass the handle to specific encode() function
	// encode() should return back the compressed file handle
	std::fstream inputf;
	inputf.open(ifilename, std::fstream::in | std::fstream::binary);
	if(!inputf.is_open()) {
		Logf(this, "[Error] The file \"%s\" does not exist.", ifilename.c_str());
		throw "fstream::open() failed.";
	}

	fsize_t file_size = getFileSize(inputf);
    std::cout << "Size: " << file_size << " bytes" << std::endl;
    
	// Read bytes from input file
	byte *in_data_byte_buff = new byte[file_size + 1];
	inputf.read((char*)in_data_byte_buff, file_size);
	in_data_byte_buff[file_size] = 0; // Append null at the end because read() doesn't
	//std::cout << "file_buff: " << in_data_byte_buff << std::endl;

	// Get the compressed byte stream
	byte* compressed_buff = m_Compressor->encoder().encode(in_data_byte_buff, &file_size);
	if(!compressed_buff) {
	    // TODO... Cleanup code like should be placed somewhere else systematically.
	    Logf(this, "Error: File too small to be compressed.");
	    delete[] in_data_byte_buff;
	    inputf.close();
	    return;
	}
	std::cout << "Compressed size = " << file_size << " bytes" << std::endl;

	// Open output file. The size will be obviously less than or impossibly
	// equal to the input file size;
	// Then write the compressed byte stream to it.
	std::fstream outputf;
	outputf.open(ofilename, std::fstream::out | std::fstream::binary);
	outputf.write((const char*)compressed_buff,
		file_size);

	inputf.close();
	outputf.close();
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
cmpq $0x0, 0x38(%rdi)
je 0x3b80
movq %rsi, %r14
movq %rdi, %rbx
movq 0x56d0(%rip), %r15      # 0x8fc0
leaq 0x286e(%rip), %rsi      # 0x6165
movl $0xc, %edx
movq %r15, %rdi
callq 0x2200
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x3574
leaq 0x280d(%rip), %rsi      # 0x6172
movq %r15, %rdi
callq 0x2350
leaq 0x28(%rsp), %rdi
callq 0x2210
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
movl $0xc, %edx
callq 0x2280
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0x2090
testb %al, %al
je 0x3b8f
movq %r14, %rdi
callq 0x2090
movq $-0x1, %r14
testb %al, %al
je 0x39e0
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2270
leaq 0x28(%rsp), %rdi
callq 0x2310
movq %rax, %r14
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2270
movq %r14, (%rsp)
movq 0x55d5(%rip), %rdi      # 0x8fc0
leaq 0x27a4(%rip), %rsi      # 0x6196
movl $0x6, %edx
callq 0x2200
movq 0x55bd(%rip), %rdi      # 0x8fc0
movq %r14, %rsi
callq 0x21b0
movq %rax, %r15
leaq 0x2788(%rip), %rsi      # 0x619d
movl $0x6, %edx
movq %rax, %rdi
callq 0x2200
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
leaq 0x1(%r14), %rdi
callq 0x2030
movq %rax, %r15
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x2230
movb $0x0, (%r15,%r14)
movq 0x38(%rbx), %rax
movq 0x40(%rax), %rdi
movq (%rdi), %rax
movq %rsp, %rdx
movq %r15, %rsi
callq *(%rax)
movq %rax, %rbx
testq %rax, %rax
je 0x3b4d
movq 0x5530(%rip), %rdi      # 0x8fc0
leaq 0x270d(%rip), %rsi      # 0x61a4
movl $0x12, %edx
callq 0x2200
movq (%rsp), %rsi
movq 0x5514(%rip), %rdi      # 0x8fc0
callq 0x21b0
movq %rax, %r14
leaq 0x26e2(%rip), %rsi      # 0x619d
movl $0x6, %edx
movq %rax, %rdi
callq 0x2200
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
leaq 0x238(%rsp), %rdi
callq 0x2210
leaq 0x238(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x14, %edx
callq 0x2280
leaq 0x248(%rsp), %rdi
movq (%rsp), %rdx
movq %rbx, %rsi
callq 0x2250
leaq 0x28(%rsp), %rdi
callq 0x2050
leaq 0x238(%rsp), %rdi
callq 0x2050
leaq 0x238(%rsp), %rdi
callq 0x2080
jmp 0x3b5f
movq %r15, %rdi
callq 0x2240
leaq 0x28(%rsp), %rdi
callq 0x2050
leaq 0x28(%rsp), %rdi
callq 0x2080
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x3b80
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movl $0x8, %edi
callq 0x2110
leaq 0x25de(%rip), %rcx      # 0x617e
movq %rcx, (%rax)
movq 0x53fe(%rip), %rsi      # 0x8fa8
movq %rax, %rdi
xorl %edx, %edx
callq 0x2300
jmp 0x3bd5
jmp 0x3bb8
movq %rax, %rbx
jmp 0x3be2
jmp 0x3bd5
jmp 0x3bd5
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
callq 0x2080
jmp 0x3bd8
jmp 0x3bd5
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2080
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x3bf9
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x2320
nop