// File: /workspace/github_c_makefile_star2_O2/lb3361[P]dosfs/ff.c
// Function: put_fat
static FRESULT put_fat (	/* FR_OK(0):succeeded, !=0:error */
	FATFS* fs,		/* Corresponding filesystem object */
	DWORD clst,		/* FAT index number (cluster number) to be changed */
	DWORD val		/* New value to be set to the entry */
)
{
	UINT bc;
	BYTE *p;
	FRESULT res = FR_INT_ERR;


	if (clst >= 2 && clst < fs->n_fatent) {	/* Check if in valid range */
		switch (fs->fs_type) {
		case FS_FAT12:
			bc = (UINT)clst; bc += bc / 2;	/* bc: byte offset of the entry */
			res = move_window(fs, fs->fatbase + (bc / SS(fs)));
			if (res != FR_OK) break;
			p = fs->win + bc++ % SS(fs);
			*p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;		/* Update 1st byte */
			fs->wflag = 1;
			res = move_window(fs, fs->fatbase + (bc / SS(fs)));
			if (res != FR_OK) break;
			p = fs->win + bc % SS(fs);
			*p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));	/* Update 2nd byte */
			fs->wflag = 1;
			break;

		case FS_FAT16:
			res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
			if (res != FR_OK) break;
			st_word(fs->win + clst * 2 % SS(fs), (WORD)val);	/* Simple WORD array */
			fs->wflag = 1;
			break;

		case FS_FAT32:
#if FF_FS_EXFAT
		case FS_EXFAT:
#endif
			res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
			if (res != FR_OK) break;
			if (!FF_FS_EXFAT || fs->fs_type != FS_EXFAT) {
				val = (val & 0x0FFFFFFF) | (ld_dword(fs->win + clst * 4 % SS(fs)) & 0xF0000000);
			}
			st_dword(fs->win + clst * 4 % SS(fs), val);
			fs->wflag = 1;
			break;
		}
	}
	return res;
}

// Assembly:
pushq $0x2
popq %rax
cmpl $0x2, %esi
jb 0xaa1c
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %esi, %r14d
movq %rdi, %rbx
cmpl %esi, 0x28(%rdi)
jbe 0xaa14
movl %edx, %ebp
movzbl (%rbx), %ecx
leal -0x3(%rcx), %edx
cmpl $0x2, %edx
jb 0xa9a2
cmpl $0x2, %ecx
je 0xa9c7
cmpl $0x1, %ecx
jne 0xaa14
movl %r14d, %r12d
shrl %r12d
addl %r14d, %r12d
movl %r12d, %esi
shrl $0x9, %esi
addq 0x38(%rbx), %rsi
movq %rbx, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0xaa14
leal 0x1(%r12), %r15d
andl $0x1ff, %r12d           # imm = 0x1FF
movl %ebp, %eax
testb $0x1, %r14b
je 0xa95b
movzbl 0x60(%rbx,%r12), %ecx
andl $0xf, %ecx
movzbl %bpl, %eax
shll $0x4, %eax
orl %ecx, %eax
movb %al, 0x60(%rbx,%r12)
movb $0x1, 0x3(%rbx)
movl %r15d, %esi
shrl $0x9, %esi
addq 0x38(%rbx), %rsi
movq %rbx, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0xaa14
andl $0x1ff, %r15d           # imm = 0x1FF
testb $0x1, %r14b
jne 0xaa1d
movzbl 0x60(%rbx,%r15), %eax
andl $-0x10, %eax
shrl $0x8, %ebp
andl $0xf, %ebp
orl %eax, %ebp
jmp 0xaa20
movl %r14d, %esi
shrl $0x7, %esi
addq 0x38(%rbx), %rsi
movq %rbx, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0xaa14
cmpb $0x4, (%rbx)
jne 0xa9e8
andl $0x7f, %r14d
shll $0x2, %r14d
jmp 0xaa06
movl %r14d, %esi
shrl $0x8, %esi
addq 0x38(%rbx), %rsi
movq %rbx, %rdi
callq 0x5bf6
testl %eax, %eax
jne 0xaa14
movzbl %r14b, %eax
movw %bp, 0x60(%rbx,%rax,2)
jmp 0xaa0b
andl $0xfffffff, %ebp        # imm = 0xFFFFFFF
andl $0x7f, %r14d
shll $0x2, %r14d
movzwl 0x62(%rbx,%r14), %eax
shll $0x10, %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
orl %eax, %ebp
movl %ebp, 0x60(%rbx,%r14)
addq $0x3, %rbx
movb $0x1, (%rbx)
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
shrl $0x4, %ebp
movb %bpl, 0x60(%rbx,%r15)
jmp 0xaa0b