// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movb $0x12, -0x11(%rbp)
movb $0x1, -0x12(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzbl -0x12(%rbp), %edx
callq 0xeaa0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzbl -0x12(%rbp), %edx
callq 0x5030
movq %rax, %rdi
callq 0x56f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x5340
movq -0x20(%rbp), %rax
movb $-0x1, 0x1(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x192(%rax)
movb $0x0, -0x21(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jae 0xd19b
movzbl -0x21(%rbp), %eax
cmpl $0x8, %eax
jne 0xcfd6
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %dl
movb -0x12(%rbp), %cl
movq -0x10(%rbp), %rax
movb 0x28(%rax), %al
movq -0x10(%rbp), %rsi
movl 0x24(%rsi), %r8d
andl $0x200, %r8d            # imm = 0x200
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0xebb0
movb %al, -0x29(%rbp)
movzbl -0x29(%rbp), %eax
cmpl $0xff, %eax
jne 0xcf5e
movq -0x10(%rbp), %rax
movl 0x24(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xcef4
jmp 0xd1b1
jmp 0xcef6
movzbl -0x29(%rbp), %eax
cmpl $0xff, %eax
jne 0xcf5c
movq -0x10(%rbp), %rdi
addq $0x24, %rdi
movzbl -0x11(%rbp), %esi
callq 0xece0
movb %al, -0x2a(%rbp)
movzbl -0x2a(%rbp), %eax
cmpl $0x12, %eax
jne 0xcf23
jmp 0xd1b1
movb -0x2a(%rbp), %al
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %dl
movb -0x12(%rbp), %cl
movq -0x10(%rbp), %rax
movb 0x28(%rax), %al
movq -0x10(%rbp), %rsi
movl 0x24(%rsi), %r8d
andl $0x200, %r8d            # imm = 0x200
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
callq 0xebb0
movb %al, -0x29(%rbp)
jmp 0xcef6
jmp 0xcf5e
movb -0x29(%rbp), %al
movb %al, -0x12(%rbp)
movb -0x11(%rbp), %cl
movq -0x20(%rbp), %rax
movb %cl, (%rax)
movb -0x12(%rbp), %cl
movq -0x20(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzbl -0x12(%rbp), %edx
callq 0x5030
movq %rax, %rdi
callq 0x56f0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x2c, %rax
movzbl -0x11(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x15, %rcx, %rcx
addq %rcx, %rax
movzbl -0x12(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movq -0x20(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x5340
movq -0x20(%rbp), %rax
movb $-0x1, 0x1(%rax)
movb $0x0, -0x21(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x28(%rbp), %esi
callq 0x5360
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movzbl -0x21(%rbp), %ecx
shll $0x5, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x52c0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0xd025
movq -0x38(%rbp), %rdi
callq 0x57f0
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, 0x2(%rax)
jmp 0xd037
movl -0x44(%rbp), %eax
movzbl %al, %eax
andl $0xf, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, 0x2(%rax)
movq -0x38(%rbp), %rdi
callq 0x53d0
cmpl $0x0, %eax
je 0xd053
movq -0x40(%rbp), %rax
movzbl 0x2(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x2(%rax)
movq -0x38(%rbp), %rdi
callq 0x5490
cmpl $0x0, %eax
je 0xd072
movq -0x40(%rbp), %rax
movzbl 0x2(%rax), %ecx
orl $0x80, %ecx
movb %cl, 0x2(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x6, %rcx, %rcx
addq %rcx, %rax
movb 0x2(%rax), %cl
movq -0x40(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x6, %rcx, %rcx
addq %rcx, %rax
movb 0x3(%rax), %cl
movq -0x40(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x40(%rbp), %rdi
addq $0x5, %rdi
movl $0xa0, %esi
movl $0x10, %edx
callq 0x5340
movq -0x38(%rbp), %rdi
leaq -0x45(%rbp), %rsi
callq 0x5380
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
addq $0x5, %rdi
movq -0x50(%rbp), %rsi
movzbl -0x45(%rbp), %eax
movl %eax, %edx
callq 0x54c0
movq -0x38(%rbp), %rdi
callq 0x57f0
cmpl $0x4, %eax
jne 0xd141
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x6, %rcx, %rcx
addq %rcx, %rax
movb 0x4(%rax), %cl
movq -0x40(%rbp), %rax
movb %cl, 0x15(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x6, %rcx, %rcx
addq %rcx, %rax
movb 0x5(%rax), %cl
movq -0x40(%rbp), %rax
movb %cl, 0x16(%rax)
movq -0x38(%rbp), %rdi
callq 0x54f0
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, 0x17(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x6, %rcx, %rcx
addq %rcx, %rax
movzwl (%rax), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, 0x1e(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x28(%rbp), %ecx
imulq $0x6, %rcx, %rcx
addq %rcx, %rax
movzwl (%rax), %eax
sarl $0x8, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, 0x1f(%rax)
movb -0x21(%rbp), %al
addb $0x1, %al
movb %al, -0x21(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xce87
movq -0x10(%rbp), %rax
movl 0x20(%rax), %ecx
andl $-0x5, %ecx
movl %ecx, 0x20(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xd1c9
movl $0x1, %edi
leaq 0x722d(%rip), %rsi      # 0x143ea
callq 0x5a60
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)