// File: /workspace/github_c_makefile_star2_O3/ijleesw[P]data-compression/src/checker/main.cpp
// Function: main
int main(int argc, char** argv)
{
	uchar* original;
	uchar* compressed;
	uchar* decompressed;
	int o_numofsymbols;
	int c_numofsymbols;
	long o_length, o_actual_length;
	long c_length, c_actual_length;
	long d_length;
	double ratio;
	bool is_same = true;
	bool skip = false;
	string result;


	if (argc != 5) {
		cout << "Usage: "<<argv[0];
		cout << " <data name> <original file path> <compressed file path> <decompressed file path (or -skip)>\n";
		return -1;
	}
	if (strcmp(argv[4], "-skip") == 0) skip = true;

	cout << argv[1] << ": ";

	if (!readbyte2buf(argv[2], original, &o_length)) {
		perror("File open error");
		return -1;
	}
	if (!readbyte2buf(argv[3], compressed, &c_length)) {
		perror("File open error");
		return -1;
	}

	o_numofsymbols = symbol_counter(original, o_length);
	o_actual_length = o_length * ceil_log2(o_numofsymbols) / 8;
	c_numofsymbols = symbol_counter(compressed, c_length);
	c_actual_length = c_length * ceil_log2(c_numofsymbols) / 8;

	ratio = (double)c_length/o_length;

	if (!skip){
		if (!readbyte2buf(argv[4], decompressed, &d_length)) {
			perror("File open error");
			return -1;
		}

		if (o_length != d_length) is_same = false;

		if (is_same) {
		for (long i = 0; i < o_length; i++) {
			if (original[i] != decompressed[i]) {
				is_same = false;
				break;
			}
		}
		}
	}

	if (is_same) {
		printf("%ld(%ld, %d) -> %ld(%ld, %d)   Ratio: %.3f%% (%.3f%%)\n",
			    o_length, o_actual_length, o_numofsymbols, c_length, c_actual_length, c_numofsymbols,
			    100*ratio, (double)100*c_actual_length/o_actual_length);
	/*
		printf("%ld(%ld) -> %ld   Ratio: %.3f%% (%.3f%%)\n",
			    o_length, o_actual_length, c_length,
			    100*ratio, (double)100*c_length/o_actual_length);	
	*/
	}
	else cout << "decompressed file not identical to the original file.\n";


	delete original;
	delete compressed;
	if (!skip) delete decompressed;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r14
leaq -0x88(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x5, %edi
jne 0x276c
movq 0x20(%r14), %rdi
leaq 0x937(%rip), %rsi       # 0x3076
callq 0x21c0
movl %eax, -0x34(%rbp)
movq 0x8(%r14), %rbx
testq %rbx, %rbx
je 0x27b2
movq %rbx, %rdi
callq 0x20c0
movq 0x2879(%rip), %rdi      # 0x4fd8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2190
jmp 0x27d0
movq 0x2865(%rip), %rbx      # 0x4fd8
leaq 0x896(%rip), %rsi       # 0x3010
movl $0x7, %edx
movq %rbx, %rdi
callq 0x2190
movq (%r14), %r14
testq %r14, %r14
je 0x2924
movq %r14, %rdi
callq 0x20c0
movq 0x2836(%rip), %rdi      # 0x4fd8
movq %r14, %rsi
movq %rax, %rdx
callq 0x2190
jmp 0x293b
movq 0x281f(%rip), %rax      # 0x4fd8
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rdi
movl 0x20(%rax,%rcx), %esi
orl $0x1, %esi
callq 0x21e0
movq 0x2801(%rip), %rdi      # 0x4fd8
leaq 0x837(%rip), %rsi       # 0x3015
movl $0x2, %edx
callq 0x2190
movq 0x10(%r14), %rdi
leaq -0x78(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x246c
testl %eax, %eax
je 0x2996
movq 0x18(%r14), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x246c
testl %eax, %eax
je 0x2996
movq -0x78(%rbp), %r15
movq -0x68(%rbp), %r13
movq %r15, %rdi
movl %r13d, %esi
callq 0x29f3
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %edi
callq 0x2a6d
cltq
imulq %r13, %rax
leaq 0x7(%rax), %rbx
testq %rax, %rax
cmovnsq %rax, %rbx
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq %rdi, -0x50(%rbp)
movq %rsi, -0x40(%rbp)
callq 0x29f3
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x2a6d
cltq
imulq -0x40(%rbp), %rax
leaq 0x7(%rax), %r12
testq %rax, %rax
cmovnsq %rax, %r12
cmpl $0x0, -0x34(%rbp)
je 0x28c4
movq 0x20(%r14), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x246c
testl %eax, %eax
je 0x2996
cmpq -0x58(%rbp), %r13
jne 0x295a
testq %r13, %r13
jle 0x28c4
movq -0x48(%rbp), %rax
xorl %ecx, %ecx
movb (%r15,%rcx), %dl
cmpb (%rax,%rcx), %dl
jne 0x295a
incq %rcx
cmpq %rcx, %r13
jne 0x28af
sarq $0x3, %rbx
movq -0x40(%rbp), %r8
cvtsi2sd %r8, %xmm0
sarq $0x3, %r12
cvtsi2sd %r13, %xmm1
divsd %xmm1, %xmm0
movsd 0x722(%rip), %xmm2      # 0x3008
mulsd %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r12, %xmm1
mulsd %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rbx, %xmm2
divsd %xmm2, %xmm1
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
leaq 0x77d(%rip), %rdi       # 0x308c
movq %r13, %rsi
movq %rbx, %rdx
movl -0x30(%rbp), %ecx
movq %r12, %r9
movb $0x2, %al
callq 0x2050
jmp 0x2972
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x21e0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x2691(%rip), %rdi      # 0x4fd8
leaq 0x6ca(%rip), %rsi       # 0x3018
movl $0x5d, %edx
callq 0x2190
jmp 0x29a7
movq 0x2677(%rip), %rdi      # 0x4fd8
leaq 0x75b(%rip), %rsi       # 0x30c3
movl $0x36, %edx
callq 0x2190
movq %r15, %rdi
callq 0x2120
movq -0x50(%rbp), %rdi
callq 0x2120
xorl %ebx, %ebx
cmpl $0x0, -0x34(%rbp)
je 0x29a7
movq -0x48(%rbp), %rdi
callq 0x2120
jmp 0x29a7
leaq 0x6df(%rip), %rdi       # 0x307c
callq 0x2100
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x29bf
callq 0x2120
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x29eb
callq 0x2120
movq %rbx, %rdi
callq 0x2200