// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %rdi, %rbx
movq (%rdi), %r14
xorl %esi, %esi
callq 0x72ea
testl %eax, %eax
je 0xb33c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x4, (%r14)
jne 0xb3e7
movq 0x10(%r14), %rdi
callq 0xb5bd
movl %eax, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x74aa
testl %eax, %eax
jne 0xb32d
movw %bp, 0x10(%rsp)
movq 0x18(%r14), %rax
cmpw %bp, 0x24(%rax)
jne 0xb3cf
movzbl 0x23(%rax), %r12d
testq %r12, %r12
je 0xb3bc
movl $0x40, %eax
xorl %r13d, %r13d
movl %eax, %ebp
orl $0x2, %ebp
testb $0x1f, %al
cmovnel %eax, %ebp
movq 0x18(%r14), %rax
movzwl (%rax,%rbp), %edi
callq 0xbab9
movl %eax, %r15d
movq 0x10(%r14), %rax
movzwl (%rax,%r13,2), %edi
callq 0xbab9
cmpl %eax, %r15d
jne 0xb3cf
addl $0x2, %ebp
incq %r13
movl %ebp, %eax
cmpb %r13b, %r12b
jne 0xb380
jmp 0xb3bf
xorl %r13d, %r13d
movq 0x10(%r14), %rax
cmpw $0x0, (%rax,%r13,2)
je 0xb5b6
movq %rbx, %rdi
xorl %esi, %esi
callq 0x74aa
testl %eax, %eax
movzwl 0x10(%rsp), %ebp
je 0xb364
jmp 0xb32d
movl $0xffffffff, 0x54(%rbx) # imm = 0xFFFFFFFF
movb $-0x1, %r13b
movb $-0x1, %bpl
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x6114
testl %eax, %eax
jne 0xb32d
movq 0x40(%rbx), %r12
movb (%r12), %al
testb %al, %al
je 0xb5ac
movb 0xb(%r12), %dl
movl %edx, %ecx
andb $0x3f, %cl
movb %cl, 0xa(%rbx)
cmpb $-0x1b, %al
je 0xb43b
testb $0x8, %dl
sete %dl
cmpb $0xf, %cl
sete %sil
orb %dl, %sil
jne 0xb45b
movl $0xffffffff, 0x54(%rbx) # imm = 0xFFFFFFFF
movb $-0x1, %r15b
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7952
movl %r15d, %r13d
testl %eax, %eax
je 0xb3f4
jmp 0xb32d
cmpb $0xf, %cl
jne 0xb478
testb $0x40, 0x53(%rbx)
jne 0xb59a
testb $0x40, %al
jne 0xb4bd
movb $-0x1, %r15b
cmpb %r13b, %al
jne 0xb445
jmp 0xb4cd
testb %r13b, %r13b
jne 0xb498
xorl %eax, %eax
xorl %ecx, %ecx
rorb %cl
addb (%r12,%rax), %cl
incq %rax
cmpl $0xb, %eax
jne 0xb481
cmpb %cl, %bpl
je 0xb5b6
testb $0x1, 0x53(%rbx)
jne 0xb43b
xorl %eax, %eax
movb (%r12,%rax), %cl
movb 0x48(%rbx,%rax), %dl
cmpl $0xa, %eax
je 0xb4b4
incq %rax
cmpb %dl, %cl
je 0xb4a0
cmpb %dl, %cl
jne 0xb43b
jmp 0xb5b6
movb 0xd(%r12), %bpl
andb $-0x41, %al
movl 0x30(%rbx), %ecx
movl %ecx, 0x54(%rbx)
movl %eax, %r13d
movb $-0x1, %r15b
cmpb 0xd(%r12), %bpl
jne 0xb445
cmpw $0x0, 0x1a(%r12)
jne 0xb445
movb %bpl, 0xf(%rsp)
movq 0x10(%r14), %rax
movq %rax, 0x18(%rsp)
movzbl (%r12), %eax
andl $0x3f, %eax
leal (%rax,%rax,2), %ecx
leal (%rax,%rcx,4), %edx
addl $-0xd, %edx
movw $0x1, %ax
xorl %esi, %esi
leaq 0x1f1c(%rip), %rcx      # 0xd430
movzbl (%rsi,%rcx), %ecx
movzwl (%r12,%rcx), %edi
testw %ax, %ax
je 0xb563
cmpl $0xff, %edx
ja 0xb5a2
movq %rsi, 0x20(%rsp)
movl %edi, 0x14(%rsp)
movl %edx, %ebp
callq 0xbab9
movl %eax, 0x10(%rsp)
movl %ebp, %eax
movq 0x18(%rsp), %rcx
movzwl (%rcx,%rax,2), %edi
callq 0xbab9
cmpl %eax, 0x10(%rsp)
jne 0xb5a2
incl %ebp
movl %ebp, %edx
movl 0x14(%rsp), %eax
movq 0x20(%rsp), %rsi
jmp 0xb56b
cmpw $-0x1, %di
jne 0xb5a2
xorl %eax, %eax
incq %rsi
cmpq $0xd, %rsi
jne 0xb50d
testb $0x40, (%r12)
movb 0xf(%rsp), %bpl
je 0xb597
testw %ax, %ax
je 0xb597
movl %edx, %eax
movq 0x18(%rsp), %rcx
cmpw $0x0, (%rcx,%rax,2)
jne 0xb445
decb %r13b
movl %r13d, %r15d
jmp 0xb445
movb 0xf(%rsp), %bpl
jmp 0xb445
movl $0x4, %eax
jmp 0xb32d
xorl %eax, %eax
jmp 0xb32d