// File: /workspace/github_c_makefile_star2_O2/enzo1982[P]smooth/misc/codecs.cpp
// Function: ProbeComp(int, int, int*, int*, int, int, bool, int&, int&)
int ProbeComp(int s, int e, int line[], int prevline[], int sx, int ctsize, bool nvRLE, int &rBits, int &rBitsSAA)
{
	int	 pcirle;
	int	 allinpal = 0;
	int	 gallinpal = 0;
	int	 lbits = 0;
	int	 mod04 = 0;
	int	 mod16 = 0;
	int	 comp = 0;
	int	 compsaa = 0;

	int	 oldpalette[388];
	int	 oldpalentries = palentries;

	for (int i = 0; i < 388; i++) oldpalette[i] = palette[i];

	for (int x = s; x < e; x++)
	{
		int	 previousColor = (x > 0 ? line[x - 1] : 0);

		if ((x % 16) == mod16)
		{
			gallinpal = 1;

			for (int x2 = 0; x2 < 16; x2++)
			{
				if ((x + x2) < sx) if (GetPaletteEntry(line[x + x2]) == -1 ) { gallinpal = 0; break; }
			}

			comp++;
		}

		if (((x % 4) == mod04) && !gallinpal)
		{
			allinpal = 1;

			for (int x2 = 0; x2 < 4; x2++)
			{
				if ((x + x2) < sx) if (GetPaletteEntry(line[x + x2]) == -1 ) { allinpal = 0; break; }
			}

			comp++;
		}

		if ((GetPaletteEntry(line[x]) == -1))
		{
			if (GetDelta(line, prevline, x, ctsize)) comp +=  9;
			else					 comp += 26;

			if (palentries < 338)
			{
				palette[palentries] = line[x];
				palentries++;
			}
			else
			{
				RotatePalette(line[x]);
			}
		}
		else
		{
			if (!allinpal && !gallinpal) comp++;

			compsaa++;

			int	 saa = 0;

			if (GetSAA(line, prevline, x)) saa = 1;

			if ((palentries - 1) - GetPaletteEntry(line[x]) < 2)
			{
				comp += 2;

				if (saa) compsaa -= 2;
			}
			else
			{
				if ((palentries - 1) - GetPaletteEntry(line[x]) < 18)
				{
					lbits = Math::Min(GetMinimumBits(palentries - 2), 4);

					comp += (lbits + 2);

					if (saa) compsaa -= (lbits + 2);
				}
				else
				{
					if ((palentries - 1) - GetPaletteEntry(line[x]) < 82)
					{
						lbits = Math::Min(GetMinimumBits(palentries - 18), 6);

						comp += (lbits + 3);

						if (saa) compsaa -= (lbits + 3);
					}
					else
					{
						comp += (GetMinimumBits(palentries - 83) + 3);

						if (saa) compsaa -= (GetMinimumBits(palentries - 83) + 3);
					}
				}
			}


			int	 paletteEntry = GetPaletteEntry(line[x]);

			if (paletteEntry >= 0) RotatePaletteEntry(paletteEntry);
		}

		if (line[x] == previousColor)
		{
			pcirle = GetRLE(line, sx, x, 8);

			if (pcirle >= 2)
			{
				if (pcirle > 9) comp += 10;
				else		comp += 5;

				x = x + pcirle;

				allinpal = 0;
				gallinpal = 0;

				mod04 = (x + 1) %  4;
				mod16 = (x + 1) % 16;
			}
			else
			{
				comp++;
			}
		}
		else if (line[x] == prevline[x] && !nvRLE)
		{
			pcirle = GetVRLE(line, prevline, sx, x, 8);

			if (pcirle >= 2)
			{
				if (pcirle > 9) comp += 10;
				else		comp += 5;

				x = x + pcirle;

				allinpal = 0;
				gallinpal = 0;

				mod04 = (x + 1) %  4;
				mod16 = (x + 1) % 16;
			}
			else
			{
				comp++;
			}
		}
	}

	palentries = oldpalentries;

	for (int j = 0; j < 388; j++) palette[j] = oldpalette[j];

	rBits	 = comp;
	rBitsSAA = comp + compsaa;

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x678, %rsp            # imm = 0x678
movl %r9d, 0x50(%rsp)
movl %r8d, 0x24(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %r12
movl %esi, 0x54(%rsp)
movl %edi, %r13d
movl 0xba48e(%rip), %eax     # 0x16ca30
movl %eax, 0x4c(%rsp)
xorl %eax, %eax
leaq 0xb9e71(%rip), %rdx     # 0x16c420
cmpq $0x610, %rax            # imm = 0x610
je 0xb25c4
movl (%rax,%rdx), %ecx
movl %ecx, 0x60(%rsp,%rax)
addq $0x4, %rax
jmp 0xb25af
movslq 0x24(%rsp), %r14
movq $0x0, 0x38(%rsp)
movl $0x0, 0x28(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
movq %r12, 0x40(%rsp)
cmpl 0x54(%rsp), %r13d
jge 0xb28f8
testl %r13d, %r13d
jle 0xb2618
movl %r13d, %eax
movl -0x4(%r12,%rax,4), %eax
movl %eax, 0x2c(%rsp)
jmp 0xb2620
movl $0x0, 0x2c(%rsp)
movl %r13d, %eax
cltd
pushq $0x10
popq %rcx
idivl %ecx
movslq %r13d, %r15
cmpl 0x28(%rsp), %edx
jne 0xb2679
movq %rbx, %rbp
leaq (%r12,%r15,4), %rbx
xorl %r12d, %r12d
cmpq $0x10, %r12
je 0xb265e
leaq (%r15,%r12), %rax
cmpq %r14, %rax
jge 0xb2659
movl (%rbx,%r12,4), %edi
callq 0xb294f
cmpl $-0x1, %eax
je 0xb2667
incq %r12
jmp 0xb263c
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
jmp 0xb266f
movl $0x0, 0x10(%rsp)
movq %rbp, %rbx
incl %ebx
movq 0x40(%rsp), %r12
movl %r13d, %eax
cltd
pushq $0x4
popq %rcx
idivl %ecx
xorl 0x38(%rsp), %edx
orl 0x10(%rsp), %edx
jne 0xb26d3
movq %rbx, %rbp
leaq (%r12,%r15,4), %rbx
xorl %r12d, %r12d
cmpq $0x4, %r12
je 0xb26b8
leaq (%r15,%r12), %rax
cmpq %r14, %rax
jge 0xb26b3
movl (%rbx,%r12,4), %edi
callq 0xb294f
cmpl $-0x1, %eax
je 0xb26c1
incq %r12
jmp 0xb2696
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
jmp 0xb26c9
movl $0x0, 0x14(%rsp)
movq %rbp, %rbx
incl %ebx
movq 0x40(%rsp), %r12
movq %r12, %rbp
movl (%r12,%r15,4), %r12d
movl %r12d, %edi
callq 0xb294f
cmpl $-0x1, %eax
je 0xb2728
xorl %esi, %esi
movl 0x14(%rsp), %ecx
orl 0x10(%rsp), %ecx
sete %r8b
movq 0x18(%rsp), %rdx
leal 0x1(%rdx), %ebp
movq 0x30(%rsp), %rcx
movl (%rcx,%r15,4), %ecx
movl 0xba324(%rip), %edi     # 0x16ca30
subl %edi, %eax
cmpl $-0x2, %eax
movq %rbx, 0x58(%rsp)
jl 0xb2775
decl %edx
cmpl %ecx, %r12d
cmovel %edx, %ebp
pushq $0x2
popq %rbx
jmp 0xb27ab
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movl %r13d, %edx
movl 0x50(%rsp), %ecx
callq 0xb297b
leal 0x9(%rbx), %ecx
addl $0x1a, %ebx
testl %eax, %eax
cmovnel %ecx, %ebx
movslq 0xba2e2(%rip), %rax     # 0x16ca30
movl (%rbp,%r15,4), %edi
cmpq $0x151, %rax            # imm = 0x151
movq %rbp, %r12
jg 0xb27b4
leaq 0xb9cbb(%rip), %rcx     # 0x16c420
movl %edi, (%rcx,%rax,4)
incl %eax
movl %eax, 0xba2c0(%rip)     # 0x16ca30
jmp 0xb2858
cmpl $-0x12, %eax
movl %ecx, 0x20(%rsp)
jb 0xb27be
addl $-0x2, %edi
movl %r8d, %ebx
callq 0xb34ff
movl %ebx, %r8d
xorl %esi, %esi
movl %eax, %ebx
cmpl $0x4, %eax
pushq $0x4
popq %rax
cmovgel %eax, %ebx
addl $0x2, %ebx
cmpl 0x20(%rsp), %r12d
movl $0x0, %eax
cmovel %ebx, %eax
subl %eax, %ebp
movl %ebp, %eax
movq %rax, 0x18(%rsp)
jmp 0xb2830
callq 0xb34bd
jmp 0xb2858
movb %r8b, 0xf(%rsp)
cmpl $-0x52, %eax
jb 0xb27ef
addl $-0x12, %edi
callq 0xb34ff
movl %eax, %ebx
cmpl $0x6, %eax
pushq $0x6
popq %rax
cmovgel %eax, %ebx
addl $0x3, %ebx
cmpl 0x20(%rsp), %r12d
movl $0x0, %eax
cmovel %ebx, %eax
subl %eax, %ebp
jmp 0xb2822
addl $-0x53, %edi
callq 0xb34ff
movl %eax, %ebx
addl $0x3, %ebx
cmpl 0x20(%rsp), %r12d
jne 0xb2822
movl 0xba227(%rip), %edi     # 0x16ca30
addl $-0x53, %edi
callq 0xb34ff
movq 0x18(%rsp), %rcx
subl %eax, %ecx
addl $-0x2, %ecx
movq %rcx, 0x18(%rsp)
jmp 0xb2829
movl %ebp, %eax
movq %rax, 0x18(%rsp)
xorl %esi, %esi
movb 0xf(%rsp), %r8b
movb %r8b, %sil
movq 0x58(%rsp), %rax
addl %esi, %eax
addl %ebx, %eax
movq %rax, %rbx
movq 0x40(%rsp), %r12
movl (%r12,%r15,4), %edi
callq 0xb294f
testl %eax, %eax
js 0xb2858
movl %eax, %edi
callq 0xb3541
movl (%r12,%r15,4), %eax
cmpl 0x2c(%rsp), %eax
jne 0xb28bf
movq %r12, %rdi
movl 0x24(%rsp), %esi
movl %r13d, %edx
pushq $0x8
popq %rcx
callq 0xb24b4
cmpl $0x2, %eax
jl 0xb28ee
xorl %ecx, %ecx
cmpl $0xa, %eax
setae %cl
leal (%rcx,%rcx,4), %ecx
leal 0x5(%rbx,%rcx), %ebx
leal 0x1(%rax,%r13), %ecx
leal (%rax,%r13), %esi
movl %ecx, %eax
cltd
pushq $0x4
popq %rdi
idivl %edi
movq %rdx, 0x38(%rsp)
movl %ecx, %eax
cltd
pushq $0x10
popq %rcx
idivl %ecx
movl %edx, 0x28(%rsp)
movl $0x0, 0x10(%rsp)
movl %esi, %r13d
movl $0x0, 0x14(%rsp)
jmp 0xb28f0
movq 0x30(%rsp), %rcx
cmpl (%rcx,%r15,4), %eax
jne 0xb28f0
cmpb $0x0, 0x6b0(%rsp)
jne 0xb28f0
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movl 0x24(%rsp), %edx
movl %r13d, %ecx
pushq $0x8
popq %r8
callq 0xb3574
jmp 0xb2874
incl %ebx
incl %r13d
jmp 0xb25fa
movl 0x4c(%rsp), %eax
movl %eax, 0xba12e(%rip)     # 0x16ca30
xorl %eax, %eax
leaq 0xb9b15(%rip), %rdx     # 0x16c420
movq 0x18(%rsp), %rsi
cmpq $0x610, %rax            # imm = 0x610
je 0xb2925
movl 0x60(%rsp,%rax), %ecx
movl %ecx, (%rax,%rdx)
addq $0x4, %rax
jmp 0xb2910
movq 0x6b8(%rsp), %rax
movl %ebx, (%rax)
addl %ebx, %esi
movq 0x6c0(%rsp), %rax
movl %esi, (%rax)
xorl %eax, %eax
addq $0x678, %rsp            # imm = 0x678
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq