// File: /workspace/github_c_makefile_star2_O3/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 0x23e1
testl %eax, %eax
je 0x228b
callq 0x1e23
movq 0x54c4(%rip), %rax      # 0x7630
movl $0x1, (%rax)
leaq 0x2f67(%rip), %rbx      # 0x50e0
movq (%rbx), %rcx
movb (%rcx), %bpl
leaq 0x2f62(%rip), %rcx      # 0x50e8
movl (%rcx), %ecx
cmpl $0x1, %ecx
je 0x2297
cmpl $0x2, %ecx
jne 0x239d
leaq 0x2f37(%rip), %r12      # 0x50d8
movl (%r12), %ecx
testq %rcx, %rcx
je 0x239d
xorl %edx, %edx
movl $0x1, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x21b0
cmpl $0x1, %ecx
je 0x239d
xorl %r15d, %r15d
movl $0x1, %r14d
movl %ebp, %eax
movq (%rbx), %rcx
movb (%rcx,%r14), %bpl
movzbl %bpl, %esi
movzbl %al, %edi
movl %r14d, %edx
callq 0x2750
testl %eax, %eax
je 0x23d8
testl %r15d, %r15d
jne 0x2273
movl %r14d, %edi
callq 0x2419
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %r13d, %r15d
shlq $0x2, %r15
addq 0x2da9(%rip), %r15      # 0x4fc0
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1ebf
testl %eax, %eax
je 0x23b4
movzwl (%r15), %ecx
shll $0x3, %ecx
subl %eax, %ecx
cmpl %ebx, %ecx
cmovgl %r13d, %r12d
cmovgl %ecx, %ebx
incl %r13d
cmpw $0x0, 0x2(%r15)
jne 0x2209
movq 0x53e1(%rip), %rax      # 0x7630
movl %r12d, %ecx
leaq 0x4fd7(%rip), %rsi      # 0x7230
movl (%rsi,%rcx,4), %edx
movl %edx, (%rax,%r14,8)
movzwl (%rsi,%rcx,4), %r15d
leaq 0x2e74(%rip), %rbx      # 0x50e0
leaq 0x2e65(%rip), %r12      # 0x50d8
decl %r15d
incq %r14
movl (%r12), %eax
cmpq %rax, %r14
jb 0x21d1
jmp 0x239d
leaq 0x119a(%rip), %rdi      # 0x342c
jmp 0x23bb
movl $0x0, 0x4(%rax)
movl $0x8, 0xc(%rax)
leaq 0x2e2c(%rip), %r13      # 0x50d8
cmpl $0x2, (%r13)
jb 0x239d
movl $0x2, %ecx
movl %ecx, %edx
movl $0xffffffff, 0x4(%rax,%rdx,8) # imm = 0xFFFFFFFF
incl %ecx
movl (%r13), %edx
cmpl %edx, %ecx
jbe 0x22bc
cmpl $0x2, %edx
jb 0x239d
movl $0x1, %r14d
leaq 0x4f4a(%rip), %r12      # 0x7230
movl %ebp, %eax
movq (%rbx), %rcx
movb (%rcx,%r14), %bpl
movzbl %bpl, %esi
movzbl %al, %edi
movl %r14d, %edx
callq 0x2750
testl %eax, %eax
je 0x23d8
movl %r14d, %edi
callq 0x2419
xorl %ebx, %ebx
movl %ebx, %r15d
shlq $0x2, %r15
addq %r12, %r15
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1ebf
testl %eax, %eax
je 0x23b4
movzwl (%r15), %ecx
addl %r14d, %ecx
movq 0x52f4(%rip), %rdx      # 0x7630
addl 0x4(%rdx,%r14,8), %eax
cmpl %eax, 0x4(%rdx,%rcx,8)
jbe 0x2351
movl %eax, 0x4(%rdx,%rcx,8)
movl (%r15), %eax
movl %eax, (%rdx,%rcx,8)
incl %ebx
cmpw $0x0, 0x2(%r15)
jne 0x2310
incq %r14
movl (%r13), %eax
cmpq %rax, %r14
leaq 0x2d74(%rip), %rbx      # 0x50e0
jb 0x22e6
cmpl $0x2, %eax
jb 0x239d
movq 0x52b2(%rip), %rcx      # 0x7630
movd (%rcx,%rax,8), %xmm0
pextrw $0x0, %xmm0, %edx
subl %edx, %eax
movd (%rcx,%rax,8), %xmm1
movd %xmm0, (%rcx,%rax,8)
movdqa %xmm1, %xmm0
cmpl $0x1, %eax
ja 0x2383
callq 0x2626
movl $0x1, %ebx
testl %eax, %eax
jne 0x23c2
leaq 0x1097(%rip), %rdi      # 0x3449
jmp 0x23bb
leaq 0x10ca(%rip), %rdi      # 0x3485
callq 0x1120
xorl %ebx, %ebx
callq 0x26e1
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1087(%rip), %rdi      # 0x3466
jmp 0x23bb