// File: /workspace/github_c_makefile_star2_O3/hoobaa[P]ficl/lzuncompress.c
// Function: ficlLzUncompress
int ficlLzUncompress(const unsigned char *compressed, unsigned char **uncompressed_p, size_t *uncompressedSize_p)
	{
	unsigned char *window;
	unsigned char *buffer;
	unsigned char *uncompressed;
	unsigned char *initialWindow;

	int bitstreamLength;
	int inputPosition;
	int uncompressedSize;

	*uncompressed_p = NULL;

	inputPosition = 0;
	bitstreamLength = ficlLzDecodeHeaderField(compressed, &inputPosition);
	uncompressedSize = ficlLzDecodeHeaderField(compressed, &inputPosition);

	inputPosition <<= 3; /* same as * 8 */

	bitstreamLength += inputPosition;

	uncompressed = (unsigned char *)calloc(uncompressedSize + 1, 1);
	if (uncompressed == NULL)
		return -1;
	window = buffer = uncompressed;
	initialWindow = buffer + FICL_LZ_WINDOW_SIZE;

	while (inputPosition != bitstreamLength)
		{
		int length;
		int token = ficlBitGet(compressed, inputPosition);
		inputPosition++;

		if (token)
			{
			/* phrase token */
			int offset = 0;
			ficlBitGetString((unsigned char *)&offset, compressed, inputPosition, FICL_LZ_PHRASE_BITS - (1 + FICL_LZ_NEXT_BITS), sizeof(offset) * 8);
			offset = ficlNetworkUnsigned32(offset);
			inputPosition += FICL_LZ_PHRASE_BITS - (1 + FICL_LZ_NEXT_BITS);

			length = (offset & ((1 << FICL_LZ_LENGTH_BITS) - 1)) + FICL_LZ_MINIMUM_USEFUL_MATCH;
			offset >>= FICL_LZ_LENGTH_BITS;

			memmove(buffer, window + offset, length);
			buffer += length;
			length++;
			}
		else
			length = 1;

		/* symbol token */
		*buffer = 0;
		ficlBitGetString(buffer++, compressed, inputPosition, FICL_LZ_NEXT_BITS, sizeof(*buffer) * 8);
		inputPosition += FICL_LZ_NEXT_BITS;
		if (buffer > initialWindow)
			window = buffer - FICL_LZ_WINDOW_SIZE;
		}

	*uncompressed_p = uncompressed;
	*uncompressedSize_p = uncompressedSize;

	return 0;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbp
movq $0x0, (%rsi)
leaq 0x44(%rsp), %rbx
movl $0x0, (%rbx)
movq %rbx, %rsi
callq 0xb0d4
movl %eax, %r14d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xb0d4
movl (%rbx), %ebx
movq %rax, 0x30(%rsp)
incl %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3190
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0xb2be
movq %r12, 0x20(%rsp)
movq %r15, 0x28(%rsp)
testl %r14d, %r14d
je 0xb2a0
movl %ebx, %r15d
shll $0x3, %r15d
leal (%r14,%rbx,8), %eax
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rbx
leaq -0x1000(%rbx), %rax
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movq %rbx, %r13
movslq %r15d, %r14
movq %rbp, %rdi
movq %r14, %rsi
callq 0xdd90
incl %r14d
testl %eax, %eax
je 0xb246
movl $0x0, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
movq %rbp, %rsi
movl %r14d, %edx
movl $0x11, %ecx
movl $0x20, %r8d
callq 0xddd0
movl 0xc(%rsp), %edi
callq 0xde31
addl $0x12, %r15d
movl %eax, %r14d
andl $0x1f, %r14d
leaq 0x2(%r14), %rdx
sarl $0x5, %eax
movslq %eax, %rsi
addq %rbx, %rsi
movq %r13, %rdi
callq 0x3290
addq %r14, %r12
addq $0x2, %r12
movl %r15d, %r14d
movq 0x10(%rsp), %r13
leaq (%r12,%r13), %rdi
movb $0x0, (%r13,%r12)
leaq 0x1(%r12), %r15
movq %rbp, %rsi
movl %r14d, %edx
movl $0x8, %ecx
movl $0x8, %r8d
callq 0xddd0
addl $0x8, %r14d
cmpq $0x1000, %r12           # imm = 0x1000
movq 0x38(%rsp), %rax
leaq 0x1(%rax,%r12), %rax
cmovgeq %rax, %rbx
addq %r12, %r13
incq %r13
movq %r15, %r12
movl %r14d, %r15d
cmpl 0x1c(%rsp), %r14d
jne 0xb1db
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movslq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
jmp 0xb2c3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop