// File: /workspace/github_c_makefile_star2_O2/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: follow_path
static
FRESULT follow_path (	/* FR_OK(0): successful, !=0: error code */
	DIR* dp,			/* Directory object to return last directory and found object */
	const TCHAR* path	/* Full-path string to find a file or directory */
)
{
	FRESULT res;
	BYTE ns;
	FATFS *fs = dp->obj.fs;


#if FF_FS_RPATH != 0
	if (*path != '/' && *path != '\\') {	/* Without heading separator */
		dp->obj.sclust = fs->cdir;				/* Start from current directory */
	} else
#endif
	{										/* With heading separator */
		while (*path == '/' || *path == '\\') path++;	/* Strip heading separator */
		dp->obj.sclust = 0;					/* Start from root directory */
	}
#if FF_FS_EXFAT
	dp->obj.n_frag = 0;	/* Invalidate last fragment counter of the object */
#if FF_FS_RPATH != 0
	if (fs->fs_type == FS_EXFAT && dp->obj.sclust) {	/* exFAT: Retrieve the sub-directory's status */
		DIR dj;

		dp->obj.c_scl = fs->cdc_scl;
		dp->obj.c_size = fs->cdc_size;
		dp->obj.c_ofs = fs->cdc_ofs;
		res = load_obj_xdir(&dj, &dp->obj);
		if (res != FR_OK) return res;
		dp->obj.objsize = ld_dword(fs->dirbuf + XDIR_FileSize);
		dp->obj.stat = fs->dirbuf[XDIR_GenFlags] & 2;
	}
#endif
#endif

	if ((UINT)*path < ' ') {				/* Null path name is the origin directory itself */
		dp->fn[NSFLAG] = NS_NONAME;
		res = dir_sdi(dp, 0);

	} else {								/* Follow path */
		for (;;) {
			res = create_name(dp, &path);	/* Get a segment name of the path */
			if (res != FR_OK) break;
			res = dir_find(dp);				/* Find an object with the segment name */
			ns = dp->fn[NSFLAG];
			if (res != FR_OK) {				/* Failed to find the object */
				if (res == FR_NO_FILE) {	/* Object is not found */
					if (FF_FS_RPATH && (ns & NS_DOT)) {	/* If dot entry is not exist, stay there */
						if (!(ns & NS_LAST)) continue;	/* Continue to follow if not last segment */
						dp->fn[NSFLAG] = NS_NONAME;
						res = FR_OK;
					} else {							/* Could not find the object */
						if (!(ns & NS_LAST)) res = FR_NO_PATH;	/* Adjust error code if not last segment */
					}
				}
				break;
			}
			if (ns & NS_LAST) break;			/* Last segment matched. Function completed. */
			/* Get into the sub-directory */
			if (!(dp->obj.attr & AM_DIR)) {		/* It is not a sub-directory and cannot follow */
				res = FR_NO_PATH; break;
			}
#if FF_FS_EXFAT
			if (fs->fs_type == FS_EXFAT) {		/* Save containing directory information for next dir */
				dp->obj.c_scl = dp->obj.sclust;
				dp->obj.c_size = ((DWORD)dp->obj.objsize & 0xFFFFFF00) | dp->obj.stat;
				dp->obj.c_ofs = dp->blk_ofs;
				init_alloc_info(fs, &dp->obj);	/* Open next directory */
			} else
#endif
			{
				dp->obj.sclust = ld_clust(fs, fs->win + dp->dptr % SS(fs));	/* Open next directory */
			}
		}
	}

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movq %rdi, %r14
movq (%rdi), %rcx
movzbl (%r15), %eax
cmpl $0x5c, %eax
je 0x1a95
cmpl $0x2f, %eax
jne 0x1a9a
incq %r15
jmp 0x1a87
andq $0x0, 0x10(%r14)
cmpb $0x20, (%r15)
jae 0x1ac0
movb $-0x80, 0x4b(%r14)
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2bf4
movq %rcx, -0x38(%rbp)
leaq 0x40(%r14), %r12
movabsq $0x2020202020202020, %rcx # imm = 0x2020202020202020
pushq $0x8
popq %rdx
pushq $0x6
popq %rax
movl %eax, -0x2c(%rbp)
movq %rcx, (%r12)
movl $0x20202020, 0x7(%r12)  # imm = 0x20202020
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %eax, %eax
leal -0x1(%rdx), %edi
movl %ebx, %r8d
leal 0x1(%r8), %ebx
movl %r8d, %esi
movzbl (%r15,%rsi), %esi
movl %ebx, %r10d
cmpl $0x21, %esi
jb 0x1c20
cmpl $0x2e, %esi
je 0x1bea
cmpl $0x5c, %esi
je 0x1c0d
cmpl $0x2f, %esi
je 0x1c0d
cmpl %edx, %eax
jae 0x1bea
movl %eax, %r9d
cmpb $-0x80, %sil
jbe 0x1ba6
cmpb $-0x60, %sil
setb %r11b
leal 0x20(%rsi), %r13d
cmpb $0x1d, %r13b
setb %r13b
orb %r11b, %r13b
cmpb $0x1, %r13b
jne 0x1ba6
movzbl (%r15,%r10), %r10d
cmpl $0x40, %r10d
jb 0x1d01
cmpb $0x7f, %r10b
jb 0x1b85
leal -0xfd(%r10), %r11d
cmpl $0x3, %r11d
jb 0x1d01
cmpl $0x7f, %r10d
je 0x1d01
cmpl %edi, %eax
jae 0x1d01
addl $0x2, %r8d
movb %sil, 0x40(%r14,%r9)
movslq %eax, %rsi
addl $0x2, %eax
movb %r10b, 0x41(%r14,%rsi)
jmp 0x1af4
leaq 0x392b(%rip), %r8       # 0x54d8
movsbl (%r8), %r10d
testl %r10d, %r10d
je 0x1bbe
incq %r8
cmpl %esi, %r10d
jne 0x1bad
testb %r10b, %r10b
jne 0x1d01
leal -0x61(%rsi), %r8d
leal -0x20(%rsi), %r10d
cmpb $0x1a, %r8b
movzbl %sil, %esi
movzbl %r10b, %r8d
cmovael %esi, %r8d
incl %eax
movb %r8b, (%r12,%r9)
jmp 0x1af1
cmpb $0x2e, %sil
setne %al
orb %al, %cl
pushq $0xb
popq %rax
movl %eax, %edx
pushq $0x8
popq %rax
testb $0x1, %cl
movb $0x1, %cl
je 0x1aee
jmp 0x1d01
incl %ebx
movl %ebx, %ebx
movzbl (%r15,%rbx), %ecx
cmpl $0x5c, %ecx
je 0x1c0b
cmpl $0x2f, %ecx
je 0x1c0b
jmp 0x1c23
movq %r10, %rbx
testl %eax, %eax
je 0x1d01
cmpb $-0x1b, (%r12)
jne 0x1c37
movb $0x5, (%r12)
cmpb $0x21, %sil
setb %al
shlb $0x2, %al
movb %al, 0x4b(%r14)
movq (%r14), %r13
movq %r14, %rdi
callq 0x2bf4
testl %eax, %eax
jne 0x1d1f
addq %rbx, %r15
movq 0x30(%r14), %rsi
movq %r13, %rdi
callq 0x1e87
testl %eax, %eax
jne 0x1ce9
movq 0x38(%r14), %rdi
cmpb $0x0, (%rdi)
je 0x1cf1
movb 0xb(%rdi), %bl
movl %ebx, %eax
andb $0x3f, %al
movb %al, 0xa(%r14)
testb $0x8, 0xb(%rdi)
jne 0x1c94
movq %r12, %rsi
pushq $0xb
popq %rdx
callq 0x419b
testl %eax, %eax
je 0x1ca4
movq %r14, %rdi
xorl %esi, %esi
callq 0x2e47
testl %eax, %eax
je 0x1c5b
jmp 0x1ce9
testb $0x4, 0x4b(%r14)
jne 0x1d13
testb $0x10, %bl
je 0x1d1c
movq 0x20(%r14), %rcx
andl $0x1ff, %ecx            # imm = 0x1FF
movq -0x38(%rbp), %rdx
movzwl 0x72(%rdx,%rcx), %eax
cmpb $0x3, (%rdx)
jne 0x1cd3
movzwl 0x6c(%rdx,%rcx), %ecx
shll $0x10, %ecx
orq %rcx, %rax
movabsq $0x2020202020202020, %rcx # imm = 0x2020202020202020
pushq $0x8
popq %rdx
movq %rax, 0x10(%r14)
jmp 0x1adb
movl %eax, -0x2c(%rbp)
cmpl $0x4, %eax
jne 0x1d01
movb 0x4b(%r14), %al
andb $0x4, %al
xorl %ecx, %ecx
cmpb $0x1, %al
adcl $0x4, %ecx
movl %ecx, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, -0x2c(%rbp)
jmp 0x1d01
pushq $0x5
popq %rax
movl %eax, -0x2c(%rbp)
jmp 0x1d01