// File: /workspace/github_c_makefile_star2_O1/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, %r14
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
movl $0x0, (%rcx)
leaq -0x48(%rbp), %rsi
callq 0x23bb
testl %eax, %eax
jne 0x269e
movzbl 0x21(%rbx), %eax
testl %eax, %eax
jne 0x269e
movl $0x7, %eax
testb $0x2, 0x20(%rbx)
je 0x269e
movq %r14, -0x40(%rbp)
movq 0x28(%rbx), %r14
movl %r12d, %eax
addq %r14, %rax
notl %r14d
cmovael %r12d, %r14d
testl %r14d, %r14d
je 0x2698
movq -0x48(%rbp), %rdi
leaq 0x50(%rbx), %r12
movq %rdi, -0x30(%rbp)
movq %r12, -0x38(%rbp)
movq 0x28(%rbx), %rax
testl $0x1ff, %eax            # imm = 0x1FF
jne 0x25b6
movq %rax, %rcx
shrq $0x9, %rcx
movzwl 0xa(%rdi), %r13d
decl %r13d
andl %ecx, %r13d
jne 0x250c
testq %rax, %rax
je 0x24cf
movq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x26c3
movq -0x30(%rbp), %rdi
jmp 0x24dc
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x2691
testq %rax, %rax
je 0x2698
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x26b8
cmpq $0x1, %rax
je 0x26ad
movq %rax, 0x30(%rbx)
cmpq $0x0, 0x10(%rbx)
jne 0x250c
movq %rax, 0x10(%rbx)
cmpb $0x0, 0x20(%rbx)
jns 0x2537
movzbl 0x1(%rdi), %edi
movq 0x38(%rbx), %rdx
movq %r12, %rsi
movl $0x1, %ecx
callq 0x1321
testl %eax, %eax
jne 0x26b8
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 0x255a
movzwl 0xa(%rdi), %eax
imulq %rax, %r12
addq 0x48(%rdi), %r12
jmp 0x255d
xorl %r12d, %r12d
testq %r12, %r12
je 0x26ad
movl %r13d, %eax
addq %rax, %r12
cmpl $0x200, %r14d           # imm = 0x200
jae 0x2621
cmpq %r12, 0x38(%rbx)
je 0x25ae
movq 0x28(%rbx), %rax
cmpq 0x18(%rbx), %rax
jae 0x25ae
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edi
movq -0x38(%rbp), %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x12c6
movq -0x30(%rbp), %rdi
testl %eax, %eax
jne 0x26b8
movq %r12, 0x38(%rbx)
movq -0x38(%rbp), %r12
movq 0x28(%rbx), %rax
andl $0x1ff, %eax            # imm = 0x1FF
movl $0x200, %ecx            # imm = 0x200
subl %eax, %ecx
cmpl %r14d, %ecx
movl %r14d, %r13d
cmovbl %ecx, %r13d
addq %r12, %rax
movl %r14d, %edx
cmpq %rcx, %rdx
cmovbq %rdx, %rcx
xorl %edx, %edx
movb (%r15,%rdx), %sil
movb %sil, (%rax,%rdx)
incq %rdx
cmpl %edx, %ecx
jne 0x25df
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 0x2495
jmp 0x2698
movl %r14d, %eax
shrl $0x9, %eax
movzwl 0xa(%rdi), %ecx
movl %r13d, %edx
movl %ecx, %r13d
subl %edx, %r13d
addl %eax, %edx
cmpl %ecx, %edx
cmovbel %eax, %r13d
movzbl 0x1(%rdi), %edi
movq %r15, %rsi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x1321
testl %eax, %eax
jne 0x26b8
movq 0x38(%rbx), %rax
subq %r12, %rax
movl %r13d, %ecx
cmpq %rcx, %rax
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %r12
jae 0x2688
shlq $0x9, %rax
addq %r15, %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
movb %dl, 0x50(%rbx,%rcx)
incq %rcx
cmpl $0x200, %ecx            # imm = 0x200
jne 0x2672
andb $0x7f, 0x20(%rbx)
shll $0x9, %r13d
jmp 0x25f2
xorl %esi, %esi
jmp 0x24c1
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)
movl $0x2, %eax
jmp 0x269e
movb $0x1, 0x21(%rbx)
movl $0x1, %eax
jmp 0x269e