// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
movl $0x1, 0x34(%rsp)
callq 0x2f80
cmpl $0x0, %eax
je 0x2f4e
callq 0x2470
movq 0x5afa(%rip), %rax      # 0x8630
movw $0x0, 0x2(%rax)
movq 0x5aed(%rip), %rax      # 0x8630
movw $0x1, (%rax)
leaq 0x3591(%rip), %rax      # 0x60e0
movq (%rax), %rax
movb (%rax), %al
movb %al, 0x2f(%rsp)
leaq 0x3589(%rip), %rax      # 0x60e8
cmpl $0x1, (%rax)
jne 0x2d3b
movq 0x5ac1(%rip), %rax      # 0x8630
movl $0x0, 0x4(%rax)
movq 0x5ab3(%rip), %rax      # 0x8630
movl $0x8, 0xc(%rax)
movl $0x2, 0x20(%rsp)
movl 0x20(%rsp), %eax
leaq 0x3541(%rip), %rcx      # 0x60d8
cmpl (%rcx), %eax
ja 0x2bbb
movq 0x5a8e(%rip), %rax      # 0x8630
movl 0x20(%rsp), %ecx
movl $0xffffffff, 0x4(%rax,%rcx,8) # imm = 0xFFFFFFFF
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x2b8c
movl $0x1, 0x30(%rsp)
movl 0x30(%rsp), %eax
leaq 0x350a(%rip), %rcx      # 0x60d8
cmpl (%rcx), %eax
jae 0x2cd3
movb 0x2f(%rsp), %al
movb %al, 0x2e(%rsp)
leaq 0x34fb(%rip), %rax      # 0x60e0
movq (%rax), %rax
movl 0x30(%rsp), %ecx
movb (%rax,%rcx), %al
movb %al, 0x2f(%rsp)
movb 0x2e(%rsp), %cl
movb 0x2f(%rsp), %al
movl 0x30(%rsp), %edx
movzbl %cl, %edi
movzbl %al, %esi
callq 0x35e0
cmpl $0x0, %eax
jne 0x2c2a
leaq 0x185f(%rip), %rdi      # 0x4475
movb $0x0, %al
callq 0x1040
movl $0x0, 0x34(%rsp)
jmp 0x2f66
movl 0x30(%rsp), %edi
callq 0x2fe0
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
leaq 0x55ea(%rip), %rsi      # 0x8230
shlq $0x2, %rax
addq %rax, %rsi
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %edx
callq 0x25c0
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jne 0x2c7e
leaq 0x182b(%rip), %rdi      # 0x4495
movb $0x0, %al
callq 0x1040
movl $0x0, 0x34(%rsp)
jmp 0x2f66
movl 0x30(%rsp), %edi
movl 0x1c(%rsp), %esi
movl 0x20(%rsp), %eax
leaq 0x559f(%rip), %rdx      # 0x8230
shlq $0x2, %rax
addq %rax, %rdx
callq 0x3320
movl 0x20(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rsp)
movl %eax, %eax
movl %eax, %ecx
leaq 0x557b(%rip), %rax      # 0x8230
cmpw $0x0, 0x2(%rax,%rcx,4)
jne 0x2c3b
jmp 0x2cc3
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x2bc3
leaq 0x33fe(%rip), %rax      # 0x60d8
movl (%rax), %eax
movl %eax, 0x20(%rsp)
movq 0x5949(%rip), %rax      # 0x8630
movl 0x20(%rsp), %ecx
movl (%rax,%rcx,8), %eax
movl %eax, 0x26(%rsp)
cmpl $0x1, 0x20(%rsp)
jbe 0x2d36
movl 0x26(%rsp), %eax
movl %eax, 0x2a(%rsp)
movl 0x20(%rsp), %eax
movzwl 0x2a(%rsp), %ecx
subl %ecx, %eax
movl %eax, 0x20(%rsp)
movq 0x5919(%rip), %rax      # 0x8630
movl 0x20(%rsp), %ecx
movl (%rax,%rcx,8), %eax
movl %eax, 0x26(%rsp)
movq 0x5907(%rip), %rax      # 0x8630
movl 0x20(%rsp), %ecx
movl 0x2a(%rsp), %edx
movl %edx, (%rax,%rcx,8)
jmp 0x2cf2
jmp 0x2f2c
leaq 0x33a6(%rip), %rax      # 0x60e8
cmpl $0x2, (%rax)
jne 0x2f2a
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
leaq 0x337a(%rip), %rcx      # 0x60d8
cmpl (%rcx), %eax
jae 0x2d92
movq 0x58c7(%rip), %rax      # 0x8630
movl 0x20(%rsp), %ecx
movw $0x0, 0x2(%rax,%rcx,8)
movq 0x58b5(%rip), %rax      # 0x8630
movl 0x20(%rsp), %ecx
movw $0x1, (%rax,%rcx,8)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x2d53
movl $0x0, 0x18(%rsp)
movl $0x1, 0x30(%rsp)
movl 0x30(%rsp), %eax
leaq 0x332b(%rip), %rcx      # 0x60d8
cmpl (%rcx), %eax
jae 0x2f28
movb 0x2f(%rsp), %al
movb %al, 0x2e(%rsp)
leaq 0x331c(%rip), %rax      # 0x60e0
movq (%rax), %rax
movl 0x30(%rsp), %ecx
movb (%rax,%rcx), %al
movb %al, 0x2f(%rsp)
movb 0x2e(%rsp), %cl
movb 0x2f(%rsp), %al
movl 0x30(%rsp), %edx
movzbl %cl, %edi
movzbl %al, %esi
callq 0x35e0
cmpl $0x0, %eax
jne 0x2e09
leaq 0x1680(%rip), %rdi      # 0x4475
movb $0x0, %al
callq 0x1040
movl $0x0, 0x34(%rsp)
jmp 0x2f66
cmpl $0x0, 0x18(%rsp)
je 0x2e20
movl 0x18(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x2f16
movl 0x30(%rsp), %edi
callq 0x2fe0
movl $0xfffffffe, 0xc(%rsp)  # imm = 0xFFFFFFFE
movl $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x20(%rsp), %eax
leaq 0x53e4(%rip), %rsi      # 0x8230
shlq $0x2, %rax
addq %rax, %rsi
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %edx
callq 0x25c0
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jne 0x2e84
leaq 0x1625(%rip), %rdi      # 0x4495
movb $0x0, %al
callq 0x1040
movl $0x0, 0x34(%rsp)
jmp 0x2f66
movl 0x20(%rsp), %eax
movl %eax, %ecx
leaq 0x539f(%rip), %rax      # 0x8230
movzwl (%rax,%rcx,4), %eax
shll $0x3, %eax
subl 0x1c(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0xc(%rsp), %eax
jle 0x2eba
movl 0x10(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x14(%rsp)
jmp 0x2ebc
movl 0x20(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rsp)
movl %eax, %eax
movl %eax, %ecx
leaq 0x535c(%rip), %rax      # 0x8230
cmpw $0x0, 0x2(%rax,%rcx,4)
jne 0x2e41
movq 0x5749(%rip), %rax      # 0x8630
movl 0x30(%rsp), %ecx
movl 0x14(%rsp), %edx
movl %edx, %esi
leaq 0x5338(%rip), %rdx      # 0x8230
movl (%rdx,%rsi,4), %edx
movl %edx, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
movl %eax, %ecx
leaq 0x5325(%rip), %rax      # 0x8230
movzwl (%rax,%rcx,4), %eax
subl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x2f18
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x2da2
jmp 0x2f2a
jmp 0x2f2c
callq 0x33f0
cmpl $0x0, %eax
jne 0x2f4c
leaq 0x1578(%rip), %rdi      # 0x44b5
movb $0x0, %al
callq 0x1040
movl $0x0, 0x34(%rsp)
jmp 0x2f64
leaq 0x157e(%rip), %rdi      # 0x44d3
movb $0x0, %al
callq 0x1040
movl $0x0, 0x34(%rsp)
jmp 0x2f66
callq 0x3520
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)