// File: /workspace/github_c_makefile_star2_O3/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_lseek
FRESULT f_lseek (
	FIL* fp,		/* Pointer to the file object */
	FSIZE_t ofs		/* File pointer from top of file */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD clst, bcs, nsect;
	FSIZE_t ifptr;
#if FF_USE_FASTSEEK
	DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
#endif

	res = validate(&fp->obj, &fs);		/* Check validity of the file object */
	if (res == FR_OK) res = (FRESULT)fp->err;
#if FF_FS_EXFAT && !FF_FS_READONLY
	if (res == FR_OK && fs->fs_type == FS_EXFAT) {
		res = fill_last_frag(&fp->obj, fp->clust, 0xFFFFFFFF);	/* Fill last fragment on the FAT if needed */
	}
#endif
	if (res != FR_OK) LEAVE_FF(fs, res);

#if FF_USE_FASTSEEK
	if (fp->cltbl) {	/* Fast seek */
		if (ofs == CREATE_LINKMAP) {	/* Create CLMT */
			tbl = fp->cltbl;
			tlen = *tbl++; ulen = 2;	/* Given table size and required table size */
			cl = fp->obj.sclust;		/* Origin of the chain */
			if (cl != 0) {
				do {
					/* Get a fragment */
					tcl = cl; ncl = 0; ulen += 2;	/* Top, length and used items */
					do {
						pcl = cl; ncl++;
						cl = get_fat(&fp->obj, cl);
						if (cl <= 1) ABORT(fs, FR_INT_ERR);
						if (cl == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
					} while (cl == pcl + 1);
					if (ulen <= tlen) {		/* Store the length and top of the fragment */
						*tbl++ = ncl; *tbl++ = tcl;
					}
				} while (cl < fs->n_fatent);	/* Repeat until end of chain */
			}
			*fp->cltbl = ulen;	/* Number of items used */
			if (ulen <= tlen) {
				*tbl = 0;		/* Terminate table */
			} else {
				res = FR_NOT_ENOUGH_CORE;	/* Given table size is smaller than required */
			}
		} else {						/* Fast seek */
			if (ofs > fp->obj.objsize) ofs = fp->obj.objsize;	/* Clip offset at the file size */
			fp->fptr = ofs;				/* Set file pointer */
			if (ofs > 0) {
				fp->clust = clmt_clust(fp, ofs - 1);
				dsc = clst2sect(fs, fp->clust);
				if (dsc == 0) ABORT(fs, FR_INT_ERR);
				dsc += (DWORD)((ofs - 1) / SS(fs)) & (fs->csize - 1);
				if (fp->fptr % SS(fs) && dsc != fp->sect) {	/* Refill sector cache if needed */
#if !FF_FS_TINY
#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, dsc, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);	/* Load current sector */
#endif
					fp->sect = dsc;
				}
			}
		}
	} else
#endif

	/* Normal Seek */
	{
#if FF_FS_EXFAT
		if (fs->fs_type != FS_EXFAT && ofs >= 0x100000000) ofs = 0xFFFFFFFF;	/* Clip at 4 GiB - 1 if at FATxx */
#endif
		if (ofs > fp->obj.objsize && (FF_FS_READONLY || !(fp->flag & FA_WRITE))) {	/* In read-only mode, clip offset with the file size */
			ofs = fp->obj.objsize;
		}
		ifptr = fp->fptr;
		fp->fptr = nsect = 0;
		if (ofs > 0) {
			bcs = (DWORD)fs->csize * SS(fs);	/* Cluster size (byte) */
			if (ifptr > 0 &&
				(ofs - 1) / bcs >= (ifptr - 1) / bcs) {	/* When seek to same or following cluster, */
				fp->fptr = (ifptr - 1) & ~(FSIZE_t)(bcs - 1);	/* start from the current cluster */
				ofs -= fp->fptr;
				clst = fp->clust;
			} else {									/* When seek to back cluster, */
				clst = fp->obj.sclust;					/* start from the first cluster */
#if !FF_FS_READONLY
				if (clst == 0) {						/* If no cluster chain, create a new chain */
					clst = create_chain(&fp->obj, 0);
					if (clst == 1) ABORT(fs, FR_INT_ERR);
					if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
					fp->obj.sclust = clst;
				}
#endif
				fp->clust = clst;
			}
			if (clst != 0) {
				while (ofs > bcs) {						/* Cluster following loop */
					ofs -= bcs; fp->fptr += bcs;
#if !FF_FS_READONLY
					if (fp->flag & FA_WRITE) {			/* Check if in write mode or not */
						if (FF_FS_EXFAT && fp->fptr > fp->obj.objsize) {	/* No FAT chain object needs correct objsize to generate FAT value */
							fp->obj.objsize = fp->fptr;
							fp->flag |= FA_MODIFIED;
						}
						clst = create_chain(&fp->obj, clst);	/* Follow chain with forceed stretch */
						if (clst == 0) {				/* Clip file size in case of disk full */
							ofs = 0; break;
						}
					} else
#endif
					{
						clst = get_fat(&fp->obj, clst);	/* Follow cluster chain if not in write mode */
					}
					if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
					if (clst <= 1 || clst >= fs->n_fatent) ABORT(fs, FR_INT_ERR);
					fp->clust = clst;
				}
				fp->fptr += ofs;
				if (ofs % SS(fs)) {
					nsect = clst2sect(fs, clst);	/* Current sector */
					if (nsect == 0) ABORT(fs, FR_INT_ERR);
					nsect += (DWORD)(ofs / SS(fs));
				}
			}
		}
		if (!FF_FS_READONLY && fp->fptr > fp->obj.objsize) {	/* Set file change flag if the file size is extended */
			fp->obj.objsize = fp->fptr;
			fp->flag |= FA_MODIFIED;
		}
		if (fp->fptr % SS(fs) && nsect != fp->sect) {	/* Fill sector cache if needed */
#if !FF_FS_TINY
#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, nsect, 1) != RES_OK) ABORT(fs, FR_DISK_ERR);	/* Fill sector cache */
#endif
			fp->sect = nsect;
		}
	}

	LEAVE_FF(fs, res);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq -0x30(%rbp), %rsi
callq 0x23af
movl %eax, %r12d
testl %eax, %eax
jne 0x2a59
movzbl 0x21(%rbx), %r12d
testl %r12d, %r12d
je 0x2a6b
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rax
cmpq %r14, %rax
jae 0x2a8c
testb $0x2, 0x20(%rbx)
je 0x2a8f
leaq 0x28(%rbx), %r15
movq 0x28(%rbx), %rcx
movq $0x0, 0x28(%rbx)
jmp 0x2aab
movq %r14, %rax
movq 0x28(%rbx), %rcx
movq $0x0, 0x28(%rbx)
testq %rax, %rax
je 0x2bb1
leaq 0x28(%rbx), %r15
movq %rax, %r14
movq -0x30(%rbp), %r12
movzwl 0xa(%r12), %r13d
shll $0x9, %r13d
testq %rcx, %rcx
je 0x2ada
leaq -0x1(%r14), %rax
xorl %edx, %edx
divq %r13
movq %rax, %rsi
decq %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r13
cmpq %rax, %rsi
jae 0x2aed
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x2c16
movq %rax, 0x30(%rbx)
jmp 0x2b01
movq %r13, %rax
negq %rax
andq %rax, %rcx
movq %rcx, 0x28(%rbx)
subq %rcx, %r14
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x2bb1
movq %r15, -0x38(%rbp)
cmpq %r13, %r14
jbe 0x2b6a
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
addq %r13, 0x28(%rbx)
testb $0x2, 0x20(%rbx)
jne 0x2b30
movq (%rbx), %rdi
movq %rax, %rsi
callq 0x1fb2
jmp 0x2b40
movq %rbx, %rdi
movq %rax, %rsi
callq 0x26be
testq %rax, %rax
je 0x2bb1
cmpq %r15, %rax
je 0x2c3f
cmpq $0x2, %rax
jb 0x2c30
cmpq 0x20(%r12), %rax
jae 0x2c30
subq %r13, %r14
movq %rax, 0x30(%rbx)
cmpq %r13, %r14
ja 0x2b19
movq -0x38(%rbp), %rcx
addq %r14, (%rcx)
testl $0x1ff, %r14d           # imm = 0x1FF
je 0x2bb1
addq $-0x2, %rax
movq 0x20(%r12), %rcx
addq $-0x2, %rcx
cmpq %rcx, %rax
jae 0x2c30
movzwl 0xa(%r12), %ecx
imulq %rcx, %rax
addq 0x48(%r12), %rax
je 0x2c30
shrq $0x9, %r14
addq %r14, %rax
movq %rax, %r15
jmp 0x2bb4
xorl %r15d, %r15d
movq 0x28(%rbx), %rax
cmpq 0x18(%rbx), %rax
jbe 0x2bc6
movq %rax, 0x18(%rbx)
orb $0x40, 0x20(%rbx)
xorl %r12d, %r12d
shlq $0x37, %rax
je 0x2a59
movq 0x38(%rbx), %rdx
cmpq %rdx, %r15
je 0x2a59
cmpb $0x0, 0x20(%rbx)
movq -0x30(%rbp), %r14
js 0x2c4e
movzbl 0x1(%r14), %edi
leaq 0x50(%rbx), %rsi
movl $0x1, %r12d
movq %r15, %rdx
movl $0x1, %ecx
callq 0x12c4
testl %eax, %eax
jne 0x2c6b
movq %r15, 0x38(%rbx)
xorl %r12d, %r12d
jmp 0x2a59
movq %rbx, %rdi
xorl %esi, %esi
callq 0x26be
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x2c3f
cmpq $0x1, %rax
jne 0x2c74
movb $0x2, 0x21(%rbx)
movl $0x2, %r12d
jmp 0x2a59
movb $0x1, 0x21(%rbx)
movl $0x1, %r12d
jmp 0x2a59
movzbl 0x1(%r14), %edi
leaq 0x50(%rbx), %rsi
movl $0x1, %r12d
movl $0x1, %ecx
callq 0x131f
testl %eax, %eax
je 0x2c7d
movb $0x1, 0x21(%rbx)
jmp 0x2a59
movq %rax, 0x10(%rbx)
jmp 0x2ae7
andb $0x7f, 0x20(%rbx)
jmp 0x2bea