// File: /workspace/github_c_makefile_star2_O3/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, %r13d
leal -0x6(%r13), %eax
cmpl $-0x5, %eax
jae 0x9392
leaq 0x5b4d(%rip), %rsi      # 0xeebd
movl $0x1, %edi
callq 0x591c
xorl %r12d, %r12d
movq %r12, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r15
movq %rsi, %r14
cmpl $0x5, %r13d
movq %rdi, 0x8(%rsp)
jne 0x93b8
movq %r15, %rdi
callq 0x5430
movq 0x8(%rsp), %rdi
testl %eax, %eax
je 0x980e
leal -0x1(%r13), %ebx
leaq 0x5c3d(%rip), %rax      # 0xf000
cmpq %r14, (%rax,%rbx,8)
jbe 0x93df
leaq 0x5b39(%rip), %rsi      # 0xef09
movl $0x2, %edi
callq 0x591c
movq 0x8(%rsp), %rdi
cmpl $0x1, %r13d
jne 0x9452
movq %r15, %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x50c0
movq %rax, %rdi
callq 0x5670
leaq 0x5b4e(%rip), %rsi      # 0xef51
testq %r14, %r14
je 0x9370
movq 0x8(%rsp), %rdi
movb $-0x2, (%rdi)
cmpq $0x1, %r14
je 0x9370
movb $0x3, 0x1(%rdi)
cmpq $0x2, %r14
je 0x9370
movb 0xa2(%rax), %cl
movb %cl, 0x2(%rdi)
cmpq $0x3, %r14
je 0x9370
movb 0xa3(%rax), %al
movl $0x4, %r12d
movl $0x3, %ecx
jmp 0x947c
leaq 0x5af8(%rip), %rsi      # 0xef51
testq %r14, %r14
je 0x9370
movb $0x0, (%rdi)
cmpq $0x1, %r14
je 0x9370
movl $0x2, %r12d
movb $0x4, %al
movl $0x1, %ecx
movb %al, (%rdi,%rcx)
leaq 0x5ba2(%rip), %rax      # 0xf028
movzbl (%rbx,%rax), %r11d
movl %r13d, %ecx
movzbl (%rcx,%rax), %eax
movl %eax, 0x14(%rsp)
cmpb %al, %r11b
jae 0x937d
leaq 0x2(%rdi), %rax
movq %rax, 0x28(%rsp)
movl $0x2, %eax
subq %r14, %rax
movq %rax, 0x20(%rsp)
movaps 0x5b74(%rip), %xmm0     # 0xf030
movl $0xb, %ebp
movq %r15, 0x30(%rsp)
movl %r11d, (%rsp)
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x4d(%rsp)
movzbl %r11b, %esi
movq %r15, %rdi
callq 0x5060
movq %rax, 0x38(%rsp)
movq %rax, %rdi
callq 0x5740
movl (%rsp), %ecx
xorl %ebx, %ebx
cmpl $0x19, %ecx
movl $0x0, %edx
adcw $0x9, %dx
cmpl $0x12, %ecx
cmovbl %ebp, %edx
movl %edx, 0x1c(%rsp)
movzbl %al, %esi
movl %esi, 0x18(%rsp)
movzbl %bl, %esi
movq 0x38(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x5480
movq %rax, %rdi
callq 0x5670
movq %rax, %r13
movzbl (%rax), %r15d
xorl %ebp, %ebp
movl $0x100, %edx            # imm = 0x100
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x5320
movq 0x8(%rsp), %rdi
incb 0x60(%rsp,%r15)
movl $0x1, %eax
leaq 0x1(%rax), %rcx
movb $0x1, %dl
movb -0x1(%r13,%rcx), %sil
cmpb %r15b, %sil
jne 0x957c
cmpb $0x3, %dl
sbbb $-0x1, %bpl
incq %rcx
incb %dl
cmpq $0x101, %rcx            # imm = 0x101
jne 0x955b
jmp 0x9595
movzbl %sil, %eax
incb 0x60(%rsp,%rax)
movq %rcx, %rax
movl %esi, %r15d
cmpq $0x100, %rcx            # imm = 0x100
jne 0x9555
jmp 0x959e
movl $0x1, %ecx
cmpb $0x1, %al
je 0x95bf
xorl %ecx, %ecx
cmpb $0x3, %bpl
jb 0x95bf
movb $-0x1, %r15b
incb %r15b
movzbl %r15b, %eax
cmpb $0x0, 0x60(%rsp,%rax)
jne 0x95a9
negb %bpl
movl $0x2, %ecx
cmpq %r14, %r12
movl (%rsp), %edx
je 0x981a
movl %ecx, %eax
shlb $0x6, %al
orb %dl, %al
leaq 0x1(%r12), %rsi
movb %al, (%rdi,%r12)
cmpq %r14, %rsi
je 0x981a
leaq 0x2(%r12), %rax
movb %bl, 0x1(%rdi,%r12)
cmpl $0x2, %ecx
je 0x963c
cmpl $0x1, %ecx
jne 0x971d
movl $0x4, %edi
movq %rax, %rbx
leaq 0x5994(%rip), %rsi      # 0xef9f
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x5949
movl (%rsp), %r11d
movq 0x8(%rsp), %r10
cmpq %r14, %rbx
je 0x981a
movq %rbx, %rcx
movb (%r13), %al
addq $0x3, %r12
movb %al, (%r10,%rcx)
jmp 0x9796
movl $0x4, %edi
movq %rax, %rbx
leaq 0x5983(%rip), %rsi      # 0xefce
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x5949
movl (%rsp), %r11d
movq 0x8(%rsp), %r10
cmpq %r14, %rbx
je 0x981a
leaq 0x3(%r12), %rax
movb %bpl, 0x2(%r10,%r12)
cmpq %r14, %rax
je 0x981a
movb %r15b, 0x3(%r10,%r12)
addq $0x4, %r12
movq %r13, %rcx
incq %rcx
xorl %edx, %edx
movslq %edx, %rdi
movb (%r13,%rdi), %al
cmpl $0xfe, %edi
jg 0x97e4
movl %edi, %esi
notb %sil
movl $0xff, %r8d
subq %rdi, %r8
addq %rcx, %rdi
xorl %r9d, %r9d
cmpb %al, (%rdi,%r9)
jne 0x96c4
incq %r9
cmpq %r9, %r8
jne 0x96b4
jmp 0x96c7
movl %r9d, %esi
cmpb $0x3, %sil
jb 0x96fa
cmpq %r14, %r12
je 0x981a
leaq 0x1(%r12), %rdi
movb %r15b, (%r10,%r12)
cmpq %r14, %rdi
je 0x981a
movzbl %sil, %edi
incb %sil
movb %sil, 0x1(%r10,%r12)
addq $0x2, %r12
addl %edi, %edx
cmpq %r14, %r12
je 0x981a
movb %al, (%r10,%r12)
incq %r12
leal 0x1(%rdx), %eax
cmpl $0xff, %edx
movl %eax, %edx
jl 0x968c
jmp 0x9796
movq %rax, %rbx
movl $0x4, %edi
leaq 0x5843(%rip), %rsi      # 0xef6f
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0x5949
movl (%rsp), %r11d
cmpq %r14, %rbx
je 0x981a
movq 0x28(%rsp), %rax
leaq (%rax,%r12), %rcx
movq 0x20(%rsp), %rax
leaq (%rax,%r12), %rdx
movq $-0x1, %rax
movb 0x1(%r13,%rax), %sil
movb %sil, 0x1(%rcx,%rax)
cmpq $0xfe, %rax
je 0x978f
leaq (%rdx,%rax), %rsi
incq %rsi
incq %rax
cmpq $-0x1, %rsi
jne 0x975d
movq %r14, %r12
cmpq $0xff, %rax
jae 0x9796
jmp 0x981a
addq %rax, %r12
addq $0x4, %r12
movl 0x4(%rsp), %eax
addl 0x1c(%rsp), %eax
xorl %edx, %edx
movl 0x18(%rsp), %esi
divw %si
movl %edx, %ebx
movq 0x30(%rsp), %r15
movl $0xb, %ebp
xorl %ecx, %ecx
movzbl %bl, %eax
cmpb $0x0, 0x40(%rsp,%rax)
je 0x97d4
incb %bl
cmpb %sil, %bl
movzbl %bl, %ebx
cmovel %ecx, %ebx
movzbl %bl, %eax
cmpw %ax, %dx
jne 0x97b5
jmp 0x97f4
movb $0x1, 0x40(%rsp,%rax)
cmpb $-0x1, %bl
jne 0x9513
jmp 0x97f4
cmpq %r14, %r12
je 0x981a
movq %r12, %rcx
incq %r12
jmp 0x9633
incl %r11d
cmpl 0x14(%rsp), %r11d
movaps 0x582d(%rip), %xmm0     # 0xf030
jb 0x94c6
jmp 0x937d
leaq 0x56c4(%rip), %rsi      # 0xeed9
jmp 0x9370
leaq 0x5730(%rip), %rsi      # 0xef51
jmp 0x9370
nop