// File: /workspace/github_c_makefile_star2_O3/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 0x65ec
testl %eax, %eax
je 0x6c2c
movl %eax, %ebp
jmp 0x6c35
movb 0x30(%rbx), %al
xorl %ebp, %ebp
testb $0x40, %al
jne 0x6c45
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 0x6d97
callq 0x2e66
movl %eax, %r15d
cmpb $0x4, (%r14)
jne 0x6d24
movq %rbx, %rdi
callq 0x6dc4
testl %eax, %eax
jne 0x6c28
movl 0x40(%rbx), %esi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6e11
testl %eax, %eax
jne 0x6c28
leaq 0x70(%rsp), %rax
movq %rax, 0x10(%r14)
leaq 0x270(%rsp), %rax
movq %rax, 0x18(%r14)
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x6e63
testl %eax, %eax
jne 0x6c28
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), %rax
movq 0x10(%rbx), %rcx
movq %rcx, 0x38(%rax)
movq 0x18(%r14), %rax
movq 0x10(%rbx), %rcx
movq %rcx, 0x28(%rax)
movq 0x18(%r14), %rax
movl %r15d, 0xc(%rax)
movq 0x18(%r14), %rax
movb $0x0, 0x15(%rax)
movq 0x18(%r14), %rax
movl $0x0, 0x10(%rax)
leaq 0x10(%rsp), %rdi
callq 0x5ee7
testl %eax, %eax
jne 0x6c28
movq %r14, %rdi
callq 0x6ea3
movl %eax, %ebp
andb $-0x41, 0x30(%rbx)
jmp 0x6c35
movq 0x50(%rbx), %rsi
movq %r14, %rdi
callq 0x6114
testl %eax, %eax
jne 0x6c28
movq 0x58(%rbx), %rax
orb $0x20, 0xb(%rax)
movq (%rbx), %rdx
movl 0xc(%rbx), %ecx
movw %cx, 0x1a(%rax)
cmpb $0x3, (%rdx)
jne 0x6d5d
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x14(%rax)
shrl $0x18, %ecx
movb %cl, 0x15(%rax)
movl 0x10(%rbx), %ecx
movl %ecx, 0x1c(%rax)
movl %r15d, %edx
movb %dl, 0x16(%rax)
movb %dh, 0x17(%rax)
movl %r15d, %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 0x6ea3
andb $-0x41, 0x30(%rbx)
jmp 0x6c37
movzbl 0x1(%r14), %edi
leaq 0x60(%rbx), %rsi
movq 0x48(%rbx), %rdx
movl $0x1, %ebp
movl $0x1, %ecx
callq 0x2d75
testl %eax, %eax
jne 0x6c35
andb $0x7f, 0x30(%rbx)
jmp 0x6c52