// File: /workspace/github_c_makefile_star2_O2/excess-c64[P]lib1541img/src/lib/1541img/lynx.c
// Function: archiveLynxFiles
SOEXPORT FileData *archiveLynxFiles(
	const CbmdosFile **files, unsigned filecount)
{
    FileData *archive = FileData_create();
    FileData *result = 0;

    if (FileData_append(archive, lynxheader, sizeof lynxheader) < 0) goto done;
    size_t pos = sizeof lynxheader;
    uint8_t buf[4];
    int l;

    if ((l = formatPetsciiNum(buf, sizeof buf, filecount)) < 0) goto done;
    if (FileData_append(archive, buf, l) < 0) goto done;
    pos += l;
    if (FileData_appendByte(archive, 0x20) < 0) goto done;
    if (FileData_appendByte(archive, 0x0d) < 0) goto done;
    pos += 2;

    uint8_t namebuf[16];
    for (unsigned i = 0; i < filecount; ++i)
    {
	const CbmdosFile *file = files[i];
	memset(namebuf, 0xa0, sizeof namebuf);
	uint8_t namelen;
	const char *name = CbmdosFile_name(file, &namelen);
	memcpy(namebuf, name, namelen);
	if (FileData_append(archive, namebuf, sizeof namebuf) < 0) goto done;
	pos += sizeof namebuf;
	if (FileData_appendByte(archive, 0x0d) < 0) goto done;
	if (FileData_appendByte(archive, 0x20) < 0) goto done;
	pos += 2;
	uint16_t blocksize = CbmdosFile_realBlocks(file);
	if ((l = formatPetsciiNum(buf, sizeof buf, blocksize)) < 0) goto done;
	if (FileData_append(archive, buf, l) < 0) goto done;
	pos += l;
	if (FileData_appendByte(archive, 0x20) < 0) goto done;
	if (FileData_appendByte(archive, 0x0d) < 0) goto done;
	pos += 2;
	CbmdosFileType type = CbmdosFile_type(file);
	uint8_t lynxtype = lynxfiletype[type];
	if (!lynxtype) goto done;
	if (FileData_appendByte(archive, lynxtype) < 0) goto done;
	if (FileData_appendByte(archive, 0x0d) < 0) goto done;
	if (FileData_appendByte(archive, 0x20) < 0) goto done;
	pos += 3;
	if (type == CFT_REL)
	{
	    if ((l = formatPetsciiNum(buf, sizeof buf,
			    CbmdosFile_recordLength(file))) < 0) goto done;
	    if (FileData_append(archive, buf, l) < 0) goto done;
	    pos +=l;
	    if (FileData_appendByte(archive, 0x20) < 0) goto done;
	    if (FileData_appendByte(archive, 0x0d) < 0) goto done;
	    if (FileData_appendByte(archive, 0x20) < 0) goto done;
	    pos += 3;
	}
	const FileData *fileData = CbmdosFile_rdata(file);
	unsigned lsu = (unsigned)(
		FileData_size(fileData) - 254*(blocksize-1) + 1);
	if ((l = formatPetsciiNum(buf, sizeof buf, lsu)) < 0) goto done;
	if (FileData_append(archive, buf, l) < 0) goto done;
	pos += l;
	if (FileData_appendByte(archive, 0x20) < 0) goto done;
	if (FileData_appendByte(archive, 0x0d) < 0) goto done;
	pos += 2;
    }
    size_t pad = 254 - (pos%254);
    if (FileData_appendBytes(archive, 0, pad) < 0) goto done;
    pos += pad;
    if ((l = formatPetsciiNum(buf, 3, pos / 254)) < 0) goto done;
    if (FileData_setByte(archive, buf[0], 0x60) < 0) goto done;
    if (l == 2 && FileData_setByte(archive, buf[1], 0x61) < 0) goto done;
    for (unsigned i = 0; i < filecount; ++i)
    {
	const CbmdosFile *file = files[i];
	if (CbmdosFile_type(file) == CFT_REL)
	{
	    uint8_t sidesects[6 * 254] = {0};
	    uint16_t blocksize = CbmdosFile_realBlocks(file);
	    if (blocksize > 720) goto done;
	    uint8_t sidesectnum = (blocksize / 120) + !!(blocksize % 120);
	    uint8_t recordlength = CbmdosFile_recordLength(file);
	    for (uint8_t j = 0; j < sidesectnum; ++j)
	    {
		sidesects[254*j] = j;
		sidesects[254*j + 1] = recordlength;
		for (uint8_t k = 0; k < sidesectnum; ++k)
		{
		    sidesects[254*j + 2*k + 2] = 19;
		    sidesects[254*j + 2*k + 3] = lynxfakess[k];
		}
		for (uint8_t k = 0;
			k < (j < sidesectnum - 1 ? 120 : blocksize % 120); ++k)
		{
		    sidesects[254*j + 2*k + 14] = 1;
		    sidesects[254*j + 2*k + 15] = 1;
		}
	    }
	    size_t sslen = sidesectnum * 254;
	    if (FileData_append(archive, sidesects, sslen) < 0) goto done;
	    pos += sslen;
	}
	const FileData *fileData = CbmdosFile_rdata(file);
	size_t fileSize = FileData_size(fileData);
	if (FileData_append(archive,
		    FileData_rcontent(fileData), fileSize) < 0) goto done;
	pos += fileSize;
	if (i < filecount - 1)
	{
	    pad = 254 - (pos%254);
	    if (FileData_appendBytes(archive, 0, pad) < 0) goto done;
	    pos += pad;
	}
    }

    result = archive;
done:
    if (!result) FileData_destroy(archive);
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x648, %rsp            # imm = 0x648
movq %rsi, 0x18(%rsp)
movq %rdi, 0x38(%rsp)
callq 0x5520
movq %rax, %rbx
leaq 0x2fd0(%rip), %rsi      # 0xe800
pushq $0x7d
popq %rdx
movq %rax, %rdi
callq 0x5760
testl %eax, %eax
js 0xbd42
leaq 0xa(%rsp), %rdi
pushq $0x4
popq %rsi
movq 0x18(%rsp), %rdx
callq 0xbd61
testl %eax, %eax
js 0xbd42
movl %eax, %r12d
leaq 0xa(%rsp), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5760
testl %eax, %eax
js 0xbd42
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x51e0
testl %eax, %eax
js 0xbd42
pushq $0xd
popq %rsi
movq %rbx, %rdi
callq 0x51e0
testl %eax, %eax
js 0xbd42
addq $0x7f, %r12
movl 0x18(%rsp), %eax
xorl %ecx, %ecx
movq %rax, 0x20(%rsp)
cmpq %rcx, %rax
je 0xbb00
movq 0x38(%rsp), %rax
movq %rcx, 0x10(%rsp)
movq (%rax,%rcx,8), %rbp
movaps 0x2225(%rip), %xmm0     # 0xdaf0
movaps %xmm0, 0x40(%rsp)
movq %rbp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x5370
movzbl 0x50(%rsp), %edx
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x54a0
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x10
popq %rdx
callq 0x5760
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0xd
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbp, %rdi
callq 0x56c0
movzwl %ax, %r13d
leaq 0xa(%rsp), %rdi
pushq $0x4
popq %rsi
movl %r13d, %edx
callq 0xbd61
testl %eax, %eax
js 0xbd42
movl %eax, %r15d
movq %rbx, %rdi
leaq 0xa(%rsp), %rsi
movq %r15, %rdx
callq 0x5760
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0xd
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbp, %rdi
callq 0x57c0
testl %eax, %eax
je 0xbd42
movl %eax, %r14d
movl %eax, %eax
leaq 0x2e41(%rip), %rcx      # 0xe7f0
movzbl (%rax,%rcx), %esi
movq %rbx, %rdi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0xd
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
addq %r15, %r12
addq $0x17, %r12
cmpl $0x4, %r14d
jne 0xba71
movq %rbp, %rdi
callq 0x54d0
movzbl %al, %edx
leaq 0xa(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0xbd61
testl %eax, %eax
js 0xbd42
movl %eax, %r15d
movq %rbx, %rdi
leaq 0xa(%rsp), %rsi
movq %r15, %rdx
callq 0x5760
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0xd
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
addq %r15, %r12
addq $0x3, %r12
movq %rbp, %rdi
callq 0x5180
movq %rax, %rdi
callq 0x5530
imull $0xffffff02, %r13d, %ecx # imm = 0xFFFFFF02
leal (%rcx,%rax), %edx
addl $0xff, %edx
leaq 0xa(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0xbd61
testl %eax, %eax
js 0xbd42
movl %eax, %r14d
movq %rbx, %rdi
leaq 0xa(%rsp), %rsi
movq %r14, %rdx
callq 0x5760
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
movq %rbx, %rdi
pushq $0xd
popq %rsi
callq 0x51e0
testl %eax, %eax
js 0xbd42
addq %r14, %r12
addq $0x2, %r12
movq 0x10(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %rax
jmp 0xb8ad
movl $0xfe, %r14d
movq %r12, %rax
xorl %edx, %edx
divq %r14
movl $0xfe, %eax
subq %rdx, %rax
movq %rbx, %rdi
xorl %esi, %esi
movq %rax, %r15
movq %rax, %rdx
callq 0x5040
testl %eax, %eax
js 0xbd42
movq %r15, %rax
addq %r12, %rax
movq %rax, 0x10(%rsp)
xorl %edx, %edx
divq %r14
leaq 0xa(%rsp), %rdi
pushq $0x3
popq %rsi
movl %eax, %edx
callq 0xbd61
testl %eax, %eax
js 0xbd42
movl %eax, %ebp
movzbl 0xa(%rsp), %esi
pushq $0x60
popq %rdx
movq %rbx, %rdi
callq 0x5450
testl %eax, %eax
js 0xbd42
cmpl $0x2, %ebp
jne 0xbb8c
movzbl 0xb(%rsp), %esi
pushq $0x61
popq %rdx
movq %rbx, %rdi
callq 0x5450
testl %eax, %eax
js 0xbd42
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x18(%rsp)
xorl %ecx, %ecx
pushq $0x78
popq %r12
movl $0xfe, %r13d
leaq 0x2cd2(%rip), %rbp      # 0xe87d
movq 0x20(%rsp), %rax
cmpq %rax, %rcx
je 0xbd3d
movq 0x38(%rsp), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %r14
movq %r14, %rdi
callq 0x57c0
cmpl $0x4, %eax
jne 0xbcc5
movl $0x5f4, %edx            # imm = 0x5F4
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
callq 0x5330
movq %r14, 0x28(%rsp)
movq %r14, %rdi
callq 0x56c0
cmpw $0x2d0, %ax             # imm = 0x2D0
ja 0xbd42
xorl %edx, %edx
movw $0x78, %cx
divw %cx
movw %dx, 0xe(%rsp)
xorl %r15d, %r15d
cmpw $0x1, %dx
sbbw $-0x1, %ax
movzwl %ax, %r14d
movq 0x28(%rsp), %rdi
callq 0x54d0
leal -0x1(%r14), %ecx
movzbl 0xe(%rsp), %edx
leaq 0x5f(%rsp), %rsi
leaq 0x53(%rsp), %rdi
cmpb %r14b, %r15b
jae 0xbc9c
movzbl %r15b, %r8d
movl %r8d, %r9d
imulq $0xfe, %r9, %r9
movb %r15b, 0x50(%rsp,%r9)
movb %al, 0x51(%rsp,%r9)
xorl %r9d, %r9d
cmpq %r9, %r14
je 0xbc72
movb $0x13, -0x1(%rdi,%r9,2)
movb (%r9,%rbp), %r10b
movb %r10b, (%rdi,%r9,2)
incq %r9
jmp 0xbc5a
cmpl %r8d, %ecx
movl %edx, %r8d
cmovgl %r12d, %r8d
xorl %r9d, %r9d
cmpb %r9b, %r8b
jbe 0xbc91
movw $0x101, -0x1(%rsi,%r9,2) # imm = 0x101
incq %r9
jmp 0xbc7f
incb %r15b
addq %r13, %rdi
addq %r13, %rsi
jmp 0xbc3a
imull $0xfe, %r14d, %r14d
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
movq %r14, %rdx
callq 0x5760
testl %eax, %eax
js 0xbd42
addq %r14, 0x10(%rsp)
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x5180
movq %rax, %r14
movq %rax, %rdi
callq 0x5530
movq %rax, %r15
movq %r14, %rdi
callq 0x5680
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x5760
testl %eax, %eax
js 0xbd42
addq 0x10(%rsp), %r15
movq 0x30(%rsp), %rcx
cmpq 0x18(%rsp), %rcx
jae 0xbd30
movq %r15, %rax
xorl %edx, %edx
divq %r13
movl $0xfe, %r14d
subq %rdx, %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x5040
testl %eax, %eax
js 0xbd42
addq %r14, %r15
movq 0x30(%rsp), %rcx
incq %rcx
movq %r15, 0x10(%rsp)
jmp 0xbbab
testq %rbx, %rbx
jne 0xbd4c
movq %rbx, %rdi
callq 0x53f0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq