// File: /workspace/github_c_makefile_star2_O2/AK6DN[P]dec-utilities-for-pdp/d8tape/main.c
// Function: writebin
static  void
writebin (void)
{
    char    fname [PATH_MAX];
    char    leader [LEADER_LENGTH];
    FILE    *fp;
    int     i;
    int     cksum;
    int     state;

    // create filename and open it
    sprintf (fname, "%s.bin", tapename);
    if ((fp = fopen (fname, "w")) == NULL) {
        fprintf (stderr, "%s:  unable to open BIN output file %s for w, errno %d (%s); creation of output file skipped\n", progname, fname, errno, strerror (errno));
        return;
    }

    // write leader
    memset (leader, 0x80, sizeof (leader));
    fwrite (leader, 1, sizeof (leader), fp);

    // now scan through "core" and write the data out...
    cksum = 0;
    state = WBIN_Initial;
    for (i = 0; i < CORE_SIZE; i++) {
        switch (state) {
        case    WBIN_Initial:               // transit out of WBIN_Initial on a "used" core position
            if (core [i] != -1) {
                state = WBIN_Writing;
                fprintf (fp, "%c%c", 0x40 | ((i & 07700) >> 6), i & 00077);     // write origin directive
                fprintf (fp, "%c%c", (core [i] & 07700) >> 6, core [i] & 00077);    // write data
                cksum += (0x40 | ((i & 07700) >> 6)) + (i & 00077) + ((core [i] & 07700) >> 6) + (core [i] & 00077);
            }
            break;
        case    WBIN_Writing:
            if (core [i] == -1) {
                state = WBIN_Initial;       // waiting again for a used core position
            } else {
                fprintf (fp, "%c%c", (core [i] & 07700) >> 6, core [i] & 00077);
                cksum += ((core [i] & 07700) >> 6) + (core [i] & 00077);
            }
            break;
        }
    }

    // now write the checksum
    fprintf (fp, "%c%c", (cksum & 07700) >> 6, cksum & 00077);

    // write trailer
    fwrite (leader, 1, sizeof (leader), fp);

    fclose (fp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movq 0xd172(%rip), %rdx      # 0x10f50
leaq 0x2b0e(%rip), %rsi      # 0x68f3
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x31a0
leaq 0x323b(%rip), %rsi      # 0x7036
movq %rbx, %rdi
callq 0x3170
testq %rax, %rax
je 0x3f77
movq %rax, %rbx
movaps 0x221a(%rip), %xmm0     # 0x6030
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
movq %rax, %rcx
callq 0x31c0
pushq $0x1
popq %rax
leaq 0x910a(%rip), %r13      # 0xcf40
xorl %r15d, %r15d
xorl %r14d, %r14d
cmpq $0x1000, %r15           # imm = 0x1000
je 0x3f2a
movzwl (%r13), %ecx
cmpl $0x1, %eax
jne 0x3edb
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x3f1b
movl %r15d, %r12d
shrl $0x6, %r12d
movl %r12d, %edx
orl $0x40, %edx
movl %r15d, %ebp
andl $0x3f, %ebp
movq %rbx, %rdi
movq %r14, 0x8(%rsp)
movq %rbx, %r14
leaq 0x2b39(%rip), %rbx      # 0x69c1
movq %rbx, %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3120
movzwl (%r13), %ecx
movl %ecx, %edx
shrl $0x6, %edx
andl $0x3f, %edx
andl $0x3f, %ecx
movq %r14, %rdi
movq %rbx, %rsi
movq %r14, %rbx
movq 0x8(%rsp), %r14
xorl %eax, %eax
callq 0x3120
movzwl (%r13), %eax
movl %eax, %ecx
shrl $0x6, %ecx
andl $0x3f, %ecx
andl $0x3f, %eax
addl %ebp, %r14d
addl %r12d, %r14d
addl $0x40, %r14d
addl %eax, %r14d
addl %ecx, %r14d
jmp 0x3f17
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x3f1b
movl %ecx, %edx
shrl $0x6, %edx
andl $0x3f, %edx
andl $0x3f, %ecx
movq %rbx, %rdi
leaq 0x2ac9(%rip), %rsi      # 0x69c1
xorl %eax, %eax
callq 0x3120
movzwl (%r13), %eax
movl %eax, %ecx
shrl $0x6, %ecx
andl $0x3f, %ecx
andl $0x3f, %eax
addl %r14d, %eax
addl %ecx, %eax
movl %eax, %r14d
pushq $0x2
jmp 0x3f1d
pushq $0x1
popq %rax
incq %r15
addq $0x2, %r13
jmp 0x3e3c
movl %r14d, %edx
shrl $0x6, %edx
andl $0x3f, %edx
andl $0x3f, %r14d
leaq 0x2a83(%rip), %rsi      # 0x69c1
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3120
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
movq %rbx, %rcx
callq 0x31c0
movq %rbx, %rdi
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30a0
movq 0x807a(%rip), %rax      # 0xbff8
movq (%rax), %rbx
movq 0x8188(%rip), %r14      # 0xc110
callq 0x3060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x31d0
leaq 0x295d(%rip), %rsi      # 0x68fa
leaq 0x20(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rdx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x3120
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq