// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rcx, 0x10(%rsp)
cmpq $0x4, %rcx
ja 0x5e09
leaq 0x8360(%rip), %rsi      # 0xe164
jmp 0x609c
movq %rdx, %r12
movl %esi, %ebx
movq %rdi, %r13
movzwl (%rdx), %eax
movl $0x2, %ecx
movq %rcx, 0x28(%rsp)
movl $0x2, %r14d
cmpl $0x400, %eax            # imm = 0x400
je 0x5e3c
cmpl $0x3fe, %eax            # imm = 0x3FE
jne 0x6095
movl $0x4, %r14d
movq 0x10(%rsp), %rax
subq %rax, 0x28(%rsp)
xorl %eax, %eax
movl %ebx, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %r13, 0x30(%rsp)
testl %ebx, %ebx
setns %dl
cmpl %ebx, %eax
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
je 0x60cf
leaq 0x1(%r14), %rcx
cmpq 0x10(%rsp), %rcx
je 0x60e8
movl %eax, 0x24(%rsp)
movzbl (%r12,%r14), %r15d
movzbl 0x1(%r12,%r14), %ebp
movl $0x1, %edi
callq 0x5a3b
movl %r15d, %ebx
andl $0x3f, %ebx
movq %r13, %rdi
movl %ebx, %esi
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
callq 0x5030
movq %rax, %r13
xorl %edi, %edi
callq 0x5a3b
testq %r13, %r13
je 0x60f1
movq 0x10(%rsp), %rbp
leaq 0x2(%r14), %r12
movq %r13, %rdi
callq 0x5690
movq %rax, %r13
shrl $0x6, %r15d
leaq 0x8279(%rip), %rcx      # 0xe154
movslq (%rcx,%r15,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, %edi
leaq 0x8363(%rip), %rsi      # 0xe253
movl %ebx, %edx
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x5949
cmpq %rbp, %r12
movq 0x18(%rsp), %r12
movq 0x28(%rsp), %rdx
je 0x60aa
addq $0x3, %r14
movq $-0xff, %rax
movb -0x1(%r12,%r14), %cl
movb %cl, 0xff(%r13,%rax)
testq %rax, %rax
je 0x6079
leaq (%rdx,%r14), %rcx
incq %rcx
incq %r14
incq %rax
cmpq $0x3, %rcx
jne 0x5f1b
jmp 0x60aa
movl $0x4, %edi
leaq 0x835b(%rip), %rsi      # 0xe2b0
movl %ebx, %edx
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x5949
cmpq %rbp, %r12
je 0x613c
leaq 0x3(%r14), %rax
cmpq 0x10(%rsp), %rax
je 0x6148
movq 0x18(%rsp), %r12
movb 0x3(%r12,%r14), %al
addq $0x4, %r14
xorl %ecx, %ecx
movq %r14, %rbx
cmpq 0x10(%rsp), %rbx
je 0x60fa
leaq 0x1(%rbx), %r14
movb (%r12,%rbx), %dl
cmpb %al, %dl
jne 0x6002
cmpq 0x10(%rsp), %r14
je 0x6103
leaq 0x2(%rbx), %rdx
cmpq 0x10(%rsp), %rdx
je 0x610c
movzbl 0x1(%r12,%rbx), %edx
movb 0x2(%r12,%rbx), %sil
addq $0x3, %rbx
movslq %ecx, %r9
leaq (%r9,%r13), %rdi
movl $0xff, %r8d
subq %r9, %r8
movl %edx, %r9d
xorl %r10d, %r10d
cmpl %r10d, %r9d
je 0x6011
movb %sil, (%rdi,%r10)
leaq 0x1(%r10), %r11
cmpq %r10, %r8
movq %r11, %r10
jne 0x5fe3
movq %r11, %r10
cmpl %edx, %r11d
jae 0x5fe3
jmp 0x6065
movslq %ecx, %rsi
incl %ecx
movb %dl, (%r13,%rsi)
movq %r14, %rbx
jmp 0x6017
addl %r10d, %ecx
movq %rbx, %r14
cmpl $0x100, %ecx            # imm = 0x100
jl 0x5f8d
jmp 0x6079
movl $0x4, %edi
leaq 0x8251(%rip), %rsi      # 0xe282
movl %ebx, %edx
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x5949
cmpq %rbp, %r12
je 0x6130
movq 0x18(%rsp), %r12
movzbl 0x2(%r12,%r14), %esi
addq $0x3, %r14
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
callq 0x5320
jmp 0x6079
movl $0x2, %edi
leaq 0x8478(%rip), %rsi      # 0xe4e9
callq 0x591c
movq %rbx, %r14
movl 0x24(%rsp), %eax
incl %eax
cmpq 0x10(%rsp), %r14
movl 0x20(%rsp), %ebx
movq 0x30(%rsp), %r13
jb 0x5e56
jmp 0x60c0
leaq 0x80e9(%rip), %rsi      # 0xe185
movl $0x1, %edi
xorl %eax, %eax
callq 0x5949
jmp 0x60bb
leaq 0x825e(%rip), %rsi      # 0xe30f
movl $0x1, %edi
callq 0x591c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x80d4(%rip), %rsi      # 0xe1aa
movl $0x3, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x5949
movl %ebx, %eax
jmp 0x60c0
leaq 0x80f4(%rip), %rsi      # 0xe1e3
jmp 0x60b1
leaq 0x8130(%rip), %rsi      # 0xe228
jmp 0x611c
leaq 0x831b(%rip), %rsi      # 0xe41c
jmp 0x60b1
leaq 0x834b(%rip), %rsi      # 0xe455
jmp 0x60b1
leaq 0x838d(%rip), %rsi      # 0xe4a0
jmp 0x60b1
leaq 0x81c1(%rip), %rsi      # 0xe2dd
movl $0x1, %edi
movl %ebx, %edx
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x5949
jmp 0x60bb
leaq 0x820e(%rip), %rsi      # 0xe345
jmp 0x60b1
leaq 0x8234(%rip), %rsi      # 0xe377
jmp 0x60b1
leaq 0x8277(%rip), %rsi      # 0xe3c6
jmp 0x60b1