// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x6d0, %rsp            # imm = 0x6D0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
callq 0x5540
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x3f52(%rip), %rsi      # 0x14830
movl $0x7d, %edx
callq 0x5790
cmpl $0x0, %eax
jge 0x108f2
jmp 0x1110e
movq $0x7d, -0x28(%rbp)
leaq -0x2c(%rbp), %rdi
movl -0xc(%rbp), %edx
movl $0x4, %esi
callq 0x11130
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jge 0x10918
jmp 0x1110e
movq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movslq -0x30(%rbp), %rdx
callq 0x5790
cmpl $0x0, %eax
jge 0x10933
jmp 0x1110e
movslq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl $0x20, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10957
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0xd, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x1096f
jmp 0x1110e
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0xc(%rbp), %eax
jae 0x10cbc
movq -0x8(%rbp), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
leaq -0x40(%rbp), %rdi
movl $0xa0, %esi
movl $0x10, %edx
callq 0x5340
movq -0x50(%rbp), %rdi
leaq -0x51(%rbp), %rsi
callq 0x5380
movq %rax, -0x60(%rbp)
leaq -0x40(%rbp), %rdi
movq -0x60(%rbp), %rsi
movzbl -0x51(%rbp), %eax
movl %eax, %edx
callq 0x54c0
movq -0x18(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl $0x10, %edx
callq 0x5790
cmpl $0x0, %eax
jge 0x109f0
jmp 0x1110e
movq -0x28(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl $0xd, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10a14
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0x20, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10a2c
jmp 0x1110e
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x50(%rbp), %rdi
callq 0x56e0
movw %ax, -0x62(%rbp)
leaq -0x2c(%rbp), %rdi
movzwl -0x62(%rbp), %edx
movl $0x4, %esi
callq 0x11130
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jge 0x10a64
jmp 0x1110e
movq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movslq -0x30(%rbp), %rdx
callq 0x5790
cmpl $0x0, %eax
jge 0x10a7f
jmp 0x1110e
movslq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl $0x20, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10aa3
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0xd, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10abb
jmp 0x1110e
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movq -0x50(%rbp), %rdi
callq 0x57f0
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, %ecx
leaq 0x3d41(%rip), %rax      # 0x14820
movb (%rax,%rcx), %al
movb %al, -0x69(%rbp)
cmpb $0x0, -0x69(%rbp)
jne 0x10af0
jmp 0x1110e
movq -0x18(%rbp), %rdi
movzbl -0x69(%rbp), %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10b07
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0xd, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10b1f
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0x20, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10b37
jmp 0x1110e
movq -0x28(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rbp)
cmpl $0x4, -0x68(%rbp)
jne 0x10bfd
leaq -0x2c(%rbp), %rax
movq %rax, -0x6b8(%rbp)
movq -0x50(%rbp), %rdi
callq 0x54f0
movq -0x6b8(%rbp), %rdi
movzbl %al, %edx
movl $0x4, %esi
callq 0x11130
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jge 0x10b82
jmp 0x1110e
movq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movslq -0x30(%rbp), %rdx
callq 0x5790
cmpl $0x0, %eax
jge 0x10b9d
jmp 0x1110e
movslq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl $0x20, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10bc1
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0xd, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10bd9
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0x20, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10bf1
jmp 0x1110e
movq -0x28(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5190
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x5550
movzwl -0x62(%rbp), %ecx
subl $0x1, %ecx
imull $0xfe, %ecx, %ecx
movslq %ecx, %rcx
subq %rcx, %rax
addq $0x1, %rax
movl %eax, -0x7c(%rbp)
leaq -0x2c(%rbp), %rdi
movl -0x7c(%rbp), %edx
movl $0x4, %esi
callq 0x11130
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jge 0x10c4b
jmp 0x1110e
movq -0x18(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movslq -0x30(%rbp), %rdx
callq 0x5790
cmpl $0x0, %eax
jge 0x10c66
jmp 0x1110e
movslq -0x30(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movl $0x20, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10c8a
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0xd, %esi
callq 0x51f0
cmpl $0x0, %eax
jge 0x10ca2
jmp 0x1110e
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x28(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x10982
movq -0x28(%rbp), %rax
movl $0xfe, %ecx
xorl %edx, %edx
divq %rcx
movl $0xfe, %eax
subq %rdx, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movq -0x88(%rbp), %rdx
xorl %esi, %esi
callq 0x5040
cmpl $0x0, %eax
jge 0x10cf5
jmp 0x1110e
movq -0x88(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq -0x2c(%rbp), %rdi
movq -0x28(%rbp), %rax
movl $0xfe, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, %edx
movl $0x3, %esi
callq 0x11130
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jge 0x10d2f
jmp 0x1110e
movq -0x18(%rbp), %rdi
movl $0x60, %edx
movzbl -0x2c(%rbp), %esi
callq 0x5470
cmpl $0x0, %eax
jge 0x10d4b
jmp 0x1110e
cmpl $0x2, -0x30(%rbp)
jne 0x10d6d
movq -0x18(%rbp), %rdi
movl $0x61, %edx
movzbl -0x2b(%rbp), %esi
callq 0x5470
cmpl $0x0, %eax
jge 0x10d6d
jmp 0x1110e
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jae 0x11106
movq -0x8(%rbp), %rax
movl -0x8c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x57f0
cmpl $0x4, %eax
jne 0x1102f
leaq -0x690(%rbp), %rdi
xorl %esi, %esi
movl $0x5f4, %edx            # imm = 0x5F4
callq 0x5340
movq -0x98(%rbp), %rdi
callq 0x56e0
movw %ax, -0x692(%rbp)
movzwl -0x692(%rbp), %eax
cmpl $0x2d0, %eax            # imm = 0x2D0
jle 0x10de9
jmp 0x1110e
movzwl -0x692(%rbp), %eax
movl $0x78, %ecx
cltd
idivl %ecx
movl %eax, -0x6bc(%rbp)
movzwl -0x692(%rbp), %eax
movl $0x78, %ecx
cltd
idivl %ecx
movl -0x6bc(%rbp), %eax
cmpl $0x0, %edx
setne %cl
xorb $-0x1, %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
addl %ecx, %eax
movb %al, -0x693(%rbp)
movq -0x98(%rbp), %rdi
callq 0x54f0
movb %al, -0x694(%rbp)
movb $0x0, -0x695(%rbp)
movzbl -0x695(%rbp), %eax
movzbl -0x693(%rbp), %ecx
cmpl %ecx, %eax
jge 0x10fe9
movb -0x695(%rbp), %cl
movzbl -0x695(%rbp), %eax
imull $0xfe, %eax, %eax
cltq
movb %cl, -0x690(%rbp,%rax)
movb -0x694(%rbp), %cl
movzbl -0x695(%rbp), %eax
imull $0xfe, %eax, %eax
addl $0x1, %eax
cltq
movb %cl, -0x690(%rbp,%rax)
movb $0x0, -0x696(%rbp)
movzbl -0x696(%rbp), %eax
movzbl -0x693(%rbp), %ecx
cmpl %ecx, %eax
jge 0x10f1c
movzbl -0x695(%rbp), %eax
imull $0xfe, %eax, %eax
movzbl -0x696(%rbp), %ecx
shll %ecx
addl %ecx, %eax
addl $0x2, %eax
cltq
movb $0x13, -0x690(%rbp,%rax)
movzbl -0x696(%rbp), %eax
movl %eax, %ecx
leaq 0x39c8(%rip), %rax      # 0x148ad
movb (%rax,%rcx), %cl
movzbl -0x695(%rbp), %eax
imull $0xfe, %eax, %eax
movzbl -0x696(%rbp), %edx
shll %edx
addl %edx, %eax
addl $0x3, %eax
cltq
movb %cl, -0x690(%rbp,%rax)
movb -0x696(%rbp), %al
addb $0x1, %al
movb %al, -0x696(%rbp)
jmp 0x10e9e
movb $0x0, -0x697(%rbp)
movzbl -0x697(%rbp), %eax
movl %eax, -0x6c0(%rbp)
movzbl -0x695(%rbp), %eax
movzbl -0x693(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x10f52
movl $0x78, %edx
movl %edx, -0x6c4(%rbp)
jmp 0x10f67
movzwl -0x692(%rbp), %eax
movl $0x78, %ecx
cltd
idivl %ecx
movl %edx, -0x6c4(%rbp)
movl -0x6c0(%rbp), %eax
movl -0x6c4(%rbp), %ecx
cmpl %ecx, %eax
jge 0x10fd4
movzbl -0x695(%rbp), %eax
imull $0xfe, %eax, %eax
movzbl -0x697(%rbp), %ecx
shll %ecx
addl %ecx, %eax
addl $0xe, %eax
cltq
movb $0x1, -0x690(%rbp,%rax)
movzbl -0x695(%rbp), %eax
imull $0xfe, %eax, %eax
movzbl -0x697(%rbp), %ecx
shll %ecx
addl %ecx, %eax
addl $0xf, %eax
cltq
movb $0x1, -0x690(%rbp,%rax)
movb -0x697(%rbp), %al
addb $0x1, %al
movb %al, -0x697(%rbp)
jmp 0x10f23
jmp 0x10fd6
movb -0x695(%rbp), %al
addb $0x1, %al
movb %al, -0x695(%rbp)
jmp 0x10e46
movzbl -0x693(%rbp), %eax
imull $0xfe, %eax, %eax
cltq
movq %rax, -0x6a0(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x690(%rbp), %rsi
movq -0x6a0(%rbp), %rdx
callq 0x5790
cmpl $0x0, %eax
jge 0x11020
jmp 0x1110e
movq -0x6a0(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x98(%rbp), %rdi
callq 0x5190
movq %rax, -0x6a8(%rbp)
movq -0x6a8(%rbp), %rdi
callq 0x5550
movq %rax, -0x6b0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x6d0(%rbp)
movq -0x6a8(%rbp), %rdi
callq 0x56a0
movq -0x6d0(%rbp), %rdi
movq %rax, %rsi
movq -0x6b0(%rbp), %rdx
callq 0x5790
cmpl $0x0, %eax
jge 0x1108c
jmp 0x1110e
movq -0x6b0(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x8c(%rbp), %eax
movl -0xc(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x110f0
movq -0x28(%rbp), %rax
movl $0xfe, %ecx
xorl %edx, %edx
divq %rcx
movl $0xfe, %eax
subq %rdx, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rdi
movq -0x88(%rbp), %rdx
xorl %esi, %esi
callq 0x5040
cmpl $0x0, %eax
jge 0x110e1
jmp 0x1110e
movq -0x88(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x110f2
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x10d77
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x1111e
movq -0x18(%rbp), %rdi
callq 0x5410
movq -0x20(%rbp), %rax
addq $0x6d0, %rsp            # imm = 0x6D0
popq %rbp
retq
nopl (%rax,%rax)