// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
testl %edi, %edi
je 0x3f79
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 0x29dc(%rip), %r15      # 0x604e
leaq 0x29db(%rip), %r12      # 0x6054
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 0x3f79
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 0x3f79
movq 0x88d7(%rip), %rax      # 0xbfd8
movl (%rax), %eax
movl %eax, 0x18(%rsp)
cmpl %ebx, %eax
jge 0x3ed4
leaq 0x982a(%rip), %r13      # 0xcf40
movl 0x18(%rsp), %eax
movl %ebx, 0x4(%rsp)
movq %r14, 0x10(%rsp)
cltq
movq (%r14,%rax,8), %rdi
movq %rdi, 0xd820(%rip)      # 0x10f50
xorl %esi, %esi
xorl %eax, %eax
callq 0x3160
cmpl $-0x1, %eax
je 0x3f7e
movl %eax, %ebp
movl %eax, %edi
leaq 0x28(%rsp), %rsi
callq 0x31e0
movq 0x58(%rsp), %r15
movl $0x1, %edi
movq %r15, %rsi
callq 0x3110
movq %rax, 0x97cf(%rip)      # 0xcf38
testq %rax, %rax
je 0x3f83
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 0xb7aa(%rip), %rdi      # 0xef40
xorl %esi, %esi
callq 0x30d0
movl $0x0, 0xd7a1(%rip)      # 0x10f48
movq $0x0, 0xd78e(%rip) # 0x10f40
movl %ebp, %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3100
movl %ebp, %edi
callq 0x30e0
movq 0x976b(%rip), %r12      # 0xcf38
testl %r15d, %r15d
movl $0x0, %ecx
jle 0x37f3
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %r13d, %r13d
cmpb (%r12,%r13), %cl
jo 0x37f3
incq %r13
cmpq %r13, %rax
jne 0x37e2
movl %r15d, %r13d
cmpl %r15d, %r13d
je 0x3db4
jge 0x3817
movl %r13d, %eax
addq %r12, %rax
cmpb $0x0, (%rax)
jns 0x3817
incl %r13d
incq %rax
cmpl %r15d, %r13d
jl 0x3804
movl %r15d, %r13d
movl %r15d, %ebp
subl %r13d, %ebp
je 0x3dbe
cmpl $0x3, %ebp
jle 0x3dc8
cmpl $0x0, 0x96fd(%rip)      # 0xcf30
jne 0x3dd2
movl %r13d, %ebx
leaq (%r12,%rbx), %r10
cmpl $0x2, %ebp
movl $0x1, %r14d
cmovgel %ebp, %r14d
xorl %r13d, %r13d
cmpb (%r10,%r13), %cl
jo 0x3861
incq %r13
cmpq %r13, %r14
jne 0x3850
movl %r14d, %r13d
cmpl %ebp, %r13d
movq %r10, 0x8(%rsp)
je 0x3e12
cmpl $0x3, 0x96ba(%rip)      # 0xcf30
jl 0x38a1
movq 0x8891(%rip), %rsi      # 0xc110
movq 0xd6ca(%rip), %rdx      # 0x10f50
subl %r13d, %ebp
leaq 0x2cba(%rip), %rdi      # 0x654a
movl %ebp, %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x30c0
movq 0x8(%rsp), %r10
testb $0x3, %r13b
je 0x38dd
cmpl $0x3, 0x9682(%rip)      # 0xcf30
jl 0x3ad6
movq 0x8855(%rip), %rsi      # 0xc110
movq 0xd68e(%rip), %rdx      # 0x10f50
leaq 0x2d15(%rip), %rdi      # 0x65de
movl %r13d, %ecx
xorl %eax, %eax
callq 0x30c0
movq 0x8(%rsp), %r10
jmp 0x3ad6
testl %r13d, %r13d
je 0x3cf8
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 0x3a88
cmpb $0x3f, -0x2(%rax,%rcx)
ja 0x3a88
cmpb $0x3f, -0x1(%rax,%rcx)
ja 0x3a88
cmpb $0x40, (%rax,%rcx)
jae 0x3a88
addq $0x4, %rcx
cmpl %ecx, %r13d
jg 0x38f0
movl %r13d, %r15d
movl $0x1, %ebx
movw $0xffff, %ax            # imm = 0xFFFF
movl %eax, 0x1c(%rsp)
movl $0xc, %ebp
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %eax, (%rsp)
cmpl $0x3, 0x95dd(%rip)      # 0xcf30
jl 0x39be
movl %r13d, %eax
imulq $0x4ec4ec4f, %rax, %rax # imm = 0x4EC4EC4F
shrq $0x22, %rax
leal (%rax,%rax,2), %ecx
movq %rbx, 0x20(%rsp)
movq %r15, %rbx
movl %r14d, %r15d
leal (%rax,%rcx,4), %r14d
addl %ebp, %r14d
movzbl (%r10,%r12), %esi
leaq 0x2d2a(%rip), %rdi      # 0x66ae
xorl %eax, %eax
callq 0x30c0
movq 0x863e(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x3140
movq 0x8(%rsp), %r10
testl %r14d, %r14d
movl %r15d, %r14d
movq %rbx, %r15
movq 0x20(%rsp), %rbx
jne 0x39be
movl $0xa, %edi
callq 0x3040
movq 0x8(%rsp), %r10
leal -0x1(%rbx), %eax
cmpl $0x3, %eax
ja 0x3a72
leaq 0x2643(%rip), %rcx      # 0x6014
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl (%r10,%r12), %eax
movl $0x1, %ebx
testb $0x40, %al
je 0x3a72
movzwl %ax, %r14d
andl $0x3f, %r14d
shll $0x6, %r14d
movl $0x2, %ebx
jmp 0x3a72
movzbl (%r10,%r12), %eax
andl $0x3f, %eax
shll $0x6, %eax
movl %eax, (%rsp)
movl $0x4, %ebx
jmp 0x3a72
movzbl (%r10,%r12), %eax
andl $0x3f, %eax
movl (%rsp), %ecx
orl %eax, %ecx
movzwl %r14w, %edi
leaq 0x9514(%rip), %rax      # 0xcf40
movl %ecx, (%rsp)
movw %cx, (%rax,%rdi,2)
movl 0x1c(%rsp), %ebx
cmpw %bx, %di
jne 0x3a55
callq 0x43e6
jmp 0x3a62
movzbl (%r10,%r12), %eax
andl $0x3f, %eax
orl %eax, %r14d
movl $0x3, %ebx
jmp 0x3a72
movl $0x1, %esi
callq 0x4427
movl %r14d, %ebx
incl %ebx
movl %ebx, 0x1c(%rsp)
movl $0x1, %ebx
movq 0x8(%rsp), %r10
incq %r12
decl %ebp
incl %r13d
cmpq %r12, %r15
jne 0x394c
jmp 0x3cf8
cmpl $0x3, 0x94a1(%rip)      # 0xcf30
jl 0x3ad6
movq 0x8678(%rip), %rsi      # 0xc110
movq 0xd4b1(%rip), %rdx      # 0x10f50
movl %ecx, %eax
andl $-0x4, %eax
movzbl 0x1(%r10,%rax), %r9d
movzbl 0x2(%r10,%rax), %r10d
movq 0x8(%rsp), %rdi
movzbl 0x3(%rdi,%rax), %r11d
leaq 0x2b63(%rip), %rdi      # 0x6625
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x30c0
movq 0x18(%rsp), %r10
addq $0x10, %rsp
testb $0x1, %r13b
jne 0x3e5b
movb (%r10), %al
movl %eax, %ecx
andb $-0x40, %cl
cmpb $0x40, %cl
jne 0x3da1
cmpb $0x40, 0x1(%r10)
jae 0x3da1
cmpl $0x3, %r13d
jbe 0x3e70
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, 0x9406(%rip)      # 0xcf30
jl 0x3b4f
movq 0x85dd(%rip), %rsi      # 0xc110
movq 0xd416(%rip), %rdx      # 0x10f50
leaq 0x2d11(%rip), %rdi      # 0x6852
movl %ebp, %ecx
xorl %eax, %eax
callq 0x30c0
movq 0x8(%rsp), %r10
cmpl $0x2, %r12d
movl $0x1, %eax
cmovll %eax, %r12d
xorl %eax, %eax
xorl %r13d, %r13d
movzbl (%r10,%rax), %ecx
addl %ecx, %r13d
incq %rax
cmpq %rax, %r12
jne 0x3b61
andl $0xfff, %r13d           # imm = 0xFFF
cmpl $0x2, 0x93b1(%rip)      # 0xcf30
jl 0x3ba5
movq 0x8588(%rip), %rsi      # 0xc110
movq 0xd3c1(%rip), %rdx      # 0x10f50
leaq 0x2ce2(%rip), %rdi      # 0x6878
movl %r13d, %ecx
xorl %eax, %eax
callq 0x30c0
movq 0x8(%rsp), %r10
cmpl %r13d, %ebp
jne 0x3e77
movl $0x1, %r15d
movl $0xc, %ebp
xorl %r13d, %r13d
xorl %r14d, %r14d
movl %eax, (%rsp)
cmpl $0x3, 0x9367(%rip)      # 0xcf30
jl 0x3c34
movl %r13d, %eax
imulq $0x4ec4ec4f, %rax, %rax # imm = 0x4EC4EC4F
shrq $0x22, %rax
leal (%rax,%rax,2), %ecx
movq %r15, 0x20(%rsp)
movq %rbx, %r15
movq %r12, %rbx
leal (%rax,%rcx,4), %r12d
addl %ebp, %r12d
movzbl (%r10,%r14), %esi
leaq 0x2ab4(%rip), %rdi      # 0x66ae
xorl %eax, %eax
callq 0x30c0
movq 0x83c8(%rip), %rax      # 0xbfd0
movq (%rax), %rdi
callq 0x3140
movq 0x8(%rsp), %r10
testl %r12d, %r12d
movq %rbx, %r12
movq %r15, %rbx
movq 0x20(%rsp), %r15
jne 0x3c34
movl $0xa, %edi
callq 0x3040
movq 0x8(%rsp), %r10
leal -0x1(%r15), %eax
cmpl $0x3, %eax
ja 0x3ce7
leaq 0x23bc(%rip), %rcx      # 0x6004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl (%r10,%r14), %eax
movl $0x1, %r15d
testb $0x40, %al
je 0x3ce7
movzwl %ax, %ebx
andl $0x3f, %ebx
shll $0x6, %ebx
movl $0x2, %r15d
jmp 0x3ce7
movzbl (%r10,%r14), %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, %ebx
movl (%rsp), %eax
cmovel %ecx, %eax
movl %eax, (%rsp)
jmp 0x3ce7
movzbl (%r10,%r14), %eax
andl $0x3f, %eax
movl (%rsp), %edx
addl %eax, %edx
movzwl %bx, %eax
leaq 0x928d(%rip), %rcx      # 0xcf40
movl %edx, (%rsp)
movw %dx, (%rcx,%rax,2)
callq 0x43e6
movq 0x8(%rsp), %r10
incl %ebx
jmp 0x3ce1
movzbl (%r10,%r14), %eax
andl $0x3f, %eax
addl %eax, %ebx
movzwl %bx, %edi
xorl %esi, %esi
callq 0x4427
movq 0x8(%rsp), %r10
movl $0x3, %r15d
incq %r14
decl %ebp
incl %r13d
cmpq %r14, %r12
jne 0x3bc2
cmpl $0x2, 0x9231(%rip)      # 0xcf30
jle 0x3d0b
movl $0xa, %edi
callq 0x3040
cmpl $0x0, 0x9212(%rip)      # 0xcf24
movl 0x4(%rsp), %ebx
movq 0x10(%rsp), %r14
leaq 0x921e(%rip), %r13      # 0xcf40
jne 0x3d3a
callq 0x5824
callq 0x55e5
leaq 0x2c91(%rip), %rdi      # 0x69c6
callq 0x3090
movq 0x91f7(%rip), %rdi      # 0xcf38
callq 0x3030
movl 0x91d4(%rip), %eax      # 0xcf20
movl 0x91d6(%rip), %ecx      # 0xcf28
orl %eax, %ecx
je 0x3ebf
xorl %ecx, %ecx
movzwl (%rcx,%r13), %edx
cmpw $-0x1, %dx
jne 0x3d77
leaq 0x2(%rcx), %rsi
cmpq $0x1ffe, %rcx           # imm = 0x1FFE
movq %rsi, %rcx
jne 0x3d5c
cmpw $-0x1, %dx
je 0x3f72
testl %eax, %eax
je 0x3d8a
callq 0x3f8b
cmpl $0x0, 0x9197(%rip)      # 0xcf28
je 0x3ebf
callq 0x4167
jmp 0x3ebf
movzbl %al, %edi
movq %r10, %rsi
movq %r10, %rbx
callq 0x3200
jmp 0x3e66
callq 0x3337
jmp 0x3ea3
callq 0x3311
jmp 0x3ea3
callq 0x32eb
jmp 0x3ea3
movq 0x821f(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x832d(%rip), %rdx      # 0xc110
movq 0xd166(%rip), %rcx      # 0x10f50
subq $0x8, %rsp
leaq 0x26cf(%rip), %rsi      # 0x64c4
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
movq 0x81df(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x82ed(%rip), %rdx      # 0xc110
movq 0xd126(%rip), %rcx      # 0x10f50
leaq 0x26e8(%rip), %rsi      # 0x6519
xorl %eax, %eax
callq 0x3120
movq 0x8(%rsp), %r10
xorl %r13d, %r13d
cmpb $0x0, (%r10,%r13)
js 0x386f
incq %r13
cmpq %r13, %r14
jne 0x3e40
movl %r14d, %r13d
jmp 0x386f
movl %r13d, %edi
movq %r10, %rbx
callq 0x3232
movq %rbx, %rdi
callq 0x325a
jmp 0x3ea3
callq 0x329f
jmp 0x3ea3
movq 0x817a(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq 0x8288(%rip), %rdx      # 0xc110
movq 0xd0c1(%rip), %rcx      # 0x10f50
leaq 0x2a0a(%rip), %rsi      # 0x68a0
movl %r13d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x3120
movl 0x4(%rsp), %ebx
movq 0x10(%rsp), %r14
leaq 0x908d(%rip), %r13      # 0xcf40
movq 0x907e(%rip), %rdi      # 0xcf38
callq 0x3030
movq 0x8112(%rip), %rcx      # 0xbfd8
movl (%rcx), %eax
incl %eax
movl %eax, (%rcx)
cmpl %ebx, %eax
jl 0x3723
cmpl $0x0, 0x9051(%rip)      # 0xcf2c
je 0x3f5a
leaq 0x905c(%rip), %rbx      # 0xcf40
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
movl $0xff, %esi
callq 0x30d0
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %edx
shrl $0x6, %edx
movl %eax, %esi
andl $0xfc0, %esi            # imm = 0xFC0
orl %edx, %esi
movw %si, (%rbx)
incq %rcx
addl $0x40, %eax
addq $0x2, %rbx
cmpq $0xf80, %rcx            # imm = 0xF80
jne 0x3efa
leaq 0x21f1(%rip), %rax      # 0x6117
movq %rax, 0xd023(%rip)      # 0x10f50
cmpl $0x0, 0x8fec(%rip)      # 0xcf20
je 0x3f3b
callq 0x3f8b
cmpl $0x0, 0x8fe6(%rip)      # 0xcf28
je 0x3f60
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4167
cmpl %ebx, 0x18(%rsp)
jge 0x3f79
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x32c5
jmp 0x3f60
callq 0x4269
callq 0x33bb
movq %r15, %rdi
callq 0x335d