// File: /workspace/github_c_makefile_star2_O1/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 0x3463
movl $0x0, (%rbx)
xorl %edx, %edx
jmp 0x3546
movl %edx, %r12d
movq %rdi, %r15
leal (%r12,%r12), %ebp
testl %edx, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnsl %ebp, %eax
movslq %eax, %rdi
callq 0x2030
cmpl $0x11, 0x4(%r15)
jge 0x3577
movq %rax, 0x10(%rsp)
movl %ebp, 0x8(%rsp)
movl $0x0, (%rbx)
xorl %ebp, %ebp
testl %r12d, %r12d
jle 0x351c
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 0x42ce
movl 0xc(%rsp), %eax
subl %ebp, %eax
cltq
movq 0x78(%r15), %rcx
movl (%rcx,%rax,4), %ebp
cmpq $0xffffff, %rbp         # imm = 0xFFFFFF
je 0x3558
testl %ebp, %ebp
js 0x350d
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 0x3514
movl 0x70(%r15), %eax
addl %eax, %r14d
addl %eax, %r13d
cmpl %r12d, %r13d
jl 0x34a6
testl %ebp, %ebp
movq 0x10(%rsp), %rdx
jns 0x353e
movq 0x78(%r15), %rax
negl %ebp
movl (%rax,%rbp,4), %ebp
testl %ebp, %ebp
js 0x3529
movslq (%rbx), %rax
leal 0x1(%rax), %ecx
movl %ecx, (%rbx)
movb %bpl, (%rdx,%rax)
movl 0x8(%rsp), %eax
cmpl %eax, (%rbx)
jg 0x3596
movq %rdx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2c1d(%rip), %rdi      # 0x617c
leaq 0x2b10(%rip), %rsi      # 0x6076
leaq 0x2bd0(%rip), %rcx      # 0x613d
movl $0x21a, %edx            # imm = 0x21A
callq 0x20f0
leaq 0x2bb0(%rip), %rdi      # 0x612e
leaq 0x2af1(%rip), %rsi      # 0x6076
leaq 0x2bb1(%rip), %rcx      # 0x613d
movl $0x20d, %edx            # imm = 0x20D
callq 0x20f0
leaq 0x2bf2(%rip), %rdi      # 0x618f
leaq 0x2ad2(%rip), %rsi      # 0x6076
leaq 0x2b92(%rip), %rcx      # 0x613d
movl $0x22c, %edx            # imm = 0x22C
callq 0x20f0
nop