// File: /workspace/github_c_makefile_star2_O2/lb3361[P]dosfs/ff.c
// Function: dir_find
static FRESULT dir_find (	/* FR_OK(0):succeeded, !=0:error */
	DIR* dp					/* Pointer to the directory object with the file name */
)
{
	FRESULT res;
	FATFS *fs = dp->obj.fs;
	BYTE c;
#if FF_USE_LFN
	BYTE a, ord, sum;
#endif

	res = dir_sdi(dp, 0);			/* Rewind directory object */
	if (res != FR_OK) return res;
#if FF_FS_EXFAT
	if (fs->fs_type == FS_EXFAT) {	/* On the exFAT volume */
		BYTE nc;
		UINT di, ni;
		WORD hash = xname_sum(fs->lfnbuf);		/* Hash value of the name to find */

		while ((res = DIR_READ_FILE(dp)) == FR_OK) {	/* Read an item */
#if FF_MAX_LFN < 255
			if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue;			/* Skip comparison if inaccessible object name */
#endif
			if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue;	/* Skip comparison if hash mismatched */
			for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) {	/* Compare the name */
				if ((di % SZDIRE) == 0) di += 2;
				if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break;
			}
			if (nc == 0 && !fs->lfnbuf[ni]) break;	/* Name matched? */
		}
		return res;
	}
#endif
	/* On the FAT/FAT32 volume */
#if FF_USE_LFN
	ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	/* Reset LFN sequence */
#endif
	do {
		res = move_window(fs, dp->sect);
		if (res != FR_OK) break;
		c = dp->dir[DIR_Name];
		if (c == 0) { res = FR_NO_FILE; break; }	/* Reached to end of table */
#if FF_USE_LFN		/* LFN configuration */
		dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;
		if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) {	/* An entry without valid data */
			ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	/* Reset LFN sequence */
		} else {
			if (a == AM_LFN) {			/* An LFN entry is found */
				if (!(dp->fn[NSFLAG] & NS_NOLFN)) {
					if (c & LLEF) {		/* Is it start of LFN sequence? */
						sum = dp->dir[LDIR_Chksum];
						c &= (BYTE)~LLEF; ord = c;	/* LFN start order */
						dp->blk_ofs = dp->dptr;	/* Start offset of LFN */
					}
					/* Check validity of the LFN entry and compare it with given name */
					ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
				}
			} else {					/* An SFN entry is found */
				if (ord == 0 && sum == sum_sfn(dp->dir)) break;	/* LFN matched? */
				if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break;	/* SFN matched? */
				ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	/* Reset LFN sequence */
			}
		}
#else		/* Non LFN configuration */
		dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
		if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break;	/* Is it a valid entry? */
#endif
		res = dir_next(dp, 0);	/* Next entry */
	} while (res == FR_OK);

	return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq (%rdi), %r14
xorl %esi, %esi
callq 0x6d3b
testl %eax, %eax
je 0xa5d2
movl %eax, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x4, (%r14)
jne 0xa65e
movq %rbx, (%rsp)
movq 0x10(%r14), %rdi
callq 0xa82a
movl %eax, %r15d
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x6ee7
testl %eax, %eax
jne 0xa5bf
movq 0x18(%r14), %rax
cmpw %r15w, 0x24(%rax)
jne 0xa5ec
movzbl 0x23(%rax), %r13d
xorl %r12d, %r12d
pushq $0x40
popq %rax
cmpb %r12b, %r13b
je 0xa64b
movl %eax, %ebx
orl $0x2, %ebx
testb $0x1f, %al
cmovnel %eax, %ebx
movq 0x18(%r14), %rax
movzwl (%rax,%rbx), %edi
callq 0xad7c
movl %eax, %ebp
movq 0x10(%r14), %rax
movzwl (%rax,%r12,2), %edi
callq 0xad7c
cmpl %eax, %ebp
jne 0xa5ec
addl $0x2, %ebx
incq %r12
movl %ebx, %eax
jmp 0xa611
movq 0x10(%r14), %rax
cmpw $0x0, (%rax,%r12,2)
jne 0xa5ec
xorl %ebp, %ebp
jmp 0xa5c1
orl $-0x1, 0x54(%rbx)
leaq 0x48(%rbx), %rax
movq %rax, 0x30(%rsp)
movb $-0x1, %r15b
pushq $0x4
popq %rbp
movb $-0x1, (%rsp)
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0xa5bf
movq 0x40(%rbx), %r13
movb (%r13), %al
testb %al, %al
je 0xa5c1
movb 0xb(%r13), %dl
movl %edx, %ecx
andb $0x3f, %cl
movb %cl, 0xa(%rbx)
cmpb $-0x1b, %al
je 0xa6bb
testb $0x8, %dl
sete %dl
cmpb $0xf, %cl
sete %sil
orb %dl, %sil
jne 0xa6d8
orl $-0x1, 0x54(%rbx)
movb $-0x1, %r12b
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7336
movl %r12d, %r15d
testl %eax, %eax
je 0xa675
jmp 0xa5bf
cmpb $0xf, %cl
jne 0xa6f5
testb $0x40, 0x53(%rbx)
jne 0xa822
testb $0x40, %al
jne 0xa72a
movb $-0x1, %r12b
cmpb %r15b, %al
jne 0xa6c2
jmp 0xa73c
testb %r15b, %r15b
jne 0xa70b
movq %r13, %rdi
callq 0xa87f
cmpb %al, (%rsp)
je 0xa657
testb $0x1, 0x53(%rbx)
jne 0xa6bb
movq %r13, %rdi
movq 0x30(%rsp), %rsi
pushq $0xb
popq %rdx
callq 0x9f7d
testl %eax, %eax
jne 0xa6bb
jmp 0xa657
movb 0xd(%r13), %cl
movb %cl, (%rsp)
andb $-0x41, %al
movl 0x30(%rbx), %ecx
movl %ecx, 0x54(%rbx)
movl %eax, %r15d
movb $-0x1, %r12b
movb (%rsp), %al
cmpb 0xd(%r13), %al
jne 0xa6c2
cmpw $0x0, 0x1a(%r13)
jne 0xa6c2
movq 0x10(%r14), %rax
movq %rax, 0x18(%rsp)
movzbl (%r13), %eax
andl $0x3f, %eax
imull $0xd, %eax, %edx
addl $-0xd, %edx
movw $0x1, %ax
xorl %esi, %esi
movl %edx, %edi
cmpq $0xd, %rsi
je 0xa803
leaq 0x1cd8(%rip), %rcx      # 0xc460
movzbl (%rsi,%rcx), %ecx
movzwl (%r13,%rcx), %ecx
testw %ax, %ax
je 0xa7ef
cmpl $0xff, %edx
ja 0xa6c2
movq %rsi, 0x20(%rsp)
movl %edx, 0x10(%rsp)
movl %ecx, 0xc(%rsp)
movq %rdi, 0x28(%rsp)
movzwl %cx, %edi
callq 0xad7c
movl %eax, 0x14(%rsp)
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rcx
movzwl (%rax,%rcx,2), %edi
callq 0xad7c
cmpl %eax, 0x14(%rsp)
jne 0xa6c2
movl 0x10(%rsp), %edx
incl %edx
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %eax
jmp 0xa7fb
cmpw $-0x1, %cx
jne 0xa6c2
xorl %eax, %eax
incq %rsi
jmp 0xa775
testb $0x40, (%r13)
je 0xa81f
testw %ax, %ax
je 0xa81f
movq 0x18(%rsp), %rax
cmpw $0x0, (%rax,%rdi,2)
jne 0xa6c2
decb %r15b
movl %r15d, %r12d
jmp 0xa6c2