// File: /workspace/github_c_makefile_star2_O3/ijleesw[P]data-compression/src/tunstall/tunstall.hpp
// Function: Tunstall::compress(unsigned char*, int, int&)
unsigned char *Tunstall::compress(unsigned char *data, int input_size, int &output_size) {
	if(probabilities.size() == 1) {
		output_size = 0;
		return NULL;
	}

	unsigned char *output = new unsigned char[input_size*2]; //use entropy here!

	assert(wordsize <= 16);
	output_size = 0;
	int input_offset = 0;
	int word_offset = 0;
	int offset = 0;
	while(input_offset < input_size) {
		//read lookup_size symbols and compute code:
		int d = input_size - input_offset;
		if(d > lookup_size)
			d = lookup_size;
		int low, high;
		wordCode(data + input_offset, d, low, high);
		offset = offsets[-offset + low];
		assert(offset != 0xffffff);
		if(offset >= 0) { //ready to ouput a symbol
			output[output_size++] = offset;
			input_offset += lengths[offset] - word_offset;
			offset = 0;
			word_offset = 0;
		} else {
			word_offset += lookup_size;
			input_offset += lookup_size;
		}
	}
	//end of stream can be tricky:
	//we could have a partial read (need to encode half a word)
	if(offset < 0) {
		while(offset < 0)
			offset = offsets[-offset];
		output[output_size++] = offset;
	}
	assert(output_size <= input_size*2);
#ifdef DEBUG_ENTROPY
	cout << "Compressed to: E: " << ((float)output_size*8.0f)/input_size << " tot: " << output_size << endl;
#endif
	return output;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %rbx
movq %rsi, 0x18(%rsp)
movq 0x18(%rdi), %rax
subq 0x10(%rdi), %rax
cmpq $0x2, %rax
jne 0x33d5
movl $0x0, (%rbx)
xorl %eax, %eax
jmp 0x34c4
movl %edx, %r12d
movq %rdi, %r15
leal (%r12,%r12), %r14d
testl %edx, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnsl %r14d, %eax
movslq %eax, %rdi
callq 0x2030
cmpl $0x11, 0x4(%r15)
jge 0x34f2
movl $0x0, (%rbx)
testl %r12d, %r12d
jle 0x34a2
movq %rax, 0x10(%rsp)
movl %r14d, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %r12d, %edx
subl %r13d, %edx
movl 0x70(%r15), %eax
cmpl %eax, %edx
cmovgel %eax, %edx
movslq %r13d, %rsi
addq 0x18(%rsp), %rsi
movq %r15, %rdi
leaq 0xc(%rsp), %rcx
leaq 0x24(%rsp), %r8
callq 0x4242
movl 0xc(%rsp), %eax
subl %ebp, %eax
cltq
movq 0x78(%r15), %rcx
movl (%rcx,%rax,4), %ebp
cmpq $0xffffff, %rbp         # imm = 0xFFFFFF
je 0x34d3
testl %ebp, %ebp
js 0x3485
movslq (%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%rbx)
movq 0x10(%rsp), %rcx
movb %bpl, (%rcx,%rax)
movq 0x40(%r15), %rax
movl (%rax,%rbp,4), %eax
subl %r14d, %eax
xorl %r14d, %r14d
xorl %ebp, %ebp
jmp 0x348c
movl 0x70(%r15), %eax
addl %eax, %r14d
addl %eax, %r13d
cmpl %r12d, %r13d
jl 0x341e
testl %ebp, %ebp
movl 0x8(%rsp), %r14d
movq 0x10(%rsp), %rax
js 0x34a6
movl (%rbx), %edx
jmp 0x34bf
movq 0x78(%r15), %rcx
negl %ebp
movl (%rcx,%rbp,4), %ebp
testl %ebp, %ebp
js 0x34aa
movslq (%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, (%rbx)
movb %bpl, (%rax,%rcx)
cmpl %r14d, %edx
jg 0x3511
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2ca2(%rip), %rdi      # 0x617c
leaq 0x2b95(%rip), %rsi      # 0x6076
leaq 0x2c55(%rip), %rcx      # 0x613d
movl $0x21a, %edx            # imm = 0x21A
callq 0x20f0
leaq 0x2c35(%rip), %rdi      # 0x612e
leaq 0x2b76(%rip), %rsi      # 0x6076
leaq 0x2c36(%rip), %rcx      # 0x613d
movl $0x20d, %edx            # imm = 0x20D
callq 0x20f0
leaq 0x2c77(%rip), %rdi      # 0x618f
leaq 0x2b57(%rip), %rsi      # 0x6076
leaq 0x2c17(%rip), %rcx      # 0x613d
movl $0x22c, %edx            # imm = 0x22C
callq 0x20f0