// File: /workspace/github_c_makefile_star2_O1/lb3361[P]dosfs/ff.c
// Function: f_unlink
FRESULT f_unlink (
	const TCHAR* path		/* Pointer to the file or directory path */
)
{
	FRESULT res;
	DIR dj, sdj;
	DWORD dclst = 0;
	FATFS *fs;
#if FF_FS_EXFAT
	FFOBJID obj;
#endif
	DEF_NAMBUF


	/* Get logical drive */
	res = mount_volume(&path, &fs, FA_WRITE);
	if (res == FR_OK) {
		dj.obj.fs = fs;
		INIT_NAMBUF(fs);
		res = follow_path(&dj, path);		/* Follow the file path */
		if (FF_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT)) {
			res = FR_INVALID_NAME;			/* Cannot remove dot entry */
		}
#if FF_FS_LOCK != 0
		if (res == FR_OK) res = chk_lock(&dj, 2);	/* Check if it is an open object */
#endif
		if (res == FR_OK) {					/* The object is accessible */
			if (dj.fn[NSFLAG] & NS_NONAME) {
				res = FR_INVALID_NAME;		/* Cannot remove the origin directory */
			} else {
				if (dj.obj.attr & AM_RDO) {
					res = FR_DENIED;		/* Cannot remove R/O object */
				}
			}
			if (res == FR_OK) {
#if FF_FS_EXFAT
				obj.fs = fs;
				if (fs->fs_type == FS_EXFAT) {
					init_alloc_info(fs, &obj);
					dclst = obj.sclust;
				} else
#endif
				{
					dclst = ld_clust(fs, dj.dir);
				}
				if (dj.obj.attr & AM_DIR) {			/* Is it a sub-directory? */
#if FF_FS_RPATH != 0
					if (dclst == fs->cdir) {		 	/* Is it the current directory? */
						res = FR_DENIED;
					} else
#endif
					{
						sdj.obj.fs = fs;				/* Open the sub-directory */
						sdj.obj.sclust = dclst;
#if FF_FS_EXFAT
						if (fs->fs_type == FS_EXFAT) {
							sdj.obj.objsize = obj.objsize;
							sdj.obj.stat = obj.stat;
						}
#endif
						res = dir_sdi(&sdj, 0);
						if (res == FR_OK) {
							res = DIR_READ_FILE(&sdj);			/* Test if the directory is empty */
							if (res == FR_OK) res = FR_DENIED;	/* Not empty? */
							if (res == FR_NO_FILE) res = FR_OK;	/* Empty? */
						}
					}
				}
			}
			if (res == FR_OK) {
				res = dir_remove(&dj);			/* Remove the directory entry */
				if (res == FR_OK && dclst != 0) {	/* Remove the cluster chain if exist */
#if FF_FS_EXFAT
					res = remove_chain(&obj, dclst, 0);
#else
					res = remove_chain(&dj.obj, dclst, 0);
#endif
				}
				if (res == FR_OK) res = sync_fs(fs);
			}
		}
		FREE_NAMBUF();
	}

	LEAVE_FF(fs, res);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x568, %rsp            # imm = 0x568
leaq 0x8(%rsp), %rax
movq %rdi, (%rax)
movq %rsp, %rsi
movq %rax, %rdi
movl $0x2, %edx
callq 0x4c82
testl %eax, %eax
jne 0x848a
movq (%rsp), %rbx
leaq 0xa0(%rsp), %rdi
movq %rbx, (%rdi)
leaq 0x100(%rsp), %rax
movq %rax, 0x10(%rbx)
leaq 0x300(%rsp), %rax
movq %rax, 0x18(%rbx)
movq 0x8(%rsp), %rsi
callq 0x5747
testl %eax, %eax
jne 0x848a
xorl %ebp, %ebp
testb $0x1, 0xaa(%rsp)
sete %cl
movl $0x7, %edx
cmovel %ebp, %edx
cmpb $0x0, 0xf3(%rsp)
setns %sil
movl $0x6, %eax
cmovnsl %edx, %eax
andb %cl, %sil
cmpb $0x1, %sil
jne 0x8457
movq %rbx, 0x10(%rsp)
movb (%rbx), %r14b
cmpb $0x4, %r14b
jne 0x83d4
movq 0x18(%rbx), %r15
movl 0x34(%r15), %ebp
movl %ebp, 0x1c(%rsp)
leaq 0x38(%r15), %rdi
callq 0xaded
movq %rax, 0x20(%rsp)
movb 0x21(%r15), %al
andb $0x2, %al
movb %al, 0x1b(%rsp)
movl $0x0, 0x2c(%rsp)
jmp 0x83f8
movq 0xe0(%rsp), %rax
movzwl 0x1a(%rax), %ebp
cmpb $0x3, %r14b
jne 0x83f8
movzbl 0x15(%rax), %ecx
movzbl 0x14(%rax), %eax
shll $0x18, %ecx
shll $0x10, %eax
orl %ecx, %eax
orl %eax, %ebp
xorl %eax, %eax
testb $0x10, 0xaa(%rsp)
je 0x8457
movq %rbx, 0x40(%rsp)
movl %ebp, 0x4c(%rsp)
cmpb $0x4, %r14b
jne 0x8425
movq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
movb 0x1b(%rsp), %al
movb %al, 0x4b(%rsp)
leaq 0x40(%rsp), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x7545
testl %eax, %eax
jne 0x8457
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x7708
movl %eax, %ecx
testl %eax, %eax
movl $0x7, %eax
cmovnel %ecx, %eax
cmpl $0x4, %eax
cmovel %r14d, %eax
testl %eax, %eax
jne 0x848a
leaq 0xa0(%rsp), %rdi
callq 0x8498
testl %eax, %eax
jne 0x847e
testl %ebp, %ebp
je 0x847e
leaq 0x10(%rsp), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x619a
testl %eax, %eax
jne 0x848a
movq %rbx, %rdi
callq 0x70fe
addq $0x568, %rsp            # imm = 0x568
popq %rbx
popq %r14
popq %r15
popq %rbp
retq