// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %rdx, %rbp
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movl %ecx, 0xc(%rsp)
movslq %ecx, %r13
pushq $-0x1
popq %rax
leaq 0x90(%rsp), %r15
movl %eax, 0x8(%rsp)
movl %eax, 0x4(%rsp)
xorl %r12d, %r12d
movq %rdx, 0x10(%rsp)
cmpl $0x10, %r12d
je 0x5456
leaq 0x110(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x2130
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
callq 0x2130
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x2130
movaps 0xacda(%rip), %xmm0     # 0x10030
movaps %xmm0, 0x70(%rsp)
xorl %eax, %eax
movb %al, 0x80(%rsp)
movaps %xmm0, 0x50(%rsp)
movb %al, 0x60(%rsp)
movaps %xmm0, 0x30(%rsp)
movb %al, 0x40(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movl %r13d, %ecx
movl %r12d, %r8d
callq 0x48ff
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0xf22e
movq %rbx, %rdi
callq 0xf384
movl %eax, %r14d
movl 0x8(%rsp), %edx
cmpl $-0x1, %edx
setne %al
cmpl %r14d, %edx
setl %cl
testb %cl, %al
jne 0x544e
cmpl $-0x1, %edx
sete 0x3(%rsp)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movl 0xc(%rsp), %r15d
movl %edx, %ebp
movl %r15d, %edx
callq 0xf22e
movq %rbx, %rdi
callq 0xf384
movl %r15d, %edx
movl %eax, %r15d
addl %r14d, %r15d
cmpl %r15d, %ebp
setge %al
orb 0x3(%rsp), %al
cmpb $0x1, %al
jne 0x5441
cmpl $-0x1, %ebp
sete %bl
leaq 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0xd0(%rsp), %rsi
callq 0xf22e
movq %r14, %rdi
callq 0xf384
addl %r15d, %eax
cmpl %eax, %ebp
setg %cl
orb %bl, %cl
movl %r12d, %ecx
jne 0x5439
movl %ebp, %eax
movl 0x4(%rsp), %ecx
movl %eax, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
movq 0x10(%rsp), %rbp
leaq 0x90(%rsp), %r15
incl %r12d
jmp 0x5307
movq 0x18(%rsp), %rax
movl 0x8(%rsp), %ecx
movl %ecx, (%rax)
movl 0x4(%rsp), %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq