// File: /workspace/github_c_makefile_star2_O0/salextpuru[P]speccy-toochain/src/megalz/MegaLZ_pack.c
// Function: make_LZ_codes
void make_LZ_codes(ULONG curpos)
{ // actual generation of all LZ codes for the given byte @curpos
  // result in codes[256] array (see MegaLZ_lz.h, MegaLZ_lz.c)

	UBYTE curbyte,nextbyte;
	ULONG i;
	ULONG tbi;
	ULONG was_twobyter,last_match;
	ULONG lzlen;

	struct tb_chain * curtb;



	tbi=0;
	curtb=NULL;

	// init lz codes collecting
	start_lz();


	// find 1-byte code

	curbyte=indata[curpos];

	i=(curpos>8)?(curpos-8):0;
	while( i<curpos )
	{
		if( indata[i]==curbyte )
		{
			add_lz((LONG)i-(LONG)curpos,1);
			break;
		}

		i++;
	}



	// find 2-byte code

	was_twobyter=0;

	if( (inlen-1)>curpos ) // if we have enough bytes ahead
	{
		nextbyte=indata[curpos+1];
		tbi=(((ULONG)curbyte)<<8)+((ULONG)nextbyte);

		curtb=tb_entry[tbi]; // get twobyter

		if( curtb )
		{
			if( (curpos-curtb->pos)<=256 )
			{
				add_lz((LONG)curtb->pos-(LONG)curpos,2);
			}

			if( (curpos-curtb->pos)<=4352 )
			{
				was_twobyter=1; // there is something for 3 and more bytes finding
			}
			else
			{
				cutoff_twobyte_chain(tbi,curpos); // flush older twobyters
			}
		}
	}



	// find other codes (len=3..255)

	if( was_twobyter && ((inlen-2)>curpos) ) // was twobyter within -1..-4352 range
	                                         // and at least 3-byter possible
	{
		// some inits

		//curtb=tb_entry[tbi]; // already done in 2-byter search

		last_match=1; // was last match (for 2 bytes)

		// loop for all possible lengths
		for( lzlen=3; (lzlen<=255)&&((inlen-lzlen+1)>curpos);/*NOTHING!*/)
		{
			if( last_match )
			{ // there were last match (lzlen-1)

				// compare last bytes of current match
				if( indata[curpos+lzlen-1]==indata[curtb->pos+lzlen-1] )
				{ // current match OK
					add_lz((LONG)curtb->pos-(LONG)curpos,lzlen);
					lzlen++;
				}
				else
				{ // current match failed: - take next (older) twobyter
MATCH_FAILED:
					curtb=curtb->next;
					if( !curtb ) break; // stop search if no more twobyters
					if( (curpos-curtb->pos)>4352 )
					{ // if twobyters out of range
						cutoff_twobyte_chain(tbi,curpos);
						break;
					}
					last_match=0; // indicate match fail for next step
				}
			}
			else
			{ // last match failed so compare all strings

				// first compare hashes of the ends of both strings
				if( hash[curpos+lzlen-1]==hash[curtb->pos+lzlen-1] )
				{
					// if hashes are the same, compare complete strings
					if( !memcmp( &indata[curpos], &indata[curtb->pos], lzlen) )
					{ //match OK
						last_match=1;
						add_lz((LONG)curtb->pos-(LONG)curpos,lzlen);
						lzlen++;
					}
					else goto MATCH_FAILED;
				}
				else goto MATCH_FAILED;
			}
		}
	}

	// stop collecting codes
	end_lz();

}

// Assembly:
subq $0x38, %rsp
movl %edi, 0x34(%rsp)
movl $0x0, 0x28(%rsp)
movq $0x0, 0x10(%rsp)
callq 0x2510
leaq 0x30db(%rip), %rax      # 0x60e0
movq (%rax), %rax
movl 0x34(%rsp), %ecx
movb (%rax,%rcx), %al
movb %al, 0x33(%rsp)
cmpl $0x8, 0x34(%rsp)
jbe 0x3027
movl 0x34(%rsp), %eax
subl $0x8, %eax
movl %eax, 0xc(%rsp)
jmp 0x302f
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x302f
movl 0xc(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x34(%rsp), %eax
jae 0x307d
leaq 0x3098(%rip), %rax      # 0x60e0
movq (%rax), %rax
movl 0x2c(%rsp), %ecx
movzbl (%rax,%rcx), %eax
movzbl 0x33(%rsp), %ecx
cmpl %ecx, %eax
jne 0x3070
movl 0x2c(%rsp), %edi
subl 0x34(%rsp), %edi
movl $0x1, %esi
callq 0x2570
jmp 0x307d
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x3037
movl $0x0, 0x24(%rsp)
leaq 0x304c(%rip), %rax      # 0x60d8
movl (%rax), %eax
subl $0x1, %eax
cmpl 0x34(%rsp), %eax
jbe 0x313d
leaq 0x303e(%rip), %rax      # 0x60e0
movq (%rax), %rax
movl 0x34(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %al
movb %al, 0x32(%rsp)
movzbl 0x33(%rsp), %eax
shll $0x8, %eax
movzbl 0x32(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, %ecx
leaq 0x557b(%rip), %rax      # 0x8650
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x313b
movl 0x34(%rsp), %eax
movq 0x10(%rsp), %rcx
subl 0x8(%rcx), %eax
cmpl $0x100, %eax            # imm = 0x100
ja 0x310f
movq 0x10(%rsp), %rax
movl 0x8(%rax), %edi
subl 0x34(%rsp), %edi
movl $0x2, %esi
callq 0x2570
movl 0x34(%rsp), %eax
movq 0x10(%rsp), %rcx
subl 0x8(%rcx), %eax
cmpl $0x1100, %eax           # imm = 0x1100
ja 0x312c
movl $0x1, 0x24(%rsp)
jmp 0x3139
movl 0x28(%rsp), %edi
movl 0x34(%rsp), %esi
callq 0x3700
jmp 0x313b
jmp 0x313d
cmpl $0x0, 0x24(%rsp)
je 0x3312
leaq 0x2f89(%rip), %rax      # 0x60d8
movl (%rax), %eax
subl $0x2, %eax
cmpl 0x34(%rsp), %eax
jbe 0x3312
movl $0x1, 0x20(%rsp)
movl $0x3, 0x1c(%rsp)
xorl %eax, %eax
cmpl $0xff, 0x1c(%rsp)
movb %al, 0xb(%rsp)
ja 0x3199
leaq 0x2f53(%rip), %rax      # 0x60d8
movl (%rax), %eax
subl 0x1c(%rsp), %eax
addl $0x1, %eax
cmpl 0x34(%rsp), %eax
seta %al
movb %al, 0xb(%rsp)
movb 0xb(%rsp), %al
testb $0x1, %al
jne 0x31a6
jmp 0x3310
cmpl $0x0, 0x20(%rsp)
je 0x325f
leaq 0x2f28(%rip), %rax      # 0x60e0
movq (%rax), %rax
movl 0x34(%rsp), %ecx
addl 0x1c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
leaq 0x2f0d(%rip), %rcx      # 0x60e0
movq (%rcx), %rcx
movq 0x10(%rsp), %rdx
movl 0x8(%rdx), %edx
addl 0x1c(%rsp), %edx
subl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x3211
movq 0x10(%rsp), %rax
movl 0x8(%rax), %edi
subl 0x34(%rsp), %edi
movl 0x1c(%rsp), %esi
callq 0x2570
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x325a
jmp 0x3213
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jne 0x322d
jmp 0x3310
movl 0x34(%rsp), %eax
movq 0x10(%rsp), %rcx
subl 0x8(%rcx), %eax
cmpl $0x1100, %eax           # imm = 0x1100
jbe 0x3252
movl 0x28(%rsp), %edi
movl 0x34(%rsp), %esi
callq 0x3700
jmp 0x3310
movl $0x0, 0x20(%rsp)
jmp 0x330b
leaq 0x4faa(%rip), %rax      # 0x8210
movq (%rax), %rax
movl 0x34(%rsp), %ecx
addl 0x1c(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
leaq 0x4f8f(%rip), %rcx      # 0x8210
movq (%rcx), %rcx
movq 0x10(%rsp), %rdx
movl 0x8(%rdx), %edx
addl 0x1c(%rsp), %edx
subl $0x1, %edx
movl %edx, %edx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x3304
leaq 0x2e3c(%rip), %rax      # 0x60e0
movq (%rax), %rdi
movl 0x34(%rsp), %eax
addq %rax, %rdi
leaq 0x2e2b(%rip), %rax      # 0x60e0
movq (%rax), %rsi
movq 0x10(%rsp), %rax
movl 0x8(%rax), %eax
addq %rax, %rsi
movl 0x1c(%rsp), %eax
movl %eax, %edx
callq 0x1060
cmpl $0x0, %eax
jne 0x32fd
movl $0x1, 0x20(%rsp)
movq 0x10(%rsp), %rax
movl 0x8(%rax), %edi
subl 0x34(%rsp), %edi
movl 0x1c(%rsp), %esi
callq 0x2570
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x3302
jmp 0x3213
jmp 0x3309
jmp 0x3213
jmp 0x330b
jmp 0x316e
jmp 0x3312
callq 0x2520
addq $0x38, %rsp
retq
nopl (%rax)