// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
callq 0x70e0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7240
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x7200
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x8087
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x807d
movq -0x30(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x21, %eax
jne 0x807d
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
jmp 0x8085
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x8087
movq -0x28(%rbp), %rdi
callq 0x5270
movq %rax, %rdi
addq $0x3, %rdi
callq 0x58d0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movb $0x21, 0x1(%rax)
movq -0x38(%rbp), %rdi
addq $0x2, %rdi
movq -0x28(%rbp), %rsi
callq 0x5100
movq -0x20(%rbp), %rdi
callq 0x7220
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x80dd
movq -0x20(%rbp), %rdi
leaq 0xb613(%rip), %rsi      # 0x136eb
callq 0x75e0
movq -0x38(%rbp), %rax
movb $0x31, (%rax)
movq -0x38(%rbp), %rax
movsbl (%rax), %eax
movq -0x10(%rbp), %rcx
movl (%rcx), %esi
movl $0x35, %ecx
movl $0x36, %edx
cmpl $0x1, %esi
cmovel %edx, %ecx
cmpl %ecx, %eax
jge 0x823c
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x75a0
movq -0x20(%rbp), %rdi
callq 0x71c0
movq %rax, %rdi
leaq 0xb5c6(%rip), %rsi      # 0x136ef
callq 0x56b0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x8176
movq -0x38(%rbp), %rdi
callq 0x5070
movq -0x20(%rbp), %rdi
callq 0x71c0
movq %rax, %rdx
movl $0x1, %edi
leaq 0xb598(%rip), %rsi      # 0x136f2
movb $0x0, %al
callq 0x5ab0
movq -0x20(%rbp), %rdi
callq 0x7620
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8255
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movsbl (%rcx), %ecx
subl $0x31, %ecx
movslq %ecx, %rcx
movq 0x10(%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x56a0
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5550
movq -0x58(%rbp), %rdi
movq %rax, %rsi
movq -0x48(%rbp), %rcx
movl $0x1, %edx
callq 0x5750
cmpq $0x0, %rax
jne 0x8204
movq -0x48(%rbp), %rdi
callq 0x5240
movq -0x38(%rbp), %rdi
callq 0x5070
movq -0x20(%rbp), %rdi
callq 0x71c0
movq %rax, %rdx
movl $0x1, %edi
leaq 0xb537(%rip), %rsi      # 0x13722
movb $0x0, %al
callq 0x5ab0
movq -0x20(%rbp), %rdi
callq 0x7620
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x8255
movq -0x20(%rbp), %rdi
callq 0x71c0
movq %rax, %rdx
movl $0x3, %edi
leaq 0xb52d(%rip), %rsi      # 0x13749
movb $0x0, %al
callq 0x5ab0
movq -0x48(%rbp), %rdi
callq 0x5240
movq -0x38(%rbp), %rax
movb (%rax), %cl
addb $0x1, %cl
movb %cl, (%rax)
jmp 0x80e4
movq -0x38(%rbp), %rdi
callq 0x5070
movq -0x20(%rbp), %rdi
callq 0x7620
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop