// File: /workspace/github_c_makefile_star2_O2/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 0x64d4
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x655c
movq 0x8(%r14), %r12
movq %rbx, %rdi
callq 0x6552
testq %rax, %rax
je 0x6e0e
cmpb $0x0, (%rax)
je 0x6e0b
xorl %ecx, %ecx
cmpb $0x21, 0x1(%rax)
sete %cl
leaq (%rax,%rcx,2), %r12
jmp 0x6e0e
movq %rax, %r12
movq %r12, %rdi
callq 0x5260
leaq 0x3(%rax), %rdi
callq 0x589c
movq %rax, %r15
movb $0x21, 0x1(%rax)
leaq 0x2(%rax), %rdi
movq %r12, %rsi
callq 0x5100
movq %rbx, %rdi
callq 0x6557
testq %rax, %rax
jne 0x6e4e
leaq 0x6888(%rip), %rsi      # 0xd6ce
movq %rbx, %rdi
callq 0x677b
movb $0x31, %al
movb %al, (%r15)
xorl %ecx, %ecx
cmpl $0x1, (%r14)
movsbl %al, %eax
sete %cl
addl $0x35, %ecx
cmpl %eax, %ecx
jle 0x6ef7
movq %rbx, %rdi
movq %r15, %rsi
callq 0x674d
movq %rbx, %rdi
callq 0x6549
movq %rax, %rdi
leaq 0x684b(%rip), %rsi      # 0xd6d2
callq 0x5690
testq %rax, %rax
je 0x6f03
movq %rax, %rbp
movsbq (%r15), %rax
movq -0x178(%r14,%rax,8), %r13
movq %r13, %rdi
callq 0x5680
movq %rax, %r12
movq %r13, %rdi
callq 0x5530
movq %r12, %rdi
movq %rax, %rsi
pushq $0x1
popq %rdx
movq %rbp, %rcx
callq 0x5720
testq %rax, %rax
je 0x6f1c
movq %rbx, %rdi
callq 0x6549
pushq $0x3
popq %rdi
leaq 0x6851(%rip), %rsi      # 0xd72c
movq %rax, %rdx
xorl %eax, %eax
callq 0x598d
movq %rbp, %rdi
callq 0x5230
movb (%r15), %al
incb %al
jmp 0x6e50
movq %r15, %rdi
callq 0x5080
xorl %ebp, %ebp
jmp 0x6f4b
movq %r15, %rdi
callq 0x5080
movq %rbx, %rdi
callq 0x6549
leaq 0x67bb(%rip), %rsi      # 0xd6d5
jmp 0x6f3b
movq %rbp, %rdi
callq 0x5230
movq %r15, %rdi
callq 0x5080
movq %rbx, %rdi
callq 0x6549
leaq 0x67ca(%rip), %rsi      # 0xd705
pushq $0x1
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x598d
pushq $-0x1
popq %rbp
movq %rbx, %rdi
callq 0x67a9
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq