// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1050, %rsp           # imm = 0x1050
leaq -0x1000(%rbp), %rdi
movq 0xf467(%rip), %rdx      # 0x12f70
leaq 0x4e57(%rip), %rsi      # 0x8967
movb $0x0, %al
callq 0x3180
leaq -0x1000(%rbp), %rdi
leaq 0x54cd(%rip), %rsi      # 0x8ff2
callq 0x3140
movq %rax, -0x1018(%rbp)
cmpq $0x0, %rax
jne 0x3baf
movq 0xa4ba(%rip), %rax      # 0xdff8
movq (%rax), %rax
movq %rax, -0x1040(%rbp)
movq 0xa5b1(%rip), %rax      # 0xe100
movq %rax, -0x1038(%rbp)
leaq -0x1000(%rbp), %rax
movq %rax, -0x1030(%rbp)
callq 0x3050
movl (%rax), %eax
movl %eax, -0x1028(%rbp)
callq 0x3050
movl (%rax), %edi
callq 0x31b0
movq -0x1040(%rbp), %rdi
movq -0x1038(%rbp), %rdx
movq -0x1030(%rbp), %rcx
movl -0x1028(%rbp), %r8d
movq %rax, %r9
leaq 0x4dcb(%rip), %rsi      # 0x896e
movb $0x0, %al
callq 0x30f0
jmp 0x3e48
leaq -0x1010(%rbp), %rdi
movl $0x80, %esi
movl $0x10, %edx
callq 0x30b0
leaq -0x1010(%rbp), %rdi
movq -0x1018(%rbp), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x31a0
movl $0x0, -0x1020(%rbp)
movl $0x1, -0x1024(%rbp)
movl $0x0, -0x101c(%rbp)
cmpl $0x1000, -0x101c(%rbp)  # imm = 0x1000
jge 0x3df2
movl -0x1024(%rbp), %eax
movl %eax, -0x1044(%rbp)
subl $0x1, %eax
je 0x3c37
jmp 0x3c23
movl -0x1044(%rbp), %eax
subl $0x2, %eax
je 0x3d32
jmp 0x3ddc
movslq -0x101c(%rbp), %rcx
leaq 0xb31b(%rip), %rax      # 0xef60
movswl (%rax,%rcx,2), %eax
cmpl $-0x1, %eax
je 0x3d2d
movl $0x2, -0x1024(%rbp)
movq -0x1018(%rbp), %rdi
movl -0x101c(%rbp), %edx
andl $0xfc0, %edx            # imm = 0xFC0
sarl $0x6, %edx
orl $0x40, %edx
movl -0x101c(%rbp), %ecx
andl $0x3f, %ecx
leaq 0x4db0(%rip), %rsi      # 0x8a35
movb $0x0, %al
callq 0x30f0
movq -0x1018(%rbp), %rdi
movslq -0x101c(%rbp), %rcx
leaq 0xb2bf(%rip), %rax      # 0xef60
movswl (%rax,%rcx,2), %edx
andl $0xfc0, %edx            # imm = 0xFC0
sarl $0x6, %edx
movslq -0x101c(%rbp), %rcx
leaq 0xb2a4(%rip), %rax      # 0xef60
movswl (%rax,%rcx,2), %ecx
andl $0x3f, %ecx
leaq 0x4d6b(%rip), %rsi      # 0x8a35
movb $0x0, %al
callq 0x30f0
movl -0x101c(%rbp), %eax
andl $0xfc0, %eax            # imm = 0xFC0
sarl $0x6, %eax
orl $0x40, %eax
movl -0x101c(%rbp), %ecx
andl $0x3f, %ecx
addl %ecx, %eax
movslq -0x101c(%rbp), %rdx
leaq 0xb265(%rip), %rcx      # 0xef60
movswl (%rcx,%rdx,2), %ecx
andl $0xfc0, %ecx            # imm = 0xFC0
sarl $0x6, %ecx
addl %ecx, %eax
movslq -0x101c(%rbp), %rdx
leaq 0xb248(%rip), %rcx      # 0xef60
movswl (%rcx,%rdx,2), %ecx
andl $0x3f, %ecx
addl %ecx, %eax
addl -0x1020(%rbp), %eax
movl %eax, -0x1020(%rbp)
jmp 0x3ddc
movslq -0x101c(%rbp), %rcx
leaq 0xb220(%rip), %rax      # 0xef60
movswl (%rax,%rcx,2), %eax
cmpl $-0x1, %eax
jne 0x3d58
movl $0x1, -0x1024(%rbp)
jmp 0x3dda
movq -0x1018(%rbp), %rdi
movslq -0x101c(%rbp), %rcx
leaq 0xb1f3(%rip), %rax      # 0xef60
movswl (%rax,%rcx,2), %edx
andl $0xfc0, %edx            # imm = 0xFC0
sarl $0x6, %edx
movslq -0x101c(%rbp), %rcx
leaq 0xb1d8(%rip), %rax      # 0xef60
movswl (%rax,%rcx,2), %ecx
andl $0x3f, %ecx
leaq 0x4c9f(%rip), %rsi      # 0x8a35
movb $0x0, %al
callq 0x30f0
movslq -0x101c(%rbp), %rcx
leaq 0xb1b5(%rip), %rax      # 0xef60
movswl (%rax,%rcx,2), %eax
andl $0xfc0, %eax            # imm = 0xFC0
sarl $0x6, %eax
movslq -0x101c(%rbp), %rdx
leaq 0xb19b(%rip), %rcx      # 0xef60
movswl (%rcx,%rdx,2), %ecx
andl $0x3f, %ecx
addl %ecx, %eax
addl -0x1020(%rbp), %eax
movl %eax, -0x1020(%rbp)
jmp 0x3ddc
jmp 0x3dde
movl -0x101c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x101c(%rbp)
jmp 0x3c00
movq -0x1018(%rbp), %rdi
movl -0x1020(%rbp), %edx
andl $0xfc0, %edx            # imm = 0xFC0
sarl $0x6, %edx
movl -0x1020(%rbp), %ecx
andl $0x3f, %ecx
leaq 0x4c1d(%rip), %rsi      # 0x8a35
movb $0x0, %al
callq 0x30f0
leaq -0x1010(%rbp), %rdi
movq -0x1018(%rbp), %rcx
movl $0x1, %esi
movl $0x10, %edx
callq 0x31a0
movq -0x1018(%rbp), %rdi
callq 0x3080
addq $0x1050, %rsp           # imm = 0x1050
popq %rbp
retq
nopw %cs:(%rax,%rax)