// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x1770
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x494a
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x20(%rcx), %rcx
subq $0x2, %rcx
cmpq %rcx, %rax
ja 0x47b8
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x4948
movq $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x4841
movq $0x2, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x29e0
movq %rax, -0x48(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x48(%rbp)
jne 0x4801
movl $0x1, -0x1c(%rbp)
jmp 0x483c
cmpq $0x1, -0x48(%rbp)
jne 0x4811
movl $0x2, -0x1c(%rbp)
jmp 0x483c
cmpq $0x0, -0x48(%rbp)
jne 0x4824
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x4826
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jb 0x47dc
jmp 0x4923
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x488e
movq -0x28(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
callq 0x28b0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x488c
jmp 0x4921
jmp 0x488e
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x2, %eax
jne 0x48cc
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
movl -0x4c(%rbp), %eax
addq %rax, %rdi
callq 0x4960
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x48c1
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl -0x4c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x4c(%rbp)
jmp 0x4900
movq -0x28(%rbp), %rdi
addq $0x58, %rdi
movl -0x4c(%rbp), %eax
addq %rax, %rdi
callq 0x2940
andq $0xfffffff, %rax        # imm = 0xFFFFFFF
cmpq $0x0, %rax
jne 0x48f7
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl -0x4c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
andl $0x1ff, %eax            # imm = 0x1FF
movl %eax, -0x4c(%rbp)
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x4860
jmp 0x4923
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x4(%rax)
jmp 0x494a
movl -0x1c(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)