// File: /workspace/github_c_makefile_star2_O2/excess-c64[P]lib1541img/src/lib/1541img/cbmdosfs.c
// Function: updateDir
static int updateDir(CbmdosFs *self)
{
    uint8_t trackno = 18;
    uint8_t sectno = 1;
    deleteChain(self, trackno, sectno);
    uint8_t *dir = Sector_content(D64_sector(self->d64, trackno, sectno));
    memset(dir, 0, 256);
    dir[1] = 0xff;
    self->bam[17][1] = 1;
    uint8_t dirpos = 0;
    for (unsigned i = 0; i < self->dir.size; ++i)
    {
        if (dirpos == 8)
        {
            uint8_t nextsect = freeSectorOnTrack(self, trackno, sectno,
                    self->options.dirInterleave,
                    (self->options.flags & CFF_SIMPLEINTERLEAVE));
            if (nextsect == 0xff)
            {
                if (!(self->options.flags & CFF_ALLOWLONGDIR)) goto fail;
                while (nextsect == 0xff)
                {
                    uint8_t nexttrack = nextTrack(&(self->options), trackno);
                    if (nexttrack == 18) goto fail;
                    trackno = nexttrack;
                    nextsect = freeSectorOnTrack(self, trackno, sectno,
                            self->options.dirInterleave,
                            (self->options.flags & CFF_SIMPLEINTERLEAVE));
                }
            }
            sectno = nextsect;
            dir[0] = trackno;
            dir[1] = sectno;
            dir = Sector_content(D64_sector(self->d64, trackno, sectno));
	    self->bam[trackno-1][sectno] = 1;
            memset(dir, 0, 256);
            dir[1] = 0xff;
            dirpos = 0;
        }
        const CbmdosFile *file = CbmdosVfs_rfile(self->vfs, i);
        uint8_t *dirent = dir + 0x20*dirpos;
	int invalidType = CbmdosFile_invalidType(file);
	if (invalidType < 0) dirent[2] = CbmdosFile_type(file);
	else dirent[2] = (uint8_t)invalidType & 0xf;
        if (CbmdosFile_locked(file)) dirent[2] |= 1<<6;
        if (CbmdosFile_closed(file)) dirent[2] |= 1<<7;
        dirent[3] = self->dir.entries[i].starttrack;
        dirent[4] = self->dir.entries[i].startsector;
        memset(dirent+5, 0xa0, 0x10);
        uint8_t namelen;
        const char *name = CbmdosFile_name(file, &namelen);
        memcpy(dirent+5, name, namelen);
        if (CbmdosFile_type(file) == CFT_REL)
        {
            dirent[0x15] = self->dir.entries[i].sidetrack;
            dirent[0x16] = self->dir.entries[i].sidesector;
            dirent[0x17] = CbmdosFile_recordLength(file);
        }
        dirent[0x1e] = self->dir.entries[i].blocks & 0xff;
        dirent[0x1f] = self->dir.entries[i].blocks >> 8;
	++dirpos;
    }
    self->status &= ~CFS_DIRFULL;
    return 0;

fail:
    logmsg(L_ERROR, "CbmdosFs: no space left writing directory.");
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
pushq $0x12
popq %rbp
pushq $0x1
popq %r14
movl %ebp, %esi
movl %r14d, %edx
callq 0xa925
movq (%rbx), %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x5030
movq %rax, %rdi
callq 0x56d0
movq %rax, %r14
xorl %r13d, %r13d
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x5330
movq %r14, 0x8(%rsp)
movb $-0x1, 0x1(%r14)
leaq 0x2c(%rbx), %rax
movq %rax, 0x10(%rsp)
movb $0x1, 0x192(%rbx)
leaq 0x24(%rbx), %r14
movb $0x12, %bpl
movb $0x1, %dl
movl $0x200, %r12d           # imm = 0x200
xorl %ecx, %ecx
movq %r14, 0x18(%rsp)
movl 0x18(%rbx), %eax
cmpq %rax, %r13
jae 0x9b62
cmpb $0x8, %cl
jne 0x99ef
movzbl 0x28(%rbx), %ecx
movl 0x24(%rbx), %r8d
andl %r12d, %r8d
movzbl %bpl, %esi
movzbl %dl, %r15d
movq %rbx, %rdi
movl %r15d, %edx
callq 0xa9c5
cmpb $-0x1, %al
je 0x9b11
movq 0x8(%rsp), %rcx
movb %bpl, (%rcx)
movb %al, 0x1(%rcx)
movq (%rbx), %rdi
movzbl %bpl, %r15d
movb %al, 0x2(%rsp)
movzbl %al, %r12d
movl %r15d, %esi
movl %r12d, %edx
callq 0x5030
movq %rax, %rdi
callq 0x56d0
movq %rax, %r14
imulq $0x15, %r15, %rax
addq 0x10(%rsp), %rax
movb $0x1, -0x15(%r12,%rax)
movl $0x0, 0x4(%rsp)
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
xorl %esi, %esi
callq 0x5330
movq %r14, 0x8(%rsp)
movb $-0x1, 0x1(%r14)
jmp 0x99f7
movl %ecx, 0x4(%rsp)
movb %dl, 0x2(%rsp)
movq 0x8(%rbx), %rdi
movl %r13d, %esi
callq 0x5350
movq %rax, %r15
movq %rax, %rdi
callq 0x52b0
testl %eax, %eax
js 0x9a16
andb $0xf, %al
jmp 0x9a1e
movq %r15, %rdi
callq 0x57c0
movzbl 0x4(%rsp), %r14d
shll $0x5, %r14d
addq 0x8(%rsp), %r14
movb %al, 0x2(%r14)
movq %r15, %rdi
callq 0x53c0
testl %eax, %eax
je 0x9a42
orb $0x40, 0x2(%r14)
movq %r15, %rdi
callq 0x5470
testl %eax, %eax
je 0x9a53
orb $-0x80, 0x2(%r14)
movq 0x10(%rbx), %rax
movq %r13, 0x20(%rsp)
imulq $0x6, %r13, %r13
movb 0x2(%rax,%r13), %al
movb %al, 0x3(%r14)
movq 0x10(%rbx), %rax
movb 0x3(%rax,%r13), %al
movb %al, 0x4(%r14)
leaq 0x5(%r14), %r12
movaps 0x406f(%rip), %xmm0     # 0xdaf0
movups %xmm0, 0x5(%r14)
movq %r15, %rdi
leaq 0x3(%rsp), %rsi
callq 0x5370
movzbl 0x3(%rsp), %edx
movq %r12, %rdi
movq %rax, %rsi
callq 0x54a0
movq %r15, %rdi
callq 0x57c0
cmpl $0x4, %eax
jne 0x9ad6
movq 0x10(%rbx), %rax
movb 0x4(%rax,%r13), %al
movb %al, 0x15(%r14)
movq 0x10(%rbx), %rax
movb 0x5(%rax,%r13), %al
movb %al, 0x16(%r14)
movq %r15, %rdi
callq 0x54d0
movb %al, 0x17(%r14)
movq 0x10(%rbx), %rax
movb (%rax,%r13), %al
movb %al, 0x1e(%r14)
movq 0x10(%rbx), %rax
movb 0x1(%rax,%r13), %al
movb %al, 0x1f(%r14)
movl 0x4(%rsp), %ecx
incb %cl
movq 0x20(%rsp), %r13
incq %r13
movq 0x18(%rsp), %r14
movl $0x200, %r12d           # imm = 0x200
movb 0x2(%rsp), %dl
jmp 0x9952
testb $0x1, (%r14)
je 0x9b4e
movb $-0x1, %al
cmpb $-0x1, %al
jne 0x998d
movzbl %bpl, %esi
movq %r14, %rdi
callq 0xaa5f
cmpb $0x12, %al
je 0x9b4e
movl %eax, %ebp
movzbl 0x28(%rbx), %ecx
movl 0x24(%rbx), %r8d
andl %r12d, %r8d
movzbl %al, %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0xa9c5
jmp 0x9b19
leaq 0x4865(%rip), %rsi      # 0xe3ba
pushq $0x1
popq %rdi
callq 0x5960
pushq $-0x1
popq %rax
jmp 0x9b68
andb $-0x5, 0x20(%rbx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq