// File: /workspace/github_c_makefile_star2_O2/lb3361[P]dosfs/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);		/* Update start cluster */
						st_qword(fs->dirbuf + XDIR_FileSize, fp->obj.objsize);		/* Update file size */
						st_qword(fs->dirbuf + XDIR_ValidFileSize, fp->obj.objsize);	/* (FatFs does not support Valid File Size feature) */
						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
pushq %r15
pushq %r14
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdi, %rbx
leaq 0x8(%rsp), %rsi
callq 0x607b
testl %eax, %eax
je 0x669c
movl %eax, %ebp
jmp 0x66a5
movb 0x30(%rbx), %al
xorl %ebp, %ebp
testb $0x40, %al
jne 0x66b5
movl %ebp, %eax
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r14
testb %al, %al
js 0x67f8
callq 0x2992
movl %eax, %r15d
cmpb $0x4, (%r14)
jne 0x6795
movq %rbx, %rdi
callq 0x6820
testl %eax, %eax
jne 0x6698
movl 0x40(%rbx), %esi
pushq $-0x1
popq %rdx
movq %rbx, %rdi
callq 0x6868
testl %eax, %eax
jne 0x6698
leaq 0x70(%rsp), %rax
movq %rax, 0x10(%r14)
leaq 0x270(%rsp), %rax
movq %rax, 0x18(%r14)
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x68a9
testl %eax, %eax
jne 0x6698
movq 0x18(%r14), %rax
orb $0x20, 0x4(%rax)
movb 0xb(%rbx), %al
orb $0x1, %al
movq 0x18(%r14), %rcx
movb %al, 0x21(%rcx)
movq 0x18(%r14), %rax
movl 0xc(%rbx), %ecx
movl %ecx, 0x34(%rax)
movq 0x18(%r14), %rdi
addq $0x38, %rdi
movq 0x10(%rbx), %rsi
callq 0x68e6
movq 0x18(%r14), %rdi
addq $0x28, %rdi
movq 0x10(%rbx), %rsi
callq 0x68e6
movq 0x18(%r14), %rax
movl %r15d, 0xc(%rax)
movq 0x18(%r14), %rax
movb $0x0, 0x15(%rax)
movq 0x18(%r14), %rax
andl $0x0, 0x10(%rax)
leaq 0x10(%rsp), %rdi
callq 0x59d7
testl %eax, %eax
jne 0x6698
movq %r14, %rdi
callq 0x68ea
movl %eax, %ebp
andb $-0x41, 0x30(%rbx)
jmp 0x66a5
movq 0x50(%rbx), %rsi
movq %r14, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0x6698
movq 0x58(%rbx), %rbp
orb $0x20, 0xb(%rbp)
movq (%rbx), %rdi
movl 0xc(%rbx), %edx
movq %rbp, %rsi
callq 0x5bde
movl 0x10(%rbx), %eax
movl %eax, 0x1c(%rbp)
movl %r15d, %ecx
movb %cl, 0x16(%rbp)
movb %ch, 0x17(%rbp)
movl %r15d, %eax
shrl $0x10, %eax
movb %al, 0x18(%rbp)
shrl $0x18, %ecx
movb %cl, 0x19(%rbp)
andw $0x0, 0x12(%rbp)
movb $0x1, 0x3(%r14)
movq %r14, %rdi
callq 0x68ea
andb $-0x41, 0x30(%rbx)
jmp 0x66a7
movzbl 0x1(%r14), %edi
leaq 0x60(%rbx), %rsi
movq 0x48(%rbx), %rdx
pushq $0x1
popq %rbp
movl %ebp, %ecx
callq 0x28af
testl %eax, %eax
jne 0x66a5
andb $0x7f, 0x30(%rbx)
jmp 0x66c2