// File: /workspace/github_c_makefile_star2_O3/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 0x6554
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x65ed
movq 0x8(%r14), %r12
movq %rbx, %rdi
callq 0x65e3
testq %rax, %rax
je 0x6ed9
cmpb $0x0, (%rax)
je 0x6ed6
xorl %ecx, %ecx
cmpb $0x21, 0x1(%rax)
sete %cl
leaq (%rax,%rcx,2), %r12
jmp 0x6ed9
movq %rax, %r12
movq %r12, %rdi
callq 0x5250
leaq 0x3(%rax), %rdi
callq 0x585c
movq %rax, %r15
movb $0x21, 0x1(%rax)
leaq 0x2(%rax), %rdi
movq %r12, %rsi
callq 0x5100
movq %rbx, %rdi
callq 0x65e8
testq %rax, %rax
jne 0x6f19
leaq 0x77bd(%rip), %rsi      # 0xe6ce
movq %rbx, %rdi
callq 0x6813
movb $0x31, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x67e5
movq %rbx, %rdi
callq 0x65da
movq %rax, %rdi
leaq 0x7798(%rip), %rsi      # 0xe6d2
callq 0x5650
testq %rax, %rax
je 0x6fd3
movq %rax, %rbp
movsbq (%r15), %rax
movq -0x178(%r14,%rax,8), %r13
movq %r13, %rdi
callq 0x5640
movq %rax, %r12
movq %r13, %rdi
callq 0x5500
movl $0x1, %edx
movq %r12, %rdi
movq %rax, %rsi
movq %rbp, %rcx
callq 0x56e0
testq %rax, %rax
je 0x6fec
movq %rbx, %rdi
callq 0x65da
movl $0x3, %edi
leaq 0x7796(%rip), %rsi      # 0xe72c
movq %rax, %rdx
xorl %eax, %eax
callq 0x5949
movq %rbp, %rdi
callq 0x5220
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 0x6f1d
movq %r15, %rdi
callq 0x5080
xorl %ebp, %ebp
jmp 0x701f
movq %r15, %rdi
callq 0x5080
movq %rbx, %rdi
callq 0x65da
leaq 0x76eb(%rip), %rsi      # 0xe6d5
jmp 0x700b
movq %rbp, %rdi
callq 0x5220
movq %r15, %rdi
callq 0x5080
movq %rbx, %rdi
callq 0x65da
leaq 0x76fa(%rip), %rsi      # 0xe705
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x5949
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x6841
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq