// File: /workspace/github_c_makefile_star2_O1/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, %r14
callq 0x44f0
movq %rax, %rbx
leaq 0x3561(%rip), %rsi      # 0xe800
movl $0x7d, %edx
movq %rax, %rdi
callq 0x4720
testl %eax, %eax
js 0xb98d
leaq 0x27f0(%rip), %rdx      # 0xdaab
xorl %ebp, %ebp
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x4290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovbl %eax, %ecx
testl %ecx, %ecx
js 0xb98f
movl %ecx, %r12d
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x4720
testl %eax, %eax
js 0xb98d
movq %rbx, %rdi
movl $0x20, %esi
callq 0x41d0
testl %eax, %eax
js 0xb98d
movq %rbx, %rdi
movl $0xd, %esi
callq 0x41d0
testl %eax, %eax
js 0xb98d
addq $0x7f, %r12
testl %r15d, %r15d
movq %r14, 0x8(%rsp)
je 0xb623
movq %r15, 0x18(%rsp)
movl %r15d, %eax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq (%r14,%rbp,8), %r13
movaps 0x2798(%rip), %xmm0     # 0xdaf0
movaps %xmm0, 0x50(%rsp)
movq %r13, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4360
movzbl 0x60(%rsp), %edx
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x4470
movl $0x10, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4720
movl $0x2, %r15d
testl %eax, %eax
js 0xb60b
leaq 0x10(%r12), %r14
movq %rbx, %rdi
movl $0xd, %esi
callq 0x41d0
testl %eax, %eax
js 0xb603
movq %rbx, %rdi
movl $0x20, %esi
callq 0x41d0
testl %eax, %eax
js 0xb603
movq %r13, %rdi
callq 0x4680
movzwl %ax, %r14d
movl $0x4, %esi
leaq 0x10(%rsp), %rdi
leaq 0x26c2(%rip), %rdx      # 0xdaab
movl %r14d, %ecx
xorl %eax, %eax
callq 0x4290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
jb 0xb400
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x12, %r12
testl %eax, %eax
js 0xb606
movl %r14d, 0x14(%rsp)
movl %eax, %r14d
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x4720
testl %eax, %eax
js 0xb606
addq %r14, %r12
movq %rbx, %rdi
movl $0x20, %esi
callq 0x41d0
testl %eax, %eax
js 0xb606
movq %rbx, %rdi
movl $0xd, %esi
callq 0x41d0
testl %eax, %eax
js 0xb606
leaq 0x2(%r12), %r14
movq %r13, %rdi
callq 0x4780
testl %eax, %eax
je 0xb603
movl %eax, 0x28(%rsp)
movl %eax, %eax
leaq 0x3375(%rip), %rcx      # 0xe7f0
movzbl (%rax,%rcx), %esi
movq %rbx, %rdi
callq 0x41d0
testl %eax, %eax
js 0xb603
movq %rbx, %rdi
movl $0xd, %esi
callq 0x41d0
testl %eax, %eax
js 0xb603
movq %rbx, %rdi
movl $0x20, %esi
callq 0x41d0
testl %eax, %eax
js 0xb603
addq $0x5, %r12
cmpl $0x4, 0x28(%rsp)
jne 0xb566
movq %r13, %rdi
callq 0x44a0
movzbl %al, %ecx
movl $0x4, %esi
leaq 0x10(%rsp), %rdi
leaq 0x25c7(%rip), %rdx      # 0xdaab
xorl %eax, %eax
callq 0x4290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
jb 0xb4f8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0xb606
movl %eax, %r14d
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x4720
testl %eax, %eax
js 0xb606
addq %r14, %r12
movq %rbx, %rdi
movl $0x20, %esi
callq 0x41d0
testl %eax, %eax
js 0xb606
movq %rbx, %rdi
movl $0xd, %esi
callq 0x41d0
testl %eax, %eax
movq 0x8(%rsp), %r14
js 0xb60b
movq %rbx, %rdi
movl $0x20, %esi
callq 0x41d0
testl %eax, %eax
js 0xb60b
addq $0x3, %r12
movl 0x14(%rsp), %r14d
movq %r13, %rdi
callq 0x4180
movq %rax, %rdi
callq 0x4500
imull $0xffffff02, %r14d, %ecx # imm = 0xFFFFFF02
addl %eax, %ecx
addl $0xff, %ecx
movl $0x4, %esi
leaq 0x10(%rsp), %rdi
leaq 0x2510(%rip), %rdx      # 0xdaab
xorl %eax, %eax
callq 0x4290
leal -0x1(%rax), %ecx
cmpl $0x3, %ecx
jb 0xb5af
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0xb606
movl %eax, %r14d
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x4720
testl %eax, %eax
js 0xb606
addq %r14, %r12
movq %rbx, %rdi
movl $0x20, %esi
callq 0x41d0
testl %eax, %eax
js 0xb606
movq %rbx, %rdi
movl $0xd, %esi
callq 0x41d0
leaq 0x2(%r12), %rcx
movl %eax, %r15d
shrl $0x1e, %r15d
andl $-0x2, %r15d
testl %eax, %eax
cmovnsq %rcx, %r12
jmp 0xb606
movq %r14, %r12
movq 0x8(%rsp), %r14
testl %r15d, %r15d
jne 0xb62a
incq %rbp
cmpq %rbp, 0x30(%rsp)
jne 0xb34d
movq 0x18(%rsp), %r15
movl $0x3, %eax
jmp 0xb632
movl %r15d, %eax
movq 0x18(%rsp), %r15
cmpl $0x2, %eax
je 0xb98d
cmpl $0x3, %eax
jne 0xb99c
movq %r12, %rax
shrq %rax
movabsq $0x4081020408102041, %r14 # imm = 0x4081020408102041
mulq %r14
movq %rdx, %r13
shrq $0x5, %r13
leaq (,%r13,2), %rax
shlq $0x8, %r13
subq %rax, %r13
subq %r12, %r13
addq $0xfe, %r13
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x4040
testl %eax, %eax
js 0xb98f
addq %r12, %r13
movq %r13, %rax
shrq %rax
mulq %r14
movq %rdx, %rcx
shrq $0x5, %rcx
leaq 0x2403(%rip), %rdx      # 0xdaab
xorl %ebp, %ebp
leaq 0x10(%rsp), %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0x4290
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmovbl %eax, %r14d
testl %r14d, %r14d
js 0xb98f
movq %r13, 0x18(%rsp)
movzbl 0x10(%rsp), %esi
movl $0x60, %edx
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
js 0xb98d
cmpl $0x2, %r14d
jne 0xb713
movzbl 0x11(%rsp), %esi
movl $0x61, %edx
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
js 0xb98d
testl %r15d, %r15d
je 0xb979
leal -0x1(%r15), %eax
movq %rax, 0x40(%rsp)
movl %r15d, %ecx
xorl %r15d, %r15d
leaq 0x314b(%rip), %rbp      # 0xe87d
movq 0x8(%rsp), %rax
movabsq $0x4081020408102041, %r12 # imm = 0x4081020408102041
movq %rcx, %r13
movq %rcx, 0x20(%rsp)
movq (%rax,%r15,8), %r14
movq %r14, %rdi
callq 0x4780
cmpl $0x4, %eax
jne 0xb8b5
movl $0x5f4, %edx            # imm = 0x5F4
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x4320
movq %r14, %rdi
callq 0x4680
movq %r14, %rdi
movzwl %ax, %r14d
movl $0x2, %ecx
cmpl $0x2d0, %r14d           # imm = 0x2D0
movq %rdi, 0x28(%rsp)
ja 0xb8a3
movq %r15, 0x48(%rsp)
imull $0x8889, %r14d, %eax    # imm = 0x8889
shrl $0x16, %eax
imull $0x78, %eax, %ecx
subl %ecx, %r14d
cmpw $0x1, %r14w
sbbw $-0x1, %ax
movzwl %ax, %r15d
callq 0x44a0
movb %al, 0x30(%rsp)
movq %r15, 0x38(%rsp)
testw %r15w, %r15w
je 0xb857
movzwl %r14w, %eax
movl %eax, 0x14(%rsp)
movq 0x38(%rsp), %rcx
leal -0x1(%rcx), %eax
movslq %eax, %r13
movl %ecx, %r12d
leaq 0x63(%rsp), %r15
xorl %r14d, %r14d
movq %r14, %rcx
shlq $0x8, %rcx
subq %r14, %rcx
subq %r14, %rcx
leaq 0x6e(%rsp), %rax
leaq (%rax,%rcx), %rdi
movb %r14b, 0x60(%rsp,%rcx)
movb 0x30(%rsp), %al
movb %al, 0x61(%rsp,%rcx)
xorl %eax, %eax
movb $0x13, -0x1(%r15,%rax,2)
movb (%rax,%rbp), %cl
movb %cl, (%r15,%rax,2)
incq %rax
cmpq %rax, %r12
jne 0xb812
cmpq %r13, %r14
movl 0x14(%rsp), %eax
movl %eax, %edx
movl $0x78, %eax
cmovll %eax, %edx
testl %edx, %edx
je 0xb848
addl %edx, %edx
movl $0x1, %esi
callq 0x4320
incq %r14
addq $0xfe, %r15
cmpq %r12, %r14
jne 0xb7ed
movq 0x38(%rsp), %r14
leal (%r14,%r14), %eax
shll $0x8, %r14d
subl %eax, %r14d
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
movq %r14, %rdx
callq 0x4720
movl %eax, %ecx
shrl $0x1e, %ecx
andl $-0x2, %ecx
testl %eax, %eax
movl $0x0, %eax
cmovnsq %r14, %rax
addq %rax, 0x18(%rsp)
movabsq $0x4081020408102041, %r12 # imm = 0x4081020408102041
movq 0x20(%rsp), %r13
movq 0x48(%rsp), %r15
testl %ecx, %ecx
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %r14
jne 0xb969
movq %r15, %r13
movq %r14, %rdi
callq 0x4180
movq %rax, %r14
movq %rax, %rdi
callq 0x4500
movq %rax, %r15
movq %r14, %rdi
callq 0x4640
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x4720
testl %eax, %eax
js 0xb94b
movq 0x18(%rsp), %rax
addq %r15, %rax
xorl %ecx, %ecx
movq %r13, %r15
cmpq 0x40(%rsp), %r13
jae 0xb95a
movq %rax, %r13
shrq %rax
mulq %r12
movq %rdx, %r14
shrq $0x5, %r14
leaq (%r14,%r14), %rax
shlq $0x8, %r14
subq %rax, %r14
subq %r13, %r14
addq $0xfe, %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x4040
movl %eax, %ecx
shrl $0x1e, %ecx
andl $-0x2, %ecx
testl %eax, %eax
movl $0x0, %eax
cmovnsq %r14, %rax
addq %rax, %r13
movq %r13, 0x18(%rsp)
jmp 0xb95f
movl $0x2, %ecx
movq 0x8(%rsp), %rax
movq %r13, %r15
jmp 0xb964
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %r13
testl %ecx, %ecx
jne 0xb97e
incq %r15
cmpq %r13, %r15
jne 0xb749
movl $0x6, %ecx
cmpl $0x2, %ecx
je 0xb98d
cmpl $0x6, %ecx
jne 0xb99c
movq %rbx, %rbp
jmp 0xb98f
xorl %ebp, %ebp
testq %rbp, %rbp
jne 0xb99c
movq %rbx, %rdi
callq 0x43d0
movq %rbp, %rax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq