// File: /workspace/github_c_makefile_star2_O3/salextpuru[P]speccy-toochain/src/megalz/MegaLZ_depacker.c
// Function: depacker
ULONG depacker(void)
{
	ULONG i;

	ULONG dbflush; // position from which to flush buffer

	ULONG byte;
	ULONG bits;

	LONG disp;

	ULONG finished;

	// init depack buffer with zeros
	for(i=0;i<DBSIZE;i++) dbuf[i]=0;


	dbpos=0;
	dbflush=0;

	// get first byte of packed file and write to output
	byte=get_byte();
#ifdef ERRORS
	if( byte==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif
	dbuf[dbpos++]=(UBYTE)byte;


	// second byte goes to bitstream
	byte=get_byte();
#ifdef ERRORS
	if( byte==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif
	bitstream=(UBYTE)byte;
	bitcount=8;


	// actual depacking loop!
	finished=0;
	do
	{
		// get 1st bit - either OUTBYTE (see MegaLZ_lz.h) or beginning of LZ code
		bits=get_bits(1);
#ifdef ERRORS
		if( bits==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif

		if( bits )
		{ // OUTBYTE
                        byte=get_byte();
#ifdef ERRORS
			if( byte==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif
			dbuf[(dbpos++)&DBMASK]=(UBYTE)byte;
		}
		else
		{ // LZ code
			bits=get_bits(2);
#ifdef ERRORS
			if( bits==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif

			switch( bits )
			{
			case 0: // 000
				bits=get_bits(3);
#ifdef ERRORS
				if( bits==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif
				repeat((LONG)(0xFFFFFFF8|bits),1);
				break;
			case 1: // 001
				byte=get_byte();
#ifdef ERRORS
				if( byte==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif
				repeat((LONG)(0xFFFFFF00|byte),2);
				break;
			case 2: // 010
				disp=get_bigdisp();
#ifdef ERRORS
				if( !disp ) return DEPACKER_NOEOF;
#endif
				repeat(disp,3);
				break;
			case 3: // 011
				// extract num of length bits
				i=0;
				do
				{
					bits=get_bits(1);
#ifdef ERRORS
					if( bits==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif
					i++;
				} while( !bits );

				// check for exit code
				if( i==9 )
				{
					finished=1;
				}
				else if( i<=7 )
				{
					// get length bits itself
					bits=get_bits(i);
#ifdef ERRORS
					if( bits==0xFFFFFFFF ) return DEPACKER_NOEOF;
#endif
                        	        disp=get_bigdisp();
#ifdef ERRORS
					if( !disp ) return DEPACKER_NOEOF;
#endif
					repeat(disp,2+(1<<i)+bits);
				}
#ifdef ERRORS
				else
				{
					return DEPACKER_ERRCODE;
				}
#endif
				break;
#ifdef ERRORS
			default:
				return DEPACKER_ERRCODE; // although this should NEVER happen!
#endif
			}
		}

		// check if we need flushing buffer
		if( (((dbpos-dbflush)&DBMASK)>(DBSIZE-257)) || finished )
		{
#ifdef ERRORS
			if( !put_buffer(dbflush&DBMASK,(dbpos-dbflush)&DBMASK) )
			{
				return DEPACKER_CANTWRITE;
			}
#else
			put_buffer(dbflush&DBMASK,(dbpos-dbflush)&DBMASK);
#endif
			dbflush=dbpos;
		}

	} while( !finished );


#ifdef ERRORS
	return (get_byte()==0xFFFFFFFF)?DEPACKER_OK:DEPACKER_TOOLONG;
#else
	return DEPACKER_OK:
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x3896(%rip), %rbx      # 0x50f0
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1060
movl $0x0, 0x587d(%rip)      # 0x70f0
callq 0x177c
cmpl $-0x1, %eax
je 0x1ac2
movl 0x5869(%rip), %ecx      # 0x70f0
leal 0x1(%rcx), %edx
movl %edx, 0x5860(%rip)      # 0x70f0
movb %al, (%rcx,%rbx)
callq 0x177c
cmpl $-0x1, %eax
je 0x1ac2
movb %al, 0x584d(%rip)       # 0x70f4
movl $0x8, 0x5847(%rip)      # 0x70f8
xorl %ebp, %ebp
movl $0x1fff, %r13d          # imm = 0x1FFF
xorl %r14d, %r14d
movl %r14d, %r12d
movl $0x1, %edi
callq 0x1af8
testl %eax, %eax
je 0x1904
cmpl $-0x1, %eax
je 0x1ac2
callq 0x177c
cmpl $-0x1, %eax
je 0x1ac2
movl 0x5806(%rip), %ecx      # 0x70f0
leal 0x1(%rcx), %esi
movl %esi, 0x57fd(%rip)      # 0x70f0
andl $0x1fff, %ecx           # imm = 0x1FFF
movb %al, (%rcx,%rbx)
xorl %r14d, %r14d
jmp 0x1a64
movl $0x2, %r14d
movl $0x2, %edi
callq 0x1af8
incl %eax
cmpl $0x4, %eax
ja 0x1ac8
leaq 0x1ac2(%rip), %rcx      # 0x33e8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x3, %r14d
movl $0x3, %edi
callq 0x1af8
cmpl $-0x1, %eax
je 0x1ac8
orl $0x1ff8, %eax           # imm = 0x1FF8
movl 0x579d(%rip), %esi      # 0x70f0
addl %esi, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
movb (%rax,%rbx), %al
movl %esi, %ecx
andl $0x1fff, %ecx           # imm = 0x1FFF
movb %al, (%rcx,%rbx)
incl %esi
jmp 0x1a5b
callq 0x1b99
testl %eax, %eax
je 0x1ac2
movl $0x3, %ecx
movl 0x5769(%rip), %esi      # 0x70f0
leal (%rax,%rsi), %edx
andl %r13d, %edx
movb (%rdx,%rbx), %dl
movl %esi, %edi
andl %r13d, %edi
movb %dl, (%rdi,%rbx)
incl %esi
decl %ecx
jne 0x1987
jmp 0x1a5b
xorl %r15d, %r15d
movl $0x1, %edi
callq 0x1af8
cmpl $-0x1, %eax
je 0x1ac2
incl %r15d
testl %eax, %eax
je 0x19a6
cmpl $0x9, %r15d
je 0x1aa9
cmpl $0x7, %r15d
ja 0x1ac8
movl %r15d, %edi
callq 0x1af8
cmpl $-0x1, %eax
je 0x1ac2
movl %eax, %r14d
callq 0x1b99
testl %eax, %eax
je 0x1ac2
movl $0x1, %edx
movl %r15d, %ecx
shll %cl, %edx
addl %r14d, %edx
movl 0x56e8(%rip), %esi      # 0x70f0
addl $0x2, %edx
je 0x1a61
leal (%rax,%rsi), %ecx
andl %r13d, %ecx
movb (%rcx,%rbx), %cl
movl %esi, %edi
andl %r13d, %edi
movb %cl, (%rdi,%rbx)
incl %esi
decl %edx
jne 0x1a0d
jmp 0x1a5b
callq 0x177c
cmpl $-0x1, %eax
je 0x1ac2
orl $0x1f00, %eax           # imm = 0x1F00
movl $0x2, %ecx
movl 0x56ac(%rip), %esi      # 0x70f0
leal (%rax,%rsi), %edx
andl %r13d, %edx
movb (%rdx,%rbx), %dl
movl %esi, %edi
andl %r13d, %edi
movb %dl, (%rdi,%rbx)
incl %esi
decl %ecx
jne 0x1a44
movl %esi, 0x568f(%rip)      # 0x70f0
movl %r12d, %r14d
subl %ebp, %esi
andl $0x1fff, %esi           # imm = 0x1FFF
testl %r14d, %r14d
setne %r15b
cmpl $0x1eff, %esi           # imm = 0x1EFF
ja 0x1a87
xorl %r12d, %r12d
testl %r14d, %r14d
je 0x18bf
andl $0x1fff, %ebp           # imm = 0x1FFF
movl %ebp, %edi
callq 0x17aa
testl %eax, %eax
je 0x1ada
movl 0x5652(%rip), %ebp      # 0x70f0
testb %r15b, %r15b
je 0x18bc
jmp 0x1ae2
movl 0x5641(%rip), %esi      # 0x70f0
subl %ebp, %esi
andl $0x1fff, %esi           # imm = 0x1FFF
movl $0x1, %r14d
movb $0x1, %r15b
jmp 0x1a87
movl $0x3, %r14d
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x5, %r14d
jmp 0x1ac8
callq 0x177c
xorl %ecx, %ecx
cmpl $-0x1, %eax
setne %cl
leal (%rcx,%rcx,2), %r14d
incl %r14d
jmp 0x1ac8