// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
cmpl $0x2, -0xc(%rbp)
jb 0x658c
movl -0xc(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jb 0x6598
movl $0x1, -0x18(%rbp)
jmp 0x685d
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
movzbl (%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x3, %eax
ja 0x6854
movq -0x30(%rbp), %rax
leaq 0xbe3e(%rip), %rcx      # 0x12400
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0xc(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
shrl %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x14(%rbp), %eax
shrl $0x9, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x6480
cmpl $0x0, %eax
je 0x6602
jmp 0x685b
movq -0x20(%rbp), %rax
movl -0x14(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x14(%rbp)
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, %ecx
movzbl 0x60(%rax,%rcx), %eax
movl %eax, -0x10(%rbp)
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x14(%rbp), %eax
shrl $0x9, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x6480
cmpl $0x0, %eax
je 0x6647
jmp 0x685b
movq -0x20(%rbp), %rax
movl -0x14(%rbp), %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, %ecx
movzbl 0x60(%rax,%rcx), %eax
shll $0x8, %eax
orl -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x667a
movl -0x10(%rbp), %eax
shrl $0x4, %eax
movl %eax, -0x34(%rbp)
jmp 0x6685
movl -0x10(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x18(%rbp)
jmp 0x685b
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0xc(%rbp), %eax
shrl $0x8, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x6480
cmpl $0x0, %eax
je 0x66b6
jmp 0x685b
movq -0x20(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
shll %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl %eax, %eax
addq %rax, %rdi
callq 0x9390
movzwl %ax, %eax
movl %eax, -0x18(%rbp)
jmp 0x685b
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0xc(%rbp), %eax
shrl $0x7, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x6480
cmpl $0x0, %eax
je 0x6703
jmp 0x685b
movq -0x20(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
shll $0x2, %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl %eax, %eax
addq %rax, %rdi
callq 0x6510
andl $0xfffffff, %eax        # imm = 0xFFFFFFF
movl %eax, -0x18(%rbp)
jmp 0x685b
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x6742
movq -0x8(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x6753
movq -0x8(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x0, %eax
jne 0x684b
movl -0xc(%rbp), %eax
movq -0x8(%rbp), %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
subq $0x1, %rax
shrq $0x9, %rax
movq -0x20(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
xorl %edx, %edx
divq %rcx
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x2, %eax
jne 0x67bb
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
ja 0x67bb
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jne 0x67a7
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x38(%rbp)
jmp 0x67b0
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x18(%rbp)
jmp 0x685b
movq -0x8(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x3, %eax
jne 0x67df
movl -0x24(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jae 0x67df
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x685b
movq -0x8(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x2, %eax
je 0x6849
movq -0x8(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x67ff
movl $0x7fffffff, -0x18(%rbp) # imm = 0x7FFFFFFF
jmp 0x6847
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0xc(%rbp), %eax
shrl $0x7, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x6480
cmpl $0x0, %eax
je 0x6822
jmp 0x685b
movq -0x20(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
shll $0x2, %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl %eax, %eax
addq %rax, %rdi
callq 0x6510
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x18(%rbp)
jmp 0x685b
jmp 0x684b
movl $0x1, -0x18(%rbp)
jmp 0x685b
movl $0x1, -0x18(%rbp)
jmp 0x685d
movl -0x18(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)