// File: /workspace/github_c_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %ebx
callq 0x1e70
leaq 0x2cc1(%rip), %rax      # 0x50e0
movq (%rax), %rax
movl %ebx, %ecx
movq %rcx, 0x10(%rsp)
movzbl (%rax,%rcx), %ebp
xorl %edx, %edx
movl %ebx, %ecx
subl $0x8, %ecx
cmovbl %edx, %ecx
cmpl %ebx, %ecx
jae 0x2465
cmpl $0x8, %ebx
movl $0x8, %edi
cmovbl %ebx, %edi
movl %ecx, %ecx
addq %rcx, %rax
cmpb %bpl, (%rax)
je 0x2459
incq %rax
decl %edi
jne 0x244b
jmp 0x2465
negl %edi
movl $0x1, %esi
callq 0x1ea4
leaq 0x2c6c(%rip), %r14      # 0x50d8
movl (%r14), %eax
decl %eax
movb $0x1, %r15b
cmpl %ebx, %eax
jbe 0x24d6
leaq 0x2c61(%rip), %rax      # 0x50e0
movq (%rax), %rax
leal 0x1(%rbx), %ecx
movzbl (%rax,%rcx), %eax
shll $0x8, %ebp
movl %ebp, %r12d
orl %eax, %r12d
leaq 0x51b7(%rip), %rax      # 0x7650
movq (%rax,%r12,8), %rbp
testq %rbp, %rbp
movq %r12, 0x8(%rsp)
je 0x24df
movl 0x8(%rbp), %edi
movl %ebx, %eax
subl %edi, %eax
cmpl $0x100, %eax            # imm = 0x100
ja 0x24c1
subl %ebx, %edi
movl $0x2, %esi
callq 0x1ea4
movl %ebx, %eax
subl 0x8(%rbp), %eax
cmpl $0x1101, %eax           # imm = 0x1101
jae 0x25b2
xorl %r15d, %r15d
jmp 0x24e1
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
testb %r15b, %r15b
jne 0x25cd
movl (%r14), %eax
addl $-0x2, %eax
cmpl %ebx, %eax
jbe 0x25cd
leal -0x1(%rbx), %r13d
movl $0x1, %eax
movl $0x3, %r15d
movl (%r14), %ecx
subl %r15d, %ecx
incl %ecx
cmpl %ebx, %ecx
jbe 0x25cd
movl 0x8(%rbp), %r12d
leal (%r15,%r12), %ecx
decl %ecx
testl %eax, %eax
je 0x2553
leaq 0x2bb4(%rip), %rax      # 0x50e0
movq (%rax), %rax
leal (%r15,%r13), %edx
movb (%rax,%rdx), %dl
cmpb (%rax,%rcx), %dl
jne 0x258b
subl %ebx, %r12d
movl %r12d, %edi
movl %r15d, %esi
callq 0x1ea4
incl %r15d
movl $0x1, %eax
jmp 0x25a3
leaq 0x4cb6(%rip), %rax      # 0x7210
movq (%rax), %rax
leal (%r15,%r13), %edx
movb (%rax,%rdx), %dl
cmpb (%rax,%rcx), %dl
jne 0x258b
leaq 0x2b70(%rip), %rax      # 0x50e0
movq (%rax), %rsi
movq 0x10(%rsp), %rax
leaq (%rsi,%rax), %rdi
addq %r12, %rsi
movl %r15d, %edx
callq 0x10a0
testl %eax, %eax
je 0x253b
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x25cd
movl %ebx, %ecx
subl 0x8(%rbp), %ecx
xorl %eax, %eax
cmpl $0x1101, %ecx           # imm = 0x1101
jae 0x25c1
cmpl $0x100, %r15d           # imm = 0x100
jb 0x2507
jmp 0x25cd
movl %r12d, %edi
movl %ebx, %esi
callq 0x2804
jmp 0x24e1
movq 0x8(%rsp), %rdi
movl %ebx, %esi
callq 0x2804
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1e7b