// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x28, %rsp
movl $0x0, 0x20(%rsp)
cmpl $0x2000, 0x20(%rsp)     # imm = 0x2000
jae 0x1bf4
movl 0x20(%rsp), %eax
movl %eax, %ecx
leaq 0x450d(%rip), %rax      # 0x60f0
movb $0x0, (%rax,%rcx)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x1bcc
movl $0x0, 0x64f2(%rip)      # 0x80f0
movl $0x0, 0x1c(%rsp)
callq 0x1a40
movl %eax, 0x18(%rsp)
cmpl $-0x1, 0x18(%rsp)
jne 0x1c23
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x18(%rsp), %eax
movb %al, %dl
movl 0x64c1(%rip), %eax      # 0x80f0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x64b6(%rip)      # 0x80f0
movl %eax, %eax
movl %eax, %ecx
leaq 0x44ab(%rip), %rax      # 0x60f0
movb %dl, (%rax,%rcx)
callq 0x1a40
movl %eax, 0x18(%rsp)
cmpl $-0x1, 0x18(%rsp)
jne 0x1c65
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x18(%rsp), %eax
movb %al, 0x6485(%rip)       # 0x80f4
movl $0x8, 0x647f(%rip)      # 0x80f8
movl $0x0, 0xc(%rsp)
movl $0x1, %edi
callq 0x1f50
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
jne 0x1ca3
movl $0x3, 0x24(%rsp)
jmp 0x1f39
cmpl $0x0, 0x14(%rsp)
je 0x1cf6
callq 0x1a40
movl %eax, 0x18(%rsp)
cmpl $-0x1, 0x18(%rsp)
jne 0x1cc7
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x18(%rsp), %eax
movb %al, %dl
movl 0x641d(%rip), %eax      # 0x80f0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x6412(%rip)      # 0x80f0
andl $0x1fff, %eax           # imm = 0x1FFF
movl %eax, %eax
movl %eax, %ecx
leaq 0x4402(%rip), %rax      # 0x60f0
movb %dl, (%rax,%rcx)
jmp 0x1eb5
movl $0x2, %edi
callq 0x1f50
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
jne 0x1d18
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x14(%rsp), %eax
movq %rax, (%rsp)
subq $0x3, %rax
ja 0x1ea6
movq (%rsp), %rax
leaq 0x26ff(%rip), %rcx      # 0x4434
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x3, %edi
callq 0x1f50
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
jne 0x1d60
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x14(%rsp), %edi
orl $0xfffffff8, %edi       # imm = 0xFFFFFFF8
movl $0x1, %esi
callq 0x2020
jmp 0x1eb3
callq 0x1a40
movl %eax, 0x18(%rsp)
cmpl $-0x1, 0x18(%rsp)
jne 0x1d96
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x18(%rsp), %edi
orl $0xffffff00, %edi       # imm = 0xFFFFFF00
movl $0x2, %esi
callq 0x2020
jmp 0x1eb3
callq 0x2090
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jne 0x1dcc
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x10(%rsp), %edi
movl $0x3, %esi
callq 0x2020
jmp 0x1eb3
movl $0x0, 0x20(%rsp)
movl $0x1, %edi
callq 0x1f50
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
jne 0x1e09
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
cmpl $0x0, 0x14(%rsp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1de7
cmpl $0x9, 0x20(%rsp)
jne 0x1e33
movl $0x1, 0xc(%rsp)
jmp 0x1ea4
cmpl $0x7, 0x20(%rsp)
ja 0x1e95
movl 0x20(%rsp), %edi
callq 0x1f50
movl %eax, 0x14(%rsp)
cmpl $-0x1, 0x14(%rsp)
jne 0x1e5b
movl $0x3, 0x24(%rsp)
jmp 0x1f39
callq 0x2090
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x10(%rsp)
jne 0x1e78
movl $0x3, 0x24(%rsp)
jmp 0x1f39
movl 0x10(%rsp), %edi
movl 0x20(%rsp), %ecx
movl $0x1, %esi
shll %cl, %esi
addl $0x2, %esi
addl 0x14(%rsp), %esi
callq 0x2020
jmp 0x1ea2
movl $0x2, 0x24(%rsp)
jmp 0x1f39
jmp 0x1ea4
jmp 0x1eb3
movl $0x2, 0x24(%rsp)
jmp 0x1f39
jmp 0x1eb5
movl 0x6235(%rip), %eax      # 0x80f0
subl 0x1c(%rsp), %eax
andl $0x1fff, %eax           # imm = 0x1FFF
cmpl $0x1eff, %eax           # imm = 0x1EFF
ja 0x1ed2
cmpl $0x0, 0xc(%rsp)
je 0x1f0a
movl 0x1c(%rsp), %edi
andl $0x1fff, %edi           # imm = 0x1FFF
movl 0x620e(%rip), %esi      # 0x80f0
subl 0x1c(%rsp), %esi
andl $0x1fff, %esi           # imm = 0x1FFF
callq 0x1a90
cmpl $0x0, %eax
jne 0x1f00
movl $0x5, 0x24(%rsp)
jmp 0x1f39
movl 0x61ea(%rip), %eax      # 0x80f0
movl %eax, 0x1c(%rsp)
jmp 0x1f0c
cmpl $0x0, 0xc(%rsp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1c81
callq 0x1a40
movl %eax, %edx
movl $0x4, %eax
movl $0x1, %ecx
cmpl $-0x1, %edx
cmovel %ecx, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)