// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x0, -0x4(%rbp)
jne 0x331d
callq 0x4030
movl $0x0, 0xbc25(%rip)      # 0xef4c
movl $0x0, 0xbc17(%rip)      # 0xef48
movl $0x0, 0xbc05(%rip)      # 0xef40
movl $0x0, -0x18(%rbp)
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x4d6e(%rip), %rdx      # 0x80be
callq 0x3160
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x3416
movl -0x14(%rbp), %eax
addl $-0x54, %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x22, %eax
ja 0x340c
movq -0xc0(%rbp), %rax
leaq 0x4c7d(%rip), %rcx      # 0x8004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0xbbaa(%rip), %eax      # 0xef40
addl $0x1, %eax
movl %eax, 0xbba1(%rip)      # 0xef40
jmp 0x3411
movl 0xbb9d(%rip), %eax      # 0xef44
addl $0x1, %eax
movl %eax, 0xbb94(%rip)      # 0xef44
jmp 0x3411
movl 0xbb90(%rip), %eax      # 0xef48
addl $0x1, %eax
movl %eax, 0xbb87(%rip)      # 0xef48
jmp 0x3411
movl 0xbb83(%rip), %eax      # 0xef4c
addl $0x1, %eax
movl %eax, 0xbb7a(%rip)      # 0xef4c
jmp 0x3411
movl 0xbb76(%rip), %eax      # 0xef50
addl $0x1, %eax
movl %eax, 0xbb6d(%rip)      # 0xef50
cmpl $0x1, 0xbb66(%rip)      # 0xef50
jle 0x340a
movq 0xac05(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movl 0xbb54(%rip), %edx      # 0xef50
leaq 0x4cc1(%rip), %rsi      # 0x80c4
movb $0x0, %al
callq 0x30f0
jmp 0x3411
callq 0x4030
jmp 0x3342
jmp 0x3418
movq 0xabb9(%rip), %rax      # 0xdfd8
movl (%rax), %eax
cmpl -0x4(%rbp), %eax
jge 0x36a7
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0xab9a(%rip), %rcx      # 0xdfd8
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xfb24(%rip)      # 0x12f70
movq 0xfb1d(%rip), %rdi      # 0x12f70
xorl %esi, %esi
movb $0x0, %al
callq 0x3130
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x34d0
movq 0xab8c(%rip), %rax      # 0xdff8
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
movq 0xac83(%rip), %rax      # 0xe100
movq %rax, -0xd0(%rbp)
movq 0xfae5(%rip), %rax      # 0x12f70
movq %rax, -0xc8(%rbp)
callq 0x3050
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rdx
movq -0xc8(%rbp), %rcx
movl (%rax), %r8d
leaq 0x4c1f(%rip), %rsi      # 0x80d5
movb $0x0, %al
callq 0x30f0
xorl %eax, %eax
movl %eax, %edi
callq 0x3150
movl $0x1, %edi
callq 0x3190
movl -0x1c(%rbp), %edi
leaq -0xb0(%rbp), %rsi
callq 0x31c0
movq -0x80(%rbp), %rsi
movl $0x1, %edi
callq 0x30e0
movq %rax, 0xba64(%rip)      # 0xef58
cmpq $0x0, 0xba5c(%rip)      # 0xef58
jne 0x3592
movq 0xaaef(%rip), %rax      # 0xdff8
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movq 0xabe6(%rip), %rax      # 0xe100
movq %rax, -0xf8(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq 0xfa3d(%rip), %rax      # 0x12f70
movq %rax, -0xe8(%rbp)
callq 0x3050
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
callq 0x3050
movl (%rax), %edi
callq 0x31b0
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rdx
movq -0xf0(%rbp), %rcx
movq -0xe8(%rbp), %r8
movl -0xdc(%rbp), %r9d
leaq 0x4b86(%rip), %rsi      # 0x8103
movq %rax, (%rsp)
movb $0x0, %al
callq 0x30f0
movl $0x1, %edi
callq 0x3190
leaq 0xb9c7(%rip), %rdi      # 0xef60
movl $0xff, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x30b0
leaq 0xd9b1(%rip), %rdi      # 0x10f60
xorl %esi, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x30b0
movl $0x0, 0xf9a3(%rip)      # 0x12f68
movq $0x0, 0xf990(%rip) # 0x12f60
movl -0x1c(%rbp), %edi
movq 0xb97e(%rip), %rsi      # 0xef58
movq -0x80(%rbp), %rdx
callq 0x30d0
movl -0x1c(%rbp), %edi
callq 0x30c0
movq 0xb966(%rip), %rdi      # 0xef58
movq -0x80(%rbp), %rax
movl %eax, %esi
callq 0x3750
movl %eax, -0xb4(%rbp)
movq 0xb94e(%rip), %rdi      # 0xef58
callq 0x3030
cmpl $0x0, -0xb4(%rbp)
jne 0x361a
jmp 0x368d
cmpl $0x0, 0xb91f(%rip)      # 0xef40
jne 0x362c
cmpl $0x0, 0xb91e(%rip)      # 0xef48
je 0x368b
leaq 0xb92d(%rip), %rdi      # 0xef60
movl $0x1000, %esi           # imm = 0x1000
callq 0x3aa0
cmpl $0x0, %eax
je 0x366d
movq 0xa9af(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0xaaad(%rip), %rdx      # 0xe100
movq 0xf916(%rip), %rcx      # 0x12f70
leaq 0x4ae8(%rip), %rsi      # 0x8149
movb $0x0, %al
callq 0x30f0
jmp 0x3746
cmpl $0x0, 0xb8cc(%rip)      # 0xef40
je 0x367b
callq 0x3af0
cmpl $0x0, 0xb8c6(%rip)      # 0xef48
je 0x3689
callq 0x3e60
jmp 0x368b
jmp 0x368d
movq 0xa944(%rip), %rax      # 0xdfd8
movl (%rax), %ecx
addl $0x1, %ecx
movq 0xa938(%rip), %rax      # 0xdfd8
movl %ecx, (%rax)
jmp 0x3418
cmpl $0x0, 0xb89e(%rip)      # 0xef4c
je 0x3739
leaq 0xb8a5(%rip), %rdi      # 0xef60
movl $0xff, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x30b0
movl $0x0, -0x20(%rbp)
cmpl $0xf80, -0x20(%rbp)     # imm = 0xF80
jge 0x370d
movl -0x20(%rbp), %eax
andl $0xfc0, %eax            # imm = 0xFC0
sarl $0x6, %eax
movl -0x20(%rbp), %ecx
andl $0x3f, %ecx
shll $0x6, %ecx
addl %ecx, %eax
movw %ax, %dx
movslq -0x20(%rbp), %rcx
leaq 0xb862(%rip), %rax      # 0xef60
movw %dx, (%rax,%rcx,2)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x36d1
leaq 0x4a73(%rip), %rax      # 0x8187
movq %rax, 0xf855(%rip)      # 0x12f70
cmpl $0x0, 0xb81e(%rip)      # 0xef40
je 0x3729
callq 0x3af0
cmpl $0x0, 0xb818(%rip)      # 0xef48
je 0x3737
callq 0x3e60
jmp 0x3746
cmpl $0x0, -0x18(%rbp)
jne 0x3744
callq 0x4030
jmp 0x3746
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop