// File: /workspace/github_c_makefile_star2_O0/salextpuru[P]speccy-toochain/src/megalz/MegaLZ_lz.c
// Function: make_lz_info
ULONG make_lz_info(UBYTE curbyte,struct lzcode * lzcode,struct lzinfo * lzinfo)
{ // on the given lzcode element, returns total bit length of this code,
  // if given ptr to lzinfo, writes there precise info over lz code
  // if lzcode==NULL or lzcode->length==0 - treated as OUTBYTE type code

  // returns 0 for wrong lz length/displacement combination, does not fill lzinfo

  // curbyte needed only for OUTBYTE lz code, can be given any value otherwise

	ULONG type,length,add_length,len,bits;
	ULONG ptrn;


	// first determine bit length of resulting LZ code
	length=0;
	if( lzcode==NULL )
	{
		length=9; // OUTBYTE
		type=OUTBYTE;
	}
	else if( lzcode->disp==0 )
	{
		length=9;
		type=OUTBYTE;
	}
	else
	{
		if( lzcode->length==1 )
		{
			if( (lzcode->disp>=(-8))&&(lzcode->disp<=(-1)) )
			{
				length=6; // LEN1
				type=LEN1;
			}
		}
		else if( lzcode->length==2 )
		{
			if( (lzcode->disp>=(-256))&&(lzcode->disp<=(-1)) )
			{
				length=11; // LEN2
				type=LEN2;
			}
		}
		else if( lzcode->length==3 )
		{
			if( (lzcode->disp>=(-256))&&(lzcode->disp<=(-1)) )
			{
				length=12; // LEN3_SHORT
				type=LEN3_SHORT;
			}
			else if( (lzcode->disp>=(-4352))&&(lzcode->disp<=(-257)) )
			{
				length=16; // LEN3_LONG
				type=LEN3_LONG;
			}
		}
		else if( lzcode->length<=255 )
		{
			if( (lzcode->disp>=(-4352))&&(lzcode->disp<=(-1)) )
			{
				if( (lzcode->disp>=(-256)) )
				{
					add_length=12; // VARLEN_SHORT
					type=VARLEN_SHORT;
				}
				else
				{
					add_length=16; // VARLEN_LONG
					type=VARLEN_LONG;
				}

				// calc bits for size
				len=(lzcode->length - 2)>>1;

				bits=0;
				while( len )
				{
					bits++;
					len>>=1;
				}

				length=bits+bits+add_length; // final length
			}
		}
	}

	// if length==0, error in code!
	if( length==0 ) return 0; // error flag




	// see if we need to fill struct lzinfo
	if( lzinfo )
	{

		// fill already known values
		lzinfo->type=type;

		if( lzcode )
		{
			lzinfo->disp=lzcode->disp;
			lzinfo->length=lzcode->length;
		}
		else
		{
			lzinfo->disp=0; // only for OUTBYTE
			lzinfo->length=1;
		}

		lzinfo->bitsize=length;

		// fill remaining values
		switch(type)
		{
		case OUTBYTE:
			lzinfo->bits=0x80000000;
			lzinfo->bitsnum=1;       // one '1' bit

			lzinfo->byte=curbyte;
		break;

		case LEN1:
			lzinfo->bits=((((ULONG)lzinfo->disp)&7)<<26);
			lzinfo->bitsnum=6;

			lzinfo->byte=0xFFFFFFFF; // no byte
		break;

		case LEN2:
			lzinfo->bits=0x20000000;
			lzinfo->bitsnum=3;

			lzinfo->byte=((ULONG)lzinfo->disp)&0x00FF;
		break;

		case LEN3_SHORT:
			lzinfo->bits=0x40000000;
			lzinfo->bitsnum=4;

			lzinfo->byte=((ULONG)lzinfo->disp)&0x00FF;
		break;

		case LEN3_LONG:
			lzinfo->bits=0x50000000|(((((ULONG)lzinfo->disp)+0x0100)&0x00000F00)<<16);
			lzinfo->bitsnum=8;

			lzinfo->byte=((ULONG)lzinfo->disp)&0x00FF;
		break;

		case VARLEN_SHORT:
			ptrn=0xFFFFFFFF>>(32-bits);
			ptrn=(ptrn&( lzinfo->length - (1<<bits) - 2 ));
			ptrn=ptrn|(1<<bits);
			ptrn<<=1;
			ptrn=0x60000000|(ptrn<<(28-bits-bits));

			lzinfo->bits=ptrn;
			lzinfo->bitsnum=length-8;

			lzinfo->byte=((ULONG)lzinfo->disp)&0x00FF;
		break;

		case VARLEN_LONG:
			ptrn=0xFFFFFFFF>>(32-bits);
			ptrn=(ptrn&( lzinfo->length - (1<<bits) - 2 ));
			ptrn=ptrn|(1<<bits);
			ptrn=(ptrn<<1)|1;
			ptrn=(ptrn<<4)|(((((ULONG)lzinfo->disp)+0x0100)&0x00000F00)>>8);
			ptrn=0x60000000|(ptrn<<(24-bits-bits));

			lzinfo->bits=ptrn;
			lzinfo->bitsnum=length-8;

			lzinfo->byte=((ULONG)lzinfo->disp)&0x00FF;
		break;
		}
	}

	return length;
}

// Assembly:
movb %dil, %al
movb %al, -0x5(%rsp)
movq %rsi, -0x10(%rsp)
movq %rdx, -0x18(%rsp)
movl $0x0, -0x20(%rsp)
cmpq $0x0, -0x10(%rsp)
jne 0x25f6
movl $0x9, -0x20(%rsp)
movl $0x1, -0x1c(%rsp)
jmp 0x27c5
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $0x0, %eax
jne 0x2619
movl $0x9, -0x20(%rsp)
movl $0x1, -0x1c(%rsp)
jmp 0x27c3
movq -0x10(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x1, %eax
jne 0x2657
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $-0x8, %eax
jl 0x2652
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $-0x1, %eax
jg 0x2652
movl $0x6, -0x20(%rsp)
movl $0x2, -0x1c(%rsp)
jmp 0x27c1
movq -0x10(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x2, %eax
jne 0x2697
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $0xffffff00, %eax       # imm = 0xFFFFFF00
jl 0x2692
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $-0x1, %eax
jg 0x2692
movl $0xb, -0x20(%rsp)
movl $0x3, -0x1c(%rsp)
jmp 0x27bf
movq -0x10(%rsp), %rax
movzwl (%rax), %eax
cmpl $0x3, %eax
jne 0x270b
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $0xffffff00, %eax       # imm = 0xFFFFFF00
jl 0x26d4
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $-0x1, %eax
jg 0x26d4
movl $0xc, -0x20(%rsp)
movl $0x4, -0x1c(%rsp)
jmp 0x2706
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $0xffffef00, %eax       # imm = 0xFFFFEF00
jl 0x2704
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $0xfffffeff, %eax       # imm = 0xFFFFFEFF
jg 0x2704
movl $0x10, -0x20(%rsp)
movl $0x5, -0x1c(%rsp)
jmp 0x2706
jmp 0x27bd
movq -0x10(%rsp), %rax
movzwl (%rax), %eax
cmpl $0xff, %eax
jg 0x27bb
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $0xffffef00, %eax       # imm = 0xFFFFEF00
jl 0x27b9
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $-0x1, %eax
jg 0x27b9
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %eax
cmpl $0xffffff00, %eax       # imm = 0xFFFFFF00
jl 0x2762
movl $0xc, -0x24(%rsp)
movl $0x6, -0x1c(%rsp)
jmp 0x2772
movl $0x10, -0x24(%rsp)
movl $0x7, -0x1c(%rsp)
movq -0x10(%rsp), %rax
movzwl (%rax), %eax
subl $0x2, %eax
sarl %eax
movl %eax, -0x28(%rsp)
movl $0x0, -0x2c(%rsp)
cmpl $0x0, -0x28(%rsp)
je 0x27a9
movl -0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rsp)
movl -0x28(%rsp), %eax
shrl %eax
movl %eax, -0x28(%rsp)
jmp 0x278b
movl -0x2c(%rsp), %eax
addl -0x2c(%rsp), %eax
addl -0x24(%rsp), %eax
movl %eax, -0x20(%rsp)
jmp 0x27bb
jmp 0x27bd
jmp 0x27bf
jmp 0x27c1
jmp 0x27c3
jmp 0x27c5
cmpl $0x0, -0x20(%rsp)
jne 0x27d9
movl $0x0, -0x4(%rsp)
jmp 0x2b04
cmpq $0x0, -0x18(%rsp)
je 0x2afc
movl -0x1c(%rsp), %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x18(%rax)
cmpq $0x0, -0x10(%rsp)
je 0x281b
movq -0x10(%rsp), %rax
movswl 0x2(%rax), %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rsp), %rax
movzwl (%rax), %ecx
movq -0x18(%rsp), %rax
movl %ecx, (%rax)
jmp 0x2832
movq -0x18(%rsp), %rax
movl $0x0, 0x4(%rax)
movq -0x18(%rsp), %rax
movl $0x1, (%rax)
movl -0x20(%rsp), %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x8(%rax)
movl -0x1c(%rsp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x38(%rsp)
subl $0x6, %eax
ja 0x2afa
movq -0x38(%rsp), %rax
leaq 0x1be4(%rip), %rcx      # 0x4444
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rsp), %rax
movl $0x80000000, 0x10(%rax) # imm = 0x80000000
movq -0x18(%rsp), %rax
movl $0x1, 0x14(%rax)
movzbl -0x5(%rsp), %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2afa
movq -0x18(%rsp), %rax
movl 0x4(%rax), %ecx
andl $0x7, %ecx
shll $0x1a, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rsp), %rax
movl $0x6, 0x14(%rax)
movq -0x18(%rsp), %rax
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
jmp 0x2afa
movq -0x18(%rsp), %rax
movl $0x20000000, 0x10(%rax) # imm = 0x20000000
movq -0x18(%rsp), %rax
movl $0x3, 0x14(%rax)
movq -0x18(%rsp), %rax
movl 0x4(%rax), %ecx
andl $0xff, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2afa
movq -0x18(%rsp), %rax
movl $0x40000000, 0x10(%rax) # imm = 0x40000000
movq -0x18(%rsp), %rax
movl $0x4, 0x14(%rax)
movq -0x18(%rsp), %rax
movl 0x4(%rax), %ecx
andl $0xff, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2afa
movq -0x18(%rsp), %rax
movl 0x4(%rax), %ecx
addl $0x100, %ecx            # imm = 0x100
andl $0xf00, %ecx            # imm = 0xF00
shll $0x10, %ecx
orl $0x50000000, %ecx       # imm = 0x50000000
movq -0x18(%rsp), %rax
movl %ecx, 0x10(%rax)
movq -0x18(%rsp), %rax
movl $0x8, 0x14(%rax)
movq -0x18(%rsp), %rax
movl 0x4(%rax), %ecx
andl $0xff, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2afa
movl $0x20, %ecx
subl -0x2c(%rsp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shrl %cl, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
movq -0x18(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, -0x3c(%rsp)
movl -0x2c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl -0x3c(%rsp), %ecx
subl %edx, %ecx
subl $0x2, %ecx
andl %ecx, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
movl -0x2c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
orl %ecx, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
shll %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
movl $0x1c, %ecx
subl -0x2c(%rsp), %ecx
subl -0x2c(%rsp), %ecx
shll %cl, %eax
orl $0x60000000, %eax       # imm = 0x60000000
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x10(%rax)
movl -0x20(%rsp), %ecx
subl $0x8, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x14(%rax)
movq -0x18(%rsp), %rax
movl 0x4(%rax), %ecx
andl $0xff, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2afa
movl $0x20, %ecx
subl -0x2c(%rsp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
shrl %cl, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
movq -0x18(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, -0x40(%rsp)
movl -0x2c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl -0x40(%rsp), %ecx
subl %edx, %ecx
subl $0x2, %ecx
andl %ecx, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
movl -0x2c(%rsp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
orl %ecx, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
shll %eax
orl $0x1, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
shll $0x4, %eax
movq -0x18(%rsp), %rcx
movl 0x4(%rcx), %ecx
addl $0x100, %ecx            # imm = 0x100
andl $0xf00, %ecx            # imm = 0xF00
shrl $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %eax
movl $0x18, %ecx
subl -0x2c(%rsp), %ecx
subl -0x2c(%rsp), %ecx
shll %cl, %eax
orl $0x60000000, %eax       # imm = 0x60000000
movl %eax, -0x30(%rsp)
movl -0x30(%rsp), %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x10(%rax)
movl -0x20(%rsp), %ecx
subl $0x8, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0x14(%rax)
movq -0x18(%rsp), %rax
movl 0x4(%rax), %ecx
andl $0xff, %ecx
movq -0x18(%rsp), %rax
movl %ecx, 0xc(%rax)
jmp 0x2afc
movl -0x20(%rsp), %eax
movl %eax, -0x4(%rsp)
movl -0x4(%rsp), %eax
retq
nopl (%rax)