// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
cmpl $0x1, -0x1c(%rbp)
jl 0xbde3
cmpl $0x5, -0x1c(%rbp)
jle 0xbe01
movl $0x1, %edi
leaq 0x80fe(%rip), %rsi      # 0x13eed
callq 0x5a60
movq $0x0, -0x8(%rbp)
jmp 0xbfd4
cmpl $0x5, -0x1c(%rbp)
jne 0xbe33
movq -0x28(%rbp), %rdi
callq 0x5480
cmpl $0x0, %eax
jne 0xbe33
movl $0x1, %edi
leaq 0x80e8(%rip), %rsi      # 0x13f09
callq 0x5a60
movq $0x0, -0x8(%rbp)
jmp 0xbfd4
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rdx
leaq 0x81e9(%rip), %rcx      # 0x14030
cmpq (%rcx,%rdx,8), %rax
jae 0xbe5e
movl $0x2, %edi
leaq 0x80e0(%rip), %rsi      # 0x13f39
callq 0x5a60
movq $0x0, -0x30(%rbp)
cmpl $0x1, -0x1c(%rbp)
jne 0xbf1a
movq -0x28(%rbp), %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x50c0
movq %rax, %rdi
callq 0x56d0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rsi
movl $0xfe, %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xbeac
jmp 0xbfbb
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rsi
movl $0x3, %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xbecc
jmp 0xbfbb
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
leaq -0x30(%rbp), %rsi
movzbl 0xa2(%rax), %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xbef2
jmp 0xbfbb
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rax
leaq -0x30(%rbp), %rsi
movzbl 0xa3(%rax), %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xbf18
jmp 0xbfbb
jmp 0xbf56
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rsi
xorl %ecx, %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xbf37
jmp 0xbfbb
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0x30(%rbp), %rsi
movl $0x4, %ecx
callq 0xbfe0
cmpl $0x0, %eax
jge 0xbf54
jmp 0xbfbb
jmp 0xbf56
movl -0x1c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x80f2(%rip), %rax      # 0x14058
movb (%rax,%rcx), %al
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
movslq -0x1c(%rbp), %rdx
leaq 0x80dd(%rip), %rcx      # 0x14058
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jge 0xbfb1
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movb -0x39(%rbp), %al
movq -0x28(%rbp), %r8
leaq -0x30(%rbp), %rdx
movzbl %al, %ecx
callq 0xc040
cmpl $0x0, %eax
jge 0xbfa5
jmp 0xbfbb
jmp 0xbfa7
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
jmp 0xbf6c
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xbfd4
movl $0x1, %edi
leaq 0x7fba(%rip), %rsi      # 0x13f81
callq 0x5a60
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nop