// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x1496
andq $0x0, (%r8,%rax,8)
incq %rax
jmp 0x1484
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x800(%r8)
xorl %eax, %eax
movsd 0xc4f(%rip), %xmm0      # 0x2100
cmpq %rax, %r14
je 0x14d0
movzbl (%r15,%rax), %edx
movsd (%r8,%rdx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%r8,%rdx,8)
incq %rax
jmp 0x14b1
leaq 0x1(%r14), %rax
movq %rax, %xmm2
punpckldq 0xc2f(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0xc37(%rip), %xmm2      # 0x2120
leaq 0x808(%r8), %rdx
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
xorl %eax, %eax
xorpd %xmm2, %xmm2
xorps %xmm3, %xmm3
xorl %edi, %edi
cmpq $0x101, %rax            # imm = 0x101
je 0x1549
movsd (%r8,%rax,8), %xmm4
ucomisd %xmm2, %xmm4
jbe 0x1544
testq %rdi, %rdi
je 0x1524
movq %rdx, 0x10(%rdi)
andq $0x0, 0x10(%rdx)
movq %rdi, 0x18(%rdx)
divsd %xmm1, %xmm4
movups %xmm3, (%rdx)
movsd %xmm4, 0x20(%rdx)
movq %rax, 0x28(%rdx)
movq %rdx, %rdi
addq $0x30, %rdx
incq %rax
jmp 0x1507
movq 0x18(%rdi), %rax
movq %rdx, %r12
testq %rax, %rax
je 0x1600
xorl %edx, %edx
movq %rdi, %rax
cmpq $0x2, %rdx
je 0x15bd
andq $0x0, -0x40(%rbp,%rdx,8)
xorl %esi, %esi
movq %rax, %rdi
movapd %xmm0, %xmm1
testq %rdi, %rdi
je 0x1595
movsd 0x20(%rdi), %xmm2
ucomisd %xmm2, %xmm1
jb 0x158f
movq %rdi, -0x40(%rbp,%rdx,8)
movq %rdi, %rsi
movapd %xmm2, %xmm1
movq 0x18(%rdi), %rdi
jmp 0x1573
movq 0x10(%rsi), %rdi
movq 0x18(%rsi), %r8
movq %r8, %rsi
testq %rdi, %rdi
je 0x15ac
movq %r8, 0x18(%rdi)
movq %rax, %rsi
testq %r8, %r8
je 0x15b5
movq %rdi, 0x10(%r8)
incq %rdx
movq %rsi, %rax
jmp 0x155e
movq -0x40(%rbp), %rdx
movq %rdx, (%r12)
movq -0x38(%rbp), %rsi
movq %rsi, 0x8(%r12)
movsd 0x20(%rdx), %xmm1
addsd 0x20(%rsi), %xmm1
movsd %xmm1, 0x20(%r12)
testq %rax, %rax
je 0x15e8
movq %r12, 0x10(%rax)
andq $0x0, 0x10(%r12)
leaq 0x30(%r12), %rdx
movq %rax, 0x18(%r12)
movq %r12, %rdi
jmp 0x154d
leaq -0x30(%rbp), %rsi
andq $0x0, (%rsi)
movq %rdi, (%rsp)
xorl %r13d, %r13d
movq %rbx, %rdi
movq %rcx, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x16da
leaq -0x30(%rbp), %rax
movq (%rax), %rdx
cmpq %r14, %r13
je 0x1673
movzbl (%r15,%r13), %ecx
shll $0x4, %ecx
movq (%r12,%rcx), %rax
movl 0x8(%r12,%rcx), %ecx
subl $0x1, %ecx
jb 0x166a
leaq 0x1(%rdx), %rsi
shrq $0x3, %rdx
movb (%rbx,%rdx), %dil
addb %dil, %dil
movl %eax, %r8d
andb $0x1, %r8b
orb %dil, %r8b
movb %r8b, (%rbx,%rdx)
shrq %rax
movq %rsi, %rdx
jmp 0x1640
movq %rdx, -0x30(%rbp)
incq %r13
jmp 0x162a
movq 0x1000(%r12), %rax
movl 0x1008(%r12), %ecx
subl $0x1, %ecx
jb 0x16ad
leaq 0x1(%rdx), %rsi
shrq $0x3, %rdx
movb (%rbx,%rdx), %dil
addb %dil, %dil
movl %eax, %r8d
andb $0x1, %r8b
orb %dil, %r8b
movb %r8b, (%rbx,%rdx)
shrq %rax
movq %rsi, %rdx
jmp 0x1683
movq %rdx, %rax
shrq $0x3, %rax
andq $0x7, %rdx
je 0x16cb
movzbl (%rbx,%rax), %esi
movb $0x8, %cl
subb %dl, %cl
shll %cl, %esi
movb %sil, (%rbx,%rax)
incq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq