// File: /workspace/github_c_makefile_star2_O2/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
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
movq %rcx, -0x48(%rbp)
andl $0x0, (%rcx)
leaq -0x50(%rbp), %rsi
callq 0x228f
testl %eax, %eax
jne 0x2570
movzbl 0x21(%rbx), %eax
testl %eax, %eax
jne 0x2570
testb $0x2, 0x20(%rbx)
jne 0x234d
pushq $0x7
popq %rax
jmp 0x2570
movq %r15, -0x38(%rbp)
movq 0x28(%rbx), %rax
movl %r14d, %ecx
addq %rax, %rcx
movl %eax, %r15d
notl %r15d
cmovael %r14d, %r15d
movq -0x50(%rbp), %rcx
movq %rcx, -0x30(%rbp)
leaq 0x50(%rbx), %rcx
movq %rcx, -0x40(%rbp)
movq -0x48(%rbp), %r14
testl %r15d, %r15d
je 0x256a
movq %rax, %rdi
andq $0x1ff, %rdi            # imm = 0x1FF
je 0x2397
movq -0x38(%rbp), %r12
jmp 0x250f
movq %rax, %rcx
shrq $0x9, %rcx
movq -0x30(%rbp), %rdx
movzwl 0xa(%rdx), %r12d
decl %r12d
andl %ecx, %r12d
jne 0x23ff
testq %rax, %rax
je 0x23c2
movq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x2595
jmp 0x23cf
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x2563
testq %rax, %rax
je 0x256a
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x258a
cmpq $0x1, %rax
je 0x257f
movq %rax, 0x30(%rbx)
cmpq $0x0, 0x10(%rbx)
jne 0x23ff
movq %rax, 0x10(%rbx)
cmpb $0x0, 0x20(%rbx)
jns 0x2429
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edi
movq 0x38(%rbx), %rdx
movq -0x40(%rbp), %rsi
pushq $0x1
popq %rcx
callq 0x131f
testl %eax, %eax
jne 0x258a
andb $0x7f, 0x20(%rbx)
movq 0x30(%rbx), %rsi
movq -0x30(%rbp), %rdi
callq 0x201a
testq %rax, %rax
je 0x257f
movq %rax, %r14
movl %r12d, %eax
addq %rax, %r14
cmpl $0x200, %r15d           # imm = 0x200
jae 0x2464
movq 0x28(%rbx), %rax
cmpq %r14, 0x38(%rbx)
jne 0x24d2
movq -0x38(%rbp), %r12
jmp 0x24ff
movl %r15d, %eax
shrl $0x9, %eax
movq -0x30(%rbp), %rdx
movzwl 0xa(%rdx), %ecx
movl %ecx, %r13d
subl %r12d, %r13d
addl %eax, %r12d
cmpl %ecx, %r12d
cmovbel %eax, %r13d
movzbl 0x1(%rdx), %edi
movq -0x38(%rbp), %r12
movq %r12, %rsi
movq %r14, %rdx
movl %r13d, %ecx
callq 0x131f
testl %eax, %eax
jne 0x258a
movq 0x38(%rbx), %rsi
subq %r14, %rsi
movl %r13d, %eax
cmpq %rax, %rsi
jae 0x24c8
shlq $0x9, %rsi
addq %r12, %rsi
movq -0x40(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
callq 0x22ea
andb $0x7f, 0x20(%rbx)
shll $0x9, %r13d
movq -0x48(%rbp), %r14
jmp 0x2537
cmpq 0x18(%rbx), %rax
movq -0x38(%rbp), %r12
jae 0x24ff
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edi
movq -0x40(%rbp), %rsi
movq %r14, %rdx
pushq $0x1
popq %rcx
callq 0x12c4
testl %eax, %eax
jne 0x258a
movq 0x28(%rbx), %rax
movq %r14, 0x38(%rbx)
movl %eax, %edi
andl $0x1ff, %edi            # imm = 0x1FF
movq -0x48(%rbp), %r14
andl $0x1ff, %eax            # imm = 0x1FF
movl $0x200, %r13d           # imm = 0x200
subl %eax, %r13d
cmpl %r15d, %r13d
cmovael %r15d, %r13d
addq -0x40(%rbp), %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x22ea
orb $-0x80, 0x20(%rbx)
addl %r13d, (%r14)
movl %r13d, %eax
addq %rax, %r12
movq %r12, -0x38(%rbp)
addq 0x28(%rbx), %rax
subl %r13d, %r15d
movq %rax, 0x28(%rbx)
movq 0x18(%rbx), %rcx
cmpq %rcx, %rax
cmovaq %rax, %rcx
movq %rcx, 0x18(%rbx)
jmp 0x2379
xorl %esi, %esi
jmp 0x23b8
orb $0x40, 0x20(%rbx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x2, 0x21(%rbx)
pushq $0x2
jmp 0x2347
movb $0x1, 0x21(%rbx)
pushq $0x1
jmp 0x2347