// File: /workspace/github_c_makefile_star2_O0/AK6DN[P]dec-utilities-for-pdp/d8tape/main.c
// Function: dumpbin
static int
dumpbin (unsigned char *t, int n)
{
    int     tape_checksum;                  // checksum stored on tape
    int     calc_checksum;                  // calculated checksum
    int     i;
    int     state;
    unsigned short int addr, data;

    if (n < 4) {
        fprintf (stderr, "%s:  tape %s is too short; tape skipped\n", progname, tapename);
        return (0);
    }

    tape_checksum = ((t [n - 2] & 0x3f) << 6) + (t [n - 1] & 0x3f);
    if (optv > 1) {
        printf ("%s:  tape %s expected checksum 0%04o\n", progname, tapename, tape_checksum);
    }
    n -= 2;                                 // tape is now shorter by the two bytes

    // now calculate checksum
    calc_checksum = 0;
    for (i = 0; i < n; i++) {
        calc_checksum += t [i];
    }
    calc_checksum &= 07777;                 // mask to 12 bits
    if (optv > 1) {
        printf ("%s:  tape %s calculated checksum 0%04o\n", progname, tapename, calc_checksum);
    }

    if (tape_checksum != calc_checksum) {
        fprintf (stderr, "%s:  tape %s calculated checksum [0%04o] != stored checksum [0%04o]; tape skipped\n", progname, tapename, calc_checksum, tape_checksum);
        return (0);
    }

    // now we can dump the binary data via the state machine
    state = BIN_Initial;
    for (i = 0; i < n; i++) {
        if (optv > 2) {
            printf ("[%03o] ", t [i]); fflush (stdout);
            if ((i % 13) == 12) {
                printf ("\n");
            }
        }
        switch (state) {
        case    BIN_Initial:
            if (t [i] & 0100) {             // indicates origin setting code
                addr = (t [i] & 0077) << 6; // store top part
                state = BIN_Origin;
            }
            break;
        case    BIN_Origin:
            addr += (t [i] & 0077);         // store bottom part
            state = BIN_DataHW;
            seg_add (addr, 0);
            break;
        case    BIN_DataHW:
            if (t [i] & 0100) {             // another origin; skip loading data and load address instead
                addr = (t [i] & 0077) << 6;
                state = BIN_Origin;
            } else {
                data = (t [i] & 0077) << 6; // store top part of data
                state = BIN_DataLW;
            }
            break;
        case    BIN_DataLW:
            data += (t [i] & 0077);
            core [addr] = data;
            seg_more (1);
            addr++;                         // the magic of BIN-format is the autoincrement of the address
            state = BIN_DataHW;
        }
    }
    if (optv > 2) {
        printf ("\n");
    }
    return (1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x4627
movq 0x99fc(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0x9afa(%rip), %rdx      # 0xe100
movq 0xe963(%rip), %rcx      # 0x12f70
leaq 0x4289(%rip), %rsi      # 0x889d
movb $0x0, %al
callq 0x30f0
movl $0x0, -0x4(%rbp)
jmp 0x48f5
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
andl $0x3f, %eax
shll $0x6, %eax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
andl $0x3f, %ecx
addl %ecx, %eax
movl %eax, -0x18(%rbp)
cmpl $0x1, 0xa8f2(%rip)      # 0xef50
jle 0x467f
movq 0x9a99(%rip), %rsi      # 0xe100
movq 0xe902(%rip), %rdx      # 0x12f70
movl -0x18(%rbp), %ecx
leaq 0x424e(%rip), %rdi      # 0x88c6
movb $0x0, %al
callq 0x30a0
movl -0x14(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x46bb
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x4696
movl -0x1c(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %eax, -0x1c(%rbp)
cmpl $0x1, 0xa883(%rip)      # 0xef50
jle 0x46ee
movq 0x9a2a(%rip), %rsi      # 0xe100
movq 0xe893(%rip), %rdx      # 0x12f70
movl -0x1c(%rbp), %ecx
leaq 0x4205(%rip), %rdi      # 0x88ec
movb $0x0, %al
callq 0x30a0
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
je 0x4730
movq 0x98fb(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0x99f9(%rip), %rdx      # 0xe100
movq 0xe862(%rip), %rcx      # 0x12f70
movl -0x1c(%rbp), %r8d
movl -0x18(%rbp), %r9d
leaq 0x41f7(%rip), %rsi      # 0x8914
movb $0x0, %al
callq 0x30f0
movl $0x0, -0x4(%rbp)
jmp 0x48f5
movl $0x1, -0x24(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x48d7
cmpl $0x2, 0xa7ff(%rip)      # 0xef50
jle 0x479c
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x3fbc(%rip), %rdi      # 0x8722
movb $0x0, %al
callq 0x30a0
movq 0x985c(%rip), %rax      # 0xdfd0
movq (%rax), %rdi
callq 0x3110
movl -0x20(%rbp), %eax
movl $0xd, %ecx
cltd
idivl %ecx
cmpl $0xc, %edx
jne 0x479a
leaq 0x3a30(%rip), %rdi      # 0x81c3
movb $0x0, %al
callq 0x30a0
jmp 0x479c
movl -0x24(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x3, %eax
ja 0x48c7
movq -0x30(%rbp), %rax
leaq 0x38e5(%rip), %rcx      # 0x80a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x47f5
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x3f, %eax
shll $0x6, %eax
movw %ax, -0x26(%rbp)
movl $0x2, -0x24(%rbp)
jmp 0x48c7
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
andl $0x3f, %ecx
movzwl -0x26(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x26(%rbp)
movl $0x3, -0x24(%rbp)
xorl %esi, %esi
movzwl -0x26(%rbp), %edi
callq 0x4990
jmp 0x48c7
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x485d
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x3f, %eax
shll $0x6, %eax
movw %ax, -0x26(%rbp)
movl $0x2, -0x24(%rbp)
jmp 0x487a
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x3f, %eax
shll $0x6, %eax
movw %ax, -0x28(%rbp)
movl $0x4, -0x24(%rbp)
jmp 0x48c7
movq -0x10(%rbp), %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
andl $0x3f, %ecx
movzwl -0x28(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x28(%rbp)
movw -0x28(%rbp), %dx
movzwl -0x26(%rbp), %eax
movl %eax, %ecx
leaq 0xa6ba(%rip), %rax      # 0xef60
movw %dx, (%rax,%rcx,2)
movl $0x1, %edi
callq 0x4900
movw -0x26(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x26(%rbp)
movl $0x3, -0x24(%rbp)
jmp 0x48c9
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x473e
cmpl $0x2, 0xa672(%rip)      # 0xef50
jle 0x48ee
leaq 0x38dc(%rip), %rdi      # 0x81c3
movb $0x0, %al
callq 0x30a0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop