// File: akw0088[P]openssl/huffman.c
// Function: huffman_compress
unsigned long huffman_compress(const unsigned char *in,unsigned long inlen,unsigned char *out,unsigned long outlen,void *huffheap)
{
	struct _huffman_encode_table *et,*eti;
	struct _huffman_node *t,*n;
	struct _huffman_node *pair[2];
	unsigned char *heapptr = (unsigned char *)huffheap;
	unsigned long i,code,byte_index,outbitctr;
	unsigned int bits,b;
	double *counts,lowest_prob,total_symbols;

	counts = (double *)heapptr;
	heapptr += (sizeof(double) * 257);
	for(i = 0; i < 256; ++i)
		counts[i] = 0.0;
	counts[256] = 1.0; /* one stop code at end */
	for(i = 0; i < inlen; ++i)
		counts[(unsigned long)in[i]] += 1.0;

	t = (struct _huffman_node *)0;
	total_symbols = (double)(inlen + 1);
	for(i=0;i<=256;++i)
	{
		if (counts[i] > 0.0)
		{
			n = (struct _huffman_node *)heapptr;
			heapptr += sizeof(struct _huffman_node);
			if (t)
				t->qprev = n;
			n->qprev = (struct _huffman_node *)0;
			n->qnext = t;
			n->lr[0] = (struct _huffman_node *)0;
			n->lr[1] = (struct _huffman_node *)0;
			n->prob = counts[i] / total_symbols;
			n->c = (unsigned int)i;
			t = n;
		}
	}

	while (t->qnext)
	{
		for(i=0;i<2;++i)
		{
			lowest_prob = 1.0;
			pair[i] = (struct _huffman_node *)0;
			n = t;
			while (n)
			{
				if (n->prob <= lowest_prob)
				{
					lowest_prob = n->prob;
					pair[i] = n;
				}
				n = n->qnext;
			}
			if (pair[i]->qprev)
				pair[i]->qprev->qnext = pair[i]->qnext;
			else
				t = pair[i]->qnext;

			if (pair[i]->qnext)
				pair[i]->qnext->qprev = pair[i]->qprev;
		}
		n = (struct _huffman_node *)heapptr;
		heapptr += sizeof(struct _huffman_node);
		n->lr[0] = pair[0];
		n->lr[1] = pair[1];
		n->prob = pair[0]->prob + pair[1]->prob;
		if (t)
			t->qprev = n;
		n->qprev = (struct _huffman_node *)0;
		n->qnext = t;
		t = n;
	}

	et = (struct _huffman_encode_table *)heapptr;
	heapptr += (sizeof(struct _huffman_encode_table) * 257);
	outbitctr = 0;
	_huffman_write_tree_and_make_encode_table(out,&outbitctr,outlen,et,0,0,t);

	for(i=0;i<inlen;++i)
	{
		eti = &(et[(unsigned long)in[i]]);
		code = eti->code;
		bits = eti->bits;
		for(b=0;b<bits;++b)
		{
			byte_index = outbitctr++ >> 3;
//			if (byte_index >= outlen) // allow file to get larger
//				return 0; // allow file to get larger
			out[byte_index] = (out[byte_index] << 1) | (unsigned char)(code & 1);
			code >>= 1;
		}
	}
	code = et[256].code;
	bits = et[256].bits;
	for(b=0;b<bits;++b)
	{
		byte_index = outbitctr++ >> 3;
//		if (byte_index >= outlen) return 0; // allow file to get larger
		out[byte_index] = (out[byte_index] << 1) | (unsigned char)(code & 1);
		code >>= 1;
	}

//	if (outbitctr > (outlen << 3))
//		return 0; // allow file to get larger
//	else
	if ((outbitctr & 7))
	{
		out[i = (outbitctr >> 3)] <<= 8 - (outbitctr & 7);
		return (i + 1);
	} else
		return (outbitctr >> 3);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x68(%rbp), %rax
addq $0x808, %rax            # imm = 0x808
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
cmpq $0x100, -0x70(%rbp)     # imm = 0x100
jae 0x15c3
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1598
movq -0x98(%rbp), %rax
movsd 0xb36(%rip), %xmm0      # 0x2108
movsd %xmm0, 0x800(%rax)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x161f
movq -0x98(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x70(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
movsd 0xb01(%rip), %xmm0      # 0x2108
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x15e2
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rax
incq %rax
movq %rax, %xmm0
movaps 0xad6(%rip), %xmm1      # 0x2110
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xada(%rip), %xmm1      # 0x2120
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movq $0x0, -0x70(%rbp)
cmpq $0x100, -0x70(%rbp)     # imm = 0x100
ja 0x1732
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x171f
movq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x68(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x16b7
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x98(%rbp), %rax
movq -0x70(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd -0xa8(%rbp), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x20(%rax)
movq -0x70(%rbp), %rax
movl %eax, %eax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x1721
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1665
jmp 0x1734
movq -0x48(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x18c5
movq $0x0, -0x70(%rbp)
cmpq $0x2, -0x70(%rbp)
jae 0x1847
movsd 0x9aa(%rip), %xmm0      # 0x2108
movsd %xmm0, -0xa0(%rbp)
movq -0x70(%rbp), %rax
movq $0x0, -0x60(%rbp,%rax,8)
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x17c5
movq -0x50(%rbp), %rax
movsd 0x20(%rax), %xmm1
movsd -0xa0(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x17b7
movq -0x50(%rbp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0xa0(%rbp)
movq -0x50(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x60(%rbp,%rax,8)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x177b
movq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
cmpq $0x0, 0x10(%rax)
je 0x17f5
movq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
movq 0x18(%rax), %rcx
movq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
movq 0x10(%rax), %rax
movq %rcx, 0x18(%rax)
jmp 0x1806
movq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
cmpq $0x0, 0x18(%rax)
je 0x1834
movq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
movq 0x10(%rax), %rcx
movq -0x70(%rbp), %rax
movq -0x60(%rbp,%rax,8), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x10(%rax)
jmp 0x1836
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x174b
movq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x68(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x58(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movsd 0x20(%rax), %xmm0
movq -0x58(%rbp), %rax
addsd 0x20(%rax), %xmm0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x20(%rax)
cmpq $0x0, -0x48(%rbp)
je 0x18a0
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x1734
movq -0x68(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x68(%rbp), %rax
addq $0x1010, %rax           # imm = 0x1010
movq %rax, -0x68(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
leaq -0x88(%rbp), %rsi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rax, (%rsp)
callq 0x1b00
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x19e2
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x70(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0x19cf
movq -0x88(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x88(%rbp)
shrq $0x3, %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movzbl (%rax,%rcx), %eax
shll %eax
movq -0x78(%rbp), %rcx
andq $0x1, %rcx
movzbl %cl, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x78(%rbp), %rax
shrq %rax
movq %rax, -0x78(%rbp)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x1960
jmp 0x19d1
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1914
movq -0x38(%rbp), %rax
movq 0x1000(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movq 0x1008(%rax), %rax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0x1a7b
movq -0x88(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x88(%rbp)
shrq $0x3, %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movzbl (%rax,%rcx), %eax
shll %eax
movq -0x78(%rbp), %rcx
andq $0x1, %rcx
movzbl %cl, %ecx
orl %ecx, %eax
movb %al, %dl
movq -0x20(%rbp), %rax
movq -0x80(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x78(%rbp), %rax
shrq %rax
movq %rax, -0x78(%rbp)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x1a0c
movq -0x88(%rbp), %rax
andq $0x7, %rax
cmpq $0x0, %rax
je 0x1ad7
movq -0x88(%rbp), %rax
andq $0x7, %rax
movl $0x8, %ecx
subq %rax, %rcx
movq -0x20(%rbp), %rax
movq -0x88(%rbp), %rdx
shrq $0x3, %rdx
movq %rdx, -0xb0(%rbp)
movq %rdx, -0x70(%rbp)
movzbl (%rax,%rdx), %edx
shll %cl, %edx
movq -0xb0(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x1ae6
movq -0x88(%rbp), %rax
shrq $0x3, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)