// File: /workspace/github_c_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movq %r8, 0x50(%rsp)
movq %rdx, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, 0x58(%rsp)
movaps 0x1177b(%rip), %xmm0    # 0x17030
leaq 0x3c(%rsp), %rax
movups %xmm0, (%rax)
xorl %edx, %edx
movb %dl, 0x10(%rax)
leaq 0x28(%rsp), %rax
movups %xmm0, (%rax)
movb %dl, 0x10(%rax)
leaq 0x14(%rsp), %rax
movups %xmm0, (%rax)
movb %dl, 0x10(%rax)
movl %ecx, 0x10(%rsp)
movslq %ecx, %r13
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
leaq 0xf0(%rsp), %rbx
xorl %r15d, %r15d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movq %r13, %rdx
callq 0x2150
leaq 0xb0(%rsp), %rbp
movq %rbp, %rdi
movq 0x60(%rsp), %rsi
movq %r13, %rdx
callq 0x2150
leaq 0x70(%rsp), %r12
movq %r12, %rdi
movq 0x68(%rsp), %rsi
movq %r13, %rdx
callq 0x2150
movaps 0x116f4(%rip), %xmm0    # 0x17030
movups %xmm0, 0x3c(%rsp)
xorl %eax, %eax
movb %al, 0x4c(%rsp)
movups %xmm0, 0x28(%rsp)
movb %al, 0x38(%rsp)
movups %xmm0, 0x14(%rsp)
movb %al, 0x24(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl %r13d, %ecx
movl %r15d, %r8d
callq 0x4daf
leaq 0x3c(%rsp), %rdi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x164b8
movl 0x40(%rsp), %edi
testl %edi, %edi
je 0x5996
callq 0x1678e
addl %eax, 0x3c(%rsp)
movl $0x0, 0x40(%rsp)
movl 0x3c(%rsp), %ebp
cmpl $-0x1, %r14d
setne %al
cmpl %ebp, %r14d
setl %cl
testb %cl, %al
jne 0x5a45
leaq 0x28(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movl 0x10(%rsp), %edx
callq 0x164b8
movl 0x2c(%rsp), %edi
testl %edi, %edi
je 0x59de
callq 0x1678e
addl %eax, 0x28(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $-0x1, %r14d
sete %al
addl 0x28(%rsp), %ebp
cmpl %ebp, %r14d
setge %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x5a45
leaq 0x14(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movl 0x10(%rsp), %edx
callq 0x164b8
movl 0x18(%rsp), %edi
testl %edi, %edi
je 0x5a22
callq 0x1678e
addl %eax, 0x14(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $-0x1, %r14d
sete %al
addl 0x14(%rsp), %ebp
cmpl %ebp, %r14d
setg %cl
orb %al, %cl
cmovnel %ebp, %r14d
movl 0xc(%rsp), %eax
cmovnel %r15d, %eax
movl %eax, 0xc(%rsp)
incl %r15d
cmpl $0x10, %r15d
jne 0x58f8
movq 0x50(%rsp), %rax
movl %r14d, (%rax)
movl 0xc(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq