// File: /workspace/github_c_makefile_star2_O2/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_read
FRESULT f_read (
	FIL* fp, 	/* Pointer to the file object */
	void* buff,	/* Pointer to data buffer */
	UINT btr,	/* Number of bytes to read */
	UINT* br	/* Pointer to number of bytes read */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD clst, sect;
	FSIZE_t remain;
	UINT rcnt, cc, csect;
	BYTE *rbuff = (BYTE*)buff;


	*br = 0;	/* Clear read 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_READ)) LEAVE_FF(fs, FR_DENIED); /* Check access mode */
	remain = fp->obj.objsize - fp->fptr;
	if (btr > remain) btr = (UINT)remain;		/* Truncate btr by remaining bytes */

	for ( ;  btr;								/* Repeat until all data read */
		btr -= rcnt, *br += rcnt, rbuff += rcnt, fp->fptr += rcnt) {
		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 cluster chain from the origin */
				} else {						/* 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 = get_fat(&fp->obj, fp->clust);	/* Follow cluster chain on the FAT */
					}
				}
				if (clst < 2) ABORT(fs, FR_INT_ERR);
				if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
				fp->clust = clst;				/* Update current cluster */
			}
			sect = clst2sect(fs, fp->clust);	/* Get current sector */
			if (sect == 0) ABORT(fs, FR_INT_ERR);
			sect += csect;
			cc = btr / SS(fs);					/* When remaining bytes >= sector size, */
			if (cc > 0) {						/* Read maximum contiguous sectors directly */
				if (csect + cc > fs->csize) {	/* Clip at cluster boundary */
					cc = fs->csize - csect;
				}
				if (disk_read(fs->pdrv, rbuff, sect, cc) != RES_OK) ABORT(fs, FR_DISK_ERR);
#if !FF_FS_READONLY && FF_FS_MINIMIZE <= 2		/* Replace one of the read sectors with cached data if it contains a dirty sector */
#if FF_FS_TINY
				if (fs->wflag && fs->winsect - sect < cc) {
					mem_cpy(rbuff + ((fs->winsect - sect) * SS(fs)), fs->win, SS(fs));
				}
#else
				if ((fp->flag & FA_DIRTY) && fp->sect - sect < cc) {
					mem_cpy(rbuff + ((fp->sect - sect) * SS(fs)), fp->buf, SS(fs));
				}
#endif
#endif
				rcnt = SS(fs) * cc;				/* Number of bytes transferred */
				continue;
			}
#if !FF_FS_TINY
			if (fp->sect != sect) {			/* Load data sector if not in cache */
#if !FF_FS_READONLY
				if (fp->flag & FA_DIRTY) {		/* Write-back dirty 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
				if (disk_read(fs->pdrv, fp->buf, sect, 1) != RES_OK)	ABORT(fs, FR_DISK_ERR);	/* Fill sector cache */
			}
#endif
			fp->sect = sect;
		}
		rcnt = SS(fs) - (UINT)fp->fptr % SS(fs);	/* Number of bytes left in the sector */
		if (rcnt > btr) rcnt = btr;					/* Clip it by btr if needed */
#if FF_FS_TINY
		if (move_window(fs, fp->sect) != FR_OK) ABORT(fs, FR_DISK_ERR);	/* Move sector window */
		mem_cpy(rbuff, fs->win + fp->fptr % SS(fs), rcnt);	/* Extract partial sector */
#else
		mem_cpy(rbuff, fp->buf + fp->fptr % SS(fs), rcnt);	/* Extract partial sector */
#endif
	}

	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, %r12
movl %edx, %r15d
movq %rsi, %r13
movq %rdi, %rbx
andl $0x0, (%rcx)
leaq -0x50(%rbp), %rsi
callq 0x228f
testl %eax, %eax
jne 0x2280
movzbl 0x21(%rbx), %eax
testl %eax, %eax
jne 0x2280
testb $0x1, 0x20(%rbx)
jne 0x208f
pushq $0x7
popq %rax
jmp 0x2280
movq 0x18(%rbx), %r14
movq 0x28(%rbx), %rax
subq %rax, %r14
movl %r15d, %ecx
cmpq %rcx, %r14
cmovael %r15d, %r14d
movq -0x50(%rbp), %rcx
movq %rcx, -0x30(%rbp)
leaq 0x50(%rbx), %rcx
movq %rcx, -0x40(%rbp)
movq %r12, -0x48(%rbp)
testl %r14d, %r14d
je 0x226e
movq %rax, %rsi
andq $0x1ff, %rsi            # imm = 0x1FF
jne 0x21bf
movq %rax, %rcx
shrq $0x9, %rcx
movq -0x30(%rbp), %rdx
movzwl 0xa(%rdx), %r12d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addq %rdx, %r12
andq %rcx, %r12
testl %r12d, %r12d
movq %r13, -0x38(%rbp)
je 0x20fb
movq 0x30(%rbx), %rsi
jmp 0x2131
testq %rax, %rax
je 0x2111
movq 0x30(%rbx), %rsi
movq %rbx, %rdi
callq 0x1ed8
movq %rax, %rsi
jmp 0x2115
movq 0x10(%rbx), %rsi
cmpq $0x2, %rsi
jb 0x2272
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %rsi
je 0x2278
movq %rsi, 0x30(%rbx)
movq -0x30(%rbp), %rdi
callq 0x201a
testq %rax, %rax
je 0x2272
movq %rax, %r13
movl %r12d, %eax
addq %rax, %r13
cmpl $0x200, %r14d           # imm = 0x200
jae 0x21fd
movq 0x38(%rbx), %rdx
cmpq %r13, %rdx
je 0x21a7
cmpb $0x0, 0x20(%rbx)
jns 0x2188
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edi
movq -0x40(%rbp), %rsi
pushq $0x1
popq %rcx
callq 0x131f
testl %eax, %eax
jne 0x2278
andb $0x7f, 0x20(%rbx)
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edi
movq -0x40(%rbp), %rsi
movq %r13, %rdx
pushq $0x1
popq %rcx
callq 0x12c4
testl %eax, %eax
jne 0x2278
movq %r13, 0x38(%rbx)
movq 0x28(%rbx), %rax
movl %eax, %esi
andl $0x1ff, %esi            # imm = 0x1FF
movq -0x48(%rbp), %r12
movq -0x38(%rbp), %r13
andl $0x1ff, %eax            # imm = 0x1FF
movl $0x200, %r15d           # imm = 0x200
subl %eax, %r15d
cmpl %r14d, %r15d
cmovael %r14d, %r15d
addq -0x40(%rbp), %rsi
movq %r13, %rdi
movl %r15d, %edx
callq 0x22ea
subl %r15d, %r14d
addl %r15d, (%r12)
movl %r15d, %eax
addq %rax, %r13
addq 0x28(%rbx), %rax
movq %rax, 0x28(%rbx)
jmp 0x20b8
movl %r14d, %eax
shrl $0x9, %eax
leal (%rax,%r12), %ecx
movq -0x30(%rbp), %rsi
movzwl 0xa(%rsi), %edx
movl %edx, %r15d
subl %r12d, %r15d
cmpl %edx, %ecx
cmovbel %eax, %r15d
movzbl 0x1(%rsi), %edi
movq -0x38(%rbp), %rsi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x12c4
testl %eax, %eax
jne 0x2278
cmpb $0x0, 0x20(%rbx)
jns 0x225d
movq 0x38(%rbx), %rdi
subq %r13, %rdi
movl %r15d, %eax
cmpq %rax, %rdi
jae 0x225d
shlq $0x9, %rdi
addq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x22ea
shll $0x9, %r15d
movq -0x48(%rbp), %r12
movq -0x38(%rbp), %r13
jmp 0x21e3
xorl %eax, %eax
jmp 0x2280
movb $0x2, %cl
pushq $0x2
jmp 0x227c
movb $0x1, %cl
pushq $0x1
popq %rax
movb %cl, 0x21(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq