// File: /workspace/github_c_makefile_star2_O1/lb3361[P]dosfs/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 && obj->sclust != 0) || obj->stat == 0) {	/* Object except root dir must have valid data length */
				DWORD cofs = clst - obj->sclust;	/* Offset from start cluster */
				DWORD clen = (DWORD)((LBA_t)((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;
				}
			}
			val = 1;	/* Internal error */
			break;
#endif
		default:
			val = 1;	/* Internal error */
		}
	}

	return val;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl $0x1, %ebp
cmpl $0x2, %esi
jb 0x656f
movl %esi, %ebx
movq (%rdi), %r14
cmpl %esi, 0x28(%r14)
jbe 0x656f
movzbl (%r14), %eax
decl %eax
cmpl $0x3, %eax
ja 0x656f
leaq 0x7022(%rip), %rcx      # 0xd3c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %ebx, %r15d
shrl %r15d
addl %ebx, %r15d
movl %r15d, %esi
shrl $0x9, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x6314
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x656f
leal 0x1(%r15), %r12d
andl $0x1ff, %r15d           # imm = 0x1FF
movzbl 0x60(%r14,%r15), %r15d
movl %r12d, %esi
shrl $0x9, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x6314
testl %eax, %eax
jne 0x656f
andl $0x1ff, %r12d           # imm = 0x1FF
movzbl 0x60(%r14,%r12), %ebp
shll $0x8, %ebp
orl %r15d, %ebp
movl %ebp, %eax
shrl $0x4, %eax
andl $0xfff, %ebp            # imm = 0xFFF
testb $0x1, %bl
cmovnel %eax, %ebp
jmp 0x656f
movl %ebx, %esi
shrl $0x7, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x6314
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x656f
andl $0x7f, %ebx
movzbl 0x63(%r14,%rbx,4), %eax
movzbl 0x62(%r14,%rbx,4), %ecx
shll $0x10, %eax
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x61(%r14,%rbx,4), %ebp
orl %ecx, %ebp
shll $0x8, %ebp
movzbl 0x60(%r14,%rbx,4), %eax
andl $0xfffff00, %ebp        # imm = 0xFFFFF00
orl %eax, %ebp
jmp 0x656f
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x6485
cmpl $0x0, 0xc(%rdi)
jne 0x648f
cmpb $0x0, 0xb(%rdi)
jne 0x656a
movl %ebx, %ecx
subl 0xc(%rdi), %ecx
decq %rax
shrq $0x9, %rax
movzwl 0xa(%r14), %esi
xorl %edx, %edx
divq %rsi
movzbl 0xb(%rdi), %edx
cmpl $0x2, %edx
jne 0x64ef
cmpl %eax, %ecx
ja 0x64ef
incl %ebx
cmpl %eax, %ecx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmovnel %ebx, %ebp
jmp 0x6564
movl %ebx, %esi
shrl $0x8, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x6314
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x656f
movzbl %bl, %eax
movzwl 0x60(%r14,%rax,2), %ebp
jmp 0x656f
movb $0x1, %al
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x2, %edx
je 0x6566
cmpl $0x3, %edx
jne 0x6512
cmpl 0x18(%rdi), %ecx
jae 0x650d
incl %ebx
xorl %eax, %eax
movl %ebx, %ebp
jmp 0x6566
cmpl $0x2, %edx
je 0x6566
cmpl $0x0, 0x1c(%rdi)
je 0x651f
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
jmp 0x6564
movl %ebx, %esi
shrl $0x7, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x6314
testl %eax, %eax
jne 0x6564
andl $0x7f, %ebx
movzbl 0x63(%r14,%rbx,4), %eax
movzbl 0x62(%r14,%rbx,4), %ecx
shll $0x10, %eax
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x61(%r14,%rbx,4), %ebp
orl %ecx, %ebp
shll $0x8, %ebp
movzbl 0x60(%r14,%rbx,4), %eax
andl $0x7fffff00, %ebp       # imm = 0x7FFFFF00
orl %eax, %ebp
xorl %eax, %eax
testb %al, %al
je 0x656f
movl $0x1, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq