// File: /workspace/github_c_makefile_star2_O0/excess-c64[P]lib1541img/src/lib/1541img/cbmdosfs.c
// Function: updateFile
static int updateFile(CbmdosFs *self, unsigned pos)
{
    uint8_t trackno;
    uint8_t sectno;
    uint16_t blocks = 0;

    const CbmdosFile *file = CbmdosVfs_rfile(self->vfs, pos);
    const FileData *fdat = CbmdosFile_rdata(file);
    size_t length = FileData_size(fdat);

    scratchFile(self, pos);

    int invalidType = CbmdosFile_invalidType(file);
    CbmdosFileType type = CbmdosFile_type(file);
    if ((invalidType < 0 && type == CFT_DEL) || !length)
    {
	uint16_t forcedBlocks = CbmdosFile_forcedBlocks(file);
	if (forcedBlocks != 0xffff)
	{
	    self->dir.entries[pos].blocks = forcedBlocks;
	}
	return 0;
    }

    CbmdosFsOptOverrides overrides = CbmdosFile_optOverrides(file);
    CbmdosFsOptions opts = self->options;
    CbmdosFsOptions_applyOverrides(&opts, &overrides);

    uint8_t tracks[720];
    uint8_t sectors[720];
    uint16_t sidesectlinkno = 0;

    if (findStartSector(self, &trackno, &sectno, &opts) < 0) goto fail;
    self->dir.entries[pos].starttrack = trackno;
    self->dir.entries[pos].startsector = sectno;
    const uint8_t *content = FileData_rcontent(fdat);
    while (length)
    {
	self->bam[trackno-1][sectno] = 1;
        if (type == CFT_REL)
        {
            tracks[sidesectlinkno] = trackno;
            sectors[sidesectlinkno] = sectno;
            if (++sidesectlinkno > 720)
            {
                scratchFile(self, pos);
                goto fail;
            }
        }
	uint8_t *block = Sector_content(D64_sector(self->d64, trackno, sectno));
	block[0] = 0;
	if (length <= 254)
	{
	    block[1] = length+1;
	    memcpy(block+2, content, length);
	    if (length < 254)
	    {
		memset(block+2+length, 0, 254-length);
	    }
	    length = 0;
	}
	else
	{
	    if (findNextSector(self, &trackno, &sectno, &opts) < 0)
	    {
		scratchFile(self, pos);
		goto fail;
	    }
	    block[0] = trackno;
	    block[1] = sectno;
	    memcpy(block+2, content, 254);
	    length -= 254;
	    content += 254;
	}
	++blocks;
    }
    uint16_t forcedBlocks = CbmdosFile_forcedBlocks(file);
    if (forcedBlocks != 0xffff) blocks = forcedBlocks;
    self->dir.entries[pos].blocks = blocks;

    if (type == CFT_REL)
    {
        uint8_t sidesectnum = sidesectlinkno / 120
            + !!(sidesectlinkno % 120);
        uint8_t *sidesects[6] = { 0 };
        uint8_t sidetracks[6] = { 0 };
        uint8_t sidesectors[6] = { 0 };
        uint8_t recordlen = CbmdosFile_recordLength(file);

        if (findStartSector(self, &trackno, &sectno, &opts) < 0)
        {
            scratchFile(self, pos);
            goto fail;
        }
        self->bam[trackno-1][sectno] = 1;
        sidetracks[0] = trackno;
        sidesectors[0] = sectno;
        self->dir.entries[pos].sidetrack = trackno;
        self->dir.entries[pos].sidesector = sectno;
        sidesects[0] = Sector_content(D64_sector(self->d64, trackno, sectno));
        sidesects[0][0] = 0;
        sidesects[0][1] = 0;
        sidesects[0][2] = 0;
        sidesects[0][3] = recordlen;
        for (uint8_t i = 1; i < sidesectnum; ++i)
        {
            if (findNextSector(self, &trackno, &sectno, &opts) < 0)
            {
                scratchFile(self, pos);
                goto fail;
            }
            sidesects[i-1][0] = trackno;
            sidesects[i-1][1] = sectno;
            self->bam[trackno-1][sectno] = 1;
            sidetracks[i] = trackno;
            sidesectors[i] = sectno;
            sidesects[i] = Sector_content(D64_sector(
                        self->d64, trackno, sectno));
            sidesects[i][0] = 0;
            sidesects[i][1] = 0;
            sidesects[i][2] = i;
            sidesects[i][3] = recordlen;
        }
        for (uint8_t i = 0; i < sidesectnum; ++i)
        {
            for (uint8_t j = 0; j < 6; ++j)
            {
                sidesects[i][4+2*j] = sidetracks[j];
                sidesects[i][5+2*j] = sidesectors[j];
            }
            for (int l = 0; l < 120; ++l)
            {
                sidesects[i][16+2*l] = tracks[l+120*i];
                sidesects[i][17+2*l] = sectors[l+120*i];
            }
        }
    }

    return 0;

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x660, %rsp            # imm = 0x660
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movw $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x14(%rbp), %esi
callq 0x5360
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5190
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x5550
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0xf0a0
movq -0x20(%rbp), %rdi
callq 0x52c0
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rdi
callq 0x57f0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0xe0a6
cmpl $0x0, -0x38(%rbp)
je 0xe0ad
cmpq $0x0, -0x30(%rbp)
jne 0xe0ea
movq -0x20(%rbp), %rdi
callq 0x5690
movw %ax, -0x3a(%rbp)
movzwl -0x3a(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0xe0de
movw -0x3a(%rbp), %cx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x6, %rdx, %rdx
addq %rdx, %rax
movw %cx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0xe7b4
movq -0x20(%rbp), %rdi
callq 0x50f0
movq %rax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x24(%rax), %rax
movq %rax, -0x4c(%rbp)
leaq -0x4c(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x5140
movw $0x0, -0x5f2(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x15(%rbp), %rsi
leaq -0x16(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
callq 0xf180
cmpl $0x0, %eax
jge 0xe138
jmp 0xe79c
movb -0x15(%rbp), %cl
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x6, %rdx, %rdx
addq %rdx, %rax
movb %cl, 0x2(%rax)
movb -0x16(%rbp), %cl
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x6, %rdx, %rdx
addq %rdx, %rax
movb %cl, 0x3(%rax)
movq -0x28(%rbp), %rdi
callq 0x56a0
movq %rax, -0x600(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xe32c
movq -0x10(%rbp), %rax
addq $0x2c, %rax
movzbl -0x15(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x15, %rcx, %rcx
addq %rcx, %rax
movzbl -0x16(%rbp), %ecx
movb $0x1, (%rax,%rcx)
cmpl $0x4, -0x38(%rbp)
jne 0xe1fb
movb -0x15(%rbp), %cl
movzwl -0x5f2(%rbp), %eax
movb %cl, -0x320(%rbp,%rax)
movb -0x16(%rbp), %cl
movzwl -0x5f2(%rbp), %eax
movb %cl, -0x5f0(%rbp,%rax)
movw -0x5f2(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x5f2(%rbp)
movzwl %ax, %eax
cmpl $0x2d0, %eax            # imm = 0x2D0
jle 0xe1f9
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0xf0a0
jmp 0xe79c
jmp 0xe1fb
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movb -0x15(%rbp), %al
movzbl %al, %esi
movzbl -0x16(%rbp), %edx
callq 0x5030
movq %rax, %rdi
callq 0x56f0
movq %rax, -0x608(%rbp)
movq -0x608(%rbp), %rax
movb $0x0, (%rax)
cmpq $0xfe, -0x30(%rbp)
ja 0xe299
movq -0x30(%rbp), %rax
addq $0x1, %rax
movb %al, %cl
movq -0x608(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x608(%rbp), %rdi
addq $0x2, %rdi
movq -0x600(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x54c0
cmpq $0xfe, -0x30(%rbp)
jae 0xe28c
movq -0x608(%rbp), %rdi
addq $0x2, %rdi
addq -0x30(%rbp), %rdi
movl $0xfe, %edx
subq -0x30(%rbp), %rdx
xorl %esi, %esi
callq 0x5340
movq $0x0, -0x30(%rbp)
jmp 0xe31b
movq -0x10(%rbp), %rdi
leaq -0x15(%rbp), %rsi
leaq -0x16(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
callq 0xf250
cmpl $0x0, %eax
jge 0xe2c4
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0xf0a0
jmp 0xe79c
movb -0x15(%rbp), %cl
movq -0x608(%rbp), %rax
movb %cl, (%rax)
movb -0x16(%rbp), %cl
movq -0x608(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x608(%rbp), %rdi
addq $0x2, %rdi
movq -0x600(%rbp), %rsi
movl $0xfe, %edx
callq 0x54c0
movq -0x30(%rbp), %rax
subq $0xfe, %rax
movq %rax, -0x30(%rbp)
movq -0x600(%rbp), %rax
addq $0xfe, %rax
movq %rax, -0x600(%rbp)
movw -0x18(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x18(%rbp)
jmp 0xe178
movq -0x20(%rbp), %rdi
callq 0x5690
movw %ax, -0x60a(%rbp)
movzwl -0x60a(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0xe355
movw -0x60a(%rbp), %ax
movw %ax, -0x18(%rbp)
movw -0x18(%rbp), %cx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x6, %rdx, %rdx
addq %rdx, %rax
movw %cx, (%rax)
cmpl $0x4, -0x38(%rbp)
jne 0xe793
movzwl -0x5f2(%rbp), %eax
movl $0x78, %ecx
cltd
idivl %ecx
movl %eax, -0x658(%rbp)
movzwl -0x5f2(%rbp), %eax
movl $0x78, %ecx
cltd
idivl %ecx
movl -0x658(%rbp), %eax
cmpl $0x0, %edx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
movb %al, -0x60b(%rbp)
leaq -0x640(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x5340
leaq -0x646(%rbp), %rdi
xorl %esi, %esi
movl $0x6, %edx
callq 0x5340
leaq -0x64c(%rbp), %rdi
xorl %esi, %esi
movl $0x6, %edx
callq 0x5340
movq -0x20(%rbp), %rdi
callq 0x54f0
movb %al, -0x64d(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x15(%rbp), %rsi
leaq -0x16(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
callq 0xf180
cmpl $0x0, %eax
jge 0xe42f
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0xf0a0
jmp 0xe79c
movq -0x10(%rbp), %rax
addq $0x2c, %rax
movzbl -0x15(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x15, %rcx, %rcx
addq %rcx, %rax
movzbl -0x16(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movb -0x15(%rbp), %al
movb %al, -0x646(%rbp)
movb -0x16(%rbp), %al
movb %al, -0x64c(%rbp)
movb -0x15(%rbp), %cl
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x6, %rdx, %rdx
addq %rdx, %rax
movb %cl, 0x4(%rax)
movb -0x16(%rbp), %cl
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x14(%rbp), %edx
imulq $0x6, %rdx, %rdx
addq %rdx, %rax
movb %cl, 0x5(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movb -0x15(%rbp), %al
movzbl %al, %esi
movzbl -0x16(%rbp), %edx
callq 0x5030
movq %rax, %rdi
callq 0x56f0
movq %rax, -0x640(%rbp)
movq -0x640(%rbp), %rax
movb $0x0, (%rax)
movq -0x640(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x640(%rbp), %rax
movb $0x0, 0x2(%rax)
movb -0x64d(%rbp), %cl
movq -0x640(%rbp), %rax
movb %cl, 0x3(%rax)
movb $0x1, -0x64e(%rbp)
movzbl -0x64e(%rbp), %eax
movzbl -0x60b(%rbp), %ecx
cmpl %ecx, %eax
jge 0xe63d
movq -0x10(%rbp), %rdi
leaq -0x15(%rbp), %rsi
leaq -0x16(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
callq 0xf250
cmpl $0x0, %eax
jge 0xe52f
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0xf0a0
jmp 0xe79c
movb -0x15(%rbp), %cl
movzbl -0x64e(%rbp), %eax
subl $0x1, %eax
cltq
movq -0x640(%rbp,%rax,8), %rax
movb %cl, (%rax)
movb -0x16(%rbp), %cl
movzbl -0x64e(%rbp), %eax
subl $0x1, %eax
cltq
movq -0x640(%rbp,%rax,8), %rax
movb %cl, 0x1(%rax)
movq -0x10(%rbp), %rax
addq $0x2c, %rax
movzbl -0x15(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x15, %rcx, %rcx
addq %rcx, %rax
movzbl -0x16(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movb -0x15(%rbp), %cl
movzbl -0x64e(%rbp), %eax
movb %cl, -0x646(%rbp,%rax)
movb -0x16(%rbp), %cl
movzbl -0x64e(%rbp), %eax
movb %cl, -0x64c(%rbp,%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movb -0x15(%rbp), %al
movzbl %al, %esi
movzbl -0x16(%rbp), %edx
callq 0x5030
movq %rax, %rdi
callq 0x56f0
movq %rax, %rcx
movzbl -0x64e(%rbp), %eax
movq %rcx, -0x640(%rbp,%rax,8)
movzbl -0x64e(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movb $0x0, (%rax)
movzbl -0x64e(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movb $0x0, 0x1(%rax)
movb -0x64e(%rbp), %cl
movzbl -0x64e(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movb %cl, 0x2(%rax)
movb -0x64d(%rbp), %cl
movzbl -0x64e(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movb %cl, 0x3(%rax)
movb -0x64e(%rbp), %al
addb $0x1, %al
movb %al, -0x64e(%rbp)
jmp 0xe4ee
movb $0x0, -0x64f(%rbp)
movzbl -0x64f(%rbp), %eax
movzbl -0x60b(%rbp), %ecx
cmpl %ecx, %eax
jge 0xe791
movb $0x0, -0x650(%rbp)
movzbl -0x650(%rbp), %eax
cmpl $0x6, %eax
jge 0xe6db
movzbl -0x650(%rbp), %eax
movb -0x646(%rbp,%rax), %dl
movzbl -0x64f(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movzbl -0x650(%rbp), %ecx
shll %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movzbl -0x650(%rbp), %eax
movb -0x64c(%rbp,%rax), %dl
movzbl -0x64f(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movzbl -0x650(%rbp), %ecx
shll %ecx
addl $0x5, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movb -0x650(%rbp), %al
addb $0x1, %al
movb %al, -0x650(%rbp)
jmp 0xe661
movl $0x0, -0x654(%rbp)
cmpl $0x78, -0x654(%rbp)
jge 0xe77c
movl -0x654(%rbp), %eax
movzbl -0x64f(%rbp), %ecx
imull $0x78, %ecx, %ecx
addl %ecx, %eax
cltq
movb -0x320(%rbp,%rax), %dl
movzbl -0x64f(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movl -0x654(%rbp), %ecx
shll %ecx
addl $0x10, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x654(%rbp), %eax
movzbl -0x64f(%rbp), %ecx
imull $0x78, %ecx, %ecx
addl %ecx, %eax
cltq
movb -0x5f0(%rbp,%rax), %dl
movzbl -0x64f(%rbp), %eax
movq -0x640(%rbp,%rax,8), %rax
movl -0x654(%rbp), %ecx
shll %ecx
addl $0x11, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x654(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x654(%rbp)
jmp 0xe6e5
jmp 0xe77e
movb -0x64f(%rbp), %al
addb $0x1, %al
movb %al, -0x64f(%rbp)
jmp 0xe644
jmp 0xe793
movl $0x0, -0x4(%rbp)
jmp 0xe7b4
movl $0x1, %edi
leaq 0x5d55(%rip), %rsi      # 0x144fd
callq 0x5a60
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x660, %rsp            # imm = 0x660
popq %rbp
retq