// File: /workspace/github_c_makefile_star2_O1/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 0x388e(%rip), %rbx      # 0x50f0
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1060
movl $0x0, 0x5875(%rip)      # 0x70f0
callq 0x1784
cmpl $-0x1, %eax
je 0x1ac9
movl 0x5861(%rip), %ecx      # 0x70f0
leal 0x1(%rcx), %edx
movl %edx, 0x5858(%rip)      # 0x70f0
movb %al, (%rcx,%rbx)
callq 0x1784
cmpl $-0x1, %eax
je 0x1ac9
movb %al, 0x5845(%rip)       # 0x70f4
movl $0x8, 0x583f(%rip)      # 0x70f8
xorl %ebp, %ebp
movl $0x1fff, %r13d          # imm = 0x1FFF
xorl %r12d, %r12d
movl $0x1, %edi
callq 0x1af1
testl %eax, %eax
je 0x1906
cmpl $-0x1, %eax
je 0x1ac9
callq 0x1784
cmpl $-0x1, %eax
je 0x1ac9
movl 0x5801(%rip), %ecx      # 0x70f0
leal 0x1(%rcx), %edx
movl %edx, 0x57f8(%rip)      # 0x70f0
andl $0x1fff, %ecx           # imm = 0x1FFF
movb %al, (%rcx,%rbx)
jmp 0x1a69
movl $0x2, %edi
callq 0x1af1
incl %eax
cmpl $0x4, %eax
ja 0x1ae9
leaq 0x1ad2(%rip), %rcx      # 0x33f4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x3, %r14d
movl $0x3, %edi
callq 0x1af1
cmpl $-0x1, %eax
je 0x1acf
movl 0x57a6(%rip), %ecx      # 0x70f0
orl $0x1ff8, %eax           # imm = 0x1FF8
addl %ecx, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
movb (%rax,%rbx), %al
movl %ecx, %edx
andl $0x1fff, %edx           # imm = 0x1FFF
movb %al, (%rdx,%rbx)
incl %ecx
jmp 0x1a63
callq 0x1b84
testl %eax, %eax
je 0x1ac9
movl $0x3, %edx
movl 0x576d(%rip), %ecx      # 0x70f0
leal (%rax,%rcx), %esi
andl %r13d, %esi
movb (%rsi,%rbx), %sil
movl %ecx, %edi
andl %r13d, %edi
movb %sil, (%rdi,%rbx)
incl %ecx
decl %edx
jne 0x1983
jmp 0x1a63
xorl %r14d, %r14d
movl $0x1, %edi
callq 0x1af1
cmpl $-0x1, %eax
je 0x1ac9
incl %r14d
testl %eax, %eax
je 0x19a4
movl $0x1, %r15d
cmpl $0x9, %r14d
je 0x1a6c
cmpl $0x7, %r14d
ja 0x1ae9
movl %r14d, %edi
callq 0x1af1
cmpl $-0x1, %eax
je 0x1ac9
movl %eax, %r15d
callq 0x1b84
testl %eax, %eax
je 0x1ac9
movl $0x1, %edx
movl %r14d, %ecx
shll %cl, %edx
addl %r15d, %edx
addl $0x2, %edx
je 0x1a69
movl 0x56df(%rip), %ecx      # 0x70f0
leal (%rax,%rcx), %esi
andl %r13d, %esi
movb (%rsi,%rbx), %sil
movl %ecx, %edi
andl %r13d, %edi
movb %sil, (%rdi,%rbx)
incl %ecx
decl %edx
jne 0x1a11
jmp 0x1a63
callq 0x1784
cmpl $-0x1, %eax
je 0x1ac9
orl $0x1f00, %eax           # imm = 0x1F00
movl $0x2, %edx
movl 0x56a6(%rip), %ecx      # 0x70f0
leal (%rax,%rcx), %esi
andl %r13d, %esi
movb (%rsi,%rbx), %sil
movl %ecx, %edi
andl %r13d, %edi
movb %sil, (%rdi,%rbx)
incl %ecx
decl %edx
jne 0x1a4a
movl %ecx, 0x5687(%rip)      # 0x70f0
movl %r12d, %r15d
movl 0x567e(%rip), %esi      # 0x70f0
subl %ebp, %esi
andl $0x1fff, %esi           # imm = 0x1FFF
cmpl $0x1f00, %esi           # imm = 0x1F00
setae %al
testl %r15d, %r15d
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x1aa7
andl $0x1fff, %ebp           # imm = 0x1FFF
movl %ebp, %edi
callq 0x17b2
testl %eax, %eax
je 0x1ae1
movl 0x5649(%rip), %ebp      # 0x70f0
movl %r15d, %r12d
testl %r15d, %r15d
je 0x18c4
callq 0x1784
xorl %ecx, %ecx
cmpl $-0x1, %eax
setne %cl
leal (%rcx,%rcx,2), %r14d
incl %r14d
jmp 0x1acf
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 0x1acf
movl $0x2, %r14d
jmp 0x1acf