// File: /workspace/github_c_makefile_star2_O3/ravenexp[P]aatrace/search.c
// Function: aatrace_search_tile
int aatrace_search_tile(const struct aatrace_pic* src,
			const struct aatrace_font* font,
			int tl, int tc,
			const struct aatrace_match_ctx* m_ctx,
			const struct aatrace_search_ctx* ctx)
{
	unsigned char tilebuf[FH][FW];
	struct match_result r, best_r = {0, -1};
	int dx, dy;
	int xr, yr;

	switch (ctx->method) {
	case AATRACE_SEARCH_METHOD_DIAMOND:
	case AATRACE_SEARCH_METHOD_BOX:
		xr = yr = 1;
		break;

	case AATRACE_SEARCH_METHOD_AREA:
		xr = ctx->hrange;
		yr = ctx->vrange;
		break;

	case AATRACE_SEARCH_METHOD_NONE:
	default:
		xr = yr = 0;
	}

	for (dy = -yr; dy <= yr; dy++)
		for (dx = -xr; dx <= xr; dx++) {
			if (tc*FW + dx < 0 || (tc + 1)*FW + dx > src->w)
				continue;
			if (tl*FH + dy < 0 || (tl + 1)*FH + dy > src->h)
				continue;

			if (ctx->method == AATRACE_SEARCH_METHOD_DIAMOND &&
			    dx*dy != 0)
				continue;

			aatrace_bitblt(&tilebuf[0][0], src->buf,
				       FW, src->ll,
				       0, 0, tc*FW + dx, tl*FH + dy,
				       FW, FH);

			r = aatrace_match_tile(&tilebuf[0][0], font->pic.buf,
					       font->nchars, m_ctx);

			if (ctx->flags & AATRACE_SEARCH_FLAG_COVERAGE) {
				r.score += aatrace_cover_tile_score(src, font, tl, tc, dx, dy, m_ctx);
			}

			if (r.score < best_r.score)
				best_r = r;

			if (best_r.score == 0)
				break;
		}

	return best_r.ch;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %r8, %r10
movq %rsi, %r11
movq %rdi, %r13
movl (%r9), %eax
movl $0x1, %edi
leal -0x2(%rax), %esi
movl $0x1, %ebx
cmpl $0x2, %esi
jb 0x20e1
cmpl $0x4, %eax
jne 0x20dd
movl 0xc(%r9), %edi
testl %edi, %edi
js 0x22e8
movl 0x8(%r9), %ebx
jmp 0x20e1
xorl %edi, %edi
xorl %ebx, %ebx
movl %edi, %r12d
negl %r12d
movl %ebx, %eax
negl %eax
movl %eax, 0x40(%rsp)
leal (,%rcx,8), %r15d
movq %rcx, 0x78(%rsp)
leal 0x8(,%rcx,8), %ecx
movl %edx, 0x44(%rsp)
shll $0x4, %edx
leal 0x10(%rdx), %eax
movq %rax, 0x68(%rsp)
movl %r15d, %eax
subl %ebx, %eax
movl %eax, 0x3c(%rsp)
movq %rdx, 0x70(%rsp)
movl %edx, %esi
subl %edi, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %edx, %edx
movq %r10, 0x28(%rsp)
movq %r11, 0x20(%rsp)
movq %r9, 0x58(%rsp)
movl %edi, 0x48(%rsp)
movl %ebx, 0x10(%rsp)
movq %rcx, 0x50(%rsp)
testl %ebx, %ebx
js 0x22b7
movq 0x70(%rsp), %rax
leal (%r12,%rax), %edi
movq 0x68(%rsp), %rax
addl %r12d, %eax
movl %eax, 0x4c(%rsp)
movl 0x3c(%rsp), %ebp
movl 0x40(%rsp), %eax
movl %eax, %r14d
movq %r12, 0x30(%rsp)
movl %esi, 0x18(%rsp)
movl %edi, 0x14(%rsp)
movl %r14d, %eax
addl %r15d, %eax
js 0x22a5
leal (%rcx,%r14), %eax
cmpl 0xc(%r13), %eax
jg 0x22a5
testl %edi, %edi
js 0x22a5
movl 0x4c(%rsp), %eax
cmpl 0x10(%r13), %eax
jg 0x22a5
cmpl $0x2, (%r9)
jne 0x21c2
movl %r14d, %eax
imull %r12d, %eax
testl %eax, %eax
jne 0x22a5
movl %r8d, 0x1c(%rsp)
movq %rdx, 0x60(%rsp)
movq (%r13), %rax
movl 0x8(%r13), %ecx
movl %esi, %edx
imull %ecx, %edx
addl %ebp, %edx
leaq 0x80(%rsp), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
leal (%rdx,%r8), %r9d
movb (%rax,%r9), %r9b
movb %r9b, (%rsi,%r8)
incq %r8
cmpq $0x8, %r8
jne 0x21e8
incq %rdi
addq $0x8, %rsi
addq %rcx, %rdx
cmpq $0x10, %rdi
jne 0x21e5
movq (%r11), %rsi
movl 0x20(%r11), %edx
leaq 0x80(%rsp), %rdi
movq %r10, %rcx
callq 0x1d31
movq %rax, %rbx
movq %rax, %r12
shrq $0x20, %r12
movq 0x58(%rsp), %r9
testb $0x1, 0x4(%r9)
je 0x226c
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movl 0x44(%rsp), %edx
movq 0x78(%rsp), %rcx
movl %r14d, %r8d
movq 0x30(%rsp), %r9
callq 0x1eec
movq 0x58(%rsp), %r9
addl %r12d, %eax
movl %eax, %r12d
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
movl 0x18(%rsp), %esi
movl 0x1c(%rsp), %r8d
movl 0x14(%rsp), %edi
cmpl %r8d, %r12d
cmovbl %r12d, %r8d
cmovbl %ebx, %edx
testl %r8d, %r8d
je 0x22d0
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %r11
movl 0x10(%rsp), %ebx
movq 0x30(%rsp), %r12
leal 0x1(%r14), %eax
incl %ebp
cmpl %ebx, %r14d
movl %eax, %r14d
jne 0x217d
leal 0x1(%r12), %eax
incl %esi
movl 0x48(%rsp), %edi
cmpl %edi, %r12d
movl %eax, %r12d
jne 0x2148
jmp 0x22ea
xorl %r8d, %r8d
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %r11
movl 0x10(%rsp), %ebx
movq 0x30(%rsp), %r12
jmp 0x22b7
xorl %edx, %edx
movl %edx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop