// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/extracters/dbtap/dbtap.c
// Function: main
int main(
  int argc,
  char *argv[]
)
{
  int ch;
  uint8 myprog, myproj;

  while ((ch = getopt(argc, argv, "lcaer:A:EP:S")) != -1) {
    switch (ch) {
      case 'l':
        list = 1;
        break;

      case 'c':
        create = 1;
        break;

      case 'a':
        append = 1;
        break;

      case 'e':
        extract = 1;
        break;

      case 'r':
        reclen = strtoul(optarg, NULL, 0);
        if (reclen < 512)
          reclen = 512;
        if (reclen > MAXRCLNT)
          reclen = MAXRCLNT;
        break;

      case 'A':
        ascii = optarg;
        break;

      case 'E':
        extra = 1;
        break;

      case 'P':
        if (sscanf(optarg, "%hho,%hho", &myprog, &myproj) != 2)
          usage();

        prog = myprog;
        proj = myproj;
        break;

      case 'S':
        strict = 1;
        break;

      case '?':
      default:
        usage();
    }
  }
  argc -= optind;
  argv += optind;

  /*
   * Only one of -l, -c, -a, -e can be specified.
   */
  if ((list + create + append + extract) != 1)
    usage();

  if (list != 0) {
    /*
     * List directories on one or more container files.
     */
    if (argc == 0)
      usage();

    while (argc >= 1) {
      switch (OpenTapeForRead(argv[0])) {
        case TIO_SUCCESS:
        case TIO_ERROR:
          printf("%s:\n\n", argv[0]);
          listDirectory();
          printf("\n");
          CloseTape();
          break;

        case TIO_CORRUPT:
          fprintf(stderr, "%s is not a SIMH .tap container file\n", argv[0]);
          exit(2);

        case TIO_OPENFAIL:
          fprintf(stderr, "%s open failed\n", argv[0]);
          exit(3);
      }
      argc--, argv++;
    }
  } else if (create != 0) {
    /*
     * Create a new container file and append 0 or more files to the tape.
     */
    if (argc <= 1)
      usage();

    switch (OpenTapeForWrite(argv[0])) {
      case TIO_SUCCESS:
        argc--, argv++;

        while (argc >= 1) {
          switch (appendFile(argv[0], ascii, prog, proj, reclen, strict)) {
            case -1:
              fprintf(stderr, "Failed to append %s to tape\n", argv[0]);
              fprintf(stderr, "Container file is probably corrupt\n");
              exit(6);

            case 1:
              fprintf(stderr, "Failed to open file %s - skipping\n", argv[0]);
              /* FALLTHROUGH */

            case 0:
              break;
          }
          argc--, argv++;
        }
        CloseTape();
        break;

      case TIO_IOERROR:
        fprintf(stderr, "Error writing to container file - %s\n", argv[0]);
        exit(5);

      case TIO_CREATEFAIL:
        fprintf(stderr, "Failed to create container file - %s\n", argv[0]);
        exit(3);
    }
  } else if (append != 0) {
    /*
     * Open an existing container file and append 0 or more files to the tape.
     */
    if (argc <= 1)
      usage();

    switch (OpenTapeForAppend(argv[0])) {
      case TIO_SUCCESS:
      case TIO_ERROR:
        argc--, argv++;

        while (argc >= 1) {
          switch (appendFile(argv[0], ascii, prog, proj, reclen, strict)) {
            case -1:
              fprintf(stderr, "Failed to append %s to tape\n", argv[0]);
              fprintf(stderr, "Container file is probably corrupt\n");
              exit(6);

            case 1:
              fprintf(stderr, "Failed to open file %s - skipping\n", argv[0]);
              /* FALLTHROUGH */

            case 0:
              break;
          }
          argc--, argv++;
        }
        CloseTape();
        break;

      case TIO_CORRUPT:
        fprintf(stderr, "%s is not a SIMH .tap container file\n", argv[0]);
        exit(2);

      case TIO_OPENFAIL:
        fprintf(stderr, "%s open failed\n", argv[0]);
        exit(3);
    }
  } else if (extract != 0) {
    /*
     * Extract files from one or more container files.
     */
    if (argc == 0)
      usage();

    while (argc >= 1) {
      switch (OpenTapeForRead(argv[0])) {
        case TIO_SUCCESS:
        case TIO_ERROR:
          printf("%s:\n\n", argv[0]);
          extractFiles(ascii, extra);
          printf("\n");
          CloseTape();
          break;

        case TIO_CORRUPT:
          fprintf(stderr, "%s is not a SIMH .tap container file\n", argv[0]);
          exit(2);

        case TIO_OPENFAIL:
          fprintf(stderr, "%s open failed\n", argv[0]);
          exit(3);
      }
      argc--, argv++;
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movl %edi, %ebx
leaq 0x1c19(%rip), %r14      # 0x3390
movq 0x385a(%rip), %r12      # 0x4fd8
leaq 0x7(%rsp), %r13
leaq 0x6(%rsp), %rbp
movl %ebx, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x11a0
cmpl $0x60, %eax
jle 0x17c2
cmpl $0x64, %eax
jle 0x17e5
cmpl $0x65, %eax
je 0x1897
cmpl $0x72, %eax
je 0x181c
cmpl $0x6c, %eax
jne 0x1b20
movl $0x1, 0x3a4c(%rip)      # 0x520c
jmp 0x1788
cmpl $0x4f, %eax
jg 0x17ff
cmpl $0x41, %eax
je 0x18a6
cmpl $0x45, %eax
jne 0x18b6
movl $0x1, 0x3a21(%rip)      # 0x5204
jmp 0x1788
cmpl $0x61, %eax
je 0x184e
cmpl $0x63, %eax
jne 0x1b20
movl $0x1, 0x3a13(%rip)      # 0x5210
jmp 0x1788
cmpl $0x50, %eax
je 0x185d
cmpl $0x53, %eax
jne 0x1b20
movl $0x1, 0x39f1(%rip)      # 0x5208
jmp 0x1788
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11b0
cmpl $0x201, %eax            # imm = 0x201
jge 0x1835
movl $0x200, %eax            # imm = 0x200
movl $0x10000, %ecx          # imm = 0x10000
cmpl %ecx, %eax
jl 0x1843
movl $0x10000, %eax          # imm = 0x10000
movl %eax, 0x38df(%rip)      # 0x5128
jmp 0x1788
movl $0x1, 0x39bc(%rip)      # 0x5214
jmp 0x1788
movq (%r12), %rdi
leaq 0x1b35(%rip), %rsi      # 0x339d
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x1160
cmpl $0x2, %eax
jne 0x1b20
movb 0x7(%rsp), %al
movb %al, 0x38a4(%rip)       # 0x512c
movb 0x6(%rsp), %al
movb %al, 0x389b(%rip)       # 0x512d
jmp 0x1788
movl $0x1, 0x3977(%rip)      # 0x5218
jmp 0x1788
movq (%r12), %rax
movq %rax, 0x386f(%rip)      # 0x5120
jmp 0x1788
cmpl $-0x1, %eax
jne 0x1b20
movl 0x3947(%rip), %edi      # 0x520c
movl 0x3945(%rip), %ecx      # 0x5210
movl 0x3943(%rip), %edx      # 0x5214
movl 0x3941(%rip), %eax      # 0x5218
leal (%rdx,%rax), %r8d
addl %ecx, %r8d
addl %edi, %r8d
cmpl $0x1, %r8d
jne 0x1b20
movq 0x36de(%rip), %rsi      # 0x4fd0
movslq (%rsi), %r12
movl %ebx, %esi
subl %r12d, %esi
leaq (%r15,%r12,8), %r14
testl %edi, %edi
je 0x1971
testl %esi, %esi
je 0x1b20
jle 0x1a8c
subl %r12d, %ebx
incl %ebx
leaq 0x1a8b(%rip), %r15      # 0x33a7
movq (%r14), %rdi
callq 0x1b68
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x194f
movq (%r14), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10e0
callq 0x2831
movl $0xa, %edi
callq 0x1040
callq 0x1d58
jmp 0x1961
cmpl $-0x3, %eax
je 0x1b2d
cmpl $-0x2, %eax
je 0x1b25
addq $0x8, %r14
decl %ebx
cmpl $0x1, %ebx
jg 0x191c
jmp 0x1a8c
testl %ecx, %ecx
je 0x19a8
cmpl $0x1, %esi
jle 0x1b20
movq (%r14), %rdi
callq 0x1d7a
testl %eax, %eax
je 0x1a2a
cmpl $-0x5, %eax
je 0x1b55
cmpl $-0x4, %eax
jne 0x1a8c
movq %r14, %rdi
callq 0x126e
testl %edx, %edx
je 0x1a9d
cmpl $0x1, %esi
jle 0x1b20
movq (%r14), %rdi
callq 0x1e09
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1b0a
subl %r12d, %ebx
incl %ebx
leaq (%r15,%r12,8), %r14
addq $0x8, %r14
movq (%r14), %rdi
movq 0x373c(%rip), %rsi      # 0x5120
movzbl 0x3741(%rip), %edx      # 0x512c
movzbl 0x373b(%rip), %ecx      # 0x512d
movl 0x372f(%rip), %r8d      # 0x5128
movl 0x3808(%rip), %r9d      # 0x5208
callq 0x2128
cmpl $0x1, %eax
je 0x1a20
cmpl $-0x1, %eax
je 0x1b3d
decl %ebx
addq $0x8, %r14
cmpl $0x2, %ebx
ja 0x19da
jmp 0x1a87
movq %r14, %rdi
callq 0x136a
jmp 0x1a13
subl %r12d, %ebx
incl %ebx
leaq (%r15,%r12,8), %r14
addq $0x8, %r14
movq (%r14), %rdi
movq 0x36df(%rip), %rsi      # 0x5120
movzbl 0x36e4(%rip), %edx      # 0x512c
movzbl 0x36de(%rip), %ecx      # 0x512d
movl 0x36d2(%rip), %r8d      # 0x5128
movl 0x37ab(%rip), %r9d      # 0x5208
callq 0x2128
cmpl $0x1, %eax
je 0x1a7d
cmpl $-0x1, %eax
je 0x1b35
decl %ebx
addq $0x8, %r14
cmpl $0x2, %ebx
ja 0x1a37
jmp 0x1a87
movq %r14, %rdi
callq 0x12bc
jmp 0x1a70
callq 0x1d58
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
je 0x1a8c
testl %esi, %esi
je 0x1b20
jle 0x1a8c
subl %r12d, %ebx
incl %ebx
leaq 0x18f4(%rip), %r15      # 0x33a7
movq (%r14), %rdi
callq 0x1b68
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1af3
movq (%r14), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10e0
movq 0x3649(%rip), %rdi      # 0x5120
movl 0x3727(%rip), %esi      # 0x5204
callq 0x24c1
movl $0xa, %edi
callq 0x1040
callq 0x1d58
jmp 0x1afd
cmpl $-0x3, %eax
je 0x1b4d
cmpl $-0x2, %eax
je 0x1b45
addq $0x8, %r14
decl %ebx
cmpl $0x1, %ebx
jg 0x1ab3
jmp 0x1a8c
cmpl $-0x3, %eax
je 0x1b5d
cmpl $-0x2, %eax
jne 0x1a8c
movq %r14, %rdi
callq 0x1343
callq 0x158c
movq %r14, %rdi
callq 0x1247
movq %r14, %rdi
callq 0x1220
movq %r14, %rdi
callq 0x12e0
movq %r14, %rdi
callq 0x138e
movq %r14, %rdi
callq 0x13f1
movq %r14, %rdi
callq 0x13ca
movq %r14, %rdi
callq 0x1295
movq %r14, %rdi
callq 0x131c
nopl (%rax)