// File: /workspace/github_c_makefile_star2_O0/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
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x1690
leaq -0x8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movl $0x2, %edx
callq 0x1770
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x537e
movq -0xc0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x2440
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x5154
movzbl -0x1d(%rbp), %eax
andl $0xa0, %eax
cmpl $0x0, %eax
je 0x5154
movl $0x6, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x537c
leaq -0xe0(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x20, %edx
callq 0x3090
leaq -0xb8(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movl $0x50, %edx
callq 0x3090
movq -0x10(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x2440
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x51e3
movq -0xa8(%rbp), %rcx
xorl %eax, %eax
cmpq -0x58(%rbp), %rcx
movb %al, -0xf1(%rbp)
jne 0x51ca
movq -0x98(%rbp), %rax
cmpq -0x48(%rbp), %rax
sete %al
movb %al, -0xf1(%rbp)
movb -0xf1(%rbp), %dl
movl $0x8, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x14(%rbp)
cmpl $0x4, -0x14(%rbp)
jne 0x5351
leaq -0xb8(%rbp), %rdi
callq 0x2590
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x534f
movq -0x80(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
addq $0xd, %rdi
leaq -0xe0(%rbp), %rsi
addq $0xd, %rsi
movl $0x13, %edx
callq 0x3090
movb -0xd5(%rbp), %cl
movq -0xe8(%rbp), %rax
movb %cl, 0xb(%rax)
movq -0xe8(%rbp), %rax
movzbl 0xb(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x5265
movq -0xe8(%rbp), %rax
movzbl 0xb(%rax), %ecx
orl $0x20, %ecx
movb %cl, 0xb(%rax)
movq -0xc0(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0xe8(%rbp), %rax
movzbl 0xb(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x534d
movq -0x58(%rbp), %rax
cmpq -0xa8(%rbp), %rax
je 0x534d
movq -0xc0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xc0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
callq 0x2620
movq -0x100(%rbp), %rdi
movq %rax, %rsi
callq 0x2bd0
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jne 0x52e2
movl $0x2, -0x14(%rbp)
jmp 0x534b
movq -0xc0(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x28b0
movl %eax, -0x14(%rbp)
movq -0xc0(%rbp), %rax
addq $0x58, %rax
addq $0x20, %rax
movq %rax, -0xe8(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x5349
movq -0xe8(%rbp), %rax
movzbl 0x1(%rax), %eax
cmpl $0x2e, %eax
jne 0x5349
movq -0xc0(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq -0xa8(%rbp), %rdx
callq 0x2710
movq -0xc0(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x534b
jmp 0x534d
jmp 0x534f
jmp 0x5351
cmpl $0x0, -0x14(%rbp)
jne 0x537a
leaq -0x68(%rbp), %rdi
callq 0x4cc0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x5378
movq -0xc0(%rbp), %rdi
callq 0x39b0
movl %eax, -0x14(%rbp)
jmp 0x537a
jmp 0x537c
jmp 0x537e
movl -0x14(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw (%rax,%rax)