// File: /workspace/github_c_makefile_star2_O3/lb3361[P]dosfs/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, stat;
	LBA_t sect;
	UINT i;
	FFOBJID obj;


	/* Get logical drive */
	res = mount_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->bitbase;			/* Bitmap sector */
					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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rsi, %r13
leaq 0x10(%rsp), %rax
movq %rdi, (%rax)
leaq 0x8(%rsp), %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x4ada
movl %eax, %ebp
testl %eax, %eax
jne 0x7ed0
movq 0x8(%rsp), %r14
movq %r14, (%r15)
movl 0x24(%r14), %eax
movl 0x28(%r14), %r12d
leal -0x2(%r12), %ebx
cmpl %ebx, %eax
jbe 0x7dce
movzbl (%r14), %eax
cmpl $0x4, %eax
movq %r13, (%rsp)
je 0x7dd9
cmpl $0x1, %eax
jne 0x7e48
leaq 0x18(%rsp), %r15
movq %r14, (%r15)
movl $0x2, %ebp
xorl %r13d, %r13d
movl $0x2, %r12d
movq %r15, %rdi
movl %r12d, %esi
callq 0x616a
testl %eax, %eax
je 0x7dbd
cmpl $-0x1, %eax
je 0x7ee5
cmpl $0x1, %eax
jne 0x7dc0
jmp 0x7ec0
incl %r13d
incl %r12d
cmpl 0x28(%r14), %r12d
jb 0x7d9b
jmp 0x7ebe
movl %eax, (%r13)
xorl %ebp, %ebp
jmp 0x7ed0
movq 0x50(%r14), %r15
xorl %r13d, %r13d
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x7dfe
movq %r14, %rdi
movq %r15, %rsi
callq 0x6114
testl %eax, %eax
jne 0x7ee1
incq %r15
testl %ebx, %ebx
je 0x7ebe
movl %r12d, %eax
movb 0x60(%r14,%rax), %cl
leal -0x1(%rbx), %eax
xorl %esi, %esi
movl %r13d, %edi
movl %esi, %edx
movl %ecx, %esi
notb %sil
andb $0x1, %sil
movzbl %sil, %r13d
addl %edi, %r13d
leal 0x1(%rdx), %esi
cmpl $0x7, %edx
je 0x7e36
shrb %cl
cmpl %edx, %eax
jne 0x7e13
incl %r12d
andl $0x1ff, %r12d           # imm = 0x1FF
subl %esi, %ebx
cmpl %edx, %eax
jne 0x7de3
jmp 0x7ebe
movq 0x38(%r14), %r15
xorl %r13d, %r13d
xorl %ebx, %ebx
testl %ebx, %ebx
jne 0x7e6a
movq %r14, %rdi
movq %r15, %rsi
callq 0x6114
testl %eax, %eax
jne 0x7ee1
incq %r15
movb (%r14), %al
movl %ebx, %ecx
cmpb $0x2, %al
jne 0x7e81
cmpw $0x0, 0x60(%r14,%rcx)
sete %cl
movl $0x2, %edx
jmp 0x7eab
movzwl 0x62(%r14,%rcx), %edx
movzbl 0x61(%r14,%rcx), %esi
shll $0x10, %edx
shll $0x8, %esi
movzbl 0x60(%r14,%rcx), %ecx
andl $0xfff0000, %edx        # imm = 0xFFF0000
orl %esi, %edx
orl %ecx, %edx
sete %cl
movl $0x4, %edx
addl %edx, %ebx
movzbl %cl, %ecx
addl %ecx, %r13d
andl $0x1ff, %ebx            # imm = 0x1FF
decl %r12d
jne 0x7e51
xorl %ebp, %ebp
movq (%rsp), %rax
movl %r13d, (%rax)
movl %r13d, 0x24(%r14)
orb $0x1, 0x4(%r14)
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
jmp 0x7ec0
movl $0x1, %ebp
jmp 0x7ec0