// File: /workspace/github_c_makefile_star2_O1/excess-c64[P]lib1541img/src/lib/1541img/zcfileset.c
// Function: ZcFileSet_save
SOEXPORT int ZcFileSet_save(const ZcFileSet *self, const char *filename)
{
    Filename *fn = Filename_create();
    Filename_setFull(fn, filename);
    const char *basename = self->name;
    const char *fnbase = Filename_base(fn);
    if (fnbase)
    {
        if (fnbase[0] && fnbase[1] == '!') basename = fnbase+2;
        else basename = fnbase;
    }

    char *nextname = xmalloc(strlen(basename) + 3);
    nextname[1] = '!';
    strcpy(nextname+2, basename);

    const char *ext = Filename_ext(fn);
    if (!ext) Filename_setExt(fn, "prg");

    for (nextname[0] = '1';
            nextname[0] < (self->type == ZT_5PACK ? '6' : '5');
            ++nextname[0])
    {
        Filename_setBase(fn, nextname);
        FILE *f = fopen_internal(Filename_full(fn), "wb");
        if (!f)
        {
            free(nextname);
            logfmt(L_ERROR, "ZcFileSet_save: error opening `%s' for writing.",
                    Filename_full(fn));
            Filename_destroy(fn);
            return -1;
        }
        const FileData *fd = self->files[nextname[0]-'1'];
        if (!fwrite(FileData_rcontent(fd), FileData_size(fd), 1, f))
        {
            fclose(f);
            free(nextname);
            logfmt(L_ERROR, "ZcFileSet_save: error writing to `%s'.",
                    Filename_full(fn));
            Filename_destroy(fn);
            return -1;
        }
        logfmt(L_INFO, "ZcFileSet_save: saved `%s'.", Filename_full(fn));
        fclose(f);
    }
    free(nextname);
    Filename_destroy(fn);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %r14
callq 0x5680
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x5719
movq 0x8(%r14), %r12
movq %rbx, %rdi
callq 0x570f
testq %rax, %rax
je 0x600d
cmpb $0x0, (%rax)
je 0x600a
xorl %ecx, %ecx
cmpb $0x21, 0x1(%rax)
sete %cl
leaq (%rax,%rcx,2), %r12
jmp 0x600d
movq %rax, %r12
movq %r12, %rdi
callq 0x4250
leaq 0x3(%rax), %rdi
callq 0x485c
movq %rax, %r15
movb $0x21, 0x1(%rax)
leaq 0x2(%rax), %rdi
movq %r12, %rsi
callq 0x4100
movq %rbx, %rdi
callq 0x5714
testq %rax, %rax
jne 0x604d
leaq 0x7689(%rip), %rsi      # 0xd6ce
movq %rbx, %rdi
callq 0x5948
movb $0x31, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x591a
movq %rbx, %rdi
callq 0x5706
movq %rax, %rdi
leaq 0x7664(%rip), %rsi      # 0xd6d2
callq 0x4650
testq %rax, %rax
je 0x6147
movq %rax, %rbp
movsbq (%r15), %rax
movq -0x178(%r14,%rax,8), %r13
movq %r13, %rdi
callq 0x4640
movq %rax, %r12
movq %r13, %rdi
callq 0x4500
movl $0x1, %edx
movq %r12, %rdi
movq %rax, %rsi
movq %rbp, %rcx
callq 0x46e0
movq %rax, %r13
testq %rax, %rax
je 0x60e1
movq %rbx, %rdi
callq 0x5706
movl $0x3, %edi
leaq 0x765f(%rip), %rsi      # 0xd72c
movq %rax, %rdx
xorl %eax, %eax
callq 0x4949
movq %rbp, %rdi
callq 0x4220
jmp 0x6117
movq %rbp, %rdi
callq 0x4220
movq %r15, %rdi
callq 0x4080
movq %rbx, %rdi
callq 0x5706
movl $0x1, %edi
leaq 0x7600(%rip), %rsi      # 0xd705
movq %rax, %rdx
xorl %eax, %eax
callq 0x4949
movq %rbx, %rdi
callq 0x5976
testq %r13, %r13
je 0x617c
movb (%r15), %al
incb %al
movb %al, (%r15)
xorl %ecx, %ecx
cmpl $0x1, (%r14)
movsbl %al, %eax
sete %cl
addl $0x35, %ecx
cmpl %eax, %ecx
jg 0x6051
movq %r15, %rdi
callq 0x4080
xorl %ebp, %ebp
jmp 0x6172
movq %r15, %rdi
callq 0x4080
movq %rbx, %rdi
callq 0x5706
leaq 0x7577(%rip), %rsi      # 0xd6d5
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x4949
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x5976
jmp 0x6181
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq