// File: /workspace/github_c_makefile_star2_O3/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 $0x688, %rsp            # imm = 0x688
movl %r9d, 0x60(%rsp)
movl %r8d, 0x2c(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, %rbx
movl %esi, %r14d
movl %edi, %r15d
movl 0xbea7e(%rip), %eax     # 0x176870
movl %eax, 0x5c(%rsp)
leaq 0xbe463(%rip), %r13     # 0x176260
leaq 0x70(%rsp), %rdi
movl $0x610, %edx            # imm = 0x610
movq %r13, %rsi
callq 0x552d0
movq %r15, %r9
xorl %r12d, %r12d
movl $0x0, %eax
movq %rax, 0x10(%rsp)
movl %r14d, 0x64(%rsp)
cmpl %r14d, %r9d
jge 0xb8309
movslq 0x2c(%rsp), %rax
movq %rax, 0x68(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
movq %rbx, 0x50(%rsp)
movl $0x0, %ebp
testl %r9d, %r9d
jle 0xb7e6f
movl %r9d, %eax
movl -0x4(%rbx,%rax,4), %ebp
leal 0xf(%r9), %eax
testl %r9d, %r9d
cmovnsl %r9d, %eax
andl $-0x10, %eax
movl %r9d, %ecx
subl %eax, %ecx
movl 0xbe9e8(%rip), %eax     # 0x176870
cmpl 0x34(%rsp), %ecx
jne 0xb7ed0
movslq %r9d, %rcx
xorl %edx, %edx
leaq (%rdx,%rcx), %rsi
cmpq 0x68(%rsp), %rsi
jge 0xb7ebe
movl (%rbx,%rsi,4), %esi
movq %rax, %rdi
xorl %r15d, %r15d
testl %edi, %edi
jle 0xb7ecd
decq %rdi
movl %edi, %r8d
cmpl %esi, (%r13,%r8,4)
jne 0xb7ea4
cmpl $-0x1, %r8d
je 0xb7ecd
incq %rdx
cmpq $0x10, %rdx
jne 0xb7e93
movl $0x1, %r15d
incl %r12d
movslq %r9d, %r11
leal 0x3(%r9), %ecx
testl %r9d, %r9d
cmovnsl %r9d, %ecx
andl $-0x4, %ecx
movl %r9d, %edx
subl %ecx, %edx
xorl 0x30(%rsp), %edx
orl %r15d, %edx
jne 0xb7f2c
xorl %ecx, %ecx
leaq (%rcx,%r11), %rdx
cmpq 0x68(%rsp), %rdx
jge 0xb7f1a
movl (%rbx,%rdx,4), %edx
movq %rax, %rsi
xorl %r14d, %r14d
testl %esi, %esi
jle 0xb7f29
decq %rsi
movl %esi, %edi
cmpl %edx, (%r13,%rdi,4)
jne 0xb7f02
cmpl $-0x1, %edi
je 0xb7f29
incq %rcx
cmpq $0x4, %rcx
jne 0xb7ef1
movl $0x1, %r14d
incl %r12d
movq %r12, 0x38(%rsp)
movl (%rbx,%r11,4), %r12d
movq %rax, %rcx
testl %ecx, %ecx
jle 0xb7f51
decq %rcx
movl %ecx, %edx
cmpl %r12d, (%r13,%rdx,4)
jne 0xb7f38
cmpl $-0x1, %edx
jne 0xb80b0
movl %ebp, 0x8(%rsp)
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
movq %r9, 0x20(%rsp)
movl %r9d, %edx
movl 0x60(%rsp), %ecx
movq %r11, %r13
callq 0xb8384
movq 0x38(%rsp), %rbp
leal 0x9(%rbp), %ecx
addl $0x1a, %ebp
testl %eax, %eax
cmovnel %ecx, %ebp
movq %rbx, %rax
movslq 0xbe8e5(%rip), %rbx     # 0x176870
movl (%rax,%r13,4), %r12d
cmpq $0x151, %rbx            # imm = 0x151
jg 0xb7fb2
movq %r13, %r11
leaq 0xbe2be(%rip), %r13     # 0x176260
movl %r12d, (%r13,%rbx,4)
leal 0x1(%rbx), %eax
movl %eax, 0xbe8c0(%rip)     # 0x176870
jmp 0xb7fde
decl %ebx
leaq (,%rbx,4), %rdx
leaq 0xbe29d(%rip), %rdi     # 0x176260
leaq 0xbe29a(%rip), %rsi     # 0x176264
callq 0x556d0
movq %r13, %r11
leaq 0xbe287(%rip), %r13     # 0x176260
movl %r12d, (%r13,%rbx,4)
movq 0x50(%rsp), %rbx
movq %rbp, %r12
movq 0x20(%rsp), %r9
movl 0x8(%rsp), %ebp
movl (%rbx,%r11,4), %eax
cmpl %ebp, %eax
jne 0xb8060
movq %rbx, %rdi
movl 0x2c(%rsp), %esi
movl %r9d, %edx
movl $0x8, %ecx
movq %r9, %rbp
callq 0xb7ce7
cmpl $0x2, %eax
jl 0xb8097
xorl %ecx, %ecx
cmpl $0xa, %eax
setae %cl
leal (%rcx,%rcx,4), %ecx
leal 0x5(%r12,%rcx), %r12d
leal 0x1(%rax,%rbp), %esi
leal (%rax,%rbp), %ecx
addl $0x4, %ecx
leal (%rax,%rbp), %edx
addl $0x10, %edx
addl %ebp, %eax
testl %esi, %esi
cmovnsl %esi, %ecx
cmovnsl %esi, %edx
andl $-0x4, %ecx
movl %esi, %edi
subl %ecx, %edi
movl %edi, 0x30(%rsp)
andl $-0x10, %edx
subl %edx, %esi
movl %esi, 0x34(%rsp)
xorl %r15d, %r15d
movl %eax, %r9d
xorl %r14d, %r14d
jmp 0xb809d
movq 0x48(%rsp), %rcx
cmpl (%rcx,%r11,4), %eax
jne 0xb809d
cmpb $0x0, 0x6c0(%rsp)
jne 0xb809d
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
movl 0x2c(%rsp), %edx
movl %r9d, %ecx
movl $0x8, %r8d
movq %r9, %rbp
callq 0xb922b
jmp 0xb800e
incl %r12d
movq %rbp, %r9
incl %r9d
cmpl 0x64(%rsp), %r9d
jl 0xb7e5e
jmp 0xb8309
xorl %ecx, %ecx
movl %r14d, %edx
orl %r15d, %edx
sete %cl
movq 0x38(%rsp), %rdx
addl %ecx, %edx
movq %rdx, 0x38(%rsp)
movq 0x10(%rsp), %r10
leal 0x1(%r10), %ebx
movq 0x48(%rsp), %rcx
movl (%rcx,%r11,4), %ecx
movl %ecx, 0x1c(%rsp)
movl %eax, %ecx
movq %rax, %rdx
decq %rdx
movq %rdx, %rdi
movl %eax, %esi
testl %esi, %esi
jle 0xb80ff
movl %edi, %r8d
decl %esi
decq %rdi
cmpl %r12d, (%r13,%r8,4)
jne 0xb80ea
jmp 0xb8104
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
subl %eax, %esi
movq %rdx, %r8
movl %ecx, %edi
cmpl $-0x3, %esi
jle 0xb812d
decl %r10d
cmpl 0x1c(%rsp), %r12d
cmovel %r10d, %ebx
movl %ebx, %eax
movl $0x2, %ebx
movq %rax, 0x10(%rsp)
jmp 0xb821a
testl %edi, %edi
jle 0xb8142
movl %r8d, %esi
decl %edi
decq %r8
cmpl %r12d, (%r13,%rsi,4)
jne 0xb812d
jmp 0xb8147
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
subl %eax, %edi
cmpl $-0x13, %edi
movq %r9, 0x20(%rsp)
movq %r11, 0x40(%rsp)
movl %ebx, 0x28(%rsp)
jle 0xb8198
addl $-0x2, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebx, %esi
callq 0x56010
ucomisd 0x8(%rsp), %xmm0
ja 0xb8188
incl %ebx
cmpl $0x21, %ebx
jne 0xb816b
xorl %ebx, %ebx
cmpl $0x4, %ebx
movl $0x4, %eax
cmovgel %eax, %ebx
addl $0x2, %ebx
jmp 0xb81f6
testl %ecx, %ecx
jle 0xb81ac
movl %edx, %esi
decl %ecx
decq %rdx
cmpl %r12d, (%r13,%rsi,4)
jne 0xb8198
jmp 0xb81b1
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
subl %eax, %ecx
cmpl $-0x52, %ecx
jl 0xb8276
addl $-0x12, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebx, %esi
callq 0x56010
ucomisd 0x8(%rsp), %xmm0
ja 0xb81e8
incl %ebx
cmpl $0x21, %ebx
jne 0xb81cb
xorl %ebx, %ebx
cmpl $0x6, %ebx
movl $0x6, %eax
cmovgel %eax, %ebx
addl $0x3, %ebx
cmpl 0x1c(%rsp), %r12d
movl $0x0, %eax
cmovel %ebx, %eax
movl 0x28(%rsp), %ecx
subl %eax, %ecx
movl %ecx, %eax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %r9
movq 0x40(%rsp), %r11
movq 0x38(%rsp), %r12
addl %ebx, %r12d
movq 0x50(%rsp), %rbx
movl (%rbx,%r11,4), %eax
movl 0xbe63f(%rip), %edi     # 0x176870
movq %rdi, %rcx
decq %rcx
testl %edi, %edi
jle 0xb7fef
movl %ecx, %edx
decl %edi
decq %rcx
cmpl %eax, (%r13,%rdx,4)
jne 0xb8237
testl %edi, %edi
js 0xb7fef
movl %ebp, 0x8(%rsp)
movq %r9, %rbp
movq %r11, %r13
callq 0xb91f4
movq %r13, %r11
leaq 0xbdff2(%rip), %r13     # 0x176260
movq %rbp, %r9
jmp 0xb7feb
addl $-0x53, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebx, %esi
callq 0x56010
ucomisd 0x8(%rsp), %xmm0
ja 0xb82a2
incl %ebx
cmpl $0x21, %ebx
jne 0xb8285
xorl %ebx, %ebx
movq 0x20(%rsp), %r9
addl $0x3, %ebx
cmpl 0x1c(%rsp), %r12d
jne 0xb82fb
movl 0xbe5b9(%rip), %eax     # 0x176870
addl $-0x53, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
movl $0x2, %edi
movl %r12d, %esi
callq 0x56010
ucomisd 0x8(%rsp), %xmm0
ja 0xb82eb
incl %r12d
cmpl $0x21, %r12d
jne 0xb82ca
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
subl %r12d, %eax
addl $-0x2, %eax
jmp 0xb820b
movl 0x28(%rsp), %eax
movq %rax, 0x10(%rsp)
jmp 0xb8215
movl 0x5c(%rsp), %eax
movl %eax, 0xbe55d(%rip)     # 0x176870
leaq 0xbdf46(%rip), %rdi     # 0x176260
leaq 0x70(%rsp), %rsi
movl $0x610, %edx            # imm = 0x610
callq 0x552d0
movq 0x6c8(%rsp), %rax
movl %r12d, (%rax)
movq 0x10(%rsp), %rcx
addl %r12d, %ecx
movq 0x6d0(%rsp), %rax
movl %ecx, (%rax)
xorl %eax, %eax
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq