// File: /workspace/github_cpp_makefile_star2_O3/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 %r8, %r13
movq %rcx, -0x38(%rbp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl $0x800, %edx            # imm = 0x800
movq %r8, %rdi
xorl %esi, %esi
callq 0x1090
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x800(%r13)
testq %r14, %r14
je 0x14e7
xorl %eax, %eax
movsd 0xc38(%rip), %xmm0      # 0x2100
movzbl (%r15,%rax), %ecx
movsd (%r13,%rcx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%r13,%rcx,8)
incq %rax
cmpq %rax, %r14
jne 0x14c8
leaq 0x1(%r14), %rax
movq %rax, %xmm1
punpckldq 0xc18(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xc20(%rip), %xmm1      # 0x2120
leaq 0x808(%r13), %rdx
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
xorl %esi, %esi
movsd (%r13,%rsi,8), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x155e
testq %rcx, %rcx
je 0x1535
movq %rdx, 0x10(%rcx)
leaq 0x30(%rdx), %rax
movq $0x0, 0x10(%rdx)
movq %rcx, 0x18(%rdx)
divsd %xmm0, %xmm3
movupd %xmm2, (%rdx)
movsd %xmm3, 0x20(%rdx)
movq %rsi, 0x28(%rdx)
movq %rdx, %rcx
movq %rax, %rdx
jmp 0x1561
movq %rdx, %rax
incq %rsi
cmpq $0x101, %rsi            # imm = 0x101
jne 0x151f
cmpq $0x0, 0x18(%rcx)
je 0x163f
leaq -0x30(%rbp), %rdx
movsd 0xb7c(%rip), %xmm0      # 0x2100
leaq -0x48(%rbp), %rsi
movb $0x1, %dil
movq %rdx, %r9
movq %rcx, %r8
movq $0x0, (%r9)
xorl %ecx, %ecx
movapd %xmm0, %xmm1
movq %r8, %r10
movsd 0x20(%r10), %xmm2
ucomisd %xmm2, %xmm1
jb 0x15b7
movq %r10, (%r9)
movq %r10, %rcx
movapd %xmm2, %xmm1
movq 0x18(%r10), %r10
testq %r10, %r10
jne 0x15a1
movq 0x10(%rcx), %r9
movq 0x18(%rcx), %r10
movq %r10, %rcx
testq %r9, %r9
je 0x15d7
movq %r10, 0x18(%r9)
movq %r8, %rcx
testq %r10, %r10
je 0x15e0
movq %r9, 0x10(%r10)
movq %rsi, %r9
testb $0x1, %dil
movl $0x0, %edi
jne 0x158e
leaq 0x30(%rax), %r13
movq -0x30(%rbp), %rdi
movq %rdi, (%rax)
movq -0x48(%rbp), %r8
movq %r8, 0x8(%rax)
movsd 0x20(%rdi), %xmm1
addsd 0x20(%r8), %xmm1
movsd %xmm1, 0x20(%rax)
testq %rcx, %rcx
je 0x1631
movq %rax, 0x10(%rcx)
movq $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
movq %rax, %rcx
movq %r13, %rax
jmp 0x1588
movq $0x0, 0x10(%rax)
movq %rcx, 0x18(%rax)
jmp 0x1645
movq %rax, %r13
movq %rcx, %rax
leaq -0x40(%rbp), %r12
movq $0x0, (%r12)
movq %rax, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq -0x38(%rbp), %rdx
movq %r13, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x173a
movq (%r12), %rdx
testq %r14, %r14
je 0x16cc
xorl %ecx, %ecx
movzbl (%r15,%rcx), %eax
shll $0x4, %eax
movl 0x8(%r13,%rax), %esi
testl %esi, %esi
je 0x16b8
addq %r13, %rax
movq (%rax), %rdi
leaq 0x1(%rdx), %rax
shrq $0x3, %rdx
movb (%rbx,%rdx), %r8b
addb %r8b, %r8b
movl %edi, %r9d
andb $0x1, %r9b
orb %r8b, %r9b
movb %r9b, (%rbx,%rdx)
shrq %rdi
movq %rax, %rdx
decl %esi
jne 0x168f
jmp 0x16bb
movq %rdx, %rax
movq %rax, -0x40(%rbp)
incq %rcx
movq %rax, %rdx
cmpq %r14, %rcx
jne 0x1678
jmp 0x16cf
movq %rdx, %rax
movl 0x1008(%r13), %ecx
testl %ecx, %ecx
je 0x170a
movq 0x1000(%r13), %rsi
leaq 0x1(%rax), %rdx
shrq $0x3, %rax
movb (%rbx,%rax), %dil
addb %dil, %dil
movl %esi, %r8d
andb $0x1, %r8b
orb %dil, %r8b
movb %r8b, (%rbx,%rax)
shrq %rsi
movq %rdx, %rax
decl %ecx
jne 0x16e1
jmp 0x170d
movq %rax, %rdx
movq %rdx, %rax
shrq $0x3, %rax
andq $0x7, %rdx
je 0x172b
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