// File: /workspace/github_c_makefile_star2_O2/gadget114514[P]fatfs-example/fatfs/source/ff.c
// Function: f_getfree
FRESULT f_getfree (
	const TCHAR* path,	/* Logical drive number */
	DWORD* nclst,		/* Pointer to a variable to return number of free clusters */
	FATFS** fatfs		/* Pointer to return pointer to corresponding filesystem object */
)
{
	FRESULT res;
	FATFS *fs;
	DWORD nfree, clst, sect, stat;
	UINT i;
	FFOBJID obj;


	/* Get logical drive */
	res = find_volume(&path, &fs, 0);
	if (res == FR_OK) {
		*fatfs = fs;				/* Return ptr to the fs object */
		/* If free_clst is valid, return it without full FAT scan */
		if (fs->free_clst <= fs->n_fatent - 2) {
			*nclst = fs->free_clst;
		} else {
			/* Scan FAT to obtain number of free clusters */
			nfree = 0;
			if (fs->fs_type == FS_FAT12) {	/* FAT12: Scan bit field FAT entries */
				clst = 2; obj.fs = fs;
				do {
					stat = get_fat(&obj, clst);
					if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
					if (stat == 1) { res = FR_INT_ERR; break; }
					if (stat == 0) nfree++;
				} while (++clst < fs->n_fatent);
			} else {
#if FF_FS_EXFAT
				if (fs->fs_type == FS_EXFAT) {	/* exFAT: Scan allocation bitmap */
					BYTE bm;
					UINT b;

					clst = fs->n_fatent - 2;	/* Number of clusters */
					sect = fs->database;		/* Assuming bitmap starts at cluster 2 */
					i = 0;						/* Offset in the sector */
					do {	/* Counts numbuer of bits with zero in the bitmap */
						if (i == 0) {
							res = move_window(fs, sect++);
							if (res != FR_OK) break;
						}
						for (b = 8, bm = fs->win[i]; b && clst; b--, clst--) {
							if (!(bm & 1)) nfree++;
							bm >>= 1;
						}
						i = (i + 1) % SS(fs);
					} while (clst);
				} else
#endif
				{	/* FAT16/32: Scan WORD/DWORD FAT entries */
					clst = fs->n_fatent;	/* Number of entries */
					sect = fs->fatbase;		/* Top of the FAT */
					i = 0;					/* Offset in the sector */
					do {	/* Counts numbuer of entries with zero in the FAT */
						if (i == 0) {
							res = move_window(fs, sect++);
							if (res != FR_OK) break;
						}
						if (fs->fs_type == FS_FAT16) {
							if (ld_word(fs->win + i) == 0) nfree++;
							i += 2;
						} else {
							if ((ld_dword(fs->win + i) & 0x0FFFFFFF) == 0) nfree++;
							i += 4;
						}
						i %= SS(fs);
					} while (--clst);
				}
			}
			*nclst = nfree;			/* Return the free clusters */
			fs->free_clst = nfree;	/* Now free_clst is valid */
			fs->fsi_flag |= 1;		/* FAT32: FSInfo is to be updated */
		}
	}

	LEAVE_FF(fs, res);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r15
movq %rsi, %rbx
leaq -0x40(%rbp), %rax
movq %rdi, (%rax)
leaq -0x38(%rbp), %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x1480
movl %eax, %ecx
testl %eax, %eax
jne 0x314f
movq -0x38(%rbp), %r14
movq %r14, (%r15)
movq 0x18(%r14), %rax
movq 0x20(%r14), %r13
leaq -0x2(%r13), %rcx
cmpq %rcx, %rax
jbe 0x30af
movq %rbx, -0x30(%rbp)
movb (%r14), %al
cmpb $0x1, %al
jne 0x30b9
leaq -0x60(%rbp), %r15
movq %r14, (%r15)
pushq $0x2
popq %r13
xorl %ebx, %ebx
pushq $0x2
popq %r12
movq %r15, %rdi
movq %r13, %rsi
callq 0x1ed8
testl %eax, %eax
je 0x309e
cmpl $-0x1, %eax
je 0x316c
cmpl $0x1, %eax
jne 0x30a1
jmp 0x3167
incq %rbx
incq %r13
cmpq 0x20(%r14), %r13
jb 0x307c
jmp 0x313d
movq %rax, (%rbx)
xorl %ecx, %ecx
jmp 0x314f
movq 0x38(%r14), %rsi
xorl %ebx, %ebx
xorl %r15d, %r15d
testl %r15d, %r15d
jne 0x30ea
movq %r14, %rdi
movq %r14, %r12
movq %rsi, %r14
callq 0x1e87
testl %eax, %eax
jne 0x3160
incq %r14
movb (%r12), %al
movq %r14, %rsi
movq %r12, %r14
movl %r15d, %ecx
cmpb $0x2, %al
jne 0x30ff
cmpw $0x0, 0x58(%r14,%rcx)
sete %cl
pushq $0x2
jmp 0x3127
movzwl 0x5a(%r14,%rcx), %edx
movzbl 0x59(%r14,%rcx), %edi
shll $0x10, %edx
shll $0x8, %edi
movzbl 0x58(%r14,%rcx), %ecx
andl $0xfff0000, %edx        # imm = 0xFFF0000
orl %edi, %edx
orq %rcx, %rdx
sete %cl
pushq $0x4
popq %rdx
addl %edx, %r15d
movzbl %cl, %ecx
addq %rcx, %rbx
andl $0x1ff, %r15d           # imm = 0x1FF
decq %r13
jne 0x30c2
xorl %ecx, %ecx
movq -0x30(%rbp), %rax
movq %rbx, (%rax)
movq %rbx, 0x18(%r14)
orb $0x1, 0x4(%r14)
movl %ecx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ecx
movq %r12, %r14
jmp 0x313f
movl %r12d, %ecx
jmp 0x313f
pushq $0x1
popq %rcx
jmp 0x313f