// File: /workspace/github_c_makefile_star2_O1/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 0x7545
testl %eax, %eax
je 0xb6c0
movl %eax, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x4, (%r14)
jne 0xb77c
movq 0x10(%r14), %rdi
callq 0xb968
movl %eax, %r15d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7708
movl %eax, %ebp
testl %eax, %eax
jne 0xb6af
movw %r15w, 0xe(%rsp)
movq %rbx, 0x10(%rsp)
movq 0x18(%r14), %rax
movzwl 0xe(%rsp), %ecx
cmpw %cx, 0x24(%rax)
jne 0xb761
movzbl 0x23(%rax), %r13d
testq %r13, %r13
je 0xb74e
movl $0x40, %eax
xorl %r15d, %r15d
movl %eax, %ebx
orl $0x2, %ebx
testb $0x1f, %al
cmovnel %eax, %ebx
movq 0x18(%r14), %rax
movzwl (%rax,%rbx), %edi
callq 0xbe55
movl %eax, %r12d
movq 0x10(%r14), %rax
movzwl (%rax,%r15,2), %edi
callq 0xbe55
cmpl %eax, %r12d
jne 0xb761
addl $0x2, %ebx
incq %r15
movl %ebx, %eax
cmpb %r15b, %r13b
jne 0xb712
jmp 0xb751
xorl %r15d, %r15d
movq 0x10(%r14), %rax
cmpw $0x0, (%rax,%r15,2)
je 0xb6af
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x7708
movl %eax, %ebp
testl %eax, %eax
je 0xb6f1
jmp 0xb6af
movl $0xffffffff, 0x54(%rbx) # imm = 0xFFFFFFFF
movb $-0x1, %r12b
movl $0xff, %r15d
movb $-0x1, 0xe(%rsp)
movq 0x38(%rbx), %rsi
movq %r14, %rdi
callq 0x6314
testl %eax, %eax
jne 0xb6ad
movq 0x40(%rbx), %r13
movb (%r13), %al
testb %al, %al
je 0xb957
movb 0xb(%r13), %dl
movl %edx, %ecx
andb $0x3f, %cl
movb %cl, 0xa(%rbx)
cmpb $-0x1b, %al
je 0xb7d7
testb $0x8, %dl
sete %dl
cmpb $0xf, %cl
sete %sil
orb %dl, %sil
jne 0xb7f4
movl $0xffffffff, 0x54(%rbx) # imm = 0xFFFFFFFF
movb $-0x1, %r12b
movq %rbx, %rdi
xorl %esi, %esi
callq 0x7c47
testl %eax, %eax
je 0xb791
jmp 0xb6ad
cmpb $0xf, %cl
jne 0xb8e7
testb $0x40, 0x53(%rbx)
jne 0xb7e1
testb $0x40, %al
je 0xb81a
movb 0xd(%r13), %cl
movb %cl, 0xe(%rsp)
andb $-0x41, %al
movl 0x30(%rbx), %ecx
movl %ecx, 0x54(%rbx)
movl %eax, %r12d
cmpb %r12b, %al
jne 0xb7de
movb 0xe(%rsp), %al
cmpb 0xd(%r13), %al
jne 0xb7de
movb 0x1a(%r13), %al
movb $0x1, %cl
orb 0x1b(%r13), %al
jne 0xb945
movq 0x10(%r14), %rax
movq %rax, 0x20(%rsp)
movzbl (%r13), %eax
andl $0x3f, %eax
leal (%rax,%rax,2), %ecx
leal (%rax,%rcx,4), %ebp
addl $-0xd, %ebp
movw $0x1, %ax
xorl %r15d, %r15d
leaq 0x1bef(%rip), %rcx      # 0xd450
movzbl (%r15,%rcx), %ecx
movzwl (%r13,%rcx), %ecx
testw %ax, %ax
je 0xb8ae
cmpl $0xff, %ebp
ja 0xb93d
movl %ecx, %edi
movl %ecx, 0x1c(%rsp)
callq 0xbe55
movl %eax, 0x10(%rsp)
movl %ebp, %eax
movq 0x20(%rsp), %rcx
movzwl (%rcx,%rax,2), %edi
callq 0xbe55
cmpl %eax, 0x10(%rsp)
jne 0xb93d
incl %ebp
movl 0x1c(%rsp), %eax
jmp 0xb8b8
cmpw $-0x1, %cx
jne 0xb93d
incq %r15
cmpq $0xd, %r15
jne 0xb85a
testb $0x40, (%r13)
movl $0xff, %r15d
movb $0x1, %cl
je 0xb8e3
testw %ax, %ax
je 0xb8e3
movl %ebp, %eax
movq 0x20(%rsp), %rdx
cmpw $0x0, (%rdx,%rax,2)
jne 0xb945
xorl %ecx, %ecx
jmp 0xb945
testb %r12b, %r12b
jne 0xb912
xorl %eax, %eax
xorl %ecx, %ecx
movzbl %cl, %edx
movl %edx, %ecx
shrl %ecx
shll $0x7, %edx
orl %ecx, %edx
movzbl (%r13,%rax), %ecx
addl %edx, %ecx
incq %rax
cmpl $0xb, %eax
jne 0xb8f0
cmpb %cl, 0xe(%rsp)
je 0xb961
testb $0x1, 0x53(%rbx)
jne 0xb7d7
xorl %eax, %eax
movb (%r13,%rax), %cl
movb 0x48(%rbx,%rax), %dl
cmpl $0xa, %eax
je 0xb933
incq %rax
cmpb %dl, %cl
je 0xb91e
cmpb %dl, %cl
jne 0xb7d7
jmp 0xb961
movl $0xff, %r15d
movb $0x1, %cl
decb %r12b
movzbl %r12b, %r12d
testb %cl, %cl
cmovnel %r15d, %r12d
jmp 0xb7e1
movl $0x4, %ebp
jmp 0xb6af
xorl %ebp, %ebp
jmp 0xb6af