// File: /workspace/github_c_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movl %edx, %r12d
leal -0x6(%r12), %eax
cmpl $-0x5, %eax
jae 0x9115
leaq 0x4dc8(%rip), %rsi      # 0xdebd
pushq $0x1
popq %rdi
callq 0x5960
xorl %r14d, %r14d
movq %r14, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r14
movq %rsi, %rcx
cmpl $0x5, %r12d
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
jne 0x9145
movq %r14, %rdi
callq 0x5460
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
testl %eax, %eax
je 0x9503
leal -0x1(%r12), %ebx
leaq 0x4eaf(%rip), %rax      # 0xe000
cmpq %rcx, (%rax,%rbx,8)
jbe 0x9170
leaq 0x4dab(%rip), %rsi      # 0xdf09
pushq $0x2
popq %rdi
callq 0x5960
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
cmpl $0x1, %r12d
jne 0x91e6
pushq $0x12
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x50c0
movq %rax, %rdi
callq 0x56b0
movq 0x18(%rsp), %rdx
leaq 0x4dba(%rip), %rsi      # 0xdf51
testq %rdx, %rdx
je 0x90f5
movq 0x10(%rsp), %rdi
movb $-0x2, (%rdi)
cmpq $0x1, %rdx
je 0x90f5
movb $0x3, 0x1(%rdi)
cmpq $0x2, %rdx
je 0x90f5
movb 0xa2(%rax), %cl
movb %cl, 0x2(%rdi)
cmpq $0x3, %rdx
je 0x90f5
movq %r14, 0x30(%rsp)
movb 0xa3(%rax), %al
pushq $0x4
popq %r14
pushq $0x3
jmp 0x9210
leaq 0x4d64(%rip), %rsi      # 0xdf51
testq %rcx, %rcx
je 0x90f5
movb $0x0, (%rdi)
cmpq $0x1, %rcx
je 0x90f5
movq %r14, 0x30(%rsp)
pushq $0x2
popq %r14
movb $0x4, %al
pushq $0x1
popq %rcx
movb %al, (%rdi,%rcx)
leaq 0x4e0d(%rip), %rax      # 0xe028
movb (%rbx,%rax), %bpl
movl %r12d, %ecx
movb (%rcx,%rax), %al
movb %al, 0xb(%rsp)
cmpb %al, %bpl
jae 0x9100
movaps 0x4df7(%rip), %xmm0     # 0xe030
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x4d(%rsp)
movzbl %bpl, %esi
movq 0x30(%rsp), %rdi
movl %esi, 0x24(%rsp)
callq 0x5060
movq %rax, %r15
movq %rax, %rdi
callq 0x5780
xorl %ebx, %ebx
cmpb $0x19, %bpl
movl $0x0, %edx
adcw $0x9, %dx
cmpb $0x12, %bpl
pushq $0xb
popq %rcx
cmovbl %ecx, %edx
movl %edx, 0x2c(%rsp)
movzbl %al, %esi
movb %bpl, 0xa(%rsp)
movq %r15, 0x38(%rsp)
movl %esi, 0x28(%rsp)
movzbl %bl, %esi
movq %r15, %rdi
movl %esi, 0xc(%rsp)
callq 0x54b0
movq %rax, %rdi
callq 0x56b0
movq %rax, %r12
movzbl (%rax), %r13d
xorl %r15d, %r15d
movl $0x100, %edx            # imm = 0x100
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x5330
movq 0x18(%rsp), %rsi
incb 0x60(%rsp,%r13)
pushq $0x1
popq %rax
xorl %ecx, %ecx
cmpq $0x100, %rax            # imm = 0x100
je 0x92fd
movb (%r12,%rax), %dl
cmpb %r13b, %dl
jne 0x92ec
incb %cl
cmpb $0x3, %cl
sbbb $-0x1, %r15b
jmp 0x92f8
movzbl %dl, %ecx
incb 0x60(%rsp,%rcx)
xorl %ecx, %ecx
movl %edx, %r13d
incq %rax
jmp 0x92d0
pushq $0x1
popq %rax
cmpb $-0x1, %cl
movq 0x10(%rsp), %rdi
je 0x9329
xorl %eax, %eax
cmpb $0x3, %r15b
jb 0x9329
movb $-0x1, %r13b
incb %r13b
movzbl %r13b, %eax
cmpb $0x0, 0x60(%rsp,%rax)
jne 0x9315
negb %r15b
pushq $0x2
popq %rax
cmpq %rsi, %r14
je 0x950f
movl %eax, %ecx
shlb $0x6, %cl
orb 0xa(%rsp), %cl
leaq 0x1(%r14), %rdx
movb %cl, (%rdi,%r14)
cmpq %rsi, %rdx
je 0x950f
leaq 0x2(%r14), %rbp
movb %bl, 0x1(%rdi,%r14)
pushq $0x4
popq %rdi
cmpl $0x2, %eax
je 0x939e
cmpl $0x1, %eax
jne 0x946a
leaq 0x4c32(%rip), %rsi      # 0xdf9f
movl 0x24(%rsp), %edx
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x598d
movq 0x10(%rsp), %rcx
cmpq 0x18(%rsp), %rbp
je 0x950f
movb (%r12), %al
movb %al, 0x2(%rcx,%r14)
addq $0x3, %r14
jmp 0x94ab
leaq 0x4c29(%rip), %rsi      # 0xdfce
movl 0x24(%rsp), %edx
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x598d
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r10
cmpq %r10, %rbp
je 0x950f
leaq 0x3(%r14), %rax
movb %r15b, 0x2(%r11,%r14)
cmpq %r10, %rax
je 0x950f
movb %r13b, 0x3(%r11,%r14)
addq $0x4, %r14
movq %r12, %rax
incq %rax
xorl %ecx, %ecx
cmpl $0xff, %ecx
jg 0x94ab
movslq %ecx, %rdi
movb (%r12,%rdi), %dl
movl %ecx, %esi
notb %sil
movl $0xff, %r8d
subq %rdi, %r8
addq %rax, %rdi
xorl %r9d, %r9d
cmpq %r9, %r8
je 0x9424
cmpb %dl, (%rdi,%r9)
jne 0x9421
incq %r9
jmp 0x9411
movl %r9d, %esi
cmpb $0x3, %sil
jb 0x9456
cmpq %r10, %r14
je 0x950f
leaq 0x1(%r14), %rdi
movb %r13b, (%r11,%r14)
cmpq %r10, %rdi
je 0x950f
movzbl %sil, %edi
incb %sil
movb %sil, 0x1(%r11,%r14)
addq $0x2, %r14
addl %edi, %ecx
cmpq %r10, %r14
je 0x950f
movb %dl, (%r11,%r14)
incq %r14
incl %ecx
jmp 0x93ea
leaq 0x4afe(%rip), %rsi      # 0xdf6f
movl 0x24(%rsp), %edx
movl 0xc(%rsp), %ecx
xorl %eax, %eax
callq 0x598d
xorl %eax, %eax
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rsi
cmpq $0x100, %rax            # imm = 0x100
je 0x94a8
cmpq %rbp, %rdx
je 0x950f
movb (%r12,%rax), %cl
movb %cl, (%rsi,%rbp)
incq %rbp
incq %rax
jmp 0x948c
movq %rbp, %r14
movb 0xa(%rsp), %bpl
movl 0xc(%rsp), %eax
addl 0x2c(%rsp), %eax
xorl %edx, %edx
movl 0x28(%rsp), %esi
divw %si
movl %edx, %ebx
xorl %ecx, %ecx
movq 0x38(%rsp), %r15
movzbl %bl, %eax
cmpb $0x0, 0x40(%rsp,%rax)
je 0x94e9
incb %bl
cmpb %sil, %bl
movzbl %bl, %ebx
cmovel %ecx, %ebx
movzbl %bl, %eax
cmpw %ax, %dx
jne 0x94ca
jmp 0x94f7
movb $0x1, 0x40(%rsp,%rax)
cmpb $-0x1, %bl
jne 0x928f
incb %bpl
movb 0xb(%rsp), %al
jmp 0x9229
leaq 0x49cf(%rip), %rsi      # 0xded9
jmp 0x90f5
leaq 0x4a3b(%rip), %rsi      # 0xdf51
jmp 0x90f5
nop