// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
leaq -0x30(%rbp), %rsi
callq 0x23bb
movl %eax, %r12d
testl %eax, %eax
jne 0x2a6e
movzbl 0x21(%rbx), %r12d
testl %r12d, %r12d
je 0x2a85
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rax
cmpq %r14, %rax
jae 0x2a96
testb $0x2, 0x20(%rbx)
cmoveq %rax, %r14
movq 0x28(%rbx), %rcx
movq $0x0, 0x28(%rbx)
testq %r14, %r14
je 0x2bde
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), %r15
testq %r15, %r15
je 0x2b68
movq %r15, 0x30(%rbx)
jmp 0x2b01
movq %r13, %rax
negq %rax
andq %rax, %rcx
movq %rcx, 0x28(%rbx)
subq %rcx, %r14
movq 0x30(%rbx), %r15
testq %r15, %r15
je 0x2bde
cmpq %r13, %r14
jbe 0x2ba7
addq %r13, 0x28(%rbx)
testb $0x2, 0x20(%rbx)
jne 0x2b2d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1fc1
movq %rax, %r15
jmp 0x2b40
movq %rbx, %rdi
movq %r15, %rsi
callq 0x26c3
movq %rax, %r15
testq %rax, %rax
je 0x2ba1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r15
je 0x2b92
cmpq $0x2, %r15
jb 0x2c7f
cmpq 0x20(%r12), %r15
jae 0x2c7f
subq %r13, %r14
movq %r15, 0x30(%rbx)
jmp 0x2b0a
movq %rbx, %rdi
xorl %esi, %esi
callq 0x26c3
movq %rax, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r15
je 0x2b92
cmpq $0x1, %r15
je 0x2c7f
movq %r15, 0x10(%rbx)
jmp 0x2ae7
movb $0x1, 0x21(%rbx)
movl $0x1, %r12d
jmp 0x2a73
xorl %r15d, %r15d
xorl %r14d, %r14d
addq %r14, 0x28(%rbx)
testl $0x1ff, %r14d           # imm = 0x1FF
je 0x2bde
addq $-0x2, %r15
movq 0x20(%r12), %rax
addq $-0x2, %rax
cmpq %rax, %r15
jae 0x2c6b
movzwl 0xa(%r12), %eax
imulq %rax, %r15
addq 0x48(%r12), %r15
jmp 0x2c6e
xorl %r15d, %r15d
movq 0x28(%rbx), %rax
cmpq 0x18(%rbx), %rax
jbe 0x2bf3
movq %rax, 0x18(%rbx)
orb $0x40, 0x20(%rbx)
xorl %r12d, %r12d
shlq $0x37, %rax
je 0x2a73
movq 0x38(%rbx), %rdx
cmpq %rdx, %r15
je 0x2a73
cmpb $0x0, 0x20(%rbx)
js 0x2c42
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edi
leaq 0x50(%rbx), %rsi
movl $0x1, %r12d
movq %r15, %rdx
movl $0x1, %ecx
callq 0x12c6
testl %eax, %eax
jne 0x2c62
movq %r15, 0x38(%rbx)
xorl %r12d, %r12d
jmp 0x2a73
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edi
leaq 0x50(%rbx), %rsi
movl $0x1, %r12d
movl $0x1, %ecx
callq 0x1321
testl %eax, %eax
je 0x2c8e
movb $0x1, 0x21(%rbx)
jmp 0x2a73
xorl %r15d, %r15d
testq %r15, %r15
je 0x2c7f
shrq $0x9, %r14
addq %r14, %r15
jmp 0x2be1
movb $0x2, 0x21(%rbx)
movl $0x2, %r12d
jmp 0x2a73
andb $0x7f, 0x20(%rbx)
jmp 0x2c13