// File: /workspace/github_c_makefile_star2_O3/zhiyb[P]png2tlg/png2tlg.cpp
// Function: DetectColorFilter(char*, char*, char*, int, int&)
int DetectColorFilter(char *b, char *g, char *r, int size, int &outsize)
{
#ifndef FILTER_TEST
	int minbits = -1;
	int mincode = -1;

	char bbuf[H_BLOCK_SIZE*W_BLOCK_SIZE];
	char gbuf[H_BLOCK_SIZE*W_BLOCK_SIZE];
	char rbuf[H_BLOCK_SIZE*W_BLOCK_SIZE];
	TryCompressGolomb bc, gc, rc;

	for(int code = 0; code < FILTER_TRY_COUNT; code++)   // 17..27 are currently not used
	{
		// copy bbuf, gbuf, rbuf into b, g, r.
		memcpy(bbuf, b, sizeof(char)*size);
		memcpy(gbuf, g, sizeof(char)*size);
		memcpy(rbuf, r, sizeof(char)*size);

		// copy compressor
		bc.Reset();
		gc.Reset();
		rc.Reset();

		// Apply color filter
		ApplyColorFilter(bbuf, gbuf, rbuf, size, code);

		// try to compress
		int bits;
		bits  = (bc.Try(bbuf, size), bc.Flush());
		if(minbits != -1 && minbits < bits) continue;
		bits += (gc.Try(gbuf, size), gc.Flush());
		if(minbits != -1 && minbits < bits) continue;
		bits += (rc.Try(rbuf, size), rc.Flush());

		if(minbits == -1 || minbits > bits)
		{
			minbits = bits, mincode = code;
		}
	}

	outsize = minbits;

	return mincode;
#else
	static int filter = 0;

	int f = filter;

	filter++;
	if(filter >= FILTER_TRY_COUNT) filter = 0;

	outsize = 0;

	return f;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r8, 0x78(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, %r12
movq %rdi, 0x88(%rsp)
movl %ecx, 0xc(%rsp)
movslq %ecx, %rbx
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r12, 0x70(%rsp)
movl %esi, 0x8(%rsp)
leaq 0x110(%rsp), %r14
movq %r14, %rdi
movq 0x88(%rsp), %rsi
movq %rbx, %rdx
callq 0x2140
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rbp
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2140
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq 0x80(%rsp), %rsi
movq %rbx, %rdx
callq 0x2140
movaps 0x125e1(%rip), %xmm0    # 0x18030
movaps %xmm0, 0x50(%rsp)
xorl %eax, %eax
movb %al, 0x60(%rsp)
movaps %xmm0, 0x30(%rsp)
movb %al, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
movb %al, 0x20(%rsp)
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movl %ebx, %ecx
movl %r15d, %r8d
callq 0x4efb
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x16cbe
movl 0x54(%rsp), %edi
testl %edi, %edi
je 0x5ab2
callq 0x16f98
movl %eax, %r14d
addl 0x50(%rsp), %r14d
movl %r14d, 0x50(%rsp)
movl $0x0, 0x54(%rsp)
jmp 0x5ab7
movl 0x50(%rsp), %r14d
movl 0x8(%rsp), %esi
cmpl $-0x1, %esi
je 0x5acd
cmpl %r14d, %esi
jge 0x5acd
movq %rbp, %r12
jmp 0x5b7e
leaq 0x30(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x16cbe
movl 0x34(%rsp), %edi
testl %edi, %edi
je 0x5b07
callq 0x16f98
movl %eax, %r13d
addl 0x30(%rsp), %r13d
movl %r13d, 0x30(%rsp)
movl $0x0, 0x34(%rsp)
jmp 0x5b0c
movl 0x30(%rsp), %r13d
addl %r14d, %r13d
movl 0x8(%rsp), %esi
cmpl $-0x1, %esi
je 0x5b1d
cmpl %r13d, %esi
jl 0x5b79
leaq 0x10(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x16cbe
movl 0x14(%rsp), %edi
testl %edi, %edi
je 0x5b52
callq 0x16f98
addl 0x10(%rsp), %eax
movl %eax, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
jmp 0x5b56
movl 0x10(%rsp), %eax
movl 0x8(%rsp), %esi
cmpl $-0x1, %esi
sete %cl
addl %r13d, %eax
cmpl %eax, %esi
setg %dl
orb %cl, %dl
cmovnel %eax, %esi
movl 0x4(%rsp), %eax
cmovnel %r15d, %eax
movl %eax, 0x4(%rsp)
movq 0x70(%rsp), %r12
incl %r15d
cmpl $0x10, %r15d
jne 0x59f5
movq 0x78(%rsp), %rax
movl %esi, (%rax)
movl 0x4(%rsp), %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq