// File: /workspace/github_c_makefile_star2_O2/ijleesw[P]data-compression/src/tunstall/main.cpp
// Function: main
int main(int argc, char** argv)
{
	unsigned char* data;
	unsigned int size;

	if (argc < 4) {
		cout << "Usage: "<<argv[0]<<" <input path> <compression path> <decompression path>\n";
		return -1;
	}
	if (!readchar2buf(argv[1], data, &size)) {
		perror("File open error.\n");
		return -1;
	}
	ofstream compressed(argv[2]);
	ofstream decompressed(argv[3]);

	// 8: bits of each code symbol
	// 2: n_symbol^2 lookup tables
	Tunstall t = Tunstall(8, 2);


	/******************************
	 *
	 *  Compression
	 *
	 ******************************/

	t.getProbabilities(data, size);
	t.createDecodingTables();
	t.createEncodingTables();
	int compressed_size;
	unsigned char *compressed_data = t.compress(data, size, compressed_size);
	
	compressed << (unsigned char)t.probabilities.size();
	int tmp_size = t.probabilities.size();
	for (int i = 0; i < tmp_size; i++) compressed << (unsigned char)t.probabilities[i].symbol << (unsigned char)t.probabilities[i].probability;
	
	compressed << size;
	compressed << compressed_size;

	for (int i = 0; i < compressed_size; i++) compressed << (unsigned char)compressed_data[i];

	compressed.close();

	compressed_size =  1 + t.probabilities.size()*2 + 4 + 4 + compressed_size;



	/******************************
	 *
	 *  Decompression
	 *
	 ******************************/

	int nsymbols = t.probabilities.size();
	void *probs = t.probabilities.data();
	t.probabilities.resize(nsymbols);
	memcpy(t.probabilities.data(), probs, nsymbols*2);
	t.createDecodingTables();
	unsigned char* output = (uchar*)malloc((size+1)*sizeof(uchar));
	if(size) {
		cout << "";
		t.decompress(compressed_data, compressed_size, output, size);
	}

	for (int i = 0; i < size; i++) decompressed << output[i];

	decompressed.close();

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movq %rsi, %rbx
cmpl $0x3, %edi
jg 0x3599
movq 0x4a6d(%rip), %rdi      # 0x7fd8
leaq 0x1bf9(%rip), %rsi      # 0x516b
callq 0x2150
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x2150
leaq 0x1bea(%rip), %rsi      # 0x5173
movq %rax, %rdi
callq 0x2150
pushq $-0x1
popq %rax
jmp 0x37f6
movq 0x8(%rbx), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
callq 0x336a
testl %eax, %eax
je 0x3808
movq 0x10(%rbx), %rsi
leaq 0xb8(%rsp), %rdi
pushq $0x10
popq %rbp
movl %ebp, %edx
callq 0x2170
movq 0x18(%rbx), %rsi
leaq 0x2b8(%rsp), %rdi
movl %ebp, %edx
callq 0x2170
movabsq $0x8000000ff, %rax      # imm = 0x8000000FF
leaq 0x10(%rsp), %rdi
movq %rax, (%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x50(%rdi)
movups %xmm0, 0x60(%rdi)
movl $0x2, 0x70(%rdi)
movups %xmm0, 0x78(%rdi)
movups %xmm0, 0x88(%rdi)
movups %xmm0, 0x98(%rdi)
movq 0x8(%rsp), %r14
movl 0x4(%rsp), %ebx
movq %r14, %rsi
movl %ebx, %edx
callq 0x23ac
leaq 0x10(%rsp), %rdi
callq 0x2b04
leaq 0x10(%rsp), %rdi
callq 0x2eda
leaq 0x10(%rsp), %rdi
movq %rsp, %rcx
movq %r14, %rsi
movl %ebx, %edx
callq 0x307c
movq %rax, %r14
movl 0x28(%rsp), %eax
subl 0x20(%rsp), %eax
shrl %eax
movsbl %al, %esi
leaq 0xb8(%rsp), %rdi
callq 0x2190
leaq 0x20(%rsp), %rax
movq 0x8(%rax), %r13
subq (%rax), %r13
shrq %r13
xorl %ebp, %ebp
testl %r13d, %r13d
cmovlel %ebp, %r13d
leaq 0xb8(%rsp), %r12
cmpq %rbp, %r13
je 0x36c6
movq 0x20(%rsp), %rax
movsbl (%rax,%rbp,2), %esi
movq %r12, %rdi
callq 0x2190
movq 0x20(%rsp), %rcx
movsbl 0x1(%rcx,%rbp,2), %esi
movq %rax, %rdi
callq 0x2190
incq %rbp
jmp 0x3699
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
callq 0x20f0
movl (%rsp), %r12d
leaq 0xb8(%rsp), %rdi
movl %r12d, %esi
callq 0x2240
xorl %ebp, %ebp
testl %r12d, %r12d
movl $0x0, %r15d
cmovgl %r12d, %r15d
leaq 0xb8(%rsp), %r13
cmpq %rbp, %r15
je 0x3718
movsbl (%r14,%rbp), %esi
movq %r13, %rdi
callq 0x2190
incq %rbp
jmp 0x3701
leaq 0xb8(%rsp), %rdi
callq 0x21e0
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
subq %r13, %rbp
movq %rbp, %rax
shrq %rax
movslq %eax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x3dbc
andl $-0x2, %ebp
leaq 0x10(%rsp), %r15
movq 0x10(%r15), %rdi
movslq %ebp, %rdx
movq %r13, %rsi
callq 0x2110
movq %r15, %rdi
callq 0x2b04
leal 0x1(%rbx), %edi
callq 0x21c0
movq %rax, %r15
testq %rbx, %rbx
je 0x37a1
movq 0x485d(%rip), %rdi      # 0x7fd8
leaq 0x1a39(%rip), %rsi      # 0x51bb
callq 0x2150
leal (%r12,%rbp), %edx
addl $0x9, %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r15, %rcx
movl %ebx, %r8d
callq 0x31fe
xorl %r12d, %r12d
leaq 0x2b8(%rsp), %r14
cmpq %r12, %rbx
je 0x37c3
movsbl (%r15,%r12), %esi
movq %r14, %rdi
callq 0x2190
incq %r12
jmp 0x37ac
leaq 0x2b8(%rsp), %rdi
callq 0x21e0
leaq 0x10(%rsp), %rdi
callq 0x3dec
leaq 0x2b8(%rsp), %rdi
callq 0x2200
leaq 0xb8(%rsp), %rdi
callq 0x2200
xorl %eax, %eax
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x199b(%rip), %rdi      # 0x51aa
callq 0x2120
jmp 0x3591
movq %rax, %rbx
jmp 0x3846
jmp 0x382c
jmp 0x382c
jmp 0x382c
jmp 0x382c
jmp 0x382c
jmp 0x382c
jmp 0x382c
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3dec
leaq 0x2b8(%rsp), %rdi
callq 0x2200
leaq 0xb8(%rsp), %rdi
callq 0x2200
movq %rbx, %rdi
callq 0x2260