// File: /workspace/github_c_makefile_star2_O2/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 $0x648, %rsp            # imm = 0x648
movl %esi, %ebx
movq %rdi, %r15
movq 0x8(%rdi), %rdi
callq 0x5350
movq %rax, %r13
movq %rax, %rdi
callq 0x5180
movq %rax, %r14
movq %rax, %rdi
callq 0x5530
movq %rax, %r12
movq %r15, %rdi
movl %ebx, %esi
callq 0xabef
movq %r13, %rdi
callq 0x52b0
movl %eax, %ebp
movq %r13, %rdi
callq 0x57c0
testl %ebp, %ebp
setns %dl
movl %eax, 0x2c(%rsp)
testl %eax, %eax
setne %cl
orb %dl, %cl
testq %r12, %r12
setne %al
movl %ebx, 0x10(%rsp)
movl %ebx, %ebx
testb %al, %cl
je 0xa542
movq %r14, %rbp
movq %r13, %rdi
callq 0x50f0
leaq 0x98(%rsp), %rsi
movq %rax, (%rsi)
movq 0x24(%r15), %rax
leaq 0x48(%rsp), %r14
movq %rax, (%r14)
movq %r14, %rdi
callq 0x5130
leaq 0xe(%rsp), %rsi
leaq 0xf(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rcx
callq 0xac52
testl %eax, %eax
js 0xa705
movq %r13, 0x58(%rsp)
leaq 0xe(%rsp), %rax
movb (%rax), %dl
movq 0x10(%r15), %rax
imulq $0x6, %rbx, %rcx
movl %edx, %ebx
movb %dl, 0x2(%rax,%rcx)
leaq 0xf(%rsp), %rdx
movb (%rdx), %r13b
movq %rcx, 0x50(%rsp)
movb %r13b, 0x3(%rax,%rcx)
movq %rbp, %rdi
callq 0x5680
movq %rax, 0x20(%rsp)
movq %r15, 0x30(%rsp)
leaq 0x2c(%r15), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
xorl %r15d, %r15d
testq %r12, %r12
je 0xa569
movzbl %bl, %esi
imulq $0x15, %rsi, %rax
addq 0x40(%rsp), %rax
movzbl %r13b, %edx
movb $0x1, -0x15(%rdx,%rax)
cmpl $0x4, 0x2c(%rsp)
jne 0xa474
movq 0x38(%rsp), %rcx
movzwl %cx, %eax
movb %bl, 0x370(%rsp,%rax)
movb %r13b, 0xa0(%rsp,%rax)
incl %ecx
movq %rcx, 0x38(%rsp)
cmpw $0x2d1, %cx             # imm = 0x2D1
jae 0xa6ed
movl %ebx, %ebp
movq 0x30(%rsp), %rbx
movq (%rbx), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x56d0
movq %rax, %r14
movb $0x0, (%rax)
cmpq $0xfe, %r12
ja 0xa4da
leal 0x1(%r12), %eax
movb %al, 0x1(%r14)
addq $0x2, %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x54a0
cmpq $0xfe, %r12
je 0xa4d5
addq %r12, %r14
movl $0xfe, %edx
subq %r12, %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x5330
xorl %r12d, %r12d
jmp 0xa538
movq %rbx, %rdi
leaq 0xe(%rsp), %rsi
leaq 0xf(%rsp), %rdx
leaq 0x48(%rsp), %rcx
callq 0xacef
testl %eax, %eax
js 0xa6f4
movb 0xe(%rsp), %bpl
movb %bpl, (%r14)
movb 0xf(%rsp), %al
movl %eax, %r13d
movb %al, 0x1(%r14)
addq $0x2, %r14
movl $0xfe, %edx
movq %r14, %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x54a0
addq $-0xfe, %r12
addq $0xfe, %rbx
movq %rbx, 0x20(%rsp)
movl %ebp, %ebx
incl %r15d
jmp 0xa426
movq %r13, %rdi
callq 0x5670
movl %eax, %ecx
xorl %eax, %eax
cmpw $-0x1, %cx
je 0xa717
movq 0x10(%r15), %rdx
imulq $0x6, %rbx, %rsi
movw %cx, (%rdx,%rsi)
jmp 0xa717
movq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x5670
movl %eax, %ecx
cmpw $-0x1, %ax
cmovel %r15d, %ecx
xorl %eax, %eax
movq 0x30(%rsp), %r15
movq 0x10(%r15), %rdx
movq 0x50(%rsp), %rsi
movw %cx, (%rdx,%rsi)
cmpl $0x4, 0x2c(%rsp)
jne 0xa717
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
xorl %eax, %eax
movb %al, 0x1f(%rsp)
andl $0x0, 0x1b(%rsp)
movb %al, 0x19(%rsp)
andl $0x0, 0x15(%rsp)
movq %rbx, %rdi
callq 0x54d0
movl %eax, %ebp
leaq 0xe(%rsp), %rsi
leaq 0xf(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movq %r15, %rdi
callq 0xac52
testl %eax, %eax
js 0xa6f9
movw $0x78, %cx
movq 0x38(%rsp), %rax
xorl %edx, %edx
divw %cx
movl %eax, %r14d
cmpw $0x1, %dx
sbbw $-0x1, %r14w
leaq 0xe(%rsp), %rax
movzbl (%rax), %esi
imulq $0x15, %rsi, %rax
leaq 0xf(%rsp), %r12
movzbl (%r12), %edx
addq 0x40(%rsp), %rax
movb $0x1, -0x15(%rdx,%rax)
movb %sil, 0x1a(%rsp)
movb %dl, 0x14(%rsp)
movq 0x10(%r15), %rax
movq 0x50(%rsp), %rcx
movb %sil, 0x4(%rax,%rcx)
movb %dl, 0x5(%rax,%rcx)
movq (%r15), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x56d0
movq %rax, 0x60(%rsp)
andw $0x0, (%rax)
movb $0x0, 0x2(%rax)
movb %bpl, 0x3(%rax)
movzbl %r14b, %eax
movl %eax, 0x20(%rsp)
pushq $0x1
popq %rbx
leaq 0x48(%rsp), %r13
movq 0x30(%rsp), %r15
cmpb %r14b, %bl
jae 0xa729
movq %r15, %rdi
leaq 0xe(%rsp), %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0xacef
testl %eax, %eax
js 0xa6f9
movzbl 0xe(%rsp), %esi
movq 0x58(%rsp,%rbx,8), %rax
movb %sil, (%rax)
movzbl 0xf(%rsp), %edx
movb %dl, 0x1(%rax)
imulq $0x15, %rsi, %rax
addq 0x40(%rsp), %rax
movb $0x1, -0x15(%rdx,%rax)
movb %sil, 0x1a(%rsp,%rbx)
movb %dl, 0x14(%rsp,%rbx)
movq (%r15), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x56d0
movq %rax, 0x60(%rsp,%rbx,8)
andw $0x0, (%rax)
movb %bl, 0x2(%rax)
movb %bpl, 0x3(%rax)
incq %rbx
jmp 0xa67c
movq 0x30(%rsp), %rdi
jmp 0xa6fc
movq %rbx, %rdi
jmp 0xa6fc
movq %r15, %rdi
movl 0x10(%rsp), %esi
callq 0xabef
leaq 0x3dc1(%rip), %rsi      # 0xe4cd
pushq $0x1
popq %rdi
callq 0x5960
pushq $-0x1
popq %rax
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x20(%rsp), %eax
leaq 0xa0(%rsp), %rcx
leaq 0x370(%rsp), %rdx
xorl %esi, %esi
cmpq %rax, %rsi
je 0xa799
movq 0x60(%rsp,%rsi,8), %rdi
xorl %r8d, %r8d
cmpq $0x6, %r8
je 0xa76c
movb 0x1a(%rsp,%r8), %r9b
movb %r9b, 0x4(%rdi,%r8,2)
movb 0x14(%rsp,%r8), %r9b
movb %r9b, 0x5(%rdi,%r8,2)
incq %r8
jmp 0xa74d
xorl %r8d, %r8d
cmpq $0x78, %r8
je 0xa78c
movb (%rdx,%r8), %r9b
movb %r9b, 0x10(%rdi,%r8,2)
movb (%rcx,%r8), %r9b
movb %r9b, 0x11(%rdi,%r8,2)
incq %r8
jmp 0xa76f
incq %rsi
addq $0x78, %rcx
addq $0x78, %rdx
jmp 0xa740
xorl %eax, %eax
jmp 0xa717