// File: /workspace/github_c_makefile_star2_O2/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_sync
FRESULT f_sync (
	FIL* fp		/* Pointer to the file object */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD tm;
	BYTE *dir;


	res = validate(&fp->obj, &fs);	/* Check validity of the file object */
	if (res == FR_OK) {
		if (fp->flag & FA_MODIFIED) {	/* Is there any change to the file? */
#if !FF_FS_TINY
			if (fp->flag & FA_DIRTY) {	/* Write-back cached data if needed */
				if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) LEAVE_FF(fs, FR_DISK_ERR);
				fp->flag &= (BYTE)~FA_DIRTY;
			}
#endif
			/* Update the directory entry */
			tm = GET_FATTIME();				/* Modified time */
#if FF_FS_EXFAT
			if (fs->fs_type == FS_EXFAT) {
				res = fill_first_frag(&fp->obj);	/* Fill first fragment on the FAT if needed */
				if (res == FR_OK) {
					res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF);	/* Fill last fragment on the FAT if needed */
				}
				if (res == FR_OK) {
					DIR dj;
					DEF_NAMBUF

					INIT_NAMBUF(fs);
					res = load_obj_xdir(&dj, &fp->obj);	/* Load directory entry block */
					if (res == FR_OK) {
						fs->dirbuf[XDIR_Attr] |= AM_ARC;				/* Set archive attribute to indicate that the file has been changed */
						fs->dirbuf[XDIR_GenFlags] = fp->obj.stat | 1;	/* Update file allocation information */
						st_dword(fs->dirbuf + XDIR_FstClus, fp->obj.sclust);
						st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize);
						st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize);
						st_dword(fs->dirbuf + XDIR_ModTime, tm);		/* Update modified time */
						fs->dirbuf[XDIR_ModTime10] = 0;
						st_dword(fs->dirbuf + XDIR_AccTime, 0);
						res = store_xdir(&dj);	/* Restore it to the directory */
						if (res == FR_OK) {
							res = sync_fs(fs);
							fp->flag &= (BYTE)~FA_MODIFIED;
						}
					}
					FREE_NAMBUF();
				}
			} else
#endif
			{
				res = move_window(fs, fp->dir_sect);
				if (res == FR_OK) {
					dir = fp->dir_ptr;
					dir[DIR_Attr] |= AM_ARC;						/* Set archive attribute to indicate that the file has been changed */
					st_clust(fp->obj.fs, dir, fp->obj.sclust);		/* Update file allocation information  */
					st_dword(dir + DIR_FileSize, (DWORD)fp->obj.objsize);	/* Update file size */
					st_dword(dir + DIR_ModTime, tm);				/* Update modified time */
					st_word(dir + DIR_LstAccDate, 0);
					fs->wflag = 1;
					res = sync_fs(fs);					/* Restore it to the directory */
					fp->flag &= (BYTE)~FA_MODIFIED;
				}
			}
		}
	}

	LEAVE_FF(fs, res);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
leaq -0x28(%rbp), %rsi
callq 0x228f
testl %eax, %eax
je 0x2763
movl %eax, %r15d
jmp 0x276d
movb 0x20(%rbx), %al
xorl %r15d, %r15d
testb $0x40, %al
jne 0x277d
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq -0x28(%rbp), %r14
testb %al, %al
js 0x27f5
callq 0x13c3
movq %rax, %r12
movq 0x40(%rbx), %rsi
movq %r14, %rdi
callq 0x1e87
testl %eax, %eax
jne 0x275e
movq 0x48(%rbx), %r15
orb $0x20, 0xb(%r15)
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movq %r15, %rsi
callq 0x1dc1
movl 0x18(%rbx), %eax
movq %r15, %rdx
movl %eax, 0x1c(%r15)
movq %r12, %rcx
movb %cl, 0x16(%r15)
movb %ch, 0x17(%rdx)
movl %ecx, %eax
shrl $0x10, %eax
movb %al, 0x18(%r15)
shrl $0x18, %ecx
movb %cl, 0x19(%r15)
andw $0x0, 0x12(%r15)
movb $0x1, 0x3(%r14)
movq %r14, %rdi
callq 0x281f
andb $-0x41, 0x20(%rbx)
jmp 0x2770
movzbl 0x1(%r14), %edi
leaq 0x50(%rbx), %rsi
movq 0x38(%rbx), %rdx
pushq $0x1
popq %r15
movl %r15d, %ecx
callq 0x131f
testl %eax, %eax
jne 0x276d
andb $0x7f, 0x20(%rbx)
jmp 0x2785