// File: /workspace/github_c_makefile_star2_O3/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 $0xb8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rdi
callq 0x5500
movq %rax, %r15
movq %rbx, %rdi
callq 0x5640
movq %rax, %r12
leaq 0x58(%rsp), %rdi
leaq 0xf(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %rax, %rcx
movq %r15, %r8
callq 0xb561
testl %eax, %eax
js 0xbb5f
movq %r14, 0x50(%rsp)
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rbx
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
movq 0x58(%rsp), %rsi
movq %rsi, %rax
notq %rax
leaq 0x18(%rsp), %r13
addq (%r13), %rax
cmpq $0x4f, %rax
movl $0x4f, %edx
cmovbq %rax, %rdx
addq %r12, %rsi
movq %rbx, %rdi
callq 0x5470
leaq 0x3de9(%rip), %rsi      # 0xf55c
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5949
leaq 0x2e(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %r15, 0x10(%rsp)
movq %r15, %rcx
callq 0xbcda
testl %eax, %eax
js 0xbb68
movzwl 0x2e(%rsp), %r15d
leal (,%r15,8), %eax
movq %rax, 0x48(%rsp)
leaq (%rax,%rax,2), %r13
movq %r13, %rdi
callq 0x585c
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x5320
testl %r15d, %r15d
movq %r15, 0x20(%rsp)
je 0xbbca
movl %r15d, %eax
decl %eax
movq %rax, 0x40(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r13
xorl %r14d, %r14d
movq %r13, %rbp
cmpq %rsi, %r13
jae 0xb81d
cmpb $0xd, (%r12,%rbp)
je 0xb81d
incb %r14b
movzbl %r14b, %ebp
addq %r13, %rbp
cmpq %rsi, %rbp
jb 0xb807
incq %rbp
cmpq %rsi, %rbp
jae 0xbc83
addq %r12, %r13
movzbl %r14b, %eax
movl %eax, %r14d
decb %r14b
cmpb $-0x60, -0x1(%r13,%rax)
je 0xb82c
callq 0x5390
movq 0x30(%rsp), %rcx
leaq (%rcx,%rcx,2), %r15
movq %rax, (%rbx,%r15,8)
incb %r14b
movzbl %r14b, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x52f0
movq %rbp, 0x18(%rsp)
leaq (%rbx,%r15,8), %rbp
addq $0x8, %rbp
movq %rbp, %rdi
leaq 0x18(%rsp), %r13
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
callq 0xbcda
testl %eax, %eax
js 0xbc8c
movq 0x18(%rsp), %rax
movb (%r12,%rax), %cl
testb %cl, %cl
je 0xbb83
leaq (%rbx,%r15,8), %rdi
movq $-0x4, %r14
movq 0x10(%rsp), %rdx
leaq 0x3f39(%rip), %rsi      # 0xf7f0
cmpb %cl, 0x5(%r14,%rsi)
je 0xb8c8
incq %r14
jne 0xb8b7
jmp 0xbb83
leaq 0x1(%rax), %rcx
cmpq %rdx, %rcx
je 0xbc98
cmpb $0xd, (%r12,%rcx)
movq 0x20(%rsp), %r15
jne 0xbca4
addq $0x2, %rax
movq %rax, 0x18(%rsp)
cmpq %rdx, %rax
je 0xbc83
leaq 0x5(%r14), %rsi
movq %rdi, 0x38(%rsp)
movq (%rdi), %rdi
callq 0x5110
cmpq $-0x1, %r14
jne 0xb93d
leaq 0xc(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
callq 0xbcda
testl %eax, %eax
js 0xbcad
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movzbl 0xc(%rsp), %esi
callq 0x5580
leaq 0xc(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
callq 0xbcda
testl %eax, %eax
js 0xb974
movzwl (%rbp), %eax
leaq (%rax,%rax), %rcx
shll $0x8, %eax
subq %rcx, %rax
movzwl 0xc(%rsp), %ecx
addq %rcx, %rax
addq $-0xff, %rax
jmp 0xb997
movq 0x30(%rsp), %rax
cmpq 0x40(%rsp), %rax
jne 0xbcb6
movl $0x3, %edi
leaq 0x3cc7(%rip), %rsi      # 0xf657
callq 0x591c
xorl %eax, %eax
movq 0x38(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x30(%rsp), %rcx
incq %rcx
movq %rcx, 0x30(%rsp)
cmpq %r15, %rcx
movq 0x10(%rsp), %rsi
jne 0xb7f7
movq 0x18(%rsp), %r13
testq %r15, %r15
je 0xbbd4
movzbl 0xf(%rsp), %r14d
leaq (%r14,%r14), %rax
shll $0x8, %r14d
subq %rax, %r14
movq 0x40(%rsp), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x30(%rsp)
leal (,%r15,8), %eax
leaq (%rax,%rax,2), %rax
movq %rax, 0x38(%rsp)
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 %rsi, %r13
jae 0xbc83
testq %rbp, %rbp
jne 0xba53
cmpq %r14, %r13
je 0xba53
movl $0x2, %edi
leaq 0x3c94(%rip), %rsi      # 0xf6e2
callq 0x591c
movq (%rbx,%rbp), %rdi
callq 0x5780
cmpl $0x4, %eax
jne 0xbaa8
movzwl 0x8(%rbx,%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
movq 0x10(%rsp), %rsi
cmpq %rsi, %r13
jb 0xbaad
jmp 0xbc83
movq 0x10(%rsp), %rsi
movq 0x10(%rbx,%rbp), %rax
cmpq %rbp, 0x30(%rsp)
jne 0xbb16
testq %rax, %rax
jne 0xbb16
movq %rsi, %rax
subq %r13, %rax
movq %rax, 0x10(%rbx,%rbp)
movzwl 0x8(%rbx,%rbp), %ecx
leaq (%rcx,%rcx), %rdx
shll $0x8, %ecx
subq %rdx, %rcx
leal -0xfe(%rcx), %edx
cmpq %rdx, %rax
jb 0xbc83
cmpq %rcx, %rax
jbe 0xbb16
movl $0x2, %edi
leaq 0x3c29(%rip), %rsi      # 0xf721
callq 0x591c
movq 0x10(%rsp), %rsi
movzwl 0x8(%rbx,%rbp), %eax
leaq (%rax,%rax), %rcx
shll $0x8, %eax
subq %rcx, %rax
movq %rax, 0x10(%rbx,%rbp)
addq %r13, %rax
cmpq %rsi, %rax
ja 0xbc83
movq (%rbx,%rbp), %rdi
callq 0x5610
leaq (%r12,%r13), %rsi
movq 0x10(%rbx,%rbp), %rdx
movq %rax, %rdi
callq 0x5720
testl %eax, %eax
js 0xbcbf
addq 0x10(%rbx,%rbp), %r13
addq $0x18, %rbp
cmpq %rbp, 0x38(%rsp)
movq 0x10(%rsp), %rsi
jne 0xb9ff
jmp 0xbbd4
leaq 0x39d2(%rip), %rsi      # 0xf538
jmp 0xbb6f
leaq 0x3a10(%rip), %rsi      # 0xf57f
movl $0x1, %edi
callq 0x591c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xbc63
leaq 0x3a75(%rip), %rsi      # 0xf5ff
movl $0x1, %edi
callq 0x591c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0xbc5b
movq 0x48(%rsp), %rax
leaq (%rax,%rax,2), %r14
xorl %r15d, %r15d
movq (%rbx,%r15), %rdi
callq 0x50e0
addq $0x18, %r15
cmpq %r15, %r14
jne 0xbbb3
jmp 0xbc5b
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rsi
cmpq %rsi, %r13
jae 0xbc18
shrq %r13
movabsq $0x4081020408102041, %rcx # imm = 0x4081020408102041
movq %r13, %rax
mulq %rcx
shrq $0x5, %rdx
leaq (%rdx,%rdx), %rax
shlq $0x8, %rdx
subq %rax, %rdx
addq $0xfe, %rdx
cmpq %rsi, %rdx
je 0xbc18
leaq 0x3b81(%rip), %rsi      # 0xf78f
movl $0x2, %edi
callq 0x591c
testq %r15, %r15
je 0xbc59
leal (,%r15,8), %eax
leaq (%rax,%rax,2), %r14
xorl %r15d, %r15d
movq 0x50(%rsp), %r12
movq (%rbx,%r15), %rsi
movq %r12, %rdi
callq 0x54b0
testl %eax, %eax
js 0xbc77
movq $0x0, (%rbx,%r15)
addq $0x18, %r15
cmpq %r15, %r14
jne 0xbc31
xorl %ebp, %ebp
jmp 0xbb99
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x5080
movl %ebp, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b42(%rip), %rsi      # 0xf7c0
jmp 0xbb8a
leaq 0x3921(%rip), %rsi      # 0xf5ab
jmp 0xbcc6
leaq 0x393d(%rip), %rsi      # 0xf5d0
jmp 0xbb8a
leaq 0x390c(%rip), %rsi      # 0xf5ab
jmp 0xbb8a
leaq 0x3954(%rip), %rsi      # 0xf5ff
jmp 0xbcc6
leaq 0x3971(%rip), %rsi      # 0xf625
jmp 0xbcc6
leaq 0x39f0(%rip), %rsi      # 0xf6ad
jmp 0xbcc6
leaq 0x3aa8(%rip), %rsi      # 0xf76e
movl $0x1, %edi
callq 0x591c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xbb9e