// File: /workspace/github_c_makefile_star2_O3/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 0x23af
testl %eax, %eax
je 0x288b
movl %eax, %r15d
jmp 0x2895
movb 0x20(%rbx), %al
xorl %r15d, %r15d
testb $0x40, %al
jne 0x28a5
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 0x2924
callq 0x13c5
movq %rax, %r12
movq 0x40(%rbx), %rsi
movq %r14, %rdi
callq 0x1f5d
testl %eax, %eax
jne 0x2886
movq 0x48(%rbx), %rax
orb $0x20, 0xb(%rax)
movq (%rbx), %rdx
movq 0x10(%rbx), %rcx
movw %cx, 0x1a(%rax)
cmpb $0x3, (%rdx)
jne 0x28eb
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x14(%rax)
shrl $0x18, %ecx
movb %cl, 0x15(%rax)
movl 0x18(%rbx), %ecx
movl %ecx, 0x1c(%rax)
movq %r12, %rdx
movb %dl, 0x16(%rax)
movb %dh, 0x17(%rax)
movl %edx, %ecx
shrl $0x10, %ecx
movb %cl, 0x18(%rax)
shrl $0x18, %edx
movb %dl, 0x19(%rax)
movw $0x0, 0x12(%rax)
movb $0x1, 0x3(%r14)
movq %r14, %rdi
callq 0x2952
andb $-0x41, 0x20(%rbx)
jmp 0x2898
movzbl 0x1(%r14), %edi
leaq 0x50(%rbx), %rsi
movq 0x38(%rbx), %rdx
movl $0x1, %r15d
movl $0x1, %ecx
callq 0x131f
testl %eax, %eax
jne 0x2895
andb $0x7f, 0x20(%rbx)
jmp 0x28ad