// File: /workspace/github_c_makefile_star2_O1/excess-c64[P]lib1541img/src/lib/1541img/lynx.c
// Function: extractLynx
SOEXPORT int extractLynx(CbmdosVfs *vfs, const FileData *file)
{
    size_t size = FileData_size(file);
    const uint8_t *content = FileData_rcontent(file);
    size_t sigpos;
    uint8_t dirblocks;
    size_t pos;
    if (findHeader(&sigpos, &dirblocks, &pos, content, size) < 0)
    {
	logmsg(L_ERROR, "extractLynx: not a valid LyNX file.");
	return -1;
    }
    char sig[80] = {0};
    size_t siglen = pos - sigpos - 1;
    if (siglen >= sizeof sig) siglen = sizeof sig - 1;
    memcpy(sig, content + sigpos, siglen);
    logfmt(L_INFO, "extractLynx: found signature `%s'.", sig);
    uint16_t numfiles;
    if (parsePetsciiNum(&numfiles, &pos, content, size) < 0)
    {
	logmsg(L_ERROR, "extractLynx: couldn't read number of files.");
	return -1;
    }
    LynxDirEntry *dir = xmalloc(numfiles * sizeof *dir);
    memset(dir, 0, numfiles * sizeof *dir);
    int rc = -1;

    for (int i = 0; i < numfiles; ++i)
    {
	uint8_t namelen = 0;
	while (pos + namelen < size
		&& content[pos + namelen] != 0x0d) ++namelen;
	if (pos + namelen + 1 >= size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	size_t tmppos = pos + namelen + 1;
	while (content[pos + namelen - 1] == 0xa0) --namelen;
	dir[i].file = CbmdosFile_create();
	CbmdosFile_setName(dir[i].file, (const char *)(content + pos), namelen);
	pos = tmppos;
	if (parsePetsciiNum(&dir[i].blocks, &pos, content, size) < 0)
	{
	    logmsg(L_ERROR, "extractLynx: error parsing block size of file.");
	    goto done;
	}
	if (!content[pos])
	{
	    logmsg(L_ERROR, "extractLynx: invalid file type found.");
	    goto done;
	}
	int t;
	for (t = 1; t < (int) sizeof lynxfiletype; ++t)
	{
	    if (lynxfiletype[t] == content[pos]) break;
	}
	if (t == sizeof lynxfiletype)
	{
	    logmsg(L_ERROR, "extractLynx: invalid file type found.");
	    goto done;
	}
	CbmdosFileType type = (CbmdosFileType) t;
	if (++pos == size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	if (content[pos] != 0x0d)
	{
	    logmsg(L_ERROR, "extractLynx: invalid file type found.");
	    goto done;
	}
	if (++pos == size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	CbmdosFile_setType(dir[i].file, type);
	if (type == CFT_REL)
	{
	    uint16_t recordlen;
	    if (parsePetsciiNum(&recordlen, &pos, content, size) < 0)
	    {
		logmsg(L_ERROR,
			"extractLynx: error parsing record length of file.");
		goto done;
	    }
	    CbmdosFile_setRecordLength(dir[i].file, recordlen);
	}
	uint16_t lsu;
	if (parsePetsciiNum(&lsu, &pos, content, size) < 0)
	{
	    if (i == numfiles - 1)
	    {
		logmsg(L_INFO, "extractLynx: last block usage of last file "
			"missing, assuming size from the container.");
		dir[i].size = 0;
	    }
	    else
	    {
		logmsg(L_ERROR, "extractLynx: error parsing last block usage "
			"of file.");
		goto done;
	    }
	}
	else
	{
	    dir[i].size = 254 * (dir[i].blocks - 1) + (lsu - 1);
	}
    }

    for (int i = 0; i < numfiles; ++i)
    {
	size_t pad = 254 - (pos%254);
	pos += pad;
	if (pos >= size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	if (i == 0 && pos != dirblocks * 254)
	{
	    logmsg(L_WARNING, "extractLynx: inconsistent file, directory "
		    "block size is wrong.");
	}
	if (CbmdosFile_type(dir[i].file) == CFT_REL)
	{
	    uint8_t sidesectnum = (dir[i].blocks / 120)
		+ !!(dir[i].blocks % 120);
	    pos += 254 * sidesectnum;
	    if (pos >= size)
	    {
		logmsg(L_ERROR, "extractLynx: unexpected end of file.");
		goto done;
	    }
	}
	if (i == numfiles - 1 && !dir[i].size)
	{
	    dir[i].size = size - pos;
	    if (dir[i].size < (dir[i].blocks-1U) * 254)
	    {
		logmsg(L_ERROR, "extractLynx: unexpected end of file.");
		goto done;
	    }
	    if (dir[i].size > dir[i].blocks * 254)
	    {
		logmsg(L_WARNING, "extractLynx: unexpected garbage after end "
			"of file, archive might be corrupt.");
		dir[i].size = dir[i].blocks * 254;
	    }
	}
	if (pos + dir[i].size > size)
	{
	    logmsg(L_ERROR, "extractLynx: unexpected end of file.");
	    goto done;
	}
	if (FileData_append(CbmdosFile_data(dir[i].file),
		    content + pos, dir[i].size) < 0)
	{
	    logmsg(L_ERROR, "extractLynx: error writing file.");
	    goto done;
	}
	pos += dir[i].size;
    }
    if (pos < size)
    {
	size_t pad = 254 - (pos%254);
	if (pos + pad != size)
	{
	    logmsg(L_WARNING,
		    "extractLynx: file doesn't have the expected size");
	}
    }

    for (int i = 0; i < numfiles; ++i)
    {
	if (CbmdosVfs_append(vfs, dir[i].file) < 0)
	{
	    logmsg(L_ERROR, "extractLynx: error adding file to filesystem.");
	    goto done;
	}
	else dir[i].file = 0;
    }

    rc = 0;

done:
    for (int i = 0; i < numfiles; ++i) CbmdosFile_destroy(dir[i].file);
    free(dir);
    return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movq %rsi, %rdi
callq 0x4500
movq %rax, %r15
movq %rbx, %rdi
callq 0x4640
movq %rax, %r13
leaq 0x48(%rsp), %rdi
leaq 0xf(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0xa935
testl %eax, %eax
js 0xae2c
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rbx
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movq 0x48(%rsp), %rsi
movq %rsi, %rax
notq %rax
leaq 0x10(%rsp), %r14
addq (%r14), %rax
cmpq $0x4f, %rax
movl $0x4f, %edx
cmovbq %rax, %rdx
addq %r13, %rsi
movq %rbx, %rdi
callq 0x4470
leaq 0x39ff(%rip), %rsi      # 0xe55c
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4949
leaq 0xc(%rsp), %rdi
movq %r14, %rsi
movq %r13, 0x18(%rsp)
movq %r13, %rdx
movq %r15, %rcx
callq 0xb18e
testl %eax, %eax
js 0xb047
movq %r12, 0x38(%rsp)
movzwl 0xc(%rsp), %r14d
leal (,%r14,8), %eax
leaq (%rax,%rax,2), %rbx
movq %rbx, %rdi
callq 0x485c
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x4320
testq %r14, %r14
movq %r12, 0x20(%rsp)
je 0xae38
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r14, 0x30(%rsp)
addl %r14d, %eax
movq %rax, 0x28(%rsp)
xorl %ebp, %ebp
movq %r15, 0x40(%rsp)
movq 0x10(%rsp), %r14
xorl %r13d, %r13d
movq %r14, %rbx
cmpq %r15, %r14
jae 0xac0e
movq 0x18(%rsp), %rax
cmpb $0xd, (%rax,%rbx)
je 0xac13
incb %r13b
movzbl %r13b, %ebx
addq %r14, %rbx
cmpq %r15, %rbx
jb 0xabf7
jmp 0xac13
movq 0x18(%rsp), %rax
incq %rbx
cmpq %r15, %rbx
jae 0xad0d
addq %rax, %r14
movzbl %r13b, %eax
movl %eax, %r13d
decb %r13b
cmpb $-0x60, -0x1(%r14,%rax)
je 0xac22
callq 0x4390
leaq (,%rbp,2), %r12
addq %rbp, %r12
movq 0x20(%rsp), %r15
movq %rax, (%r15,%r12,8)
incb %r13b
movzbl %r13b, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x42f0
movq %rbx, 0x10(%rsp)
leaq (%r15,%r12,8), %rdi
addq $0x8, %rdi
leaq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdx
movq 0x40(%rsp), %r15
movq %r15, %rcx
callq 0xb18e
testl %eax, %eax
js 0xacd4
movq 0x10(%rsp), %rax
movb (%rbx,%rax), %cl
testb %cl, %cl
leaq 0x3b55(%rip), %rsi      # 0xe7f0
je 0xace2
movq 0x20(%rsp), %rdx
leaq (%rdx,%r12,8), %r14
movq %rdx, %r12
movl $0x1, %ebx
cmpb %cl, (%rbx,%rsi)
je 0xacc1
incq %rbx
cmpq $0x5, %rbx
jne 0xacae
movl $0x5, %ebx
cmpl $0x5, %ebx
jne 0xacff
movl $0x1, %edi
leaq 0x392d(%rip), %rsi      # 0xe5ff
jmp 0xad19
movl $0x1, %edi
leaq 0x38f0(%rip), %rsi      # 0xe5d0
jmp 0xacee
movl $0x1, %edi
leaq 0x3911(%rip), %rsi      # 0xe5ff
callq 0x491c
movl $0x7, %eax
movq 0x20(%rsp), %r12
jmp 0xad23
leaq 0x1(%rax), %rcx
movq %rcx, 0x10(%rsp)
cmpq %r15, %rcx
jne 0xad3e
movl $0x1, %edi
leaq 0x3892(%rip), %rsi      # 0xe5ab
callq 0x491c
movl $0x7, %eax
testl %eax, %eax
jne 0xae3d
incq %rbp
cmpq 0x30(%rsp), %rbp
jne 0xabe2
jmp 0xae38
movq 0x18(%rsp), %rdx
cmpb $0xd, (%rdx,%rcx)
jne 0xacc6
addq $0x2, %rax
movq %rax, 0x10(%rsp)
cmpq %r15, %rax
je 0xad0d
movq (%r14), %rdi
movl %ebx, %esi
callq 0x4110
cmpl $0x4, %ebx
jne 0xadb6
leaq 0xa(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rcx
callq 0xb18e
movl %eax, %ebx
testl %eax, %eax
js 0xad98
movq (%r14), %rdi
movzbl 0xa(%rsp), %esi
callq 0x4580
xorl %eax, %eax
jmp 0xadae
movl $0x1, %edi
leaq 0x3881(%rip), %rsi      # 0xe625
callq 0x491c
movl $0x7, %eax
testl %ebx, %ebx
js 0xad23
leaq 0xa(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r15, %rcx
callq 0xb18e
testl %eax, %eax
js 0xadf4
movzwl 0x8(%r14), %eax
leaq (%rax,%rax), %rcx
shll $0x8, %eax
subq %rcx, %rax
movzwl 0xa(%rsp), %ecx
addq %rcx, %rax
addq $-0xff, %rax
movq %rax, 0x10(%r14)
jmp 0xae14
cmpq 0x28(%rsp), %rbp
jne 0xae1b
movl $0x3, %edi
leaq 0x3850(%rip), %rsi      # 0xe657
callq 0x491c
movq $0x0, 0x10(%r14)
xorl %eax, %eax
jmp 0xad23
movl $0x1, %edi
leaq 0x3886(%rip), %rsi      # 0xe6ad
jmp 0xad19
leaq 0x3705(%rip), %rsi      # 0xe538
jmp 0xb04e
movl $0x2, %eax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x7, %eax
je 0xb14c
cmpl $0x2, %eax
jne 0xb17a
movzwl 0xc(%rsp), %edx
movq 0x10(%rsp), %r13
movl $0xd, %eax
testq %rdx, %rdx
je 0xb078
movzbl 0xf(%rsp), %r14d
leaq (%r14,%r14), %rax
shll $0x8, %r14d
subq %rax, %r14
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addl %edx, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
leal (,%rdx,8), %eax
leaq (%rax,%rax,2), %rbx
xorl %ebp, %ebp
shrq %r13
movq %r13, %rax
movabsq $0x4081020408102041, %rcx # imm = 0x4081020408102041
mulq %rcx
movq %rdx, %r13
shrq $0x5, %r13
leaq (,%r13,2), %rax
shlq $0x8, %r13
subq %rax, %r13
addq $0xfe, %r13
cmpq %r15, %r13
jae 0xaf7c
testq %rbp, %rbp
jne 0xaef7
cmpq %r14, %r13
je 0xaef7
movl $0x2, %edi
leaq 0x37f0(%rip), %rsi      # 0xe6e2
callq 0x491c
movq (%r12,%rbp), %rdi
callq 0x4780
cmpl $0x4, %eax
jne 0xaf60
movzwl 0x8(%r12,%rbp), %eax
imull $0x8889, %eax, %ecx     # imm = 0x8889
shrl $0x16, %ecx
imull $0xffffeeef, %eax, %eax # imm = 0xFFFFEEEF
rorw $0x3, %ax
movzwl %ax, %eax
xorl %edx, %edx
cmpl $0x223, %eax            # imm = 0x223
setae %dl
addl %ecx, %edx
movzbl %dl, %eax
leal (%rax,%rax), %ecx
shll $0x8, %eax
subl %ecx, %eax
movzwl %ax, %eax
addq %rax, %r13
xorl %eax, %eax
cmpq %r15, %r13
jb 0xaf5b
movl $0x1, %edi
leaq 0x365a(%rip), %rsi      # 0xe5ab
callq 0x491c
movl $0x7, %eax
cmpq %r15, %r13
jae 0xaf92
cmpq %rbp, 0x30(%rsp)
jne 0xaf6f
cmpq $0x0, 0x10(%r12,%rbp)
je 0xafd8
movq 0x10(%r12,%rbp), %rax
addq %r13, %rax
cmpq %r15, %rax
jbe 0xafac
movl $0x1, %edi
leaq 0x3623(%rip), %rsi      # 0xe5ab
callq 0x491c
movl $0x7, %eax
testl %eax, %eax
jne 0xb062
addq $0x18, %rbp
cmpq %rbp, %rbx
jne 0xaea3
jmp 0xb069
movq (%r12,%rbp), %rdi
callq 0x4610
movq 0x18(%rsp), %rcx
leaq (%rcx,%r13), %rsi
movq 0x10(%r12,%rbp), %rdx
movq %rax, %rdi
callq 0x4720
testl %eax, %eax
js 0xb036
addq 0x10(%r12,%rbp), %r13
xorl %eax, %eax
jmp 0xaf92
movq %r15, %rax
subq %r13, %rax
movq %rax, 0x10(%r12,%rbp)
movzwl 0x8(%r12,%rbp), %ecx
leaq (%rcx,%rcx), %rdx
shll $0x8, %ecx
subq %rdx, %rcx
leal -0xfe(%rcx), %edx
cmpq %rdx, %rax
jb 0xaf7c
cmpq %rcx, %rax
jbe 0xaf6f
movl $0x2, %edi
leaq 0x370a(%rip), %rsi      # 0xe721
callq 0x491c
movzwl 0x8(%r12,%rbp), %eax
leaq (%rax,%rax), %rcx
shll $0x8, %eax
subq %rcx, %rax
movq %rax, 0x10(%r12,%rbp)
jmp 0xaf6f
movl $0x1, %edi
leaq 0x372c(%rip), %rsi      # 0xe76e
jmp 0xaf88
leaq 0x3531(%rip), %rsi      # 0xe57f
movl $0x1, %edi
callq 0x491c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xb17a
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xb073
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xd, %eax
movq 0x28(%rsp), %rdx
movq %r13, 0x10(%rsp)
cmpl $0x7, %eax
je 0xb14c
cmpl $0xd, %eax
jne 0xb17a
movq 0x10(%rsp), %rax
cmpq %r15, %rax
movq 0x38(%rsp), %r12
jae 0xb0e3
shrq %rax
movabsq $0x4081020408102041, %rcx # imm = 0x4081020408102041
movq %rdx, %rbx
mulq %rcx
shrq $0x5, %rdx
leaq (%rdx,%rdx), %rax
shlq $0x8, %rdx
subq %rax, %rdx
addq $0xfe, %rdx
cmpq %r15, %rdx
movq %rbx, %rdx
je 0xb0e3
leaq 0x36b9(%rip), %rsi      # 0xe78f
movl $0x2, %edi
callq 0x491c
movq %rbx, %rdx
movl $0x10, %ebx
testq %rdx, %rdx
je 0xb13b
leal (,%rdx,8), %eax
leaq (%rax,%rax,2), %r14
xorl %r15d, %r15d
movq 0x20(%rsp), %r13
movq (%r13,%r15), %rsi
movq %r12, %rdi
callq 0x44b0
testl %eax, %eax
js 0xb125
movq $0x0, (%r13,%r15)
addq $0x18, %r15
cmpq %r15, %r14
jne 0xb100
jmp 0xb13b
leaq 0x3694(%rip), %rsi      # 0xe7c0
movl $0x1, %edi
callq 0x491c
movl $0x7, %ebx
cmpl $0x7, %ebx
movq 0x20(%rsp), %r12
je 0xb14c
cmpl $0x10, %ebx
jne 0xb17a
xorl %ebp, %ebp
movzwl 0xc(%rsp), %eax
testq %rax, %rax
je 0xb172
shll $0x3, %eax
leaq (%rax,%rax,2), %rbx
xorl %r14d, %r14d
movq (%r12,%r14), %rdi
callq 0x40e0
addq $0x18, %r14
cmpq %r14, %rbx
jne 0xb160
movq %r12, %rdi
callq 0x4080
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq