// File: /workspace/github_c_makefile_star2_O1/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
movl $0x12, %esi
movl $0x1, %edx
callq 0xa0fb
movq (%rbx), %rdi
movl $0x12, %esi
movl $0x1, %edx
callq 0x4030
movq %rax, %rdi
callq 0x4690
movq %rax, %r14
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x4320
movq %r14, 0x8(%rsp)
movb $-0x1, 0x1(%r14)
movb $0x1, 0x192(%rbx)
cmpl $0x0, 0x18(%rbx)
je 0x9241
leaq 0x2c(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x24(%rbx), %rax
movq %rax, 0x18(%rsp)
movb $0x1, %r15b
movb $0x12, 0x5(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpb $0x8, %r14b
jne 0x90ab
movzbl 0x28(%rbx), %ecx
movl 0x24(%rbx), %r8d
movl $0x200, %eax            # imm = 0x200
andl %eax, %r8d
movzbl 0x5(%rsp), %esi
movzbl %r15b, %r13d
movq %rbx, %rdi
movl %r13d, %edx
callq 0xa1a9
movl %eax, %ebp
cmpb $-0x1, %al
je 0x90b2
movq 0x8(%rsp), %rax
movb 0x5(%rsp), %cl
movb %cl, (%rax)
movb %bpl, 0x1(%rax)
movq (%rbx), %rdi
movzbl %cl, %r15d
movzbl %bpl, %r13d
movl %r15d, %esi
movl %r13d, %edx
callq 0x4030
movq %rax, %rdi
callq 0x4690
movq %rax, %r14
leaq (%r15,%r15,4), %rax
leaq (%r15,%rax,4), %rax
addq 0x20(%rsp), %rax
movb $0x1, -0x15(%r13,%rax)
xorl %r13d, %r13d
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
xorl %esi, %esi
callq 0x4320
movq %r14, 0x8(%rsp)
movb $-0x1, 0x1(%r14)
xorl %r14d, %r14d
movb %bpl, 0x6(%rsp)
jmp 0x9122
movb %r15b, 0x6(%rsp)
jmp 0x912b
movl %r13d, 0x10(%rsp)
movb %r15b, 0x6(%rsp)
movl $0x5, %r13d
movq 0x18(%rsp), %rax
testb $0x1, (%rax)
je 0x9122
cmpb $-0x1, %bpl
jne 0x9042
movzbl 0x5(%rsp), %esi
movq 0x18(%rsp), %rdi
callq 0xa247
movl %eax, %r15d
movl $0x5, %r13d
cmpb $0x12, %al
je 0x911c
movzbl 0x28(%rbx), %ecx
movl 0x24(%rbx), %r8d
movl $0x200, %eax            # imm = 0x200
andl %eax, %r8d
movzbl %r15b, %esi
movq %rbx, %rdi
movl 0x10(%rsp), %edx
callq 0xa1a9
movl %eax, %ebp
xorl %r13d, %r13d
movb %r15b, 0x5(%rsp)
cmpb $0x12, %r15b
jne 0x90cc
testl %r13d, %r13d
jne 0x9247
movq 0x8(%rbx), %rdi
movl %r12d, %esi
callq 0x4340
movq %rax, %rbp
movq %rax, %rdi
callq 0x42a0
testl %eax, %eax
js 0x914a
andb $0xf, %al
jmp 0x9152
movq %rbp, %rdi
callq 0x4780
movzbl %r14b, %r13d
shll $0x5, %r13d
addq 0x8(%rsp), %r13
movb %al, 0x2(%r13)
movq %rbp, %rdi
callq 0x43a0
testl %eax, %eax
je 0x9174
orb $0x40, 0x2(%r13)
movq %rbp, %rdi
callq 0x4440
testl %eax, %eax
je 0x9185
orb $-0x80, 0x2(%r13)
movq 0x10(%rbx), %rax
movq %r12, 0x10(%rsp)
leaq (%r12,%r12), %rcx
leaq (%rcx,%rcx,2), %r12
movb 0x2(%rax,%r12), %al
movb %al, 0x3(%r13)
movq 0x10(%rbx), %rax
movb 0x3(%rax,%r12), %al
movb %al, 0x4(%r13)
leaq 0x5(%r13), %r15
movaps 0x4939(%rip), %xmm0     # 0xdaf0
movups %xmm0, 0x5(%r13)
movq %rbp, %rdi
leaq 0x7(%rsp), %rsi
callq 0x4360
movzbl 0x7(%rsp), %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x4470
movq %rbp, %rdi
callq 0x4780
cmpl $0x4, %eax
jne 0x920c
movq 0x10(%rbx), %rax
movb 0x4(%rax,%r12), %al
movb %al, 0x15(%r13)
movq 0x10(%rbx), %rax
movb 0x5(%rax,%r12), %al
movb %al, 0x16(%r13)
movq %rbp, %rdi
callq 0x44a0
movb %al, 0x17(%r13)
movq 0x10(%rbx), %rax
movb (%rax,%r12), %al
movb %al, 0x1e(%r13)
movq 0x10(%rbx), %rax
movb 0x1(%rax,%r12), %al
movb %al, 0x1f(%r13)
incb %r14b
movq 0x10(%rsp), %r12
incq %r12
movl 0x18(%rbx), %eax
cmpq %rax, %r12
movb 0x6(%rsp), %r15b
jb 0x900e
movl $0x2, %r13d
cmpl $0x5, %r13d
je 0x925b
cmpl $0x2, %r13d
jne 0x9271
andb $-0x5, 0x20(%rbx)
xorl %eax, %eax
jmp 0x9271
leaq 0x5158(%rip), %rsi      # 0xe3ba
movl $0x1, %edi
callq 0x491c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq