// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, 0x20(%rsp)
cmpq $0x4, %rcx
ja 0x4e15
leaq 0x8354(%rip), %rsi      # 0xd164
jmp 0x51fe
movl %esi, %r8d
movzwl (%rdx), %eax
cmpl $0x400, %eax            # imm = 0x400
je 0x4e34
cmpl $0x3fe, %eax            # imm = 0x3FE
jne 0x51f7
movl $0x4, %eax
jmp 0x4e39
movl $0x2, %eax
xorl %ebp, %ebp
movq %rdx, 0x10(%rsp)
movl %r8d, 0x1c(%rsp)
movq %rcx, 0x8(%rsp)
cmpq %rcx, %rax
jae 0x520f
testl %r8d, %r8d
js 0x4e61
cmpl %r8d, %ebp
je 0x5220
leaq 0x1(%rax), %r15
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %rcx, %r15
je 0x4e7a
leaq 0x2(%rax), %r15
movzbl 0x1(%rdx,%rax), %r12d
testl %r12d, %r12d
js 0x4f63
movzbl (%rdx,%rax), %r14d
movl $0x1, %edi
callq 0x4a3b
movl %r14d, %ebx
andl $0x3f, %ebx
movzbl %r12b, %edx
movq 0x20(%rsp), %rdi
movl %ebx, %esi
callq 0x4030
movq %rax, %r13
xorl %edi, %edi
callq 0x4a3b
testq %r13, %r13
je 0x4f82
movq %r13, %rdi
callq 0x4690
movq %rax, %r13
shrl $0x6, %r14d
leaq 0x8283(%rip), %rcx      # 0xd154
movslq (%rcx,%r14,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, %edi
leaq 0x836d(%rip), %rsi      # 0xd253
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x4949
movb $0x1, %bl
xorl %r12d, %r12d
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpq %rcx, %r15
je 0x4f14
movzbl (%rdx,%r15), %r14d
incq %r15
testl %r14d, %r14d
js 0x4f20
movb %r14b, (%r13,%r12)
jmp 0x4f3b
movl $0x1, %edi
leaq 0x83e3(%rip), %rsi      # 0xd30f
callq 0x491c
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
testl %r14d, %r14d
js 0x4ff4
cmpq $0xff, %r12
leaq 0x1(%r12), %rax
setb %bl
movq %rax, %r12
cmpq $0x100, %rax            # imm = 0x100
jne 0x4f01
jmp 0x4ff4
movl $0x1, %edi
leaq 0x8274(%rip), %rsi      # 0xd1e3
callq 0x491c
movq 0x10(%rsp), %rdx
movb $0x1, %bl
movq 0x8(%rsp), %rcx
jmp 0x4ff4
movl $0x1, %edi
leaq 0x829a(%rip), %rsi      # 0xd228
jmp 0x4fdc
movl $0x4, %edi
leaq 0x8314(%rip), %rsi      # 0xd2b0
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x4949
movq 0x8(%rsp), %rcx
cmpq %rcx, %r15
jne 0x5062
movl $0x1, %edi
leaq 0x83b5(%rip), %rsi      # 0xd377
callq 0x491c
movq 0x8(%rsp), %rcx
movb $0x1, %bl
jmp 0x4fef
movl $0x1, %edi
leaq 0x8301(%rip), %rsi      # 0xd2dd
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x4949
movb $0x1, %bl
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
xorb $0x1, %bl
movzbl %bl, %esi
movl %esi, %eax
andl $0x1, %eax
addl %eax, %ebp
movq %r15, %rax
testb $0x1, %sil
movl 0x1c(%rsp), %r8d
jne 0x4e4a
jmp 0x520a
movl $0x4, %edi
leaq 0x825e(%rip), %rsi      # 0xd282
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x4949
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq 0x8(%rsp), %r15
je 0x5049
movq 0x10(%rsp), %rax
movzbl (%rax,%r15), %esi
incq %r15
testl %esi, %esi
js 0x51bc
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
callq 0x4320
xorl %ebx, %ebx
jmp 0x4fea
leaq 0x1(%r15), %r14
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rcx, %r14
movq 0x10(%rsp), %rdx
je 0x5082
movzbl 0x1(%rdx,%r15), %eax
addq $0x2, %r15
movq %r15, %r14
testl %eax, %eax
js 0x51d2
xorl %r12d, %r12d
movq %r14, %r15
movl %eax, %r14d
cmpl $0x100, %r12d           # imm = 0x100
setl %bl
jge 0x4ff4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rcx, %r15
je 0x50b5
movzbl (%rdx,%r15), %eax
incq %r15
testl %eax, %eax
js 0x514f
cmpl %r14d, %eax
jne 0x515d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rcx, %r15
je 0x50d8
movzbl (%rdx,%r15), %eax
incq %r15
testl %eax, %eax
js 0x516c
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmpq %rcx, %r15
je 0x50f3
movzbl (%rdx,%r15), %r10d
incq %r15
testl %r10d, %r10d
js 0x518b
movslq %r12d, %rdi
leaq (%rdi,%r13), %rcx
movl $0xff, %esi
subq %rdi, %rsi
movl %eax, %edi
xorl %r8d, %r8d
cmpl %r8d, %edi
je 0x51a5
movb %r10b, (%rcx,%r8)
leaq 0x1(%r8), %r9
cmpq %r8, %rsi
movq %r9, %r8
jne 0x5110
movq %r9, %r8
cmpl %eax, %r9d
jge 0x5110
movl $0x2, %edi
leaq 0x83ac(%rip), %rsi      # 0xd4e9
callq 0x491c
movq 0x10(%rsp), %rdx
movl $0x100, %r12d           # imm = 0x100
jmp 0x51a8
movl $0x1, %edi
leaq 0x82c1(%rip), %rsi      # 0xd41c
jmp 0x5178
movslq %r12d, %rsi
incl %r12d
movb %al, (%r13,%rsi)
movb $0x1, %al
jmp 0x51af
movl $0x1, %edi
leaq 0x82dd(%rip), %rsi      # 0xd455
callq 0x491c
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
jmp 0x51af
movl $0x1, %edi
leaq 0x8309(%rip), %rsi      # 0xd4a0
callq 0x491c
movq 0x10(%rsp), %rdx
xorl %eax, %eax
jmp 0x51aa
addl %r8d, %r12d
movb $0x1, %al
movq 0x8(%rsp), %rcx
testb %al, %al
jne 0x5093
jmp 0x4ff4
movl $0x1, %edi
leaq 0x817d(%rip), %rsi      # 0xd345
callq 0x491c
jmp 0x4fe8
movl $0x1, %edi
leaq 0x81e8(%rip), %rsi      # 0xd3c6
callq 0x491c
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movb $0x1, %bl
movq %r14, %r15
jmp 0x4ff4
leaq 0x7f87(%rip), %rsi      # 0xd185
movl $0x1, %edi
xorl %eax, %eax
callq 0x4949
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7f83(%rip), %rsi      # 0xd1aa
movl $0x3, %edi
movl %r8d, %edx
xorl %eax, %eax
callq 0x4949
jmp 0x520f