// File: /workspace/github_c_makefile_star2_O3/gadget114514[P]fatfs-example/fatfs/source/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;		/* Put 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));	/* Put 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:
movl $0x2, %eax
cmpq $0x2, %rsi
jb 0x4616
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
cmpq %rsi, 0x20(%rdi)
jbe 0x4608
movzbl (%rdi), %ecx
cmpl $0x3, %ecx
je 0x4542
cmpl $0x2, %ecx
je 0x450f
cmpl $0x1, %ecx
jne 0x4608
movq %rdx, %r13
movl %ebx, %r15d
shrl %r15d
addl %ebx, %r15d
movl %r15d, %esi
shrl $0x9, %esi
addq 0x38(%rdi), %rsi
movq %rdi, %r14
callq 0x1f5d
testl %eax, %eax
jne 0x4608
movq %r14, %r12
leal 0x1(%r15), %r14d
andl $0x1ff, %r15d           # imm = 0x1FF
testb $0x1, %bl
jne 0x45a3
movq %r13, %rax
movq %r12, %rdi
jmp 0x45b6
movq %rdx, %r15
movq %rbx, %rsi
shrq $0x8, %rsi
addq 0x38(%rdi), %rsi
movq %rdi, %r14
callq 0x1f5d
testl %eax, %eax
jne 0x4608
movzbl %bl, %eax
movw %r15w, 0x58(%r14,%rax,2)
addq $0x3, %r14
movq %r14, %r12
jmp 0x4601
movq %rdx, %r14
movq %rbx, %rsi
shrq $0x7, %rsi
addq 0x38(%rdi), %rsi
movq %rdi, %r15
callq 0x1f5d
testl %eax, %eax
jne 0x4608
movq %r14, %rdx
movl %edx, %eax
andl $0xf000000, %eax        # imm = 0xF000000
andl $0x7f, %ebx
movq %r15, %rsi
movzwl 0x5a(%r15,%rbx,4), %ecx
shll $0x10, %ecx
andl $0xf0000000, %ecx       # imm = 0xF0000000
orl %eax, %ecx
movb %dl, 0x58(%r15,%rbx,4)
movb %dh, 0x59(%rsi,%rbx,4)
shrl $0x10, %edx
movb %dl, 0x5a(%r15,%rbx,4)
shrl $0x18, %ecx
movb %cl, 0x5b(%r15,%rbx,4)
addq $0x3, %rsi
movq %rsi, %r12
jmp 0x4601
movq %r12, %rdi
movb 0x58(%r12,%r15), %cl
andb $0xf, %cl
movq %r13, %rax
shlb $0x4, %al
orb %cl, %al
movb %al, 0x58(%rdi,%r15)
movb $0x1, 0x3(%rdi)
movl %r14d, %esi
shrl $0x9, %esi
addq 0x38(%rdi), %rsi
callq 0x1f5d
testl %eax, %eax
jne 0x4608
andl $0x1ff, %r14d           # imm = 0x1FF
testb $0x1, %bl
jne 0x45f4
movzbl 0x58(%r12,%r14), %eax
andl $-0x10, %eax
shrl $0x8, %r13d
andl $0xf, %r13d
orl %eax, %r13d
jmp 0x45f8
shrl $0x4, %r13d
movb %r13b, 0x58(%r12,%r14)
addq $0x3, %r12
movb $0x1, (%r12)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq