// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rcx, -0x48(%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(%r12)
testq %r14, %r14
je 0x14e2
xorl %eax, %eax
movsd 0xc3b(%rip), %xmm0      # 0x2100
movzbl (%r15,%rax), %ecx
movsd (%r12,%rcx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%r12,%rcx,8)
incq %rax
cmpq %rax, %r14
jne 0x14c5
leaq 0x1(%r14), %rax
movq %rax, %xmm1
punpckldq 0xc1d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xc25(%rip), %xmm1      # 0x2120
leaq 0x808(%r12), %rcx
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
xorl %eax, %eax
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
xorl %edx, %edx
movsd (%r12,%rdx,8), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x1559
testq %rax, %rax
je 0x1530
movq %rcx, 0x10(%rax)
leaq 0x30(%rcx), %r13
movq $0x0, 0x10(%rcx)
movq %rax, 0x18(%rcx)
divsd %xmm0, %xmm3
movupd %xmm2, (%rcx)
movsd %xmm3, 0x20(%rcx)
movq %rdx, 0x28(%rcx)
movq %rcx, %rax
movq %r13, %rcx
jmp 0x155c
movq %rcx, %r13
incq %rdx
cmpq $0x101, %rdx            # imm = 0x101
jne 0x151b
cmpq $0x0, 0x18(%rax)
je 0x1638
movsd 0xb85(%rip), %xmm0      # 0x2100
movq -0x48(%rbp), %rdx
movq %r13, %rcx
xorl %r9d, %r9d
movq $0x0, -0x40(%rbp,%r9,8)
testq %rax, %rax
je 0x15b7
movapd %xmm0, %xmm1
movq %rax, %rsi
movsd 0x20(%rsi), %xmm2
ucomisd %xmm2, %xmm1
jb 0x15ae
movq %rsi, -0x40(%rbp,%r9,8)
movapd %xmm2, %xmm1
movq 0x18(%rsi), %rsi
testq %rsi, %rsi
jne 0x159a
movq -0x40(%rbp,%r9,8), %rdi
movq 0x10(%rdi), %rsi
movq 0x18(%rdi), %r8
testq %rsi, %rsi
je 0x15cf
movq %r8, 0x18(%rsi)
jmp 0x15d2
movq %r8, %rax
movq 0x18(%rdi), %rdi
testq %rdi, %rdi
je 0x15df
movq %rsi, 0x10(%rdi)
leaq 0x1(%r9), %rsi
testq %r9, %r9
movq %rsi, %r9
je 0x1585
movq -0x40(%rbp), %rsi
movq %rsi, (%rcx)
movq -0x38(%rbp), %rsi
movq %rsi, 0x8(%rcx)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
movsd 0x20(%rdi), %xmm1
addsd 0x20(%rsi), %xmm1
movsd %xmm1, 0x20(%rcx)
testq %rax, %rax
je 0x161a
movq %rcx, 0x10(%rax)
leaq 0x30(%rcx), %r13
movq $0x0, 0x10(%rcx)
movq %rax, 0x18(%rcx)
testq %rax, %rax
movq %rcx, %rax
jne 0x157f
jmp 0x163f
movq %rax, %rcx
movq -0x48(%rbp), %rdx
leaq -0x30(%rbp), %rsi
movq $0x0, (%rsi)
movq %rcx, (%rsp)
movq %rbx, %rdi
movq %r13, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1724
testq %r14, %r14
je 0x16b3
movq -0x30(%rbp), %rcx
xorl %eax, %eax
movzbl (%r15,%rax), %esi
shll $0x4, %esi
movl 0x8(%r13,%rsi), %edx
testl %edx, %edx
je 0x16ab
addq %r13, %rsi
movq (%rsi), %rsi
movq %rcx, %rdi
incq %rcx
shrq $0x3, %rdi
movb (%rbx,%rdi), %r8b
addb %r8b, %r8b
movl %esi, %r9d
andb $0x1, %r9b
orb %r8b, %r9b
movb %r9b, (%rbx,%rdi)
shrq %rsi
decl %edx
jne 0x1681
movq %rcx, -0x30(%rbp)
incq %rax
cmpq %r14, %rax
jne 0x166a
movl 0x1008(%r13), %eax
testl %eax, %eax
je 0x16f3
movq -0x30(%rbp), %rdx
movq 0x1000(%r13), %rcx
movq %rdx, %rsi
incq %rdx
shrq $0x3, %rsi
movb (%rbx,%rsi), %dil
addb %dil, %dil
movl %ecx, %r8d
andb $0x1, %r8b
orb %dil, %r8b
movb %r8b, (%rbx,%rsi)
shrq %rcx
decl %eax
jne 0x16c9
movq %rdx, -0x30(%rbp)
movq -0x30(%rbp), %rdx
movq %rdx, %rax
shrq $0x3, %rax
andq $0x7, %rdx
je 0x1715
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