// File: /workspace/github_c_makefile_star2_O3/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_write
FRESULT f_write (
	FIL* fp,			/* Pointer to the file object */
	const void* buff,	/* Pointer to the data to be written */
	UINT btw,			/* Number of bytes to write */
	UINT* bw			/* Pointer to number of bytes written */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD clst, sect;
	UINT wcnt, cc, csect;
	const BYTE *wbuff = (const BYTE*)buff;


	*bw = 0;	/* Clear write byte counter */
	res = validate(&fp->obj, &fs);			/* Check validity of the file object */
	if (res != FR_OK || (res = (FRESULT)fp->err) != FR_OK) LEAVE_FF(fs, res);	/* Check validity */
	if (!(fp->flag & FA_WRITE)) LEAVE_FF(fs, FR_DENIED);	/* Check access mode */

	/* Check fptr wrap-around (file size cannot reach 4 GiB at FAT volume) */
	if ((!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) && (DWORD)(fp->fptr + btw) < (DWORD)fp->fptr) {
		btw = (UINT)(0xFFFFFFFF - (DWORD)fp->fptr);
	}

	for ( ;  btw;							/* Repeat until all data written */
		btw -= wcnt, *bw += wcnt, wbuff += wcnt, fp->fptr += wcnt, fp->obj.objsize = (fp->fptr > fp->obj.objsize) ? fp->fptr : fp->obj.objsize) {
		if (fp->fptr % SS(fs) == 0) {		/* On the sector boundary? */
			csect = (UINT)(fp->fptr / SS(fs)) & (fs->csize - 1);	/* Sector offset in the cluster */
			if (csect == 0) {				/* On the cluster boundary? */
				if (fp->fptr == 0) {		/* On the top of the file? */
					clst = fp->obj.sclust;	/* Follow from the origin */
					if (clst == 0) {		/* If no cluster is allocated, */
						clst = create_chain(&fp->obj, 0);	/* create a new cluster chain */
					}
				} else {					/* On the middle or end of the file */
#if FF_USE_FASTSEEK
					if (fp->cltbl) {
						clst = clmt_clust(fp, fp->fptr);	/* Get cluster# from the CLMT */
					} else
#endif
					{
						clst = create_chain(&fp->obj, fp->clust);	/* Follow or stretch cluster chain on the FAT */
					}
				}
				if (clst == 0) break;		/* Could not allocate a new cluster (disk full) */
				if (clst == 1) ABORT(fs, FR_INT_ERR);
				if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
				fp->clust = clst;			/* Update current cluster */
				if (fp->obj.sclust == 0) fp->obj.sclust = clst;	/* Set start cluster if the first write */
			}
#if FF_FS_TINY
			if (fs->winsect == fp->sect && sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);	/* Write-back sector cache */
#else
			if (fp->flag & FA_DIRTY) {		/* Write-back sector cache */
				if (disk_write(fs->pdrv, fp->buf, fp->sect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);
				fp->flag &= (BYTE)~FA_DIRTY;
			}
#endif
			sect = clst2sect(fs, fp->clust);	/* Get current sector */
			if (sect == 0) ABORT(fs, FR_INT_ERR);
			sect += csect;
			cc = btw / SS(fs);				/* When remaining bytes >= sector size, */
			if (cc > 0) {					/* Write maximum contiguous sectors directly */
				if (csect + cc > fs->csize) {	/* Clip at cluster boundary */
					cc = fs->csize - csect;
				}
				if (disk_write(fs->pdrv, wbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);
#if FF_FS_MINIMIZE <= 2
#if FF_FS_TINY
				if (fs->winsect - sect < cc) {	/* Refill sector cache if it gets invalidated by the direct write */
					mem_cpy(fs->win, wbuff + ((fs->winsect - sect) * SS(fs)), SS(fs));
					fs->wflag = 0;
				}
#else
				if (fp->sect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
					mem_cpy(fp->buf, wbuff + ((fp->sect - sect) * SS(fs)), SS(fs));
					fp->flag &= (BYTE)~FA_DIRTY;
				}
#endif
#endif
				wcnt = SS(fs) * cc;		/* Number of bytes transferred */
				continue;
			}
#if FF_FS_TINY
			if (fp->fptr >= fp->obj.objsize) {	/* Avoid silly cache filling on the growing edge */
				if (sync_window(fs) != FR_OK) ABORT(fs, FR_DISK_ERR);
				fs->winsect = sect;
			}
#else
			if (fp->sect != sect && 		/* Fill sector cache with file data */
				fp->fptr < fp->obj.objsize &&
				disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK) {
					ABORT(fs, FR_DISK_ERR);
			}
#endif
			fp->sect = sect;
		}
		wcnt = SS(fs) - (UINT)fp->fptr % SS(fs);	/* Number of bytes left in the sector */
		if (wcnt > btw) wcnt = btw;					/* Clip it by btw if needed */
#if FF_FS_TINY
		if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR);	/* Move sector window */
		mem_cpy(fs->win + fp->fptr % SS(fs), wbuff, wcnt);	/* Fit data to the sector */
		fs->wflag = 1;
#else
		mem_cpy(fp->buf + fp->fptr % SS(fs), wbuff, wcnt);	/* Fit data to the sector */
		fp->flag |= FA_DIRTY;
#endif
	}

	fp->flag |= FA_MODIFIED;				/* Set file change flag */

	LEAVE_FF(fs, FR_OK);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r13
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
movl $0x0, (%rcx)
leaq -0x48(%rbp), %rsi
callq 0x23af
testl %eax, %eax
jne 0x26af
movzbl 0x21(%rbx), %eax
testl %eax, %eax
jne 0x26af
movb 0x20(%rbx), %cl
movl $0x7, %eax
testb $0x2, %cl
je 0x26af
movq 0x28(%rbx), %rax
movl %r12d, %edx
addq %rax, %rdx
movl %eax, %r14d
notl %r14d
cmovael %r12d, %r14d
testl %r14d, %r14d
je 0x2691
movq %r13, -0x40(%rbp)
movq -0x48(%rbp), %rdi
leaq 0x50(%rbx), %r12
movq %rdi, -0x30(%rbp)
movq %r12, -0x38(%rbp)
movq %rax, %rcx
andq $0x1ff, %rcx            # imm = 0x1FF
jne 0x25b4
movq %rax, %rcx
shrq $0x9, %rcx
movzwl 0xa(%rdi), %r13d
decl %r13d
andl %ecx, %r13d
jne 0x2506
testq %rax, %rax
je 0x24c9
movq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x26be
movq -0x30(%rbp), %rdi
jmp 0x24d6
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x2687
testq %rax, %rax
je 0x268e
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x26a6
cmpq $0x1, %rax
je 0x269b
movq %rax, 0x30(%rbx)
cmpq $0x0, 0x10(%rbx)
jne 0x2506
movq %rax, 0x10(%rbx)
cmpb $0x0, 0x20(%rbx)
jns 0x2531
movzbl 0x1(%rdi), %edi
movq 0x38(%rbx), %rdx
movq %r12, %rsi
movl $0x1, %ecx
callq 0x131f
testl %eax, %eax
jne 0x26a6
andb $0x7f, 0x20(%rbx)
movq -0x30(%rbp), %rdi
movq 0x30(%rbx), %r12
addq $-0x2, %r12
movq 0x20(%rdi), %rax
addq $-0x2, %rax
cmpq %rax, %r12
jae 0x269b
movzwl 0xa(%rdi), %eax
imulq %rax, %r12
addq 0x48(%rdi), %r12
je 0x269b
movl %r13d, %ecx
addq %rcx, %r12
cmpl $0x200, %r14d           # imm = 0x200
jae 0x261b
movq 0x28(%rbx), %rax
cmpq %r12, 0x38(%rbx)
je 0x25a4
cmpq 0x18(%rbx), %rax
jae 0x25a4
movzbl 0x1(%rdi), %edi
movq -0x38(%rbp), %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x12c4
testl %eax, %eax
jne 0x26a6
movq 0x28(%rbx), %rax
movq -0x30(%rbp), %rdi
movq %r12, 0x38(%rbx)
movl %eax, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movq -0x38(%rbp), %r12
andl $0x1ff, %eax            # imm = 0x1FF
movl $0x200, %edx            # imm = 0x200
subl %eax, %edx
cmpl %r14d, %edx
movl %r14d, %r13d
cmovbl %edx, %r13d
addq %r12, %rcx
movl %r14d, %eax
cmpq %rdx, %rax
cmovbq %rax, %rdx
xorl %eax, %eax
movb (%r15,%rax), %sil
movb %sil, (%rcx,%rax)
incq %rax
cmpl %eax, %edx
jne 0x25d9
orb $-0x80, 0x20(%rbx)
movq -0x40(%rbp), %rax
addl %r13d, (%rax)
movl %r13d, %eax
addq %rax, %r15
addq 0x28(%rbx), %rax
movq 0x18(%rbx), %rcx
cmpq %rcx, %rax
cmovaq %rax, %rcx
movq %rax, 0x28(%rbx)
movq %rcx, 0x18(%rbx)
subl %r13d, %r14d
jne 0x248e
jmp 0x268e
movl %r14d, %ecx
shrl $0x9, %ecx
movl %r13d, %edx
movl %eax, %r13d
subl %edx, %r13d
addl %ecx, %edx
cmpl %eax, %edx
cmovbel %ecx, %r13d
movzbl 0x1(%rdi), %edi
movq %r15, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x131f
testl %eax, %eax
jne 0x26a6
movq 0x38(%rbx), %rax
subq %r12, %rax
movl %r13d, %ecx
cmpq %rcx, %rax
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %r12
jae 0x267e
shlq $0x9, %rax
addq %r15, %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
movb %dl, (%r12,%rcx)
incq %rcx
cmpl $0x200, %ecx            # imm = 0x200
jne 0x2668
andb $0x7f, 0x20(%rbx)
shll $0x9, %r13d
jmp 0x25ec
xorl %esi, %esi
jmp 0x24bb
movb 0x20(%rbx), %cl
orb $0x40, %cl
movb %cl, 0x20(%rbx)
xorl %eax, %eax
jmp 0x26af
movb $0x2, 0x21(%rbx)
movl $0x2, %eax
jmp 0x26af
movb $0x1, 0x21(%rbx)
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq