// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x30(%rbp) # imm = 0xFFFFFFFF
leaq -0x104(%rbp), %rdi
callq 0x1f2a0
leaq -0x118(%rbp), %rdi
callq 0x1f2a0
jmp 0x6a56
leaq -0x138(%rbp), %rdi
callq 0x1f2a0
jmp 0x6a64
movl $0x0, -0x13c(%rbp)
cmpl $0x10, -0x13c(%rbp)
jge 0x6c6d
movq -0x8(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x21b0
movq -0x10(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
callq 0x21b0
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
leaq -0xf0(%rbp), %rdi
callq 0x21b0
leaq -0x104(%rbp), %rdi
callq 0x1f2c0
jmp 0x6ac2
leaq -0x118(%rbp), %rdi
callq 0x1f2c0
jmp 0x6ad0
leaq -0x138(%rbp), %rdi
callq 0x1f2c0
jmp 0x6ade
movl -0x1c(%rbp), %ecx
movl -0x13c(%rbp), %r8d
leaq -0x70(%rbp), %rdi
movq %rdi, -0x148(%rbp)
leaq -0xb0(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
callq 0x4c50
movq -0x148(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq -0x104(%rbp), %rdi
callq 0x1f2f0
jmp 0x6b1e
leaq -0x104(%rbp), %rdi
callq 0x1f4c0
movl %eax, -0x14c(%rbp)
jmp 0x6b32
movl -0x14c(%rbp), %eax
movl %eax, -0x140(%rbp)
cmpl $-0x1, -0x2c(%rbp)
je 0x6ba5
movl -0x2c(%rbp), %eax
cmpl -0x140(%rbp), %eax
jge 0x6ba5
jmp 0x6c59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x6cbe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
jmp 0x6cb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x120(%rbp)
movl %eax, -0x124(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x1f510
jmp 0x6cb2
movl -0x1c(%rbp), %edx
leaq -0x118(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x1f2f0
jmp 0x6bbd
leaq -0x118(%rbp), %rdi
callq 0x1f4c0
movl %eax, -0x150(%rbp)
jmp 0x6bd1
movl -0x150(%rbp), %eax
addl -0x140(%rbp), %eax
movl %eax, -0x140(%rbp)
cmpl $-0x1, -0x2c(%rbp)
je 0x6bf6
movl -0x2c(%rbp), %eax
cmpl -0x140(%rbp), %eax
jge 0x6bf6
jmp 0x6c59
movl -0x1c(%rbp), %edx
leaq -0x138(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x1f2f0
jmp 0x6c0e
leaq -0x138(%rbp), %rdi
callq 0x1f4c0
movl %eax, -0x154(%rbp)
jmp 0x6c22
movl -0x154(%rbp), %eax
addl -0x140(%rbp), %eax
movl %eax, -0x140(%rbp)
cmpl $-0x1, -0x2c(%rbp)
je 0x6c45
movl -0x2c(%rbp), %eax
cmpl -0x140(%rbp), %eax
jle 0x6c57
movl -0x140(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x13c(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x6c59
movl -0x13c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x13c(%rbp)
jmp 0x6a6e
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x30(%rbp), %eax
movl %eax, -0x158(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x1f510
leaq -0x118(%rbp), %rdi
callq 0x1f510
leaq -0x104(%rbp), %rdi
callq 0x1f510
movl -0x158(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x118(%rbp), %rdi
callq 0x1f510
leaq -0x104(%rbp), %rdi
callq 0x1f510
movq -0x120(%rbp), %rdi
callq 0x23e0
nopw %cs:(%rax,%rax)