// File: /workspace/github_c_makefile_star2_O1/excess-c64[P]lib1541img/src/lib/1541img/zc45writer.c
// Function: zc45_write
SOEXPORT size_t zc45_write(
	uint8_t *zcfile, size_t zcfilelen, int zcfileno, const D64 *d64)
{
    if (zcfileno < 1 || zcfileno > 5)
    {
        logmsg(L_ERROR, "zc45_write: invalid fileno.");
        return 0;
    }
    if (zcfileno == 5 && D64_type(d64) == D64_STANDARD)
    {
        logmsg(L_ERROR, "zc45_write: fileno 5 invalid for 35-track disk.");
        return 0;
    }
    if (zcfilelen < filesizes[zcfileno-1])
    {
        logmsg(L_WARNING, "zc45_write: file size might be too small to hold "
                "the resulting zipcode.");
    }

    size_t wpos = 0;

    if (zcfileno == 1)
    {
        const uint8_t *bam = Sector_rcontent(D64_rsector(d64, 18, 0));
        if (writebyte(zcfile, &wpos, zcfilelen, 0xfe) < 0) goto fail;
        if (writebyte(zcfile, &wpos, zcfilelen, 0x03) < 0) goto fail;
        if (writebyte(zcfile, &wpos, zcfilelen, bam[0xa2]) < 0) goto fail;
        if (writebyte(zcfile, &wpos, zcfilelen, bam[0xa3]) < 0) goto fail;
    }
    else
    {
        if (writebyte(zcfile, &wpos, zcfilelen, 0x00) < 0) goto fail;
        if (writebyte(zcfile, &wpos, zcfilelen, 0x04) < 0) goto fail;
    }

    for (uint8_t trackno = trackrange[zcfileno-1];
            trackno < trackrange[zcfileno]; ++trackno)
    {
        if (writeTrack(zcfile, zcfilelen, &wpos, trackno, d64) < 0) goto fail;
    }
    return wpos;

fail:
    logmsg(L_ERROR, "zc45_write: not enough space.");
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movl %edx, %ebx
movq %rsi, 0x10(%rsp)
leal -0x6(%rbx), %eax
cmpl $-0x5, %eax
jae 0x8644
leaq 0x587e(%rip), %rsi      # 0xdebd
jmp 0x8b52
movq %rcx, %r15
cmpl $0x5, %ebx
movq %rdi, 0x18(%rsp)
jne 0x8662
movq %r15, %rdi
callq 0x4430
movq 0x18(%rsp), %rdi
testl %eax, %eax
je 0x86d4
leal -0x1(%rbx), %r14d
leaq 0x5993(%rip), %rax      # 0xe000
movq 0x10(%rsp), %rcx
cmpq %rcx, (%rax,%r14,8)
jbe 0x868e
leaq 0x588a(%rip), %rsi      # 0xdf09
movl $0x2, %edi
callq 0x491c
movq 0x18(%rsp), %rdi
cmpl $0x1, %ebx
jne 0x86c2
movq %r15, %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x40c0
movq %rax, %rdi
callq 0x4670
cmpq $0x0, 0x10(%rsp)
je 0x86e0
movq 0x18(%rsp), %rdi
movb $-0x2, (%rdi)
movl $0x1, %r8d
jmp 0x86e8
cmpq $0x0, 0x10(%rsp)
je 0x8714
movb $0x0, (%rdi)
movl $0x1, %eax
jmp 0x8716
leaq 0x57fe(%rip), %rsi      # 0xded9
jmp 0x8b52
movq 0x18(%rsp), %rdi
xorl %r8d, %r8d
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x876a
movq %r8, %rdx
cmpq %rcx, %r8
je 0x8703
leaq 0x1(%r8), %rdx
movb $0x3, (%rdi,%r8)
movq 0x10(%rsp), %rcx
cmpq %rcx, %r8
jne 0x8747
xorl %ecx, %ecx
movq %rdx, %r8
jmp 0x876c
xorl %eax, %eax
leaq 0x5834(%rip), %rsi      # 0xdf51
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x8b52
movq %rax, %r8
cmpq %rcx, %rax
je 0x873b
leaq 0x1(%rax), %r8
movb $0x4, (%rdi,%rax)
cmpq 0x10(%rsp), %rax
jne 0x8774
jmp 0x8b52
movq %rdx, %r8
cmpq %rcx, %rdx
je 0x875c
movb 0xa2(%rax), %cl
leaq 0x1(%rdx), %r8
movb %cl, (%rdi,%rdx)
movq 0x10(%rsp), %rcx
cmpq %rcx, %rdx
jne 0x8b74
xorl %ecx, %ecx
testb %cl, %cl
je 0x8b4b
leaq 0x58ad(%rip), %rax      # 0xe028
movzbl (%r14,%rax), %edx
movl %ebx, %ecx
movzbl (%rcx,%rax), %eax
movl %eax, 0x2c(%rsp)
cmpb %al, %dl
setb %al
jae 0x8b47
leaq 0x2(%rdi), %rcx
movq %rcx, 0x48(%rsp)
movl $0x2, %ecx
subq 0x10(%rsp), %rcx
movq %rcx, 0x40(%rsp)
movq %r15, 0x38(%rsp)
movb %al, 0xe(%rsp)
movaps 0x5873(%rip), %xmm0     # 0xe030
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x6d(%rsp)
movzbl %dl, %esi
movq %r15, %rdi
movl %edx, %ebx
movq %r8, %rbp
callq 0x4060
movq %rax, 0x58(%rsp)
movq %rax, %rdi
callq 0x4740
cmpl $0x19, %ebx
movl $0x0, %edx
adcl $0x9, %edx
movl %ebx, 0x20(%rsp)
cmpl $0x12, %ebx
movl $0xb, %ecx
cmovbl %ecx, %edx
movl %edx, 0x34(%rsp)
movzbl %al, %ecx
movl %ecx, 0x30(%rsp)
xorl %r14d, %r14d
movb %al, 0xf(%rsp)
movzbl %r14b, %esi
movq 0x58(%rsp), %rdi
movl %esi, 0x28(%rsp)
callq 0x4480
movq %rax, %rdi
callq 0x4670
movq %rax, %r13
movzbl (%rax), %ebx
xorl %r12d, %r12d
movl $0x100, %edx            # imm = 0x100
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x4320
incb 0x80(%rsp,%rbx)
movl $0x1, %eax
xorl %r15d, %r15d
movq 0x18(%rsp), %rsi
movb (%r13,%rax), %cl
cmpb %bl, %cl
jne 0x8875
incb %r12b
cmpb $0x3, %r12b
sbbb $-0x1, %r15b
jmp 0x8884
movzbl %cl, %edx
incb 0x80(%rsp,%rdx)
xorl %r12d, %r12d
movl %ecx, %ebx
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x885f
cmpb $-0x1, %r12b
movq 0x10(%rsp), %rbx
je 0x88c4
xorl %eax, %eax
cmpb $0x3, %r15b
movl 0x20(%rsp), %edx
jb 0x88cd
movb $-0x1, %r12b
incb %r12b
movzbl %r12b, %eax
cmpb $0x0, 0x80(%rsp,%rax)
jne 0x88a9
negb %r15b
movl $0x2, %eax
jmp 0x88cd
movl $0x1, %eax
movl 0x20(%rsp), %edx
movb $0x1, %cl
movl %ecx, 0x24(%rsp)
cmpq %rbx, %rbp
je 0x8b1a
movl %eax, %ecx
shlb $0x6, %cl
orb %dl, %cl
leaq 0x1(%rbp), %r8
movb %cl, (%rsi,%rbp)
cmpq %rbx, %r8
je 0x8b1d
leaq 0x2(%rbp), %r8
movb %r14b, 0x1(%rsi,%rbp)
cmpl $0x2, %eax
je 0x894a
cmpl $0x1, %eax
movl 0x28(%rsp), %r14d
jne 0x8a46
movl $0x4, %edi
leaq 0x5684(%rip), %rsi      # 0xdf9f
movl %r14d, %ecx
xorl %eax, %eax
movq %r8, %r15
callq 0x4949
movq %r15, %r8
movq 0x18(%rsp), %rcx
cmpq %rbx, %r15
je 0x8b1d
movb (%r13), %al
movb %al, 0x2(%rcx,%rbp)
addq $0x3, %rbp
jmp 0x8ac6
movl $0x4, %edi
leaq 0x5678(%rip), %rsi      # 0xdfce
movl 0x28(%rsp), %ecx
movl %ecx, %r14d
xorl %eax, %eax
movq %r8, 0x50(%rsp)
callq 0x4949
movq 0x50(%rsp), %r8
cmpq %rbx, %r8
je 0x8b1d
leaq 0x3(%rbp), %r8
movq 0x18(%rsp), %r11
movb %r15b, 0x2(%r11,%rbp)
cmpq %rbx, %r8
je 0x8b1d
movb %r12b, 0x3(%r11,%rbp)
addq $0x4, %rbp
movq %r13, %rcx
incq %rcx
xorl %edx, %edx
movb $0x1, %al
movslq %edx, %r8
movb (%r13,%r8), %sil
cmpl $0xfe, %r8d
jg 0x89d7
movl %r8d, %edi
notb %dil
movl $0xff, %r9d
subq %r8, %r9
addq %rcx, %r8
xorl %r10d, %r10d
cmpb %sil, (%r8,%r10)
jne 0x89db
incq %r10
cmpq %r10, %r9
jne 0x89c7
jmp 0x89de
xorl %edi, %edi
jmp 0x89de
movl %r10d, %edi
cmpb $0x3, %dil
jb 0x8a14
cmpq %rbx, %rbp
jne 0x89ed
xorl %edi, %edi
jmp 0x8a27
leaq 0x1(%rbp), %r8
movb %r12b, (%r11,%rbp)
cmpq %rbx, %r8
jne 0x8a01
xorl %edi, %edi
movq %r8, %rbp
jmp 0x8a27
movzbl %dil, %r8d
incb %dil
movb %dil, 0x1(%r11,%rbp)
addq $0x2, %rbp
addl %r8d, %edx
cmpq %rbx, %rbp
setne %dil
je 0x8a27
movb %sil, (%r11,%rbp)
incq %rbp
movb $0x1, %dil
testb %dil, %dil
je 0x8abf
leal 0x1(%rdx), %esi
cmpl $0xff, %edx
setl %al
movl %esi, %edx
jl 0x89a1
jmp 0x8abf
movq %r8, %r15
movl $0x4, %edi
leaq 0x551a(%rip), %rsi      # 0xdf6f
movl %r14d, %ecx
xorl %eax, %eax
callq 0x4949
cmpq %rbx, %r15
je 0x8b3e
movq 0x48(%rsp), %rax
addq %rbp, %rax
movq 0x40(%rsp), %rcx
leaq (%rcx,%rbp), %rdx
movq $-0x1, %rsi
movq %rsi, %rcx
movb 0x1(%r13,%rsi), %sil
movb %sil, 0x1(%rax,%rcx)
cmpq $0xfe, %rcx
je 0x8aa9
leaq 0x1(%rcx), %rsi
leaq (%rdx,%rcx), %rdi
incq %rdi
cmpq $-0x1, %rdi
jne 0x8a80
jmp 0x8aae
movl $0xff, %esi
cmpq $0xff, %rsi
setb %al
addq %rcx, %rbp
addq $0x4, %rbp
movq %rbp, %r8
testb $0x1, %al
jne 0x8b1d
addl 0x34(%rsp), %r14d
movl %r14d, %eax
xorl %edx, %edx
divl 0x30(%rsp)
movl %edx, %r14d
xorl %ecx, %ecx
movb 0xf(%rsp), %al
movzbl %r14b, %esi
cmpb $0x0, 0x60(%rsp,%rsi)
je 0x8b03
incb %r14b
cmpb %al, %r14b
movzbl %r14b, %r14d
cmovel %ecx, %r14d
movzbl %r14b, %esi
cmpl %esi, %edx
jne 0x8add
movb $-0x1, %r14b
jmp 0x8b08
movb $0x1, 0x60(%rsp,%rsi)
cmpb $-0x1, %r14b
jne 0x8814
movl $0x0, 0x24(%rsp)
movq %rbp, %r8
cmpb $0x0, 0x24(%rsp)
jne 0x8b43
movl 0x20(%rsp), %edx
incl %edx
cmpl 0x2c(%rsp), %edx
setb %al
movq 0x38(%rsp), %r15
jb 0x87b2
jmp 0x8b47
movq %r15, %r8
jmp 0x8b1d
movb 0xe(%rsp), %al
testb $0x1, %al
je 0x8b5f
leaq 0x53ff(%rip), %rsi      # 0xdf51
movl $0x1, %edi
callq 0x491c
xorl %r8d, %r8d
movq %r8, %rax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %rcx, %r8
setne %cl
je 0x876c
movb 0xa3(%rax), %al
movb %al, (%rdi,%r8)
incq %r8
jmp 0x876c
nop