// File: /workspace/github_c_makefile_star2_O2/salextpuru[P]speccy-toochain/src/megalz/MegaLZ_pack.c
// Function: pack
ULONG pack(void)
{
	// return value
	ULONG retcode=1;

	ULONG current_pos; // current position in file

	UBYTE curr_byte,last_byte; // current byte and last byte (for twobyter)

	struct lzcode curr, tmp;

	ULONG i,bitlen;

	ULONG skip;
	ULONG bestcode;
	LONG curgain,bestgain;

// allocate mem for arrays/tables/etc.

	if( stuff_init() )
	{
		// make hash
		make_hash();

		// very first inits
		pdata[0].best.disp=0;
		pdata[0].best.length=1; // 1st byte copied 'as-is'

		curr_byte=indata[0];


		if( mode==MODE_PACK_OPTIMAL )
		{ // !!optimal coding!!

			// initialize prices
			pdata[0].price=0;
			pdata[1].price=8;
			for(i=2;i<=inlen;i++)
			{
				pdata[i].price=0xffffffff;
			}

			// do every byte
			for( current_pos=1;current_pos<inlen;current_pos++)
			{
				last_byte=curr_byte;
				curr_byte=indata[current_pos];

				// add current twobyter
				if( !add_twobyter(last_byte,curr_byte,current_pos) )
				{
					printf("pack(): add_twobyter() failed!\n");
					retcode=0;
					goto ERROR;
				}

				// find all LZ codes for current byte
				make_LZ_codes(current_pos);

				// update prices
				i=0;
				do
				{
					bitlen=make_lz_info(0x00,&codes[i],NULL);
					if( !bitlen )
					{
						printf("pack(): make_lz_info() failed!\n");
						retcode=0;
						goto ERROR;
					}
					update_price(current_pos,bitlen,&codes[i]);
				} while( codes[i++].disp );
			}

			// reverse optimal chain
		        i=inlen;
		        tmp=pdata[i].best;
		        while(i>1)
		        {
				curr=tmp;
				i=i-curr.length;
				tmp=pdata[i].best;
				pdata[i].best=curr;
			}
		}
		else if( mode==MODE_PACK_GREEDY )
		{ // greedy coding

			// init chain
			for(i=0;i<inlen;i++)
			{
				pdata[i].best.disp=0;
				pdata[i].best.length=1;
			}

			skip=0;
			for( current_pos=1;current_pos<inlen;current_pos++)
			{
				last_byte=curr_byte;
				curr_byte=indata[current_pos];

				// add current twobyter
				if( !add_twobyter(last_byte,curr_byte,current_pos) )
				{
					printf("pack(): add_twobyter() failed!\n");
					retcode=0;
					goto ERROR;
				}

				if( skip )
				{
					skip--;
				}
				else
				{
					// find all LZ codes for current byte
					make_LZ_codes(current_pos);

					// find 'best' code on greedy basis
					bestgain=-2;
					bestcode=i=0;
					do
					{
						bitlen=make_lz_info(0x00,&codes[i],NULL);
						if( !bitlen )
						{
							printf("pack(): make_lz_info() failed!\n");
							retcode=0;
							goto ERROR;
						}

						curgain=(LONG)(8*codes[i].length)-bitlen;

						if( curgain>bestgain )
						{
							bestgain=curgain;
							bestcode=i;
						}
					} while( codes[i++].disp );

					// write found code, skip next (length-1) bytes
					pdata[current_pos].best=codes[bestcode];
					skip=codes[bestcode].length-1;
				}
			}
		}


		// generate output file
		if( !gen_output() )
		{
			printf("pack(): gen_output() failed!\n");
			retcode=0;
		}

	}
	else
	{
		printf("pack(): stuff_init() failed!\n");
		retcode=0;
	}

ERROR:
	stuff_free();

	return retcode;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
callq 0x21b3
testl %eax, %eax
je 0x1fd7
callq 0x1cae
movq 0x56a4(%rip), %rcx      # 0x7630
movl $0x1, (%rcx)
leaq 0x3147(%rip), %rbx      # 0x50e0
movq (%rbx), %rax
movb (%rax), %al
leaq 0x3143(%rip), %rdx      # 0x50e8
movl (%rdx), %edx
cmpl $0x1, %edx
je 0x20a6
cmpl $0x2, %edx
jne 0x2181
leaq 0x3118(%rip), %r12      # 0x50d8
movl (%r12), %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x1fe3
movl $0x1, (%rcx,%rsi,8)
incq %rsi
jmp 0x1fc6
leaq 0x144e(%rip), %rdi      # 0x342c
jmp 0x2196
xorl %r15d, %r15d
pushq $0x1
popq %r14
pushq $-0x2
popq %rbp
movl %edx, %ecx
cmpq %rcx, %r14
jae 0x2181
movq (%rbx), %rcx
movb (%rcx,%r14), %r13b
movzbl %r13b, %esi
movzbl %al, %edi
movl %r14d, %edx
callq 0x251d
testl %eax, %eax
je 0x2151
testl %r15d, %r15d
jne 0x2094
movl %r14d, %edi
callq 0x21eb
xorl %ebx, %ebx
xorl %r12d, %r12d
movl %ebx, %r15d
shlq $0x2, %r15
addq 0x2f8a(%rip), %r15      # 0x4fc0
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1d44
testl %eax, %eax
je 0x2148
movzwl (%r15), %ecx
shll $0x3, %ecx
subl %eax, %ecx
cmpl %ebp, %ecx
cmovgl %ebx, %r12d
cmovgl %ecx, %ebp
incl %ebx
cmpw $0x0, 0x2(%r15)
jne 0x2028
movq 0x55c3(%rip), %rax      # 0x7630
movl %r12d, %ecx
leaq 0x51b9(%rip), %rsi      # 0x7230
movl (%rsi,%rcx,4), %edx
movl %edx, (%rax,%r14,8)
movzwl (%rsi,%rcx,4), %r15d
leaq 0x3056(%rip), %rbx      # 0x50e0
leaq 0x3047(%rip), %r12      # 0x50d8
pushq $-0x2
popq %rbp
decl %r15d
incq %r14
movl (%r12), %edx
movl %r13d, %eax
jmp 0x1fed
andl $0x0, 0x4(%rcx)
movl $0x8, 0xc(%rcx)
pushq $0x2
popq %rsi
leaq 0x301d(%rip), %r13      # 0x50d8
movl (%r13), %edx
cmpl %edx, %esi
ja 0x20ce
movl %esi, %edx
orl $-0x1, 0x4(%rcx,%rdx,8)
incl %esi
jmp 0x20bb
pushq $0x1
popq %r14
leaq 0x5157(%rip), %rbp      # 0x7230
movl %edx, %ecx
cmpq %rcx, %r14
jae 0x215a
movq (%rbx), %rcx
movb (%rcx,%r14), %r12b
movzbl %r12b, %esi
movzbl %al, %edi
movl %r14d, %edx
callq 0x251d
testl %eax, %eax
je 0x2151
movl %r14d, %edi
callq 0x21eb
xorl %ebx, %ebx
movl %ebx, %r15d
shlq $0x2, %r15
addq %rbp, %r15
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1d44
testl %eax, %eax
je 0x2148
movl %r14d, %edi
movl %eax, %esi
movq %r15, %rdx
callq 0x23d0
incl %ebx
cmpw $0x0, 0x2(%r15)
jne 0x2104
incq %r14
movl (%r13), %edx
movl %r12d, %eax
leaq 0x2f9a(%rip), %rbx      # 0x50e0
jmp 0x20d9
leaq 0x1336(%rip), %rdi      # 0x3485
jmp 0x2196
leaq 0x130e(%rip), %rdi      # 0x3466
jmp 0x2196
movq 0x54cf(%rip), %rax      # 0x7630
movd (%rax,%rcx,8), %xmm0
cmpl $0x2, %edx
jb 0x2181
pextrw $0x0, %xmm0, %ecx
subl %ecx, %edx
movss (%rax,%rdx,8), %xmm1
movd %xmm0, (%rax,%rdx,8)
movaps %xmm1, %xmm0
jmp 0x2166
callq 0x240b
testl %eax, %eax
je 0x218f
pushq $0x1
popq %rbx
jmp 0x219d
leaq 0x12b3(%rip), %rdi      # 0x3449
callq 0x1110
xorl %ebx, %ebx
callq 0x24b8
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq