// File: /workspace/github_c_makefile_star2_O1/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, 0x54(%rsp)
movl %r8d, 0x24(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %rbx
movl %esi, %r14d
movl %edi, %ebp
movl 0xbec1a(%rip), %eax     # 0x177870
movl %eax, 0x50(%rsp)
leaq 0xbe5ff(%rip), %r13     # 0x177260
leaq 0x60(%rsp), %rdi
movl $0x610, %edx            # imm = 0x610
movq %r13, %rsi
callq 0x552d0
xorl %r8d, %r8d
movl $0x0, %eax
movq %rax, 0x10(%rsp)
movl %r14d, 0x58(%rsp)
cmpl %r14d, %ebp
jge 0xb9133
movslq 0x24(%rsp), %r12
xorl %r14d, %r14d
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
xorl %r8d, %r8d
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movq %rbx, 0x48(%rsp)
movq %r12, 0x38(%rsp)
movl $0x0, %eax
testl %ebp, %ebp
jle 0xb8cce
movl %ebp, %eax
movl -0x4(%rbx,%rax,4), %eax
movl %eax, 0x5c(%rsp)
leal 0xf(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
andl $-0x10, %eax
movl %ebp, %ecx
subl %eax, %ecx
movslq %ebp, %r10
cmpl 0x2c(%rsp), %ecx
jne 0xb8d2b
movl 0xbeb80(%rip), %eax     # 0x177870
xorl %ecx, %ecx
leaq (%rcx,%r10), %rdx
cmpq %r12, %rdx
jge 0xb8d19
movl (%rbx,%rdx,4), %esi
movq %rax, %rdx
xorl %r15d, %r15d
testl %edx, %edx
jle 0xb8d28
decq %rdx
movl %edx, %edi
cmpl %esi, (%r13,%rdi,4)
jne 0xb8d01
cmpl $-0x1, %edx
je 0xb8d28
incq %rcx
cmpq $0x10, %rcx
jne 0xb8cf2
movl $0x1, %r15d
incl %r8d
leal 0x3(%rbp), %eax
testl %ebp, %ebp
cmovnsl %ebp, %eax
andl $-0x4, %eax
movl %ebp, %ecx
subl %eax, %ecx
xorl 0x28(%rsp), %ecx
orl %r15d, %ecx
jne 0xb8d84
movl 0xbeb27(%rip), %eax     # 0x177870
xorl %ecx, %ecx
leaq (%rcx,%r10), %rdx
cmpq %r12, %rdx
jge 0xb8d72
movl (%rbx,%rdx,4), %esi
movq %rax, %rdx
xorl %r14d, %r14d
testl %edx, %edx
jle 0xb8d81
decq %rdx
movl %edx, %edi
cmpl %esi, (%r13,%rdi,4)
jne 0xb8d5a
cmpl $-0x1, %edx
je 0xb8d81
incq %rcx
cmpq $0x4, %rcx
jne 0xb8d4b
movl $0x1, %r14d
incl %r8d
movl (%rbx,%r10,4), %r12d
movl 0xbeae2(%rip), %eax     # 0x177870
movq %rax, %rcx
testl %ecx, %ecx
jle 0xb8daa
decq %rcx
movl %ecx, %edx
cmpl %r12d, (%r13,%rdx,4)
jne 0xb8d91
cmpl $-0x1, %ecx
jne 0xb8ed4
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movq %rbp, 0x8(%rsp)
movl %ebp, %edx
movl 0x54(%rsp), %ecx
movq %r8, %r12
movq %r10, %rbp
callq 0xb91af
movq %r12, %rdx
leal 0x9(%r12), %ecx
addl $0x1a, %edx
testl %eax, %eax
cmovnel %ecx, %edx
movq %rbx, %rax
movslq 0xbea8e(%rip), %rbx     # 0x177870
movl (%rax,%rbp,4), %r12d
cmpq $0x151, %rbx            # imm = 0x151
movq %rdx, (%rsp)
jg 0xb8e06
movq %rbp, %r10
movl %r12d, (%r13,%rbx,4)
leal 0x1(%rbx), %eax
movl %eax, 0xbea6c(%rip)     # 0x177870
jmp 0xb8e24
leal -0x1(%rbx), %edx
shlq $0x2, %rdx
movq %r13, %rdi
leaq 0xbe44d(%rip), %rsi     # 0x177264
callq 0x556d0
movq %rbp, %r10
movl %r12d, -0x4(%r13,%rbx,4)
movq 0x48(%rsp), %rbx
movq 0x38(%rsp), %r12
movq 0x8(%rsp), %rbp
movl (%rbx,%r10,4), %eax
cmpl 0x5c(%rsp), %eax
jne 0xb8ea1
movq %rbx, %rdi
movl 0x24(%rsp), %esi
movl %ebp, %edx
movl $0x8, %ecx
callq 0xb8b49
cmpl $0x2, %eax
jl 0xb8ebc
xorl %ecx, %ecx
cmpl $0xa, %eax
setge %cl
leal (%rcx,%rcx,4), %ecx
movq (%rsp), %r8
leal 0x5(%r8,%rcx), %r8d
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, 0x28(%rsp)
andl $-0x10, %edx
subl %edx, %esi
movl %esi, 0x2c(%rsp)
xorl %r15d, %r15d
movl %eax, %ebp
xorl %r14d, %r14d
jmp 0xb8ec3
movq 0x40(%rsp), %rcx
cmpl (%rcx,%r10,4), %eax
jne 0xb8eb6
cmpb $0x0, 0x6b0(%rsp)
je 0xb8f11
movq (%rsp), %r8
jmp 0xb8ec3
movq (%rsp), %r8
incl %r8d
incl %ebp
cmpl 0x58(%rsp), %ebp
jl 0xb8cbf
jmp 0xb9133
xorl %ecx, %ecx
movl %r14d, %edx
orl %r15d, %edx
sete %cl
addl %ecx, %r8d
movq 0x10(%rsp), %r9
leal 0x1(%r9), %r11d
movq 0x40(%rsp), %rcx
movl (%rcx,%r10,4), %ebx
leaq -0x1(%rax), %rcx
movq %rcx, %rdx
movl %eax, %esi
testl %esi, %esi
jle 0xb8f2f
movl %edx, %edi
decl %esi
decq %rdx
cmpl %r12d, (%r13,%rdi,4)
jne 0xb8efd
jmp 0xb8f34
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movl 0x24(%rsp), %edx
movl %ebp, %ecx
movl $0x8, %r8d
callq 0xba056
jmp 0xb8e50
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
subl %eax, %esi
movq %rcx, %rdi
movl %eax, %edx
cmpl $-0x3, %esi
jle 0xb8f5c
decl %r9d
cmpl %ebx, %r12d
cmovel %r9d, %r11d
movl $0x2, %ebx
movl %r11d, %eax
movq %rax, 0x10(%rsp)
jmp 0xb904d
testl %edx, %edx
jle 0xb8f70
movl %edi, %esi
decl %edx
decq %rdi
cmpl %r12d, (%r13,%rsi,4)
jne 0xb8f5c
jmp 0xb8f75
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
subl %eax, %edx
movl %eax, %esi
cmpl $-0x13, %edx
movq %r8, (%rsp)
movq %r10, 0x30(%rsp)
movl %r11d, 0x20(%rsp)
movl %ebx, 0x1c(%rsp)
jle 0xb8fcc
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 0xb8fbc
incl %ebx
cmpl $0x21, %ebx
jne 0xb8f9f
xorl %ebx, %ebx
cmpl $0x4, %ebx
movl $0x4, %eax
cmovgel %eax, %ebx
addl $0x2, %ebx
jmp 0xb902a
testl %esi, %esi
jle 0xb8fe0
movl %ecx, %edx
decl %esi
decq %rcx
cmpl %r12d, (%r13,%rdx,4)
jne 0xb8fcc
jmp 0xb8fe5
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
subl %eax, %esi
cmpl $-0x52, %esi
jl 0xb90aa
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 0xb901c
incl %ebx
cmpl $0x21, %ebx
jne 0xb8fff
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 0x20(%rsp), %ecx
subl %eax, %ecx
movl %ecx, %eax
movq %rax, 0x10(%rsp)
movq (%rsp), %r8
movq 0x30(%rsp), %r10
addl %ebx, %r8d
movq 0x48(%rsp), %rbx
movl (%rbx,%r10,4), %eax
movl 0xbe811(%rip), %edi     # 0x177870
movq %rdi, %rcx
decq %rcx
movq 0x38(%rsp), %r12
testl %edi, %edi
jle 0xb907e
movl %ecx, %edx
decl %edi
decq %rcx
cmpl %eax, (%r13,%rdx,4)
jne 0xb906a
jmp 0xb9083
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r8, (%rsp)
testl %edi, %edi
js 0xb8e33
movq %rbp, %r12
movq %r10, %rbp
callq 0xba01f
movq %rbp, %r10
movq %r12, %rbp
movq 0x38(%rsp), %r12
jmp 0xb8e33
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 0xb90d6
incl %ebx
cmpl $0x21, %ebx
jne 0xb90b9
xorl %ebx, %ebx
addl $0x3, %ebx
cmpl 0x1c(%rsp), %r12d
movl 0x20(%rsp), %eax
jne 0xb912e
movl 0xbe786(%rip), %eax     # 0x177870
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 0xb911e
incl %r12d
cmpl $0x21, %r12d
jne 0xb90fd
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
subl %r12d, %eax
addl $-0x2, %eax
jmp 0xb903f
jmp 0xb903f
movl 0x50(%rsp), %eax
movl %eax, 0xbe733(%rip)     # 0x177870
leaq 0xbe11c(%rip), %rdi     # 0x177260
leaq 0x60(%rsp), %rsi
movl $0x610, %edx            # imm = 0x610
movq %r8, %rbx
callq 0x552d0
movq 0x6b8(%rsp), %rax
movl %ebx, (%rax)
movq 0x10(%rsp), %rcx
addl %ebx, %ecx
movq 0x6c0(%rsp), %rax
movl %ecx, (%rax)
xorl %eax, %eax
addq $0x678, %rsp            # imm = 0x678
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq