// File: /workspace/github_c_makefile_star2_O0/excess-c64[P]lib1541img/src/lib/1541img/cbmdosfs.c
// Function: CbmdosFs_freeBlocks
SOEXPORT uint16_t CbmdosFs_freeBlocks(const CbmdosFs *self)
{
    uint16_t free = 664;
    if (self->options.flags & CFF_42TRACK)
    {
	free += 119;
    }
    else if (self->options.flags & CFF_40TRACK)
    {
	free += 85;
    }
    unsigned filecount = CbmdosVfs_fileCount(self->vfs);
    if (filecount > 144)
    {
	if (!(self->options.flags & CFF_ALLOWLONGDIR)) return 0xffff;
	unsigned extrafiles = filecount - 144;
	unsigned dirblocks = extrafiles / 8;
	if (extrafiles % 8) ++dirblocks;
	if (dirblocks > free) return 0xffff;
	free -= dirblocks;
    }
    else if (self->options.flags & CFF_FILESONDIRTRACK)
    {
	unsigned freefiles = 144 - filecount;
	unsigned freedirblocks = freefiles / 8;
	free += freedirblocks;
    }
    for (unsigned n = 0; n < filecount; ++n)
    {
	const CbmdosFile *file = CbmdosVfs_rfile(self->vfs, n);
	uint16_t fileBlocks = CbmdosFile_realBlocks(file);
        if (CbmdosFile_type(file) == CFT_REL)
        {
            uint8_t sidesects = fileBlocks / 120 + !!(fileBlocks % 120);
            fileBlocks += sidesects;
        }
	if (fileBlocks > free) return 0xffff;
	free -= fileBlocks;
    }
    return free;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movw $0x298, -0x12(%rbp)     # imm = 0x298
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xe7ee
movzwl -0x12(%rbp), %eax
addl $0x77, %eax
movw %ax, -0x12(%rbp)
jmp 0xe80a
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xe808
movzwl -0x12(%rbp), %eax
addl $0x55, %eax
movw %ax, -0x12(%rbp)
jmp 0xe80a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x5260
movl %eax, -0x18(%rbp)
cmpl $0x90, -0x18(%rbp)
jbe 0xe88a
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xe83d
movw $0xffff, -0x2(%rbp)     # imm = 0xFFFF
jmp 0xe979
movl -0x18(%rbp), %eax
subl $0x90, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shrl $0x3, %eax
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
andl $0x7, %eax
cmpl $0x0, %eax
je 0xe865
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jbe 0xe87b
movw $0xffff, -0x2(%rbp)     # imm = 0xFFFF
jmp 0xe979
movl -0x20(%rbp), %ecx
movzwl -0x12(%rbp), %eax
subl %ecx, %eax
movw %ax, -0x12(%rbp)
jmp 0xe8bc
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xe8ba
movl $0x90, %eax
subl -0x18(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
shrl $0x3, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %ecx
movzwl -0x12(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x12(%rbp)
jmp 0xe8bc
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0xe971
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x5360
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x56e0
movw %ax, -0x3a(%rbp)
movq -0x38(%rbp), %rdi
callq 0x57f0
cmpl $0x4, %eax
jne 0xe941
movzwl -0x3a(%rbp), %eax
movl $0x78, %ecx
cltd
idivl %ecx
movl %eax, -0x40(%rbp)
movzwl -0x3a(%rbp), %eax
movl $0x78, %ecx
cltd
idivl %ecx
movl -0x40(%rbp), %eax
cmpl $0x0, %edx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
movb %al, -0x3b(%rbp)
movzbl -0x3b(%rbp), %ecx
movzwl -0x3a(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x3a(%rbp)
movzwl -0x3a(%rbp), %eax
movzwl -0x12(%rbp), %ecx
cmpl %ecx, %eax
jle 0xe955
movw $0xffff, -0x2(%rbp)     # imm = 0xFFFF
jmp 0xe979
movzwl -0x3a(%rbp), %ecx
movzwl -0x12(%rbp), %eax
subl %ecx, %eax
movw %ax, -0x12(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xe8c3
movw -0x12(%rbp), %ax
movw %ax, -0x2(%rbp)
movw -0x2(%rbp), %ax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)