// File: /workspace/github_c_makefile_star2_O0/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
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, -0x68(%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 0x2c7f
movq -0x10(%rbp), %rax
movzbl 0x21(%rax), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
je 0x2c8a
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2fcc
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x2ca6
movl $0x7, -0x4(%rbp)
jmp 0x2fcc
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
subq 0x28(%rcx), %rax
movq %rax, -0x50(%rbp)
movl -0x1c(%rbp), %eax
cmpq -0x50(%rbp), %rax
jbe 0x2cca
movq -0x50(%rbp), %rax
movl %eax, -0x1c(%rbp)
jmp 0x2ccc
cmpl $0x0, -0x1c(%rbp)
je 0x2fc5
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
andq $0x1ff, %rax            # imm = 0x1FF
cmpq $0x0, %rax
jne 0x2f3c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
shrq $0x9, %rax
movq -0x38(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
andq %rcx, %rax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x2d88
movq -0x10(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x2d2d
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x2d42
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x29e0
movq %rax, -0x40(%rbp)
cmpq $0x2, -0x40(%rbp)
jae 0x2d5d
movq -0x10(%rbp), %rax
movb $0x2, 0x21(%rax)
movl $0x2, -0x4(%rbp)
jmp 0x2fcc
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x40(%rbp)
jne 0x2d7c
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x2fcc
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x2bd0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2db8
movq -0x10(%rbp), %rax
movb $0x2, 0x21(%rax)
movl $0x2, -0x4(%rbp)
jmp 0x2fcc
movl -0x5c(%rbp), %eax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
shrl $0x9, %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jbe 0x2e86
movl -0x5c(%rbp), %eax
addl -0x58(%rbp), %eax
movq -0x38(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
cmpl %ecx, %eax
jbe 0x2df6
movq -0x38(%rbp), %rax
movzwl 0xa(%rax), %eax
subl -0x5c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movb 0x1(%rax), %al
movq -0x68(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x58(%rbp), %ecx
movzbl %al, %edi
callq 0x1450
cmpl $0x0, %eax
je 0x2e29
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x2fcc
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x2e78
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
subq -0x48(%rbp), %rax
movl -0x58(%rbp), %ecx
cmpq %rcx, %rax
jae 0x2e78
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
subq -0x48(%rbp), %rax
shlq $0x9, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x50, %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x3090
movl -0x58(%rbp), %eax
shll $0x9, %eax
movl %eax, -0x54(%rbp)
jmp 0x2f89
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x48(%rbp), %rax
je 0x2f30
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x2ef5
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 0x2ee7
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x2fcc
movq -0x10(%rbp), %rax
movzbl 0x20(%rax), %ecx
andl $0x7f, %ecx
movb %cl, 0x20(%rax)
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 0x2f2e
movq -0x10(%rbp), %rax
movb $0x1, 0x21(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x2fcc
jmp 0x2f30
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, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jbe 0x2f64
movl -0x1c(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x50, %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
andq $0x1ff, %rax            # imm = 0x1FF
addq %rax, %rsi
movl -0x54(%rbp), %edx
callq 0x3090
movl -0x54(%rbp), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x54(%rbp), %ecx
movq -0x28(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x54(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x54(%rbp), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq 0x28(%rax), %rcx
movq %rcx, 0x28(%rax)
jmp 0x2ccc
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)