// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x2, -0x2c(%rbp)
cmpq $0x2, -0x10(%rbp)
jb 0x6aca
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x6aca
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x30(%rbp)
subl $0x1, %eax
je 0x6898
jmp 0x6879
movl -0x30(%rbp), %eax
subl $0x2, %eax
je 0x69d5
jmp 0x6887
movl -0x30(%rbp), %eax
subl $0x3, %eax
je 0x6a30
jmp 0x6ac8
movq -0x10(%rbp), %rax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shrl %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x1c(%rbp), %eax
shrl $0x9, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x28b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x68d4
jmp 0x6ac8
movq -0x8(%rbp), %rax
addq $0x58, %rax
movl -0x1c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x1c(%rbp)
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x691f
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
movq -0x18(%rbp), %rcx
movzbl %cl, %ecx
shll $0x4, %ecx
orl %ecx, %eax
movl %eax, -0x34(%rbp)
jmp 0x6929
movq -0x18(%rbp), %rax
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movb $0x1, 0x3(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rsi
movl -0x1c(%rbp), %eax
shrl $0x9, %eax
movl %eax, %eax
addq %rax, %rsi
callq 0x28b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x6966
jmp 0x6ac8
movq -0x8(%rbp), %rax
addq $0x58, %rax
movl -0x1c(%rbp), %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0x699e
movq -0x18(%rbp), %rax
shrq $0x4, %rax
movzbl %al, %eax
movl %eax, -0x38(%rbp)
jmp 0x69bd
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
andl $0xf0, %eax
movq -0x18(%rbp), %rcx
shrq $0x8, %rcx
movzbl %cl, %ecx
andl $0xf, %ecx
orl %ecx, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, (%rax)
movq -0x8(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x6ac8
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x10(%rbp), %rax
shrq $0x8, %rax
addq %rax, %rsi
callq 0x28b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x69ff
jmp 0x6ac8
movq -0x8(%rbp), %rdi
addq $0x58, %rdi
movq -0x10(%rbp), %rax
shlq %rax
andq $0x1ff, %rax            # imm = 0x1FF
addq %rax, %rdi
movq -0x18(%rbp), %rax
movzwl %ax, %esi
callq 0x3960
movq -0x8(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x6ac8
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x10(%rbp), %rax
shrq $0x7, %rax
addq %rax, %rsi
callq 0x28b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x6a57
jmp 0x6ac8
movq -0x18(%rbp), %rax
andq $0xfffffff, %rax        # imm = 0xFFFFFFF
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
addq $0x58, %rdi
movq -0x10(%rbp), %rax
shlq $0x2, %rax
andq $0x1ff, %rax            # imm = 0x1FF
addq %rax, %rdi
callq 0x2940
movq %rax, %rcx
movq -0x40(%rbp), %rax
movabsq $0xf0000000, %rdx       # imm = 0xF0000000
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
addq $0x58, %rdi
movq -0x10(%rbp), %rax
shlq $0x2, %rax
andq $0x1ff, %rax            # imm = 0x1FF
addq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x2680
movq -0x8(%rbp), %rax
movb $0x1, 0x3(%rax)
jmp 0x6aca
movl -0x2c(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq