// File: /workspace/github_c_makefile_star2_O2/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 0x30aa
andl $0x0, (%rbx)
xorl %eax, %eax
jmp 0x3191
movl %edx, %r12d
movq %rdi, %r15
leal (%r12,%r12), %ecx
testl %edx, %edx
pushq $-0x1
popq %rax
movl %ecx, 0x4(%rsp)
cmovnsl %ecx, %eax
movslq %eax, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
cmpl $0x11, 0x4(%r15)
jge 0x31bf
andl $0x0, (%rbx)
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %ebp, %ebp
movl %r12d, %eax
subl %r14d, %eax
jle 0x3158
movl 0x70(%r15), %edx
cmpl %edx, %eax
cmovll %eax, %edx
movslq %r14d, %rsi
addq 0x18(%rsp), %rsi
movq %r15, %rdi
leaq 0x14(%rsp), %rcx
leaq 0x24(%rsp), %r8
callq 0x3d50
movl 0x14(%rsp), %eax
subl %ebp, %eax
cltq
movq 0x78(%r15), %rcx
movl (%rcx,%rax,4), %ebp
cmpq $0xffffff, %rbp         # imm = 0xFFFFFF
je 0x31a0
testl %ebp, %ebp
js 0x314c
movslq (%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%rbx)
movq 0x8(%rsp), %rcx
movb %bpl, (%rcx,%rax)
movq 0x40(%r15), %rax
movl (%rax,%rbp,4), %eax
subl %r13d, %eax
xorl %r13d, %r13d
xorl %ebp, %ebp
jmp 0x3153
movl 0x70(%r15), %eax
addl %eax, %r13d
addl %eax, %r14d
jmp 0x30e3
testl %ebp, %ebp
js 0x3169
movl (%rbx), %esi
movl 0x4(%rsp), %edx
movq 0x8(%rsp), %rax
jmp 0x318d
movq 0x78(%r15), %rax
movl 0x4(%rsp), %edx
testl %ebp, %ebp
jns 0x317c
negl %ebp
movl (%rax,%rbp,4), %ebp
jmp 0x3171
movslq (%rbx), %rcx
leal 0x1(%rcx), %esi
movl %esi, (%rbx)
movq 0x8(%rsp), %rax
movb %bpl, (%rax,%rcx)
cmpl %edx, %esi
jg 0x31de
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f95(%rip), %rdi      # 0x513c
leaq 0x1e88(%rip), %rsi      # 0x5036
leaq 0x1f48(%rip), %rcx      # 0x50fd
movl $0x21a, %edx            # imm = 0x21A
callq 0x20e0
leaq 0x1f28(%rip), %rdi      # 0x50ee
leaq 0x1e69(%rip), %rsi      # 0x5036
leaq 0x1f29(%rip), %rcx      # 0x50fd
movl $0x20d, %edx            # imm = 0x20D
callq 0x20e0
leaq 0x1f6a(%rip), %rdi      # 0x514f
leaq 0x1e4a(%rip), %rsi      # 0x5036
leaq 0x1f0a(%rip), %rcx      # 0x50fd
movl $0x22c, %edx            # imm = 0x22C
callq 0x20e0
nop