// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movq %rdi, %r15
movq 0x8(%rdi), %rdi
callq 0x5340
movq %rax, %r13
movq %rax, %rdi
callq 0x5180
movq %rax, %rbx
movq %rax, %rdi
callq 0x5500
movq %rax, %rbp
movq %r15, %rdi
movl %r14d, %esi
callq 0xb02d
movq %r13, %rdi
callq 0x52a0
movl %eax, %r12d
movq %r13, %rdi
callq 0x5780
testl %r12d, %r12d
setns %dl
movl %eax, 0x18(%rsp)
testl %eax, %eax
setne %cl
orb %dl, %cl
movq %rbp, 0x40(%rsp)
testq %rbp, %rbp
setne %al
testb %al, %cl
je 0xa8d9
movq %r13, 0x58(%rsp)
movq %r13, %rdi
callq 0x50f0
leaq 0x98(%rsp), %rsi
movq %rax, (%rsi)
movq 0x24(%r15), %rax
leaq 0x38(%rsp), %r12
movq %rax, (%r12)
movq %r12, %rdi
callq 0x5130
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rcx
callq 0xb096
testl %eax, %eax
js 0xab5f
leaq 0x7(%rsp), %rax
movb (%rax), %bpl
movq 0x10(%r15), %rax
movl %r14d, 0x8(%rsp)
movl %r14d, %ecx
leaq (%rcx,%rcx,2), %rcx
movb %bpl, 0x2(%rax,%rcx,2)
leaq 0x6(%rsp), %rdx
movb (%rdx), %r14b
movq %rcx, 0x50(%rsp)
movb %r14b, 0x3(%rax,%rcx,2)
movq %rbx, %rdi
callq 0x5640
movq %rax, %r13
movq %r15, 0x48(%rsp)
leaq 0x2c(%r15), %rax
movq %rax, 0x30(%rsp)
movw $0x1, %r8w
movb $-0x2, %r15b
movq $0x0, 0x28(%rsp)
movq 0x40(%rsp), %r12
movq %r12, 0x20(%rsp)
movzbl %bpl, %esi
leaq (%rsi,%rsi,4), %rax
leaq (%rsi,%rax,4), %rax
addq 0x30(%rsp), %rax
movzbl %r14b, %edx
movb $0x1, -0x15(%rdx,%rax)
cmpl $0x4, 0x18(%rsp)
jne 0xa837
movq 0x28(%rsp), %rcx
movzwl %cx, %eax
movb %bpl, 0x370(%rsp,%rax)
movb %r14b, 0xa0(%rsp,%rax)
incl %ecx
movq %rcx, 0x28(%rsp)
movzwl %cx, %eax
cmpl $0x2d1, %eax            # imm = 0x2D1
jae 0xab4c
movq %r13, %r12
movl %r8d, 0x1c(%rsp)
movq 0x48(%rsp), %r14
movq (%r14), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5690
movq %rax, %r13
movb $0x0, (%rax)
movq 0x20(%rsp), %rbp
cmpq $0xfe, %rbp
jbe 0xa903
movq %r14, %rdi
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0xb1f8
testl %eax, %eax
js 0xab47
movb 0x7(%rsp), %bl
movb %bl, (%r13)
movb 0x6(%rsp), %r14b
movb %r14b, 0x1(%r13)
addq $0x2, %r13
movl $0xfe, %edx
movq %r13, %rdi
movq %r12, %r13
movq %r12, %rsi
callq 0x5470
addq $-0xfe, %rbp
movq %rbp, 0x20(%rsp)
movl %ebx, %ebp
addq $0xfe, %r13
movl 0x1c(%rsp), %r8d
incl %r8d
addb $-0x2, %r15b
jmp 0xa7e9
movq %r13, %rdi
callq 0x5630
movl %eax, %ecx
xorl %eax, %eax
cmpw $-0x1, %cx
je 0xab75
movq 0x10(%r15), %rdx
movl %r14d, %esi
leaq (%rsi,%rsi,2), %rsi
movw %cx, (%rdx,%rsi,2)
jmp 0xab75
movzbl %r15b, %eax
notl %eax
addl 0x40(%rsp), %eax
movb %al, 0x1(%r13)
addq $0x2, %r13
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x5470
cmpq $0xfe, %rbp
je 0xa941
addq %rbp, %r13
movl $0xfe, %edx
subq %rbp, %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x5320
movq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x5630
movl %eax, %ecx
cmpw $-0x1, %ax
cmovel 0x1c(%rsp), %ecx
xorl %eax, %eax
movq 0x10(%r14), %rdx
movq 0x50(%rsp), %r12
movw %cx, (%rdx,%r12,2)
cmpl $0x4, 0x18(%rsp)
jne 0xab75
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
xorl %eax, %eax
movb %al, 0x17(%rsp)
movl %eax, 0x13(%rsp)
movb %al, 0x11(%rsp)
movl %eax, 0xd(%rsp)
movq %rbx, %rdi
callq 0x54a0
movq %r14, %rbx
movl %eax, %ebp
leaq 0x7(%rsp), %rsi
leaq 0x6(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %r14, %rdi
callq 0xb096
testl %eax, %eax
js 0xab53
movzwl 0x28(%rsp), %eax
imull $0x8889, %eax, %ecx     # imm = 0x8889
shrl $0x16, %ecx
imull $0x78, %ecx, %edx
xorl %r14d, %r14d
cmpw %dx, %ax
setne %r14b
addl %ecx, %r14d
movzbl 0x7(%rsp), %esi
leaq (%rsi,%rsi,4), %rax
leaq (%rsi,%rax,4), %rax
movzbl 0x6(%rsp), %edx
addq 0x30(%rsp), %rax
movb $0x1, -0x15(%rdx,%rax)
movb %sil, 0x12(%rsp)
movb %dl, 0xc(%rsp)
movq 0x10(%rbx), %rax
movb %sil, 0x4(%rax,%r12,2)
movb %dl, 0x5(%rax,%r12,2)
movq (%rbx), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5690
movq %rax, 0x60(%rsp)
movw $0x0, (%rax)
movb $0x0, 0x2(%rax)
movb %bpl, 0x3(%rax)
movzbl %r14b, %eax
movl %eax, 0x20(%rsp)
cmpw $0x2, %ax
jb 0xaad2
movl $0x1, %r13d
leaq 0x6(%rsp), %r15
leaq 0x38(%rsp), %r12
movq %rbx, %rdi
leaq 0x7(%rsp), %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xb1f8
testl %eax, %eax
js 0xab53
movzbl 0x7(%rsp), %esi
movq 0x58(%rsp,%r13,8), %rax
movb %sil, (%rax)
movzbl 0x6(%rsp), %edx
movb %dl, 0x1(%rax)
leaq (%rsi,%rsi,4), %rax
leaq (%rsi,%rax,4), %rax
addq 0x30(%rsp), %rax
movb $0x1, -0x15(%rdx,%rax)
movb %sil, 0x12(%rsp,%r13)
movb %dl, 0xc(%rsp,%r13)
movq (%rbx), %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5690
movq %rax, 0x60(%rsp,%r13,8)
movw $0x0, (%rax)
movb %r13b, 0x2(%rax)
movb %bpl, 0x3(%rax)
incq %r13
cmpb %r14b, %r13b
jb 0xaa5c
movl 0x20(%rsp), %eax
testw %ax, %ax
je 0xab43
movzwl %ax, %eax
leaq 0xa0(%rsp), %rcx
leaq 0x370(%rsp), %rdx
xorl %esi, %esi
movq 0x60(%rsp,%rsi,8), %rdi
xorl %r8d, %r8d
movb 0x12(%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 0xaaf8
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 0xab18
incq %rsi
addq $0x78, %rcx
addq $0x78, %rdx
cmpq %rax, %rsi
jne 0xaaf0
xorl %eax, %eax
jmp 0xab75
movq %r14, %rdi
jmp 0xab56
movq 0x48(%rsp), %rdi
jmp 0xab56
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0xb02d
leaq 0x4967(%rip), %rsi      # 0xf4cd
movl $0x1, %edi
callq 0x591c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq