// File: /workspace/github_c_makefile_star2_O3/AK6DN[P]dec-utilities-for-pdp/d8tape/main.c
// Function: optproc
static void
optproc (int argc, char **argv)
{
    int     opt;
    int     got_any;
    int     fd;
    int     i;
    struct  stat statbuf;
    int     sts;

    if (!argc) {
        usageError ();
    }

    // clear out option values to defaults
    optb = optr = optT = 0;

    // handle command line options
    got_any = 0;
    while ((opt = getopt (argc, argv, "bdrTv")) != -1) {
        switch (opt) {
        case    'b':
            optb++;
            break;
        case    'd':
            optd++;
            break;
        case    'r':
            optr++;
            break;
        case    'T':
            optT++;
            break;
        case    'v':
            optv++;
            if (optv > 1) {
                fprintf (stderr, "Verbosity is %d\n", optv);
            }
            break;
        default:
            usageError ();
            break;
        }
    }

    // handle command line arguments
    for (; optind < argc; optind++) {
        got_any++;
        tapename = argv [optind];               // snap tapename to global

        // open the tape
        fd = open (tapename, O_RDONLY);
        if (fd == -1) {
            fprintf (stderr, "%s:  couldn't open %s for O_RDONLY, errno %d\n", progname, tapename, errno);
            perror (NULL);
            exit (EXIT_FAILURE);
        }
        fstat (fd, &statbuf);                   // get the size, so we can read it into memory

        tape = calloc (1, statbuf.st_size);
        if (tape == NULL) {
            fprintf (stderr, "%s:  can't allocate %ld bytes during processing of %s, errno %d (%s)\n", progname, (unsigned long)statbuf.st_size, tapename, errno, strerror (errno));
            exit (EXIT_FAILURE);
        }

        // initialize data areas
        memset (core, 0xff, sizeof (core));     // set to -1 -- since we are only using 12 bits of each 16 bit word, -1 isn't a valid PDP-8 core value
        memset (tags, 0, sizeof (tags));        // reset tags
        nsegments = 0;
        segments = NULL;

        read (fd, tape, statbuf.st_size);
        close (fd);

        // dump the tape (this also reads the tape into "core" and disassembles)
        sts = dumptape (tape, statbuf.st_size);

        free (tape);

        if (!sts) {
            continue;                           // skip tape
        }

        // convert to RIM/BIN if required (-b and/or -r)
        if (optb || optr) {
            // see if there is any data there at all..
            if (blank (core, CORE_SIZE)) {
                fprintf (stderr, "%s:  tape image from %s is empty, not creating a BIN version\n", progname, tapename);
                return;
            }
            if (optb) {
                writebin ();
            }
            if (optr) {
                writerim ();
            }
        }
    }

    // if no arguments given, dump usage message
    if (optT) {
        memset (core, 0xff, sizeof (core));     // set to -1 (assumption; all 0xff's in an int is -1; pretty safe)
        for (i = 0; i < 07600; i++) {
            core [i] = ((i & 07700) >> 6) + ((i & 00077) << 6);
        }
        tapename = "test";
        if (optb) {
            writebin ();
        }
        if (optr) {
            writerim ();
        }
    } else {
        if (!got_any) {
            usageError ();
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
testl %edi, %edi
je 0x3ff9
movq %rsi, %r14
movl %edi, %ebx
xorl %eax, %eax
movl %eax, 0x98cd(%rip)      # 0xcf2c
movl %eax, 0x98c3(%rip)      # 0xcf28
movl %eax, 0x98b5(%rip)      # 0xcf20
leaq 0x2a1c(%rip), %r15      # 0x608e
leaq 0x2a1b(%rip), %r12      # 0x6094
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3190
cmpl $0x63, %eax
jle 0x36c7
cmpl $0x64, %eax
je 0x36e9
cmpl $0x72, %eax
je 0x36e1
cmpl $0x76, %eax
jne 0x3ff9
movl 0x988c(%rip), %eax      # 0xcf30
leal 0x1(%rax), %edx
movl %edx, 0x9883(%rip)      # 0xcf30
testl %eax, %eax
jle 0x3679
movq 0x8940(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x3120
jmp 0x3679
cmpl $0x54, %eax
je 0x36d9
cmpl $0x62, %eax
jne 0x36f1
incl 0x9849(%rip)            # 0xcf20
jmp 0x3679
incl 0x984d(%rip)            # 0xcf2c
jmp 0x3679
incl 0x9841(%rip)            # 0xcf28
jmp 0x3679
incl 0x9835(%rip)            # 0xcf24
jmp 0x3679
cmpl $-0x1, %eax
jne 0x3ff9
movq 0x88d7(%rip), %rax      # 0xbfd8
movl (%rax), %eax
cmpl %ebx, %eax
jge 0x3edc
leaq 0x982e(%rip), %r13      # 0xcf40
movl %eax, 0x34(%rsp)
movl %ebx, 0xc(%rsp)
movq %r14, 0x18(%rsp)
cltq
movq (%r14,%rax,8), %rdi
movq %rdi, 0xd824(%rip)      # 0x10f50
xorl %esi, %esi
xorl %eax, %eax
callq 0x3160
cmpl $-0x1, %eax
je 0x3ffe
movl %eax, %ebp
movl %eax, %edi
leaq 0x38(%rsp), %rsi
callq 0x31e0
movq 0x68(%rsp), %r15
movl $0x1, %edi
movq %r15, %rsi
callq 0x3110
movq %rax, 0x97d3(%rip)      # 0xcf38
testq %rax, %rax
je 0x4003
movq %rax, %r12
movl $0x2000, %edx           # imm = 0x2000
movq %r13, %rdi
movl $0xff, %esi
callq 0x30d0
xorl %r13d, %r13d
movl $0x2000, %edx           # imm = 0x2000
leaq 0xb7ae(%rip), %rdi      # 0xef40
xorl %esi, %esi
callq 0x30d0
movl $0x0, 0xd7a5(%rip)      # 0x10f48
movq $0x0, 0xd792(%rip) # 0x10f40
movl %ebp, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3100
movl %ebp, %edi
callq 0x30e0
movq 0x976f(%rip), %r12      # 0xcf38
testl %r15d, %r15d
movl $0x0, %ecx
jle 0x37f1
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %r13d, %r13d
cmpb (%r12,%r13), %cl
jo 0x37f1
incq %r13
cmpq %r13, %rax
jne 0x37de
jmp 0x3d94
cmpl %r15d, %r13d
je 0x3d94
jge 0x3817
movl %r13d, %eax
addq %r12, %rax
cmpb $0x0, (%rax)
jns 0x3817
incl %r13d
incq %rax
cmpl %r15d, %r13d
jl 0x3802
jmp 0x3de7
movl %r15d, %ebp
subl %r13d, %ebp
je 0x3de7
cmpl $0x3, %ebp
jle 0x3e01
cmpl $0x0, 0x96fd(%rip)      # 0xcf30
jne 0x3e0b
movl %r13d, %ebx
leaq (%r12,%rbx), %r10
movl %ebp, %r14d
xorl %r13d, %r13d
movq %r10, 0x10(%rsp)
cmpb (%r10,%r13), %cl
jo 0x385e
incq %r13
cmpq %r13, %r14
jne 0x384b
jmp 0x3d9e
cmpl %r13d, %r14d
je 0x3d9e
cmpl $0x3, 0x96c2(%rip)      # 0xcf30
jl 0x3899
movq 0x8899(%rip), %rsi      # 0xc110
movq 0xd6d2(%rip), %rdx      # 0x10f50
subl %r13d, %ebp
leaq 0x2d02(%rip), %rdi      # 0x658a
movl %ebp, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x30c0
movq 0x10(%rsp), %r10
testb $0x3, %r13b
je 0x38d5
cmpl $0x3, 0x968a(%rip)      # 0xcf30
jl 0x3ad2
movq 0x885d(%rip), %rsi      # 0xc110
movq 0xd696(%rip), %rdx      # 0x10f50
leaq 0x2d5d(%rip), %rdi      # 0x661e
movl %r13d, %ecx
xorl %eax, %eax
callq 0x30c0
movq 0x10(%rsp), %r10
jmp 0x3ad2
testl %r13d, %r13d
je 0x3cf3
leaq (%r12,%rbx), %rax
addq $0x3, %rax
xorl %ecx, %ecx
movzbl -0x3(%rax,%rcx), %r8d
movl %r8d, %edx
andb $-0x40, %dl
cmpb $0x40, %dl
jne 0x3a84
cmpb $0x3f, -0x2(%rax,%rcx)
ja 0x3a84
cmpb $0x3f, -0x1(%rax,%rcx)
ja 0x3a84
cmpb $0x40, (%rax,%rcx)
jae 0x3a84
addq $0x4, %rcx
cmpl %ecx, %r13d
jg 0x38e8
movl %r13d, %r15d
movl $0x1, %ebx
movw $0xffff, %ax            # imm = 0xFFFF
movl %eax, 0x24(%rsp)
movl $0xc, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %eax, 0x8(%rsp)
cmpl $0x3, 0x95e4(%rip)      # 0xcf30
jl 0x39b7
movl %r13d, %eax
imulq $0x4ec4ec4f, %rax, %rax # imm = 0x4EC4EC4F
shrq $0x22, %rax
leal (%rax,%rax,2), %ecx
movq %rbx, 0x28(%rsp)
movq %r15, %rbx
movl %r14d, %r15d
leal (%rax,%rcx,4), %r14d
addl %ebp, %r14d
movzbl (%r10,%r12), %esi
leaq 0x2d71(%rip), %rdi      # 0x66ee
xorl %eax, %eax
callq 0x30c0
movq 0x8645(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x3140
movq 0x10(%rsp), %r10
testl %r14d, %r14d
movl %r15d, %r14d
movq %rbx, %r15
movq 0x28(%rsp), %rbx
jne 0x39b7
movl $0xa, %edi
callq 0x3040
movq 0x10(%rsp), %r10
leal -0x1(%rbx), %eax
cmpl $0x3, %eax
ja 0x3a6e
leaq 0x26a6(%rip), %rcx      # 0x6070
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl (%r10,%r12), %eax
movl $0x1, %ebx
testb $0x40, %al
je 0x3a6e
movzwl %ax, %r14d
andl $0x3f, %r14d
shll $0x6, %r14d
movl $0x2, %ebx
jmp 0x3a6e
movzbl (%r10,%r12), %eax
andl $0x3f, %eax
shll $0x6, %eax
movl %eax, 0x8(%rsp)
movl $0x4, %ebx
jmp 0x3a6e
movzbl (%r10,%r12), %eax
andl $0x3f, %eax
movl 0x8(%rsp), %ecx
orl %eax, %ecx
movzwl %r14w, %edi
leaq 0x9519(%rip), %rax      # 0xcf40
movl %ecx, 0x8(%rsp)
movw %cx, (%rax,%rdi,2)
movl 0x24(%rsp), %ebx
cmpw %bx, %di
jne 0x3a51
callq 0x4466
jmp 0x3a5e
movzbl (%r10,%r12), %eax
andl $0x3f, %eax
orl %eax, %r14d
movl $0x3, %ebx
jmp 0x3a6e
movl $0x1, %esi
callq 0x44a7
movl %r14d, %ebx
incl %ebx
movl %ebx, 0x24(%rsp)
movl $0x1, %ebx
movq 0x10(%rsp), %r10
incq %r12
decl %ebp
incl %r13d
cmpq %r12, %r15
jne 0x3945
jmp 0x3cf3
cmpl $0x3, 0x94a5(%rip)      # 0xcf30
jl 0x3ad2
movq 0x867c(%rip), %rsi      # 0xc110
movq 0xd4b5(%rip), %rdx      # 0x10f50
movl %ecx, %eax
andl $-0x4, %eax
movzbl 0x1(%r10,%rax), %r9d
movzbl 0x2(%r10,%rax), %r10d
movq 0x10(%rsp), %rdi
movzbl 0x3(%rdi,%rax), %r11d
leaq 0x2ba7(%rip), %rdi      # 0x6665
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x30c0
movq 0x20(%rsp), %r10
addq $0x10, %rsp
testb $0x1, %r13b
jne 0x3e4b
movb (%r10), %al
movl %eax, %ecx
andb $-0x40, %cl
cmpb $0x40, %cl
jne 0x3df1
cmpb $0x40, 0x1(%r10)
jae 0x3df1
cmpl $0x3, %r13d
jbe 0x3e60
leal -0x2(%r13), %r12d
movzbl (%r10,%r12), %eax
andl $0x3f, %eax
shll $0x6, %eax
movl %r13d, %ecx
movzbl -0x1(%r10,%rcx), %ebp
andl $0x3f, %ebp
orl %eax, %ebp
cmpl $0x2, 0x940a(%rip)      # 0xcf30
jl 0x3b4b
movq 0x85e1(%rip), %rsi      # 0xc110
movq 0xd41a(%rip), %rdx      # 0x10f50
leaq 0x2d55(%rip), %rdi      # 0x6892
movl %ebp, %ecx
xorl %eax, %eax
callq 0x30c0
movq 0x10(%rsp), %r10
xorl %eax, %eax
xorl %r13d, %r13d
movzbl (%r10,%rax), %ecx
addl %ecx, %r13d
incq %rax
cmpq %rax, %r12
jne 0x3b50
andl $0xfff, %r13d           # imm = 0xFFF
cmpl $0x2, 0x93c2(%rip)      # 0xcf30
jl 0x3b94
movq 0x8599(%rip), %rsi      # 0xc110
movq 0xd3d2(%rip), %rdx      # 0x10f50
leaq 0x2d33(%rip), %rdi      # 0x68b8
movl %r13d, %ecx
xorl %eax, %eax
callq 0x30c0
movq 0x10(%rsp), %r10
cmpl %r13d, %ebp
jne 0x3e67
movl $0x1, %r15d
movl $0xc, %ebp
xorl %r13d, %r13d
xorl %ebx, %ebx
movl %eax, 0x8(%rsp)
cmpl $0x3, 0x9378(%rip)      # 0xcf30
jl 0x3c23
movl %r13d, %eax
imulq $0x4ec4ec4f, %rax, %rax # imm = 0x4EC4EC4F
shrq $0x22, %rax
leal (%rax,%rax,2), %ecx
movq %r15, 0x28(%rsp)
movq %r14, %r15
movq %r12, %r14
leal (%rax,%rcx,4), %r12d
addl %ebp, %r12d
movzbl (%r10,%rbx), %esi
leaq 0x2b05(%rip), %rdi      # 0x66ee
xorl %eax, %eax
callq 0x30c0
movq 0x83d9(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x3140
movq 0x10(%rsp), %r10
testl %r12d, %r12d
movq %r14, %r12
movq %r15, %r14
movq 0x28(%rsp), %r15
jne 0x3c23
movl $0xa, %edi
callq 0x3040
movq 0x10(%rsp), %r10
leal -0x1(%r15), %eax
cmpl $0x3, %eax
ja 0x3ce2
leaq 0x2429(%rip), %rcx      # 0x6060
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl (%r10,%rbx), %eax
movl $0x1, %r15d
testb $0x40, %al
je 0x3ce2
movzwl %ax, %r14d
andl $0x3f, %r14d
shll $0x6, %r14d
movl $0x2, %r15d
jmp 0x3ce2
movzbl (%r10,%rbx), %eax
movl %eax, %ecx
andl $0x3f, %ecx
shll $0x6, %ecx
xorl %edx, %edx
testb $0x40, %al
sete %dl
leal 0x2(%rdx,%rdx), %r15d
cmovnel %ecx, %r14d
movl 0x8(%rsp), %eax
cmovel %ecx, %eax
movl %eax, 0x8(%rsp)
jmp 0x3ce2
movzbl (%r10,%rbx), %eax
andl $0x3f, %eax
movl 0x8(%rsp), %edx
addl %eax, %edx
movzwl %r14w, %eax
leaq 0x9296(%rip), %rcx      # 0xcf40
movl %edx, 0x8(%rsp)
movw %dx, (%rcx,%rax,2)
callq 0x4466
movq 0x10(%rsp), %r10
incl %r14d
jmp 0x3cdc
movzbl (%r10,%rbx), %eax
andl $0x3f, %eax
addl %eax, %r14d
movzwl %r14w, %edi
xorl %esi, %esi
callq 0x44a7
movq 0x10(%rsp), %r10
movl $0x3, %r15d
incq %rbx
decl %ebp
incl %r13d
cmpq %rbx, %r12
jne 0x3bb1
cmpl $0x2, 0x9236(%rip)      # 0xcf30
jle 0x3d06
movl $0xa, %edi
callq 0x3040
cmpl $0x0, 0x9217(%rip)      # 0xcf24
movl 0xc(%rsp), %ebx
movq 0x18(%rsp), %r14
leaq 0x9223(%rip), %r13      # 0xcf40
jne 0x3d35
callq 0x58d0
callq 0x5693
leaq 0x2cd6(%rip), %rdi      # 0x6a06
callq 0x3090
movq 0x91fc(%rip), %rdi      # 0xcf38
callq 0x3030
movl 0x91d9(%rip), %ecx      # 0xcf20
movl 0x91db(%rip), %eax      # 0xcf28
movl %ecx, %edx
orl %eax, %edx
je 0x3eaf
xorl %edx, %edx
cmpw $-0x1, (%rdx,%r13)
jne 0x3d73
addq $0x2, %rdx
cmpq $0x2000, %rdx           # imm = 0x2000
jne 0x3d59
jmp 0x3fe2
testl %ecx, %ecx
je 0x3d82
callq 0x400b
movl 0x91a6(%rip), %eax      # 0xcf28
testl %eax, %eax
je 0x3eaf
callq 0x41e7
jmp 0x3eaf
callq 0x3337
jmp 0x3e93
movq 0x8253(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x8361(%rip), %rdx      # 0xc110
movq 0xd19a(%rip), %rcx      # 0x10f50
leaq 0x279c(%rip), %rsi      # 0x6559
xorl %eax, %eax
callq 0x3120
movq 0x10(%rsp), %r10
xorl %r13d, %r13d
cmpb $0x0, (%r10,%r13)
js 0x3867
incq %r13
cmpq %r13, %r14
jne 0x3dcc
movl %ebp, %r13d
jmp 0x3867
callq 0x3311
jmp 0x3e93
movzbl %al, %edi
movq %r10, %rsi
movq %r10, %rbx
callq 0x3200
jmp 0x3e56
callq 0x32eb
jmp 0x3e93
movq 0x81e6(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x82f4(%rip), %rdx      # 0xc110
movq 0xd12d(%rip), %rcx      # 0x10f50
subq $0x8, %rsp
leaq 0x26d6(%rip), %rsi      # 0x6504
movl %r13d, %r8d
movl %r13d, %r9d
xorl %eax, %eax
pushq %rbp
pushq %r15
pushq %r13
callq 0x3120
xorl %ecx, %ecx
addq $0x20, %rsp
jmp 0x3839
movl %r13d, %edi
movq %r10, %rbx
callq 0x3232
movq %rbx, %rdi
callq 0x325a
jmp 0x3e93
callq 0x329f
jmp 0x3e93
movq 0x818a(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x8298(%rip), %rdx      # 0xc110
movq 0xd0d1(%rip), %rcx      # 0x10f50
leaq 0x2a5a(%rip), %rsi      # 0x68e0
movl %r13d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x3120
movl 0xc(%rsp), %ebx
movq 0x18(%rsp), %r14
leaq 0x909d(%rip), %r13      # 0xcf40
movq 0x908e(%rip), %rdi      # 0xcf38
callq 0x3030
movq 0x8122(%rip), %rcx      # 0xbfd8
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
cmpl %ebx, %eax
jl 0x371f
cmpl $0x0, 0x9061(%rip)      # 0xcf2c
jne 0x3ee9
cmpl %ebx, 0x34(%rsp)
jl 0x3fe7
jmp 0x3ff9
cmpl $0x0, 0x9049(%rip)      # 0xcf2c
je 0x3ff9
leaq 0x9050(%rip), %rbx      # 0xcf40
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
movl $0xff, %esi
callq 0x30d0
movdqa 0x2106(%rip), %xmm0     # 0x6010
movdqa 0x210e(%rip), %xmm1     # 0x6020
xorl %eax, %eax
movdqa 0x2114(%rip), %xmm2     # 0x6030
movdqa 0x211c(%rip), %xmm3     # 0x6040
movdqa %xmm1, %xmm4
movdqa %xmm0, %xmm5
movdqa %xmm1, %xmm6
psrld $0x6, %xmm6
movdqa %xmm0, %xmm7
psrld $0x6, %xmm7
movdqa %xmm5, %xmm8
pslld $0x6, %xmm8
movdqa %xmm4, %xmm9
pslld $0x6, %xmm9
pand %xmm2, %xmm9
por %xmm6, %xmm9
pand %xmm2, %xmm8
por %xmm7, %xmm8
pslld $0x10, %xmm9
psrad $0x10, %xmm9
pslld $0x10, %xmm8
psrad $0x10, %xmm8
packssdw %xmm8, %xmm9
movdqa %xmm9, (%rax,%rbx)
paddd %xmm3, %xmm1
paddd %xmm3, %xmm0
paddd %xmm3, %xmm4
paddd %xmm3, %xmm5
addq $0x10, %rax
cmpq $0x1f00, %rax           # imm = 0x1F00
jne 0x3f2c
leaq 0x21a9(%rip), %rax      # 0x6157
movq %rax, 0xcf9b(%rip)      # 0x10f50
cmpl $0x0, 0x8f64(%rip)      # 0xcf20
je 0x3fc3
callq 0x400b
cmpl $0x0, 0x8f5e(%rip)      # 0xcf28
je 0x3fe7
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41e7
callq 0x32c5
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x42e9
callq 0x33bb
movq %r15, %rdi
callq 0x335d