// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, %rbx
movl $0x12, %esi
movl $0x1, %edx
callq 0xad32
movq (%rbx), %rdi
movl $0x12, %esi
movl $0x1, %edx
callq 0x5030
movq %rax, %rdi
callq 0x5690
movq %rax, %r14
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x5320
movq %r14, 0x10(%rsp)
movb $-0x1, 0x1(%r14)
movb $0x1, 0x192(%rbx)
cmpl $0x0, 0x18(%rbx)
je 0x9e8d
leaq 0x2c(%rbx), %rax
movq %rax, 0x28(%rsp)
movb $0x1, %bpl
movb $0x12, %r12b
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpb $0x8, %r15b
movq %r14, 0x20(%rsp)
jne 0x9d70
movzbl 0x28(%rbx), %ecx
movl 0x24(%rbx), %r8d
movl $0x200, %eax            # imm = 0x200
andl %eax, %r8d
movzbl %r12b, %esi
movzbl %bpl, %r13d
movq %rbx, %rdi
movl %r13d, %edx
callq 0xade0
cmpb $-0x1, %al
jne 0x9cff
testb $0x1, 0x24(%rbx)
je 0x9e95
movl 0x24(%rbx), %ebp
movzbl %r12b, %esi
movl %ebp, %edi
callq 0xae7e
cmpb $0x12, %al
je 0x9e95
movl %eax, %r12d
movzbl 0x28(%rbx), %ecx
andl $0x200, %ebp            # imm = 0x200
movzbl %al, %esi
movq %rbx, %rdi
movl %r13d, %edx
movl %ebp, %r8d
callq 0xade0
cmpb $-0x1, %al
je 0x9cc7
movl %eax, %ebp
movq 0x10(%rsp), %rax
movb %r12b, (%rax)
movb %bpl, 0x1(%rax)
movq (%rbx), %rdi
movzbl %r12b, %r13d
movzbl %bpl, %r14d
movl %r13d, %esi
movl %r14d, %edx
callq 0x5030
movq %rax, %rdi
callq 0x5690
movq %rax, %r15
leaq (,%r13,4), %rax
addq %r13, %rax
leaq (,%rax,4), %rax
addq %r13, %rax
addq 0x28(%rsp), %rax
movb $0x1, -0x15(%r14,%rax)
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
xorl %esi, %esi
callq 0x5320
movq %r15, 0x10(%rsp)
movb $-0x1, 0x1(%r15)
xorl %r15d, %r15d
movq 0x20(%rsp), %r14
movq 0x8(%rbx), %rdi
movl %r14d, %esi
callq 0x5340
movq %rax, %r13
movq %rax, %rdi
callq 0x52a0
testl %eax, %eax
js 0x9d8f
andb $0xf, %al
jmp 0x9d97
movq %r13, %rdi
callq 0x5780
movl %r15d, 0x1c(%rsp)
movzbl %r15b, %r15d
shll $0x5, %r15d
addq 0x10(%rsp), %r15
movb %al, 0x2(%r15)
movq %r13, %rdi
callq 0x53a0
testl %eax, %eax
je 0x9dbe
orb $0x40, 0x2(%r15)
movq %r13, %rdi
callq 0x5440
testl %eax, %eax
je 0x9dcf
orb $-0x80, 0x2(%r15)
movq 0x10(%rbx), %rax
leaq (%r14,%r14), %rcx
leaq (%rcx,%rcx,2), %r14
movb 0x2(%rax,%r14), %al
movb %al, 0x3(%r15)
movq 0x10(%rbx), %rax
movb 0x3(%rax,%r14), %al
movb %al, 0x4(%r15)
leaq 0x5(%r15), %rax
movq %rax, 0x30(%rsp)
movaps 0x4cef(%rip), %xmm0     # 0xeaf0
movups %xmm0, 0x5(%r15)
movq %r13, %rdi
leaq 0xf(%rsp), %rsi
callq 0x5360
movzbl 0xf(%rsp), %edx
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x5470
movq %r13, %rdi
callq 0x5780
cmpl $0x4, %eax
jne 0x9e58
movq 0x10(%rbx), %rax
movb 0x4(%rax,%r14), %al
movb %al, 0x15(%r15)
movq 0x10(%rbx), %rax
movb 0x5(%rax,%r14), %al
movb %al, 0x16(%r15)
movq %r13, %rdi
callq 0x54a0
movb %al, 0x17(%r15)
movq 0x10(%rbx), %rax
movb (%rax,%r14), %al
movb %al, 0x1e(%r15)
movq 0x10(%rbx), %rax
movb 0x1(%rax,%r14), %al
movb %al, 0x1f(%r15)
movl 0x1c(%rsp), %r15d
incb %r15b
movq 0x20(%rsp), %r14
incq %r14
movl 0x18(%rbx), %eax
cmpq %rax, %r14
jb 0x9c87
andb $-0x5, 0x20(%rbx)
xorl %eax, %eax
jmp 0x9eab
leaq 0x551e(%rip), %rsi      # 0xf3ba
movl $0x1, %edi
callq 0x591c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq