// File: /workspace/github_c_makefile_star2_O3/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: get_fat
static
DWORD get_fat (		/* 0xFFFFFFFF:Disk error, 1:Internal error, 2..0x7FFFFFFF:Cluster status */
	FFOBJID* obj,	/* Corresponding object */
	DWORD clst		/* Cluster number to get the value */
)
{
	UINT wc, bc;
	DWORD val;
	FATFS *fs = obj->fs;


	if (clst < 2 || clst >= fs->n_fatent) {	/* Check if in valid range */
		val = 1;	/* Internal error */

	} else {
		val = 0xFFFFFFFF;	/* Default value falls on disk error */

		switch (fs->fs_type) {
		case FS_FAT12 :
			bc = (UINT)clst; bc += bc / 2;
			if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
			wc = fs->win[bc++ % SS(fs)];		/* Get 1st byte of the entry */
			if (move_window(fs, fs->fatbase + (bc / SS(fs))) != FR_OK) break;
			wc |= fs->win[bc % SS(fs)] << 8;	/* Merge 2nd byte of the entry */
			val = (clst & 1) ? (wc >> 4) : (wc & 0xFFF);	/* Adjust bit position */
			break;

		case FS_FAT16 :
			if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2))) != FR_OK) break;
			val = ld_word(fs->win + clst * 2 % SS(fs));		/* Simple WORD array */
			break;

		case FS_FAT32 :
			if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
			val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x0FFFFFFF;	/* Simple DWORD array but mask out upper 4 bits */
			break;
#if FF_FS_EXFAT
		case FS_EXFAT :
			if (obj->objsize != 0) {
				DWORD cofs = clst - obj->sclust;	/* Offset from start cluster */
				DWORD clen = (DWORD)((obj->objsize - 1) / SS(fs)) / fs->csize;	/* Number of clusters - 1 */

				if (obj->stat == 2 && cofs <= clen) {	/* Is it a contiguous chain? */
					val = (cofs == clen) ? 0x7FFFFFFF : clst + 1;	/* No data on the FAT, generate the value */
					break;
				}
				if (obj->stat == 3 && cofs < obj->n_cont) {	/* Is it in the 1st fragment? */
					val = clst + 1; 	/* Generate the value */
					break;
				}
				if (obj->stat != 2) {	/* Get value from FAT if FAT chain is valid */
					if (obj->n_frag != 0) {	/* Is it on the growing edge? */
						val = 0x7FFFFFFF;	/* Generate EOC */
					} else {
						if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4))) != FR_OK) break;
						val = ld_dword(fs->win + clst * 4 % SS(fs)) & 0x7FFFFFFF;
					}
					break;
				}
			}
			/* go to default */
#endif
		default:
			val = 1;	/* Internal error */
		}
	}

	return val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl $0x1, %ebx
cmpq $0x2, %rsi
jb 0x20e4
movq %rsi, %r14
movq %rdi, %r15
cmpq %rsi, 0x20(%rdi)
jbe 0x20e4
movzbl (%r15), %eax
cmpl $0x3, %eax
je 0x20a1
cmpl $0x2, %eax
je 0x2079
cmpl $0x1, %eax
jne 0x20e4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %r14d, %r12d
shrl %r12d
addl %r14d, %r12d
movl %r12d, %esi
shrl $0x9, %esi
addq 0x38(%r15), %rsi
movq %r15, %rdi
callq 0x1f5d
testl %eax, %eax
jne 0x20e4
leal 0x1(%r12), %r13d
andl $0x1ff, %r12d           # imm = 0x1FF
movzbl 0x58(%r15,%r12), %r12d
movl %r13d, %esi
shrl $0x9, %esi
addq 0x38(%r15), %rsi
movq %r15, %rdi
callq 0x1f5d
testl %eax, %eax
jne 0x20e4
andl $0x1ff, %r13d           # imm = 0x1FF
movzbl 0x58(%r15,%r13), %ebx
shll $0x8, %ebx
orl %r12d, %ebx
movl %ebx, %eax
shrl $0x4, %eax
andl $0xfff, %ebx            # imm = 0xFFF
testb $0x1, %r14b
cmovnel %eax, %ebx
jmp 0x20e4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %r14, %rsi
shrq $0x8, %rsi
addq 0x38(%r15), %rsi
movq %r15, %rdi
callq 0x1f5d
testl %eax, %eax
jne 0x20e4
movzbl %r14b, %eax
movzwl 0x58(%r15,%rax,2), %ebx
jmp 0x20e4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %r14, %rsi
shrq $0x7, %rsi
addq 0x38(%r15), %rsi
movq %r15, %rdi
callq 0x1f5d
testl %eax, %eax
jne 0x20e4
andl $0x7f, %r14d
movzwl 0x5a(%r15,%r14,4), %ebx
movzbl 0x59(%r15,%r14,4), %eax
shll $0x10, %ebx
shll $0x8, %eax
movzbl 0x58(%r15,%r14,4), %ecx
andl $0xfff0000, %ebx        # imm = 0xFFF0000
orl %eax, %ebx
orq %rcx, %rbx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq