// File: /workspace/github_c_makefile_star2_O1/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, %r13
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
movl $0x0, (%rcx)
leaq -0x48(%rbp), %rsi
callq 0x23bb
movl %eax, %r14d
testl %eax, %eax
jne 0x23a9
movq %rbx, %rdi
movzbl 0x21(%rbx), %r14d
testl %r14d, %r14d
jne 0x23a9
movl $0x7, %r14d
testb $0x1, 0x20(%rdi)
je 0x23a9
movq 0x18(%rdi), %rbx
subq 0x28(%rdi), %rbx
movl %r12d, %eax
cmpq %rax, %rbx
cmovael %r12d, %ebx
testl %ebx, %ebx
je 0x2377
leaq 0x50(%rdi), %rsi
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %r13, -0x40(%rbp)
movq 0x28(%rdi), %rax
testl $0x1ff, %eax            # imm = 0x1FF
jne 0x22a9
movq %rax, %rcx
shrq $0x9, %rcx
movq -0x48(%rbp), %r12
movzwl 0xa(%r12), %r14d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addq %rdx, %r14
andq %rcx, %r14
testl %r14d, %r14d
jne 0x21fa
testq %rax, %rax
je 0x21d2
movq 0x30(%rdi), %rsi
callq 0x1fc1
jmp 0x21d6
movq 0x10(%rdi), %rax
cmpq $0x2, %rax
jb 0x2390
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
movq -0x30(%rbp), %rdi
je 0x239e
movq %rax, 0x30(%rdi)
movq -0x38(%rbp), %rsi
movq 0x30(%rdi), %r13
addq $-0x2, %r13
movq 0x20(%r12), %rax
addq $-0x2, %rax
cmpq %rax, %r13
jae 0x2221
movzwl 0xa(%r12), %eax
imulq %rax, %r13
addq 0x48(%r12), %r13
jmp 0x2224
xorl %r13d, %r13d
testq %r13, %r13
je 0x237c
movl %r14d, %eax
addq %rax, %r13
cmpl $0x200, %ebx            # imm = 0x200
jae 0x22fe
movq 0x38(%rdi), %rdx
cmpq %r13, %rdx
je 0x22a1
cmpb $0x0, 0x20(%rdi)
jns 0x2278
movzbl 0x1(%r12), %edi
movl $0x1, %r14d
movl $0x1, %ecx
callq 0x1321
testl %eax, %eax
jne 0x238c
movq -0x30(%rbp), %rax
andb $0x7f, 0x20(%rax)
movq -0x38(%rbp), %rsi
movzbl 0x1(%r12), %edi
movl $0x1, %r14d
movq %r13, %rdx
movl $0x1, %ecx
callq 0x12c6
movq -0x38(%rbp), %rsi
testl %eax, %eax
movq -0x30(%rbp), %rdi
jne 0x23a4
movq %r13, 0x38(%rdi)
movq -0x40(%rbp), %r13
movq 0x28(%rdi), %rax
andl $0x1ff, %eax            # imm = 0x1FF
movl $0x200, %ecx            # imm = 0x200
subl %eax, %ecx
cmpl %ebx, %ecx
movl %ebx, %r12d
cmovbl %ecx, %r12d
addq %rsi, %rax
movl %ebx, %edx
cmpq %rcx, %rdx
cmovbq %rdx, %rcx
xorl %edx, %edx
movb (%rax,%rdx), %r8b
movb %r8b, (%r15,%rdx)
incq %rdx
cmpl %edx, %ecx
jne 0x22d0
addl %r12d, (%r13)
movl %r12d, %eax
addq %rax, 0x28(%rdi)
addq %rax, %r15
xorl %r14d, %r14d
subl %r12d, %ebx
jne 0x2192
jmp 0x23a9
movl %ebx, %eax
shrl $0x9, %eax
leal (%rax,%r14), %ecx
movzwl 0xa(%r12), %edx
movq %r12, %rsi
movl %edx, %r12d
subl %r14d, %r12d
cmpl %edx, %ecx
cmovbel %eax, %r12d
movzbl 0x1(%rsi), %edi
movq %r15, %rsi
movq %r13, %rdx
movl %r12d, %ecx
callq 0x12c6
testl %eax, %eax
jne 0x2386
movq -0x30(%rbp), %rdi
cmpb $0x0, 0x20(%rdi)
movq -0x38(%rbp), %rsi
jns 0x236a
movq 0x38(%rdi), %rax
subq %r13, %rax
movl %r12d, %ecx
cmpq %rcx, %rax
jae 0x236a
shlq $0x9, %rax
addq %r15, %rax
xorl %ecx, %ecx
movb 0x50(%rdi,%rcx), %dl
movb %dl, (%rax,%rcx)
incq %rcx
cmpl $0x200, %ecx            # imm = 0x200
jne 0x2358
shll $0x9, %r12d
movq -0x40(%rbp), %r13
jmp 0x22df
xorl %r14d, %r14d
jmp 0x23a9
movl $0x2, %r14d
movb $0x2, %al
jmp 0x23a6
movl $0x1, %r14d
movb $0x1, %al
jmp 0x2398
movl $0x2, %r14d
movb $0x2, %al
movq -0x30(%rbp), %rdi
jmp 0x23a6
movl $0x1, %r14d
movb $0x1, %al
movb %al, 0x21(%rdi)
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq