// File: /workspace/github_c_makefile_star2_O1/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_rename
FRESULT f_rename (
	const TCHAR* path_old,	/* Pointer to the object name to be renamed */
	const TCHAR* path_new	/* Pointer to the new name */
)
{
	FRESULT res;
	DIR djo, djn;
	FATFS *fs;
	BYTE buf[FF_FS_EXFAT ? SZDIRE * 2 : SZDIRE], *dir;
	DWORD dw;
	DEF_NAMBUF


	get_ldnumber(&path_new);						/* Snip the drive number of new name off */
	res = find_volume(&path_old, &fs, FA_WRITE);	/* Get logical drive of the old object */
	if (res == FR_OK) {
		djo.obj.fs = fs;
		INIT_NAMBUF(fs);
		res = follow_path(&djo, path_old);		/* Check old object */
		if (res == FR_OK && (djo.fn[NSFLAG] & (NS_DOT | NS_NONAME))) res = FR_INVALID_NAME;	/* Check validity of name */
#if FF_FS_LOCK != 0
		if (res == FR_OK) {
			res = chk_lock(&djo, 2);
		}
#endif
		if (res == FR_OK) {						/* Object to be renamed is found */
#if FF_FS_EXFAT
			if (fs->fs_type == FS_EXFAT) {	/* At exFAT volume */
				BYTE nf, nn;
				WORD nh;

				mem_cpy(buf, fs->dirbuf, SZDIRE * 2);	/* Save 85+C0 entry of old object */
				mem_cpy(&djn, &djo, sizeof djo);
				res = follow_path(&djn, path_new);		/* Make sure if new object name is not in use */
				if (res == FR_OK) {						/* Is new name already in use by any other object? */
					res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
				}
				if (res == FR_NO_FILE) { 				/* It is a valid path and no name collision */
					res = dir_register(&djn);			/* Register the new entry */
					if (res == FR_OK) {
						nf = fs->dirbuf[XDIR_NumSec]; nn = fs->dirbuf[XDIR_NumName];
						nh = ld_word(fs->dirbuf + XDIR_NameHash);
						mem_cpy(fs->dirbuf, buf, SZDIRE * 2);	/* Restore 85+C0 entry */
						fs->dirbuf[XDIR_NumSec] = nf; fs->dirbuf[XDIR_NumName] = nn;
						st_word(fs->dirbuf + XDIR_NameHash, nh);
						if (!(fs->dirbuf[XDIR_Attr] & AM_DIR)) fs->dirbuf[XDIR_Attr] |= AM_ARC;	/* Set archive attribute if it is a file */
/* Start of critical section where an interruption can cause a cross-link */
						res = store_xdir(&djn);
					}
				}
			} else
#endif
			{	/* At FAT/FAT32 volume */
				mem_cpy(buf, djo.dir, SZDIRE);			/* Save directory entry of the object */
				mem_cpy(&djn, &djo, sizeof (DIR));		/* Duplicate the directory object */
				res = follow_path(&djn, path_new);		/* Make sure if new object name is not in use */
				if (res == FR_OK) {						/* Is new name already in use by any other object? */
					res = (djn.obj.sclust == djo.obj.sclust && djn.dptr == djo.dptr) ? FR_NO_FILE : FR_EXIST;
				}
				if (res == FR_NO_FILE) { 				/* It is a valid path and no name collision */
					res = dir_register(&djn);			/* Register the new entry */
					if (res == FR_OK) {
						dir = djn.dir;					/* Copy directory entry of the object except name */
						mem_cpy(dir + 13, buf + 13, SZDIRE - 13);
						dir[DIR_Attr] = buf[DIR_Attr];
						if (!(dir[DIR_Attr] & AM_DIR)) dir[DIR_Attr] |= AM_ARC;	/* Set archive attribute if it is a file */
						fs->wflag = 1;
						if ((dir[DIR_Attr] & AM_DIR) && djo.obj.sclust != djn.obj.sclust) {	/* Update .. entry in the sub-directory if needed */
							dw = clst2sect(fs, ld_clust(fs, dir));
							if (dw == 0) {
								res = FR_INT_ERR;
							} else {
/* Start of critical section where an interruption can cause a cross-link */
								res = move_window(fs, dw);
								dir = fs->win + SZDIRE * 1;	/* Ptr to .. entry */
								if (res == FR_OK && dir[1] == '.') {
									st_clust(fs, dir, djn.obj.sclust);
									fs->wflag = 1;
								}
							}
						}
					}
				}
			}
			if (res == FR_OK) {
				res = dir_remove(&djo);		/* Remove old entry */
				if (res == FR_OK) {
					res = sync_fs(fs);
				}
			}
/* End of the critical section */
		}
		FREE_NAMBUF();
	}

	LEAVE_FF(fs, res);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xd0, %rsp
movq %rsi, %r14
movq %rdi, -0x28(%rbp)
testq %rsi, %rsi
je 0x38fa
xorl %eax, %eax
movb (%r14,%rax), %cl
incq %rax
cmpb $0x21, %cl
jb 0x38e1
cmpb $0x3a, %cl
jne 0x38d0
cmpb $0x3a, %cl
jne 0x38fa
leaq (%r14,%rax), %rcx
cmpb $0x30, (%r14)
cmovneq %r14, %rcx
cmpq $0x2, %rax
cmoveq %rcx, %r14
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x2, %edx
callq 0x146e
testl %eax, %eax
jne 0x3ac4
movq -0x30(%rbp), %rbx
leaq -0xa0(%rbp), %r15
movq %rbx, (%r15)
movq -0x28(%rbp), %rsi
movq %r15, %rdi
callq 0x1b33
movl %eax, %ecx
xorl %edx, %edx
testb $-0x60, 0x4b(%r15)
sete %sil
movl $0x6, %eax
cmovel %edx, %eax
testl %ecx, %ecx
sete %dl
cmovnel %ecx, %eax
andb %sil, %dl
cmpb $0x1, %dl
jne 0x3ac4
leaq -0xa0(%rbp), %rsi
movq 0x38(%rsi), %r15
movb 0xb(%r15), %r12b
movups 0xc(%r15), %xmm0
movaps %xmm0, -0x50(%rbp)
movl 0x1c(%r15), %eax
movl %eax, -0x40(%rbp)
leaq -0xf0(%rbp), %rax
movl $0xa, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r14, %rsi
callq 0x1b33
testl %eax, %eax
jne 0x39bf
movq -0xe0(%rbp), %rax
xorq -0x90(%rbp), %rax
movq -0x80(%rbp), %rcx
xorq -0xd0(%rbp), %rcx
xorl %edx, %edx
orq %rax, %rcx
setne %dl
leal 0x4(,%rdx,4), %eax
cmpl $0x4, %eax
jne 0x3aa9
leaq -0xf0(%rbp), %rdi
callq 0x1e2c
testl %eax, %eax
jne 0x3aa9
movq -0xb8(%rbp), %rcx
movl -0x40(%rbp), %eax
movl %eax, 0x1c(%rcx)
movups -0x4f(%rbp), %xmm0
movups %xmm0, 0xd(%rcx)
movl %r12d, %eax
notb %al
addb %al, %al
andb $0x20, %al
orb %r12b, %al
movb %al, 0xb(%rcx)
movb $0x1, 0x3(%rbx)
xorl %eax, %eax
testb $0x10, 0xb(%rcx)
je 0x3aa9
movq -0xe0(%rbp), %r14
cmpq %r14, -0x90(%rbp)
je 0x3aa9
movzwl 0x1a(%rcx), %esi
cmpb $0x3, (%rbx)
jne 0x3a40
movzbl 0x14(%rcx), %eax
movzbl 0x15(%rcx), %ecx
shll $0x18, %ecx
shll $0x10, %eax
orl %ecx, %eax
orq %rax, %rsi
addq $-0x2, %rsi
movq 0x20(%rbx), %rax
addq $-0x2, %rax
cmpq %rax, %rsi
jae 0x3a5f
movzwl 0xa(%rbx), %eax
imulq %rax, %rsi
addq 0x48(%rbx), %rsi
jmp 0x3a61
xorl %esi, %esi
testq %rsi, %rsi
je 0x3aa4
movq %rbx, %rdi
callq 0x1f73
testl %eax, %eax
jne 0x3aa9
xorl %eax, %eax
cmpb $0x2e, 0x79(%rbx)
jne 0x3aa9
movw %r14w, 0x92(%rbx)
cmpb $0x3, (%rbx)
jne 0x3a9e
movl %r14d, %ecx
shrl $0x10, %ecx
movb %cl, 0x8c(%rbx)
shrl $0x18, %r14d
movb %r14b, 0x8d(%rbx)
movb $0x1, 0x3(%rbx)
jmp 0x3aa9
movl $0x2, %eax
testl %eax, %eax
jne 0x3ac4
movq -0xa0(%rbp), %r14
movq -0x70(%rbp), %rsi
movq %r14, %rdi
callq 0x1f73
testl %eax, %eax
je 0x3ad4
addq $0xd0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movb $-0x1b, (%r15)
movb $0x1, 0x3(%r14)
movq %rbx, %rdi
addq $0xd0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2970