// File: /workspace/github_c_makefile_star2_O3/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
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %r13
movq %rcx, -0x40(%rbp)
movl $0x0, (%rcx)
leaq -0x48(%rbp), %rsi
callq 0x23af
movl %eax, %r12d
testl %eax, %eax
jne 0x239d
movzbl 0x21(%r13), %r12d
testl %r12d, %r12d
jne 0x239d
movl $0x7, %r12d
testb $0x1, 0x20(%r13)
je 0x239d
movq 0x18(%r13), %rbx
movq 0x28(%r13), %rax
subq %rax, %rbx
movl %r14d, %ecx
cmpq %rcx, %rbx
cmovael %r14d, %ebx
testl %ebx, %ebx
je 0x237f
leaq 0x50(%r13), %rsi
movq %r13, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rax, %rcx
andq $0x1ff, %rcx            # imm = 0x1FF
jne 0x22b4
movq %rax, %rcx
shrq $0x9, %rcx
movq -0x48(%rbp), %r8
movzwl 0xa(%r8), %edi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addq %rdx, %rdi
andq %rcx, %rdi
testl %edi, %edi
je 0x21ab
movq 0x30(%r13), %r13
jmp 0x21f6
movq %rdi, %r12
movq %r8, %r14
testq %rax, %rax
je 0x21c8
movq (%r13), %rdi
movq 0x30(%r13), %rsi
callq 0x1fb2
movq %rax, %r13
jmp 0x21cc
movq 0x10(%r13), %r13
cmpq $0x2, %r13
jb 0x238e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r13
je 0x2384
movq -0x30(%rbp), %rax
movq %r13, 0x30(%rax)
movq -0x38(%rbp), %rsi
movq %r14, %r8
movq %r12, %rdi
addq $-0x2, %r13
movq 0x20(%r8), %rcx
addq $-0x2, %rcx
movl $0x2, %r12d
movb $0x2, %al
cmpq %rcx, %r13
jae 0x2396
movzwl 0xa(%r8), %ecx
imulq %rcx, %r13
addq 0x48(%r8), %r13
je 0x2396
movl %edi, %eax
addq %rax, %r13
cmpl $0x200, %ebx            # imm = 0x200
jae 0x230c
movq -0x30(%rbp), %rdi
movq 0x38(%rdi), %rdx
cmpq %r13, %rdx
je 0x22a1
cmpb $0x0, 0x20(%rdi)
jns 0x2279
movq %r8, %r14
movzbl 0x1(%r8), %edi
movl $0x1, %r12d
movl $0x1, %ecx
callq 0x131f
testl %eax, %eax
jne 0x238a
movq -0x30(%rbp), %rax
andb $0x7f, 0x20(%rax)
movq -0x38(%rbp), %rsi
movq %r14, %r8
movzbl 0x1(%r8), %edi
movl $0x1, %r12d
movq %r13, %rdx
movl $0x1, %ecx
callq 0x12c4
movq -0x38(%rbp), %rsi
testl %eax, %eax
movq -0x30(%rbp), %rdi
jne 0x238a
movq %r13, 0x38(%rdi)
movq 0x28(%rdi), %rax
movl %eax, %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movq %rdi, %r13
andl $0x1ff, %eax            # imm = 0x1FF
movl $0x200, %edx            # imm = 0x200
subl %eax, %edx
cmpl %ebx, %edx
movl %ebx, %r14d
cmovbl %edx, %r14d
addq %rsi, %rcx
movl %ebx, %eax
cmpq %rdx, %rax
cmovbq %rax, %rdx
xorl %eax, %eax
movb (%rcx,%rax), %dil
movb %dil, (%r15,%rax)
incq %rax
cmpl %eax, %edx
jne 0x22d7
movq -0x40(%rbp), %rax
addl %r14d, (%rax)
movl %r14d, %eax
addq %rax, %r15
addq 0x28(%r13), %rax
xorl %r12d, %r12d
movq %rax, 0x28(%r13)
subl %r14d, %ebx
jne 0x2176
jmp 0x239d
movl %ebx, %eax
shrl $0x9, %eax
leal (%rax,%rdi), %edx
movl %ecx, %r14d
subl %edi, %r14d
cmpl %ecx, %edx
cmovbel %eax, %r14d
movzbl 0x1(%r8), %edi
movq %r15, %rsi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x12c4
testl %eax, %eax
jne 0x2384
movq -0x30(%rbp), %rax
cmpb $0x0, 0x20(%rax)
movq -0x38(%rbp), %rsi
jns 0x2372
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
subq %r13, %rax
movl %r14d, %ecx
cmpq %rcx, %rax
jae 0x2372
shlq $0x9, %rax
addq %r15, %rax
xorl %ecx, %ecx
movb (%rsi,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpl $0x200, %ecx            # imm = 0x200
jne 0x2361
shll $0x9, %r14d
movq -0x30(%rbp), %r13
jmp 0x22e6
xorl %r12d, %r12d
jmp 0x239d
movl $0x1, %r12d
movb $0x1, %al
jmp 0x2396
movl $0x2, %r12d
movb $0x2, %al
movq -0x30(%rbp), %rcx
movb %al, 0x21(%rcx)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq