// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x628, %rsp            # imm = 0x628
movl %esi, %ebx
movq %rdi, %r14
movq 0x8(%rdi), %rdi
callq 0x4340
movq %rax, %r15
movq %rax, %rdi
callq 0x4180
movq %rax, %r13
movq %rax, %rdi
callq 0x4500
movq %rax, %r12
movq %r14, %rdi
movl %ebx, 0x8(%rsp)
movl %ebx, %esi
callq 0xa3fd
movq %r15, %rdi
callq 0x42a0
movl %eax, %ebx
movq %r15, %rdi
callq 0x4780
testl %ebx, %ebx
setns %dl
movl %eax, 0x1c(%rsp)
testl %eax, %eax
setne %cl
orb %dl, %cl
testq %r12, %r12
setne %al
testb %al, %cl
je 0x9c8a
movq %r15, 0x38(%rsp)
movq %r15, %rdi
callq 0x40f0
leaq 0x78(%rsp), %rsi
movq %rax, (%rsi)
movq 0x24(%r14), %rax
leaq 0x20(%rsp), %rbx
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x4130
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0xa466
testl %eax, %eax
js 0x9f1e
leaq 0x7(%rsp), %rax
movb (%rax), %al
movq 0x10(%r14), %rcx
movl 0x8(%rsp), %edx
leaq (%rdx,%rdx,2), %rdx
movb %al, 0x2(%rcx,%rdx,2)
leaq 0x6(%rsp), %rax
movb (%rax), %al
movq %rdx, 0x30(%rsp)
movb %al, 0x3(%rcx,%rdx,2)
movq %r13, %rdi
callq 0x4640
movq %rax, %r13
leaq 0x2c(%r14), %rax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
testq %r12, %r12
je 0x9cb5
movzbl 0x7(%rsp), %eax
leaq (%rax,%rax,4), %rcx
leaq (%rax,%rcx,4), %rdx
addq 0x28(%rsp), %rdx
movzbl 0x6(%rsp), %ecx
movb $0x1, -0x15(%rcx,%rdx)
cmpl $0x4, 0x1c(%rsp)
jne 0x9bbd
movzwl %r15w, %edx
movb %al, 0x350(%rsp,%rdx)
movb %cl, 0x80(%rsp,%rdx)
incl %r15d
movzwl %r15w, %edx
cmpl $0x2d1, %edx            # imm = 0x2D1
jae 0x9e77
movq (%r14), %rdi
movzbl %al, %esi
movzbl %cl, %edx
callq 0x4030
movq %rax, %rdi
callq 0x4690
movq %rax, %rbx
movb $0x0, (%rax)
cmpq $0xfe, %r12
ja 0x9c1f
leal 0x1(%r12), %eax
movb %al, 0x1(%rbx)
addq $0x2, %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x4470
cmpq $0xfe, %r12
je 0x9c1a
addq %r12, %rbx
movl $0xfe, %edx
subq %r12, %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4320
xorl %r12d, %r12d
jmp 0x9c69
movq %r14, %rdi
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0xa5bb
testl %eax, %eax
js 0x9c7a
movb 0x7(%rsp), %al
movb %al, (%rbx)
movb 0x6(%rsp), %al
movb %al, 0x1(%rbx)
addq $0x2, %rbx
movl $0xfe, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4470
addq $-0xfe, %r12
addq $0xfe, %r13
incl %ebp
movb $0x1, %al
testb %al, %al
jne 0x9b6c
jmp 0x9f1e
movq %r14, %rdi
movl 0x8(%rsp), %esi
callq 0xa3fd
xorl %eax, %eax
jmp 0x9c6d
movq %r15, %rdi
callq 0x4630
movl %eax, %ecx
xorl %eax, %eax
cmpw $-0x1, %cx
je 0x9f34
movq 0x10(%r14), %rdx
movl 0x8(%rsp), %esi
leaq (%rsi,%rsi,2), %rsi
movw %cx, (%rdx,%rsi,2)
jmp 0x9f34
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x4630
cmpw $-0x1, %ax
cmovel %ebp, %eax
movq 0x10(%r14), %rcx
movq 0x30(%rsp), %r12
movw %ax, (%rcx,%r12,2)
cmpl $0x4, 0x1c(%rsp)
jne 0x9f46
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movw %ax, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movw %ax, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
callq 0x44a0
movl %eax, %ebp
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r14, %rdi
callq 0xa466
testl %eax, %eax
js 0x9e88
movzwl %r15w, %eax
imull $0x8889, %eax, %eax     # imm = 0x8889
shrl $0x16, %eax
imull $0x78, %eax, %ecx
xorl %ebx, %ebx
cmpw %cx, %r15w
setne %bl
addl %eax, %ebx
movzbl 0x7(%rsp), %esi
leaq (%rsi,%rsi,4), %rax
leaq (%rsi,%rax,4), %rax
movzbl 0x6(%rsp), %edx
leaq 0x2c(%r14,%rax), %rax
movb $0x1, -0x15(%rdx,%rax)
movb %sil, 0x14(%rsp)
movb %dl, 0xc(%rsp)
movq 0x10(%r14), %rax
movb %sil, 0x4(%rax,%r12,2)
movb %dl, 0x5(%rax,%r12,2)
movq (%r14), %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4690
movq %rax, 0x40(%rsp)
movw $0x0, (%rax)
movb $0x0, 0x2(%rax)
movb %bpl, 0x3(%rax)
movzbl %bl, %ebx
movl $0x5, %eax
cmpw $0x2, %bx
setae %r13b
jb 0x9ea0
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r14, %rdi
callq 0xa5bb
testl %eax, %eax
js 0x9e64
movl $0x1, %r15d
movb $0x2, %r12b
movzbl 0x7(%rsp), %esi
movq 0x38(%rsp,%r15,8), %rax
movb %sil, (%rax)
movzbl 0x6(%rsp), %edx
movb %dl, 0x1(%rax)
leaq (%rsi,%rsi,4), %rax
leaq (%rsi,%rax,4), %rax
addq 0x28(%rsp), %rax
movb $0x1, -0x15(%rdx,%rax)
movb %sil, 0x14(%rsp,%r15)
movb %dl, 0xc(%rsp,%r15)
movq (%r14), %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4690
movq %rax, 0x40(%rsp,%r15,8)
movw $0x0, (%rax)
movb %r15b, 0x2(%rax)
movb %bpl, 0x3(%rax)
movzbl %r12b, %eax
cmpw %ax, %bx
seta %r13b
jbe 0x9e9b
movq %r14, %rdi
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
leaq 0x20(%rsp), %rcx
callq 0xa5bb
incq %r15
incb %r12b
testl %eax, %eax
jns 0x9ddf
movq %r14, %rdi
movl 0x8(%rsp), %esi
callq 0xa3fd
movl $0x2, %eax
jmp 0x9ea0
movq %r14, %rdi
movl 0x8(%rsp), %esi
callq 0xa3fd
jmp 0x9f1e
movq %r14, %rdi
movl 0x8(%rsp), %esi
callq 0xa3fd
movl $0x2, %eax
jmp 0x9f19
movl $0x5, %eax
testb %r13b, %r13b
jne 0x9f19
testw %bx, %bx
je 0x9f17
movzwl %bx, %eax
leaq 0x80(%rsp), %rcx
leaq 0x350(%rsp), %rdx
xorl %esi, %esi
movq 0x40(%rsp,%rsi,8), %rdi
xorl %r8d, %r8d
movb 0x14(%rsp,%r8), %r9b
movb %r9b, 0x4(%rdi,%r8,2)
movb 0xc(%rsp,%r8), %r9b
movb %r9b, 0x5(%rdi,%r8,2)
incq %r8
cmpq $0x6, %r8
jne 0x9ec7
movq 0x40(%rsp,%rsi,8), %rdi
xorl %r8d, %r8d
movb (%rdx,%r8), %r9b
movb %r9b, 0x10(%rdi,%r8,2)
movb (%rcx,%r8), %r9b
movb %r9b, 0x11(%rdi,%r8,2)
incq %r8
cmpq $0x78, %r8
jne 0x9eec
incq %rsi
addq $0x78, %rcx
addq $0x78, %rdx
cmpq %rax, %rsi
jne 0x9ebf
xorl %eax, %eax
cmpl $0x2, %eax
jne 0x9f46
leaq 0x45a8(%rip), %rsi      # 0xe4cd
movl $0x1, %edi
callq 0x491c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x628, %rsp            # imm = 0x628
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x9f34