// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x2fe0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x313f
movq -0x10(%rbp), %rax
movzbl 0x21(%rax), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
je 0x314a
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x352c
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x3166
movl $0x7, -0x4(%rbp)
jmp 0x352c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x1c(%rbp), %ecx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x318e
movq -0x10(%rbp), %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subq 0x28(%rcx), %rax
movl %eax, -0x1c(%rbp)
jmp 0x3190
cmpl $0x0, -0x1c(%rbp)
je 0x3517
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
andq $0x1ff, %rax            # imm = 0x1FF
cmpq $0x0, %rax
jne 0x3445
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
shrq $0x9, %rax
movq -0x38(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
subl $0x1, %ecx
andl %ecx, %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0x3289
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x3209
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x3207
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3540
movq %rax, -0x40(%rbp)
jmp 0x321e
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x3540
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x322a
jmp 0x3517
cmpq $0x1, -0x40(%rbp)
jne 0x3245
movq -0x10(%rbp), %rax
movb $0x2, 0x21(%rax)
movl $0x2, -0x4(%rbp)
jmp 0x352c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x40(%rbp)
jne 0x3264
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x352c
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x3287
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x3289
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x32e6
movq -0x38(%rbp), %rax
movb 0x1(%rax), %al
movq -0x10(%rbp), %rsi
addq $0x50, %rsi
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rdx
movl $0x1, %ecx
movzbl %al, %edi
callq 0x14c0
cmpl $0x0, %eax
je 0x32d8
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x352c
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %ecx
andl $0x7f, %ecx
movb %cl, 0x20(%rax)
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x2bd0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x3316
movq -0x10(%rbp), %rax
movb $0x2, 0x21(%rax)
movl $0x2, -0x4(%rbp)
jmp 0x352c
movl -0x54(%rbp), %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
shrl $0x9, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jbe 0x33e0
movl -0x54(%rbp), %eax
addl -0x50(%rbp), %eax
movq -0x38(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
cmpl %ecx, %eax
jbe 0x3354
movq -0x38(%rbp), %rax
movzwl 0xa(%rax), %eax
subl -0x54(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movb 0x1(%rax), %al
movq -0x60(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x50(%rbp), %ecx
movzbl %al, %edi
callq 0x14c0
cmpl $0x0, %eax
je 0x3387
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x352c
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
subq -0x48(%rbp), %rax
movl -0x50(%rbp), %ecx
cmpq %rcx, %rax
jae 0x33d2
movq -0x10(%rbp), %rdi
addq $0x50, %rdi
movq -0x60(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
subq -0x48(%rbp), %rax
shlq $0x9, %rax
addq %rax, %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x3090
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %ecx
andl $0x7f, %ecx
movb %cl, 0x20(%rax)
movl -0x50(%rbp), %eax
shll $0x9, %eax
movl %eax, -0x4c(%rbp)
jmp 0x34a3
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x48(%rbp), %rax
je 0x3439
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jae 0x3439
movq -0x38(%rbp), %rax
movb 0x1(%rax), %al
movq -0x10(%rbp), %rsi
addq $0x50, %rsi
movq -0x48(%rbp), %rdx
movl $0x1, %ecx
movzbl %al, %edi
callq 0x1450
cmpl $0x0, %eax
je 0x3439
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x352c
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jbe 0x346d
movl -0x1c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
addq $0x50, %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
andq $0x1ff, %rax            # imm = 0x1FF
addq %rax, %rdi
movq -0x60(%rbp), %rsi
movl -0x4c(%rbp), %edx
callq 0x3090
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %ecx
orl $0x80, %ecx
movb %cl, 0x20(%rax)
movl -0x4c(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x4c(%rbp), %ecx
movq -0x28(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x4c(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq 0x28(%rax), %rcx
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jbe 0x34fa
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x3506
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x3190
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x20(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)