// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rbp
cmpl $0x2, %esi
jb 0x5dd7
movl %esi, %ebx
movq (%rdi), %r14
cmpl %esi, 0x28(%r14)
jbe 0x5dd7
movzbl (%r14), %eax
decl %eax
cmpl $0x3, %eax
ja 0x5dd7
leaq 0x671f(%rip), %rcx      # 0xc3a0
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 0x5bf6
pushq $-0x1
popq %rbp
testl %eax, %eax
jne 0x5dd7
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 0x5bf6
testl %eax, %eax
jne 0x5dd7
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 0x5dd7
movl %ebx, %esi
shrl $0x7, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0x5d6d
andl $0x7f, %ebx
movzwl 0x62(%r14,%rbx,4), %ebp
movzbl 0x61(%r14,%rbx,4), %eax
shll $0x10, %ebp
shll $0x8, %eax
movzbl 0x60(%r14,%rbx,4), %ecx
andl $0xfff0000, %ebp        # imm = 0xFFF0000
orl %eax, %ebp
orl %ecx, %ebp
jmp 0x5dd7
movq 0x10(%rdi), %rax
testq %rax, %rax
je 0x5d72
movl 0xc(%rdi), %edx
testl %edx, %edx
je 0x5d72
movb 0xb(%rdi), %cl
jmp 0x5d7d
movl %ebx, %esi
shrl $0x8, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x5bf6
testl %eax, %eax
je 0x5da9
pushq $-0x1
popq %rbp
jmp 0x5dd7
cmpb $0x0, 0xb(%rdi)
jne 0x5dd7
movl 0xc(%rdi), %edx
xorl %ecx, %ecx
movl %ebx, %esi
subl %edx, %esi
decq %rax
shrq $0x9, %rax
movzwl 0xa(%r14), %r8d
xorl %edx, %edx
divq %r8
cmpb $0x2, %cl
jne 0x5db4
cmpl %eax, %esi
ja 0x5db4
incl %ebx
cmpl %eax, %esi
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmovnel %ebx, %ebp
jmp 0x5dd7
movzbl %bl, %eax
movzwl 0x60(%r14,%rax,2), %ebp
jmp 0x5dd7
cmpb $0x2, %cl
je 0x5dd7
movzbl %cl, %eax
cmpl $0x3, %eax
jne 0x5dcc
cmpl 0x18(%rdi), %esi
jae 0x5dcc
incl %ebx
movl %ebx, %ebp
jmp 0x5dd7
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
cmpl $0x0, 0x1c(%rdi)
je 0x5de2
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %esi
shrl $0x7, %esi
addq 0x38(%r14), %rsi
movq %r14, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0x5d6d
andl $0x7f, %ebx
movzwl 0x62(%r14,%rbx,4), %ebp
movzbl 0x61(%r14,%rbx,4), %eax
shll $0x10, %ebp
shll $0x8, %eax
movzbl 0x60(%r14,%rbx,4), %ecx
andl $0x7fff0000, %ebp       # imm = 0x7FFF0000
jmp 0x5d3a