// File: /workspace/github_c_makefile_star2_O3/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 $0x658, %rsp            # imm = 0x658
movl %esi, %r15d
movq %rdi, %r13
callq 0x54f0
movq %rax, %rbx
leaq 0x3a10(%rip), %rsi      # 0xf800
movl $0x7d, %edx
movq %rax, %rdi
callq 0x5720
testl %eax, %eax
js 0xc433
leaq 0x2c9f(%rip), %rdx      # 0xeaab
leaq 0x8(%rsp), %rdi
movl $0x4, %esi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x5290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovbl %eax, %ecx
testl %ecx, %ecx
js 0xc433
movl %ecx, %ebp
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x5720
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0x20, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0xd, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
addq $0x7f, %rbp
testl %r15d, %r15d
je 0xc157
movl %r15d, %eax
movq %rax, 0x10(%rsp)
movaps 0x2c5a(%rip), %xmm0     # 0xeaf0
leaq 0x60(%rsp), %rsi
leaq 0x50(%rsp), %r14
xorl %eax, %eax
movq %r13, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq (%r13,%rax,8), %r13
movaps %xmm0, 0x50(%rsp)
movq %r13, %rdi
callq 0x5360
movzbl 0x60(%rsp), %edx
movq %r14, %rdi
movq %rax, %rsi
callq 0x5470
movl $0x10, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5720
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0xd, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0x20, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %r13, %rdi
callq 0x5680
movzwl %ax, %ecx
movl $0x4, %esi
leaq 0x8(%rsp), %rdi
leaq 0x2b7f(%rip), %rdx      # 0xeaab
movl %ecx, 0xc(%rsp)
xorl %eax, %eax
callq 0x5290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
jb 0xbf44
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0xc433
movl %eax, %r14d
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x5720
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0x20, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0xd, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %r13, %rdi
callq 0x5780
testl %eax, %eax
je 0xc433
movl %eax, %r12d
movl %eax, %eax
leaq 0x3843(%rip), %rcx      # 0xf7f0
movzbl (%rax,%rcx), %esi
movq %rbx, %rdi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0xd, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0x20, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
addq %r14, %rbp
addq $0x17, %rbp
cmpl $0x4, %r12d
jne 0xc095
movq %r13, %rdi
callq 0x54a0
movzbl %al, %ecx
movl $0x4, %esi
leaq 0x8(%rsp), %rdi
leaq 0x2a93(%rip), %rdx      # 0xeaab
xorl %eax, %eax
callq 0x5290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
jb 0xc02c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0xc433
movl %eax, %r14d
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x5720
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0x20, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0xd, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0x20, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
addq %r14, %rbp
addq $0x3, %rbp
movq %r13, %rdi
callq 0x5180
movq %rax, %rdi
callq 0x5500
imull $0xffffff02, 0xc(%rsp), %ecx # imm = 0xFFFFFF02
addl %eax, %ecx
addl $0xff, %ecx
movl $0x4, %esi
leaq 0x8(%rsp), %rdi
leaq 0x29e5(%rip), %rdx      # 0xeaab
xorl %eax, %eax
callq 0x5290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
jb 0xc0da
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0xc433
movl %eax, %r14d
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x5720
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0x20, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
movq %rbx, %rdi
movl $0xd, %esi
callq 0x51d0
testl %eax, %eax
js 0xc433
addq %r14, %rbp
addq $0x2, %rbp
movq 0x20(%rsp), %rax
incq %rax
cmpq %rax, 0x10(%rsp)
movq 0x18(%rsp), %r13
movaps 0x29a9(%rip), %xmm0     # 0xeaf0
leaq 0x60(%rsp), %rsi
leaq 0x50(%rsp), %r14
jne 0xbea7
movq %rbp, %rax
shrq %rax
movabsq $0x4081020408102041, %rcx # imm = 0x4081020408102041
mulq %rcx
movq %rdx, %r12
shrq $0x5, %r12
leaq (%r12,%r12), %rax
shlq $0x8, %r12
subq %rax, %r12
subq %rbp, %r12
addq $0xfe, %r12
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x5040
testl %eax, %eax
js 0xc433
addq %rbp, %r12
movq %r12, %rax
shrq %rax
movabsq $0x4081020408102041, %rcx # imm = 0x4081020408102041
mulq %rcx
movq %rdx, %rcx
shrq $0x5, %rcx
leaq 0x28ec(%rip), %rdx      # 0xeaab
leaq 0x8(%rsp), %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0x5290
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmovbl %eax, %ebp
testl %ebp, %ebp
js 0xc433
movzbl 0x8(%rsp), %esi
movl $0x60, %edx
movq %rbx, %rdi
callq 0x5420
testl %eax, %eax
js 0xc433
cmpl $0x2, %ebp
jne 0xc21f
movzbl 0x9(%rsp), %esi
movl $0x61, %edx
movq %rbx, %rdi
callq 0x5420
testl %eax, %eax
js 0xc433
testl %r15d, %r15d
je 0xc42e
leal -0x1(%r15), %ebp
movl %r15d, %eax
movq %rax, 0x48(%rsp)
xorl %eax, %eax
leaq 0x3640(%rip), %r15      # 0xf87d
movq %r13, 0x18(%rsp)
movq %rbp, 0x30(%rsp)
movq %rax, 0x10(%rsp)
movq (%r13,%rax,8), %r14
movq %r14, %rdi
callq 0x5780
cmpl $0x4, %eax
jne 0xc38e
movq %r12, 0x40(%rsp)
movl $0x5f4, %edx            # imm = 0x5F4
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x5320
movq %r14, %rdi
callq 0x5680
movzwl %ax, %ebp
cmpl $0x2d0, %ebp            # imm = 0x2D0
ja 0xc433
imull $0x8889, %ebp, %eax     # imm = 0x8889
shrl $0x16, %eax
imull $0x78, %eax, %ecx
subl %ecx, %ebp
cmpw $0x1, %bp
sbbw $-0x1, %ax
movzwl %ax, %r12d
movq %r14, 0x38(%rsp)
movq %r14, %rdi
callq 0x54a0
movb %al, 0x20(%rsp)
movq %r12, 0x28(%rsp)
testw %r12w, %r12w
je 0xc350
movzwl %bp, %eax
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rcx
leal -0x1(%rcx), %eax
movslq %eax, %r12
movl %ecx, %ebp
leaq 0x63(%rsp), %r14
xorl %r13d, %r13d
movq %r13, %rcx
shlq $0x8, %rcx
subq %r13, %rcx
subq %r13, %rcx
leaq 0x6e(%rsp), %rax
leaq (%rax,%rcx), %rdi
movb %r13b, 0x60(%rsp,%rcx)
movb 0x20(%rsp), %al
movb %al, 0x61(%rsp,%rcx)
xorl %eax, %eax
movb $0x13, -0x1(%r14,%rax,2)
movb (%rax,%r15), %cl
movb %cl, (%r14,%rax,2)
incq %rax
cmpq %rax, %rbp
jne 0xc30a
cmpq %r12, %r13
movl 0xc(%rsp), %eax
movl %eax, %edx
movl $0x78, %eax
cmovll %eax, %edx
testl %edx, %edx
je 0xc341
addl %edx, %edx
movl $0x1, %esi
callq 0x5320
incq %r13
addq $0xfe, %r14
cmpq %rbp, %r13
jne 0xc2e5
movq 0x28(%rsp), %r13
leal (,%r13,2), %eax
shll $0x8, %r13d
subl %eax, %r13d
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
movq %r13, %rdx
callq 0x5720
testl %eax, %eax
movq 0x38(%rsp), %r14
js 0xc433
movq 0x40(%rsp), %r12
addq %r13, %r12
movq 0x30(%rsp), %rbp
movq %r14, %rdi
callq 0x5180
movq %rax, %r13
movq %rax, %rdi
callq 0x5500
movq %rax, %r14
movq %r13, %rdi
callq 0x5640
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x5720
testl %eax, %eax
movq 0x18(%rsp), %r13
js 0xc433
addq %r12, %r14
movq 0x10(%rsp), %rax
cmpq %rbp, %rax
jae 0xc41d
movq %r14, %rax
shrq %rax
movabsq $0x4081020408102041, %rcx # imm = 0x4081020408102041
mulq %rcx
movq %rdx, %r12
shrq $0x5, %r12
leaq (%r12,%r12), %rax
shlq $0x8, %r12
subq %rax, %r12
subq %r14, %r12
leaq 0xfe(%r12), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5040
testl %eax, %eax
js 0xc433
addq %r12, %r14
addq $0xfe, %r14
movq 0x10(%rsp), %rax
incq %rax
movq %r14, %r12
cmpq 0x48(%rsp), %rax
jne 0xc247
testq %rbx, %rbx
jne 0xc43d
movq %rbx, %rdi
callq 0x53d0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq