// File: /workspace/github_c_makefile_star2_O1/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 0x1b2c
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 0x1b2c
movl $0x1, 0x3a13(%rip)      # 0x5210
jmp 0x1788
cmpl $0x50, %eax
je 0x185d
cmpl $0x53, %eax
jne 0x1b2c
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 0x1b2c
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 0x1b2c
movl 0x394b(%rip), %eax      # 0x5210
addl 0x3941(%rip), %eax      # 0x520c
addl 0x3943(%rip), %eax      # 0x5214
addl 0x3941(%rip), %eax      # 0x5218
cmpl $0x1, %eax
jne 0x1b2c
movq 0x36e9(%rip), %rax      # 0x4fd0
movslq (%rax), %r12
movl %ebx, %eax
subl %r12d, %eax
leaq (%r15,%r12,8), %r14
cmpl $0x0, 0x3912(%rip)      # 0x520c
je 0x196b
testl %eax, %eax
je 0x1b2c
jle 0x1a90
subl %r12d, %ebx
incl %ebx
leaq 0x1a91(%rip), %r15      # 0x33a7
movq (%r14), %rdi
callq 0x1b74
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1949
movq (%r14), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10e0
callq 0x288b
movl $0xa, %edi
callq 0x1040
callq 0x1d8a
jmp 0x195b
cmpl $-0x3, %eax
je 0x1b39
cmpl $-0x2, %eax
je 0x1b31
addq $0x8, %r14
decl %ebx
cmpl $0x1, %ebx
jg 0x1916
jmp 0x1a90
cmpl $0x0, 0x389e(%rip)      # 0x5210
je 0x19a7
cmpl $0x1, %eax
jle 0x1b2c
movq (%r14), %rdi
callq 0x1dac
testl %eax, %eax
je 0x1a2e
cmpl $-0x5, %eax
je 0x1b61
cmpl $-0x4, %eax
jne 0x1a90
movq %r14, %rdi
callq 0x126e
cmpl $0x0, 0x3866(%rip)      # 0x5214
je 0x1aa1
cmpl $0x1, %eax
jle 0x1b2c
movq (%r14), %rdi
callq 0x1e3b
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1b16
subl %r12d, %ebx
incl %ebx
leaq (%r15,%r12,8), %r14
addq $0x8, %r14
movq (%r14), %rdi
movq 0x3738(%rip), %rsi      # 0x5120
movzbl 0x373d(%rip), %edx      # 0x512c
movzbl 0x3737(%rip), %ecx      # 0x512d
movl 0x372b(%rip), %r8d      # 0x5128
movl 0x3804(%rip), %r9d      # 0x5208
callq 0x2170
cmpl $0x1, %eax
je 0x1a24
cmpl $-0x1, %eax
je 0x1b49
decl %ebx
addq $0x8, %r14
cmpl $0x2, %ebx
jg 0x19de
jmp 0x1a8b
movq %r14, %rdi
callq 0x136a
jmp 0x1a17
subl %r12d, %ebx
incl %ebx
leaq (%r15,%r12,8), %r14
addq $0x8, %r14
movq (%r14), %rdi
movq 0x36db(%rip), %rsi      # 0x5120
movzbl 0x36e0(%rip), %edx      # 0x512c
movzbl 0x36da(%rip), %ecx      # 0x512d
movl 0x36ce(%rip), %r8d      # 0x5128
movl 0x37a7(%rip), %r9d      # 0x5208
callq 0x2170
cmpl $0x1, %eax
je 0x1a81
cmpl $-0x1, %eax
je 0x1b41
decl %ebx
addq $0x8, %r14
cmpl $0x2, %ebx
jg 0x1a3b
jmp 0x1a8b
movq %r14, %rdi
callq 0x12bc
jmp 0x1a74
callq 0x1d8a
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x3770(%rip)      # 0x5218
je 0x1a90
testl %eax, %eax
je 0x1b2c
jle 0x1a90
subl %r12d, %ebx
incl %ebx
leaq 0x18eb(%rip), %r15      # 0x33a7
movq (%r14), %rdi
callq 0x1b74
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1afc
movq (%r14), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10e0
movq 0x3640(%rip), %rdi      # 0x5120
movl 0x371e(%rip), %esi      # 0x5204
callq 0x2518
movl $0xa, %edi
callq 0x1040
callq 0x1d8a
jmp 0x1b06
cmpl $-0x3, %eax
je 0x1b59
cmpl $-0x2, %eax
je 0x1b51
addq $0x8, %r14
decl %ebx
cmpl $0x1, %ebx
jg 0x1abc
jmp 0x1a90
cmpl $-0x3, %eax
je 0x1b69
cmpl $-0x2, %eax
jne 0x1a90
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)