// File: /workspace/github_c_makefile_star2_O1/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 0x23cb
testl %eax, %eax
je 0x2275
callq 0x1e13
movq 0x54d8(%rip), %rax      # 0x7630
movl $0x1, (%rax)
leaq 0x2f7b(%rip), %rbx      # 0x50e0
movq (%rbx), %rcx
movb (%rcx), %bpl
leaq 0x2f76(%rip), %rcx      # 0x50e8
movl (%rcx), %ecx
cmpl $0x1, %ecx
je 0x2281
cmpl $0x2, %ecx
jne 0x2387
leaq 0x2f4b(%rip), %r12      # 0x50d8
movl (%r12), %ecx
testq %rcx, %rcx
je 0x21a7
xorl %edx, %edx
movl $0x1, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x2198
cmpl $0x2, (%r12)
jb 0x2387
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 0x2754
testl %eax, %eax
je 0x23c2
testl %r15d, %r15d
jne 0x225d
movl %r14d, %edi
callq 0x2403
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %r13d, %r15d
shlq $0x2, %r15
addq 0x2dbf(%rip), %r15      # 0x4fc0
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1ec3
testl %eax, %eax
je 0x239e
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 0x21f3
movq 0x53f7(%rip), %rax      # 0x7630
movl %r12d, %ecx
leaq 0x4fed(%rip), %rsi      # 0x7230
movl (%rsi,%rcx,4), %edx
movl %edx, (%rax,%r14,8)
movzwl (%rsi,%rcx,4), %r15d
leaq 0x2e8a(%rip), %rbx      # 0x50e0
leaq 0x2e7b(%rip), %r12      # 0x50d8
decl %r15d
incq %r14
movl (%r12), %eax
cmpq %rax, %r14
jb 0x21bb
jmp 0x2387
leaq 0x11bc(%rip), %rdi      # 0x3438
jmp 0x23a5
movl $0x0, 0x4(%rax)
movl $0x8, 0xc(%rax)
leaq 0x2e42(%rip), %r13      # 0x50d8
cmpl $0x2, (%r13)
jb 0x22b4
movl $0x2, %ecx
movl %ecx, %edx
movl $0xffffffff, 0x4(%rax,%rdx,8) # imm = 0xFFFFFFFF
incl %ecx
cmpl (%r13), %ecx
jbe 0x22a2
movl (%r13), %eax
cmpl $0x2, %eax
jb 0x235a
movl $0x1, %r14d
leaq 0x4f62(%rip), %r12      # 0x7230
movl %ebp, %eax
movq (%rbx), %rcx
movb (%rcx,%r14), %bpl
movzbl %bpl, %esi
movzbl %al, %edi
movl %r14d, %edx
callq 0x2754
testl %eax, %eax
je 0x23c2
movl %r14d, %edi
callq 0x2403
xorl %ebx, %ebx
movl %ebx, %r15d
shlq $0x2, %r15
addq %r12, %r15
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x1ec3
testl %eax, %eax
je 0x239e
movzwl (%r15), %ecx
addl %r14d, %ecx
movq 0x530c(%rip), %rdx      # 0x7630
addl 0x4(%rdx,%r14,8), %eax
cmpl %eax, 0x4(%rdx,%rcx,8)
jbe 0x2339
movl %eax, 0x4(%rdx,%rcx,8)
movl (%r15), %eax
movl %eax, (%rdx,%rcx,8)
incl %ebx
cmpw $0x0, 0x2(%r15)
jne 0x22f8
incq %r14
movl (%r13), %eax
cmpq %rax, %r14
leaq 0x2d8c(%rip), %rbx      # 0x50e0
jb 0x22ce
cmpl $0x2, %eax
jb 0x2387
movq 0x52ca(%rip), %rcx      # 0x7630
movl %eax, %edx
movd (%rcx,%rdx,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 0x236d
callq 0x261b
movl $0x1, %ebx
testl %eax, %eax
jne 0x23ac
leaq 0x10b9(%rip), %rdi      # 0x3455
jmp 0x23a5
leaq 0x10ec(%rip), %rdi      # 0x3491
callq 0x1120
xorl %ebx, %ebx
callq 0x26e3
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10a9(%rip), %rdi      # 0x3472
jmp 0x23a5