// File: /workspace/github_c_makefile_star2_O3/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 0xcf2d(%rip), %rdx      # 0x10f50
leaq 0x2909(%rip), %rsi      # 0x6933
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x31a0
leaq 0x3036(%rip), %rsi      # 0x7076
movq %rbx, %rdi
callq 0x3170
testq %rax, %rax
je 0x41cb
movq %rax, %rbx
movaps 0x1ff5(%rip), %xmm0     # 0x6050
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x1, %esi
movl $0x10, %edx
movq %rax, %rcx
callq 0x31c0
movl $0x1, %eax
leaq 0x8ebf(%rip), %r12      # 0xcf40
xorl %r13d, %r13d
xorl %r14d, %r14d
movzwl (%r12), %ecx
cmpl $0x1, %eax
jne 0x411e
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x4161
movl %r13d, %r15d
shrl $0x6, %r15d
movl %r15d, %edx
orl $0x40, %edx
movl %r13d, %ebp
andl $0x3f, %ebp
movq %rbx, %rdi
movq %r14, 0x8(%rsp)
movq %rbx, %r14
leaq 0x293b(%rip), %rbx      # 0x6a01
movq %rbx, %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3120
movzwl (%r12), %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 (%r12), %eax
movl %eax, %ecx
shrl $0x6, %ecx
andl $0x3f, %ecx
andl $0x3f, %eax
addl %ebp, %r14d
addl %r15d, %r14d
addl $0x40, %r14d
addl %eax, %r14d
addl %ecx, %r14d
movl $0x2, %eax
jmp 0x4166
cmpl $0xffff, %ecx           # imm = 0xFFFF
je 0x4161
movl %ecx, %edx
shrl $0x6, %edx
andl $0x3f, %edx
andl $0x3f, %ecx
movq %rbx, %rdi
leaq 0x28c6(%rip), %rsi      # 0x6a01
xorl %eax, %eax
callq 0x3120
movzwl (%r12), %ecx
movl %ecx, %eax
shrl $0x6, %eax
andl $0x3f, %eax
andl $0x3f, %ecx
addl %r14d, %ecx
addl %eax, %ecx
movl $0x2, %eax
movl %ecx, %r14d
jmp 0x4166
movl $0x1, %eax
incq %r13
addq $0x2, %r12
cmpq $0x1000, %r13           # imm = 0x1000
jne 0x4087
movl %r14d, %edx
shrl $0x6, %edx
andl $0x3f, %edx
andl $0x3f, %r14d
leaq 0x2873(%rip), %rsi      # 0x6a01
movq %rbx, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3120
leaq 0x10(%rsp), %rdi
movl $0x1, %esi
movl $0x10, %edx
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
leaq 0x20(%rsp), %rdi
callq 0x3406
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq