// File: /workspace/github_c_makefile_star2_O2/excess-c64[P]lib1541img/src/lib/1541img/zc45reader.c
// Function: zc45_read
SOEXPORT int zc45_read(
        D64 *d64, int sectors, const uint8_t *zcfile, size_t zcfilelen)
{
    if (zcfilelen < 5)
    {
        logfmt(L_ERROR, "zc45_read: input file too short.");
        return -1;
    }
    uint16_t loadaddr = zcfile[0] | (zcfile[1]<<8);
    size_t pos;
    switch (loadaddr)
    {
	case 0x400:
	    pos = 2;
	    break;
	case 0x3fe:
	    pos = 4;
	    break;
	default:
            logfmt(L_ERROR, "zc45_read: not a valid zipcode file.");
	    return -1;
    }
    int rsects = 0;
    while (pos < zcfilelen)
    {
        if (sectors >= 0 && rsects == sectors)
        {
            logfmt(L_INFO, "zc45_read: ignoring extra data after reading "
                    "%d sectors.", sectors);
            break;
        }
	int rc;
	if ((rc = decodesector(d64, zcfile, &pos, zcfilelen)) < 0)
	{
	    if (rc == -2) break;
	    return -1;
	}
	++rsects;
    }
    return rsects;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpq $0x4, %rcx
ja 0x5de8
leaq 0x7381(%rip), %rsi      # 0xd164
jmp 0x6064
movq %rcx, %r14
movq %rdx, %r15
movl %esi, %ebx
movzwl (%rdx), %eax
cmpl $0x400, %eax            # imm = 0x400
je 0x5e09
cmpl $0x3fe, %eax            # imm = 0x3FE
jne 0x605d
pushq $0x4
jmp 0x5e0b
pushq $0x2
popq %rsi
xorl %edx, %edx
movl %ebx, 0x1c(%rsp)
movq %r15, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
movq %r14, 0x10(%rsp)
cmpq %r14, %rsi
jae 0x6097
testl %ebx, %ebx
setns %al
cmpl %ebx, %edx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x609b
leaq 0x1(%rsi), %rax
cmpq %r14, %rax
je 0x60b0
movl %edx, 0x4(%rsp)
movq %r15, %rax
movzbl (%r15,%rsi), %r15d
movzbl 0x1(%rax,%rsi), %ebp
movq %rsi, %r14
movq %rdi, %r13
pushq $0x1
popq %rdi
callq 0x5a59
movl %r15d, %r12d
andl $0x3f, %r12d
movq %r13, %rdi
movl %r12d, %esi
movl %ebp, (%rsp)
movl %ebp, %edx
callq 0x5030
movq %rax, %rbp
xorl %edi, %edi
callq 0x5a59
testq %rbp, %rbp
je 0x60b9
leaq 0x2(%r14), %rbx
movq %rbp, %rdi
callq 0x56d0
movq %rax, %rbp
shrl $0x6, %r15d
leaq 0x72a5(%rip), %rcx      # 0xd154
movslq (%rcx,%r15,4), %rax
addq %rcx, %rax
movq %r14, %r13
jmpq *%rax
pushq $0x4
popq %rdi
leaq 0x738e(%rip), %rsi      # 0xd253
movl %r12d, %edx
movl (%rsp), %ecx
xorl %eax, %eax
callq 0x598d
xorl %eax, %eax
movq 0x8(%rsp), %r15
movl 0x4(%rsp), %edx
movq 0x10(%rsp), %r14
cmpq $0x100, %rax            # imm = 0x100
je 0x604a
cmpq %rbx, %r14
je 0x6073
movb (%r15,%rbx), %cl
incq %rbx
movb %cl, (%rbp,%rax)
incq %rax
jmp 0x5ee2
pushq $0x4
popq %rdi
leaq 0x739f(%rip), %rsi      # 0xd2b0
movl %r12d, %edx
movl (%rsp), %ecx
xorl %eax, %eax
callq 0x598d
movq 0x10(%rsp), %r14
cmpq %r14, %rbx
je 0x60db
leaq 0x3(%r13), %rax
cmpq %r14, %rax
je 0x60ed
movq 0x8(%rsp), %r15
movb 0x3(%r15,%r13), %r12b
addq $0x4, %r13
xorl %eax, %eax
movq %r13, %rbx
leaq 0x7596(%rip), %r13      # 0xd4e9
cmpl $0xff, %eax
jg 0x6046
cmpq %r14, %rbx
je 0x607c
leaq 0x1(%rbx), %rcx
movb (%r15,%rbx), %dl
movslq %eax, %r8
cmpb %r12b, %dl
jne 0x5fe0
cmpq %r14, %rcx
je 0x6085
leaq 0x2(%rbx), %rcx
cmpq %r14, %rcx
je 0x608e
movzbl 0x1(%r15,%rbx), %ecx
movb 0x2(%r15,%rbx), %dl
addq $0x3, %rbx
leaq (%r8,%rbp), %rsi
movl $0xff, %edi
subq %r8, %rdi
movl %ecx, %r8d
xorl %r9d, %r9d
cmpl %r9d, %r8d
je 0x5fef
movb %dl, (%rsi,%r9)
leaq 0x1(%r9), %r10
cmpq %r9, %rdi
movq %r10, %r9
jne 0x5fae
movq %r10, %r9
cmpl %ecx, %r10d
jae 0x5fae
pushq $0x2
popq %rdi
movq %r13, %rsi
callq 0x5960
movl $0x100, %eax            # imm = 0x100
jmp 0x5f53
incl %eax
movb %dl, (%rbp,%r8)
movq %rcx, %rbx
jmp 0x5f53
addl %r9d, %eax
jmp 0x5f53
pushq $0x4
popq %rdi
leaq 0x7281(%rip), %rsi      # 0xd282
movl %r12d, %edx
movl (%rsp), %ecx
xorl %eax, %eax
callq 0x598d
movq 0x10(%rsp), %r14
cmpq %r14, %rbx
je 0x60e4
movq 0x8(%rsp), %r15
movb (%r15,%rbx), %al
xorl %ecx, %ecx
movl 0x4(%rsp), %edx
cmpq $0x100, %rcx            # imm = 0x100
je 0x603d
movb %al, (%rbp,%rcx)
incq %rcx
jmp 0x602b
addq $0x3, %r13
movq %r13, %rbx
jmp 0x604a
movl 0x4(%rsp), %edx
incl %edx
movq %rbx, %rsi
movl 0x1c(%rsp), %ebx
movq 0x20(%rsp), %rdi
jmp 0x5e21
leaq 0x7121(%rip), %rsi      # 0xd185
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x598d
jmp 0x60fc
leaq 0x7295(%rip), %rsi      # 0xd30f
jmp 0x60f4
leaq 0x7399(%rip), %rsi      # 0xd41c
jmp 0x60f4
leaq 0x73c9(%rip), %rsi      # 0xd455
jmp 0x60f4
leaq 0x740b(%rip), %rsi      # 0xd4a0
jmp 0x60f4
movl %edx, %ebx
jmp 0x60ff
leaq 0x7108(%rip), %rsi      # 0xd1aa
pushq $0x3
popq %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x598d
jmp 0x60ff
leaq 0x712c(%rip), %rsi      # 0xd1e3
jmp 0x60f4
leaq 0x7168(%rip), %rsi      # 0xd228
jmp 0x60c9
leaq 0x7214(%rip), %rsi      # 0xd2dd
pushq $0x1
popq %rdi
movl %r12d, %edx
movl (%rsp), %ecx
xorl %eax, %eax
callq 0x598d
jmp 0x60fc
leaq 0x7295(%rip), %rsi      # 0xd377
jmp 0x60f4
leaq 0x725a(%rip), %rsi      # 0xd345
jmp 0x60f4
leaq 0x72d2(%rip), %rsi      # 0xd3c6
pushq $0x1
popq %rdi
callq 0x5960
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq