// File: /workspace/github_c_makefile_star2_O2/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, %r14
movl %edi, %ebx
leaq 0x1ebb(%rip), %r15      # 0x3390
movq 0x3afc(%rip), %r12      # 0x4fd8
leaq 0x7(%rsp), %r13
leaq 0x6(%rsp), %rbp
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x11a0
cmpl $0x41, %eax
je 0x15ca
cmpl $0x45, %eax
je 0x15e9
cmpl $0x50, %eax
je 0x1572
cmpl $0x53, %eax
je 0x15ac
cmpl $0x61, %eax
je 0x1607
cmpl $0x63, %eax
je 0x15f8
cmpl $0x65, %eax
je 0x15da
cmpl $0x6c, %eax
je 0x15bb
cmpl $0x72, %eax
jne 0x1616
movq (%r12), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x11b0
cmpl $0x201, %eax            # imm = 0x201
jge 0x1559
movl $0x200, %eax            # imm = 0x200
movl $0x10000, %ecx          # imm = 0x10000
cmpl %ecx, %eax
jl 0x1567
movl $0x10000, %eax          # imm = 0x10000
movl %eax, 0x3bbb(%rip)      # 0x5128
jmp 0x14e6
movq (%r12), %rdi
leaq 0x1e20(%rip), %rsi      # 0x339d
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x1160
cmpl $0x2, %eax
jne 0x18c1
movb 0x7(%rsp), %al
movb %al, 0x3b8f(%rip)       # 0x512c
movb 0x6(%rsp), %al
movb %al, 0x3b86(%rip)       # 0x512d
jmp 0x14e6
movl $0x1, 0x3c52(%rip)      # 0x5208
jmp 0x14e6
movl $0x1, 0x3c47(%rip)      # 0x520c
jmp 0x14e6
movq (%r12), %rax
movq %rax, 0x3b4b(%rip)      # 0x5120
jmp 0x14e6
movl $0x1, 0x3c34(%rip)      # 0x5218
jmp 0x14e6
movl $0x1, 0x3c11(%rip)      # 0x5204
jmp 0x14e6
movl $0x1, 0x3c0e(%rip)      # 0x5210
jmp 0x14e6
movl $0x1, 0x3c03(%rip)      # 0x5214
jmp 0x14e6
cmpl $-0x1, %eax
jne 0x18c1
movl 0x3be7(%rip), %esi      # 0x520c
movl 0x3be5(%rip), %ecx      # 0x5210
movl 0x3be3(%rip), %edx      # 0x5214
movl 0x3be1(%rip), %eax      # 0x5218
leal (%rdx,%rax), %r8d
addl %ecx, %r8d
addl %esi, %r8d
cmpl $0x1, %r8d
jne 0x18c1
movq 0x397e(%rip), %rdi      # 0x4fd0
movslq (%rdi), %r12
subl %r12d, %ebx
leaq (%r14,%r12,8), %r15
testl %esi, %esi
je 0x16c4
testl %ebx, %ebx
je 0x18c1
leaq 0x1d38(%rip), %r14      # 0x33a7
pushq $0xa
popq %rbp
testl %ebx, %ebx
jle 0x181a
movq (%r15), %rdi
callq 0x1938
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x16aa
movq (%r15), %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x10e0
callq 0x2542
movl %ebp, %edi
callq 0x1040
callq 0x1add
jmp 0x16bc
cmpl $-0x3, %eax
je 0x18c6
cmpl $-0x2, %eax
je 0x18a2
decl %ebx
addq $0x8, %r15
jmp 0x1672
testl %ecx, %ecx
je 0x170c
cmpl $0x1, %ebx
jle 0x18c1
movq (%r15), %rdi
callq 0x1afc
testl %eax, %eax
je 0x17a5
cmpl $-0x5, %eax
je 0x1917
cmpl $-0x4, %eax
jne 0x181a
movq 0x38fe(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq (%r15), %rdx
leaq 0x1d66(%rip), %rsi      # 0x346d
jmp 0x18da
testl %edx, %edx
je 0x182b
cmpl $0x1, %ebx
jle 0x18c1
movq (%r15), %rdi
callq 0x1b70
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1894
leaq (%r14,%r12,8), %r12
addq $0x8, %r12
movq 0x38b8(%rip), %r15      # 0x4ff8
leaq 0x1cdd(%rip), %r14      # 0x3424
cmpl $0x2, %ebx
jl 0x1815
movq (%r12), %rdi
movq 0x39c5(%rip), %rsi      # 0x5120
movzbl 0x39ca(%rip), %edx      # 0x512c
movzbl 0x39c4(%rip), %ecx      # 0x512d
movl 0x39b8(%rip), %r8d      # 0x5128
movl 0x3a91(%rip), %r9d      # 0x5208
callq 0x1e44
cmpl $0x1, %eax
je 0x1792
cmpl $-0x1, %eax
je 0x18e5
addq $0x8, %r12
decl %ebx
jmp 0x1747
movq (%r15), %rdi
movq (%r12), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x1120
jmp 0x178a
leaq (%r14,%r12,8), %r12
addq $0x8, %r12
movq 0x3844(%rip), %r15      # 0x4ff8
leaq 0x1c69(%rip), %r14      # 0x3424
cmpl $0x2, %ebx
jl 0x1815
movq (%r12), %rdi
movq 0x3955(%rip), %rsi      # 0x5120
movzbl 0x395a(%rip), %edx      # 0x512c
movzbl 0x3954(%rip), %ecx      # 0x512d
movl 0x3948(%rip), %r8d      # 0x5128
movl 0x3a21(%rip), %r9d      # 0x5208
callq 0x1e44
cmpl $0x1, %eax
je 0x1802
cmpl $-0x1, %eax
je 0x18e5
addq $0x8, %r12
decl %ebx
jmp 0x17bb
movq (%r15), %rdi
movq (%r12), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x1120
jmp 0x17fa
callq 0x1add
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
je 0x181a
testl %ebx, %ebx
je 0x18c1
leaq 0x1b69(%rip), %r14      # 0x33a7
pushq $0xa
popq %rbp
testl %ebx, %ebx
jle 0x181a
movq (%r15), %rdi
callq 0x1938
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1882
movq (%r15), %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x10e0
movq 0x38b7(%rip), %rdi      # 0x5120
movl 0x3995(%rip), %esi      # 0x5204
callq 0x21e0
movl %ebp, %edi
callq 0x1040
callq 0x1add
jmp 0x188c
cmpl $-0x3, %eax
je 0x18c6
cmpl $-0x2, %eax
je 0x18a2
decl %ebx
addq $0x8, %r15
jmp 0x1841
cmpl $-0x3, %eax
je 0x18c6
cmpl $-0x2, %eax
jne 0x181a
movq 0x374f(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq (%r15), %rdx
leaq 0x1af7(%rip), %rsi      # 0x33ad
xorl %eax, %eax
callq 0x1120
pushq $0x2
jmp 0x1911
callq 0x130c
movq 0x372b(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq (%r15), %rdx
leaq 0x1af9(%rip), %rsi      # 0x33d3
xorl %eax, %eax
callq 0x1120
pushq $0x3
jmp 0x1911
movq (%r15), %rdi
movq (%r12), %rdx
leaq 0x1af0(%rip), %rsi      # 0x33e3
xorl %eax, %eax
callq 0x1120
movq (%r15), %rcx
leaq 0x1afc(%rip), %rdi      # 0x3400
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x11e0
pushq $0x6
popq %rdi
callq 0x11d0
movq 0x36da(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq (%r15), %rdx
leaq 0x1b1c(%rip), %rsi      # 0x3447
xorl %eax, %eax
callq 0x1120
pushq $0x5
jmp 0x1911
nop