// File: /workspace/github_c_makefile_star2_O0/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
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
cmpq $0x2, -0x10(%rbp)
jb 0x2a10
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jb 0x2a1d
movq $0x1, -0x20(%rbp)
jmp 0x2bc2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x2c(%rbp)
subl $0x1, %eax
je 0x2a56
jmp 0x2a37
movl -0x2c(%rbp), %eax
subl $0x2, %eax
je 0x2b22
jmp 0x2a45
movl -0x2c(%rbp), %eax
subl $0x3, %eax
je 0x2b6b
jmp 0x2bb8
movq -0x10(%rbp), %rax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
shrl %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x18(%rbp), %eax
shrl $0x9, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x28b0
cmpl $0x0, %eax
je 0x2a8e
jmp 0x2bc0
movq -0x28(%rbp), %rax
movl -0x18(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x18(%rbp)
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, %ecx
movzbl 0x58(%rax,%rcx), %eax
movl %eax, -0x14(%rbp)
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x18(%rbp), %eax
shrl $0x9, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x28b0
cmpl $0x0, %eax
je 0x2ad3
jmp 0x2bc0
movq -0x28(%rbp), %rax
movl -0x18(%rbp), %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, %ecx
movzbl 0x58(%rax,%rcx), %eax
shll $0x8, %eax
orl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x2b09
movl -0x14(%rbp), %eax
shrl $0x4, %eax
movl %eax, -0x30(%rbp)
jmp 0x2b14
movl -0x14(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl %eax, %eax
movq %rax, -0x20(%rbp)
jmp 0x2bc0
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x10(%rbp), %rax
shrq $0x8, %rax
addq %rax, %rsi
callq 0x28b0
cmpl $0x0, %eax
je 0x2b45
jmp 0x2bc0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
movq -0x10(%rbp), %rax
shlq %rax
andq $0x1ff, %rax            # imm = 0x1FF
addq %rax, %rdi
callq 0x4960
movzwl %ax, %eax
movq %rax, -0x20(%rbp)
jmp 0x2bc0
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x10(%rbp), %rax
shrq $0x7, %rax
addq %rax, %rsi
callq 0x28b0
cmpl $0x0, %eax
je 0x2b8e
jmp 0x2bc0
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
movq -0x10(%rbp), %rax
shlq $0x2, %rax
andq $0x1ff, %rax            # imm = 0x1FF
addq %rax, %rdi
callq 0x2940
andq $0xfffffff, %rax        # imm = 0xFFFFFFF
movq %rax, -0x20(%rbp)
jmp 0x2bc0
movq $0x1, -0x20(%rbp)
jmp 0x2bc2
movq -0x20(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)