// File: /workspace/github_c_makefile_star2_O1/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 %rcx, 0x50(%rsp)
movl %edx, 0x14(%rsp)
movq %rsi, %r11
movq %rdi, %r13
movl (%r9), %eax
movl $0x1, %ebx
leal -0x2(%rax), %ecx
movl $0x1, %edx
cmpl $0x2, %ecx
jb 0x20b6
cmpl $0x4, %eax
jne 0x20b2
movl 0x8(%r9), %ebx
movl 0xc(%r9), %edx
jmp 0x20b6
xorl %ebx, %ebx
xorl %edx, %edx
testl %edx, %edx
js 0x22c7
movl %edx, %r12d
negl %r12d
movl %ebx, %eax
negl %eax
movl %eax, 0x44(%rsp)
movq 0x50(%rsp), %rax
leal (,%rax,8), %r15d
leal 0x8(,%rax,8), %ecx
movl 0x14(%rsp), %eax
movl %eax, %esi
shll $0x4, %esi
leal 0x10(%rsi), %eax
movq %rax, 0x70(%rsp)
movl %r15d, %eax
subl %ebx, %eax
movl %eax, 0x40(%rsp)
movq %rsi, 0x78(%rsp)
movl %esi, %edi
subl %edx, %edi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %esi, %esi
movq %r10, 0x30(%rsp)
movq %r11, 0x28(%rsp)
movq %r9, 0x60(%rsp)
movl %ebx, 0x18(%rsp)
movl %edx, 0x48(%rsp)
movq %rcx, 0x58(%rsp)
testl %ebx, %ebx
js 0x2296
movq 0x78(%rsp), %rax
leal (%r12,%rax), %edx
movq 0x70(%rsp), %rax
addl %r12d, %eax
movl %eax, 0x4c(%rsp)
movl 0x40(%rsp), %ebp
movl 0x44(%rsp), %eax
movl %eax, %r14d
movq %r12, 0x38(%rsp)
movl %edi, 0x20(%rsp)
movl %edx, 0x1c(%rsp)
movl %r14d, %eax
addl %r15d, %eax
js 0x2284
leal (%rcx,%r14), %eax
cmpl 0xc(%r13), %eax
jg 0x2284
testl %edx, %edx
js 0x2284
movl 0x4c(%rsp), %eax
cmpl 0x10(%r13), %eax
jg 0x2284
cmpl $0x2, (%r9)
jne 0x21a1
movl %r14d, %eax
imull %r12d, %eax
testl %eax, %eax
jne 0x2284
movl %r8d, 0x24(%rsp)
movq %rsi, 0x68(%rsp)
movq (%r13), %rax
movl 0x8(%r13), %ecx
movl %edi, %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 0x21c7
incq %rdi
addq $0x8, %rsi
addq %rcx, %rdx
cmpq $0x10, %rdi
jne 0x21c4
movq (%r11), %rsi
movl 0x20(%r11), %edx
leaq 0x80(%rsp), %rdi
movq %r10, %rcx
callq 0x1d61
movq %rax, %rbx
movq %rax, %r12
shrq $0x20, %r12
movq 0x60(%rsp), %r9
testb $0x1, 0x4(%r9)
je 0x224b
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq 0x28(%rsp), %rsi
movl 0x14(%rsp), %edx
movq 0x50(%rsp), %rcx
movl %r14d, %r8d
movq 0x38(%rsp), %r9
callq 0x1ec4
movq 0x60(%rsp), %r9
addl %r12d, %eax
movl %eax, %r12d
movq 0x58(%rsp), %rcx
movq 0x68(%rsp), %rsi
movl 0x20(%rsp), %edi
movl 0x24(%rsp), %r8d
movl 0x1c(%rsp), %edx
cmpl %r8d, %r12d
cmovbl %r12d, %r8d
cmovbl %ebx, %esi
testl %r8d, %r8d
je 0x22af
movq 0x30(%rsp), %r10
movq 0x28(%rsp), %r11
movl 0x18(%rsp), %ebx
movq 0x38(%rsp), %r12
leal 0x1(%r14), %eax
incl %ebp
cmpl %ebx, %r14d
movl %eax, %r14d
jne 0x215c
leal 0x1(%r12), %eax
incl %edi
movl 0x48(%rsp), %edx
cmpl %edx, %r12d
movl %eax, %r12d
jne 0x2127
jmp 0x22c9
xorl %r8d, %r8d
movq 0x30(%rsp), %r10
movq 0x28(%rsp), %r11
movl 0x18(%rsp), %ebx
movq 0x38(%rsp), %r12
jmp 0x2296
xorl %esi, %esi
movl %esi, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)