// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/extracters/cpytap/cpytap.c
// Function: main
int main(
  int argc,
  char *argv[]
)
{
  int ch, i, filenumber = 1, filenum;
  char *filename;

  if (argc < 3)
    usage();

  srcfile = argv[1];
  dstfile = argv[2];
  argc -= 2;
  argv += 2;

  memset(fileops, 0, sizeof(fileops));
  memset(&appendops, 0, sizeof(appendops));

  while ((ch = getopt(argc, argv, "r:A:I:R:S:")) != -1) {
    switch (ch) {
      case 'r':
        reclen = strtoul(optarg, NULL, 0);
        if (reclen < MINRECLEN)
          reclen = MINRECLEN;
        if (reclen > MAXRECLEN)
          reclen = MAXRECLEN;
    done:
        break;

      case 'A':
        for (i = 0; i < APPFILES; i++) {
          if (appendops.insfiles[i].filename == NULL) {
            appendops.insfiles[i].reclen = reclen;
            appendops.insfiles[i].filename = argv[optind];
            goto done;
          }
        }
        fprintf(stderr, "No space for appending file - %s\n", argv[optind]);
        return 5;

      case 'I':
        if (parse(optarg, &filenum, &filename) != 0) {
          fprintf(stderr, "Invalid argument - -I %s\n", optarg);
          return 7;
        }

        for (i = 0; i < APPFILES; i++) {
          if (fileops[filenum].insfiles[i].filename == NULL) {
            fileops[filenum].insfiles[i].reclen = reclen;
            fileops[filenum].insfiles[i].filename = filename;
            goto done;
          }
        }
        fprintf(stderr, "No space for inserting file - %s\n", filename);
        return 5;

      case 'R':
        if (parse(optarg, &filenum, &filename) != 0) {
          fprintf(stderr, "Invalid argument - -R %s\n", optarg);
          return 7;
        }
        if (fileops[filenum].repfile.filename != NULL) {
          fprintf(stderr, "File %u is already being replaced.\n", filenum);
          return 8;
        }
        fileops[filenum].repfile.reclen = reclen;
        fileops[filenum].repfile.filename = filename;
        break;

      case 'S':
        filenum = strtoul(optarg, NULL, 0);
        if ((filenum == 0) || (filenum > MAXFILES)) {
          fprintf(stderr, "Invalid file number - -D %u\n", filenum);
          return 6;
        }
        fileops[filenum].skipfile = 1;
        break;

      case '?':
      default:
        usage();
    }
  }

  /*
   * Begin processing the source tape.
   */
  switch (OpenTapeForRead(&src, srcfile)) {
    case TIO_SUCCESS:
      break;

    case TIO_ERROR:
      fprintf(stderr, "%s has errors and may not copy correctly\n", srcfile);
      break;

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

    case TIO_OPENFAIL:
      fprintf(stderr, "%s open failed\n", srcfile);
      exit(3);
  }

  /*
   * Begin processing the destination tape.
   */
  switch (OpenTapeForWrite(&dst, dstfile)) {
    case TIO_SUCCESS:
      break;

    case TIO_IOERROR:
      fprintf(stderr, "Error writing to destination tape - %s\n", dstfile);
      exit(5);

    case TIO_CREATEFAIL:
      fprintf(stderr, "Failed to create destination tape - %s\n", dstfile);
      exit(3);
  }

  /*
   * Process the files on the tape.
   */
  for (;;) {
    if (filenumber <= MAXFILES) {
      struct fileop *op = &fileops[filenumber];

      /*
       * Process possible repalcement.
       */
      if (op->repfile.filename != NULL)
        writeFile(op->repfile.filename, op->repfile.reclen);

      /*
       * Process any insertions before the current file.
       */
      for (i = 0; i < APPFILES; i++)
        if (op->insfiles[i].filename != NULL)
          writeFile(op->insfiles[i].filename, op->insfiles[i].reclen);

      /*
       * Now either copy or skip over the next file on the source tape.
       */
      if ((op->skipfile != 0) || (op->repfile.filename != 0)) {
        if (SkipToNextTapeMark(src) == ST_EOM)
          break;
      } else {
        if (copyFile() == ST_EOM)
          break;
      }
      filenumber++;
    } else {
      /*
       * If there are more than MAXFILES files on the source tape, just copy
       * the remaining files.
       */
      if (copyFile() == ST_EOM)
        break;
    }
  }
  /*
   * Handle any appends
   */
  for (i = 0; i < APPFILES; i++)
    if (appendops.insfiles[i].filename != NULL)
      writeFile(appendops.insfiles[i].filename, appendops.insfiles[i].reclen);

  WriteTapeMark(dst, 0);
  CloseTape(src);
  CloseTape(dst);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
cmpl $0x2, %edi
jle 0x17bc
movq %rsi, %r15
movl %edi, %ebp
movq 0x8(%rsi), %rax
movq %rax, 0x2d1c(%rip)      # 0x40d0
movq 0x10(%rsi), %rax
addq $0x10, %r15
movq %rax, 0x2d15(%rip)      # 0x40d8
addl $-0x2, %ebp
leaq 0x2d23(%rip), %r14      # 0x40f0
movl $0x87b8, %edx           # imm = 0x87B8
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
leaq 0xb4c5(%rip), %rbx      # 0xc8a8
movl $0x158, %edx            # imm = 0x158
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1090
leaq 0x1003(%rip), %r12      # 0x23fc
movq 0x2bd8(%rip), %r13      # 0x3fd8
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1100
cmpl $0x41, %eax
je 0x14e7
cmpl $0x49, %eax
je 0x1526
cmpl $0x52, %eax
je 0x145e
cmpl $0x53, %eax
je 0x14ae
cmpl $0x72, %eax
jne 0x15d8
movq (%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
cmpl $0x2, %eax
pushq $0x1
popq %rcx
cmovll %ecx, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmpl %ecx, %eax
cmovgel %ecx, %eax
movl %eax, 0x2c5c(%rip)      # 0x40b8
jmp 0x1400
movq (%r13), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1335
testl %eax, %eax
jne 0x1735
movslq 0xc(%rsp), %rdx
imulq $0x158, %rdx, %rax      # imm = 0x158
cmpq $0x0, 0x10(%rax,%r14)
jne 0x1779
addq %r14, %rax
addq $0x8, %rax
movl 0x2c1a(%rip), %ecx      # 0x40b8
movl %ecx, (%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1400
movq (%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x1758
cmpl $0x65, %eax
jge 0x1758
cltq
imulq $0x158, %rax, %rax      # imm = 0x158
movl $0x1, 0x4(%rax,%r14)
jmp 0x1400
pushq $0x20
popq %rcx
leaq 0x10(%rcx), %rax
cmpq $0x170, %rax            # imm = 0x170
je 0x1586
cmpq $0x0, (%rcx,%rbx)
movq %rax, %rcx
jne 0x14ea
movl 0x2bae(%rip), %ecx      # 0x40b8
movl %ecx, -0x18(%rax,%rbx)
movq 0x2abb(%rip), %rcx      # 0x3fd0
movslq (%rcx), %rcx
movq (%r15,%rcx,8), %rcx
movq %rcx, -0x10(%rax,%rbx)
jmp 0x1400
movq (%r13), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1335
testl %eax, %eax
jne 0x171e
movslq 0xc(%rsp), %rax
imulq $0x158, %rax, %rax      # imm = 0x158
pushq $0x20
popq %rdx
leaq 0x10(%rdx), %rcx
cmpq $0x170, %rcx            # imm = 0x170
je 0x15a7
addq %rax, %rdx
cmpq $0x0, (%r14,%rdx)
movq %rcx, %rdx
jne 0x1550
movl 0x2b48(%rip), %edx      # 0x40b8
addq %r14, %rax
movl %edx, -0x18(%rcx,%rax)
movq 0x10(%rsp), %rdx
movq %rdx, -0x10(%rcx,%rax)
jmp 0x1400
movq 0x2a6b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x2a39(%rip), %rax      # 0x3fd0
movslq (%rax), %rax
movq (%r15,%rax,8), %rdx
leaq 0xe62(%rip), %rsi       # 0x2407
jmp 0x15bd
movq 0x2a4a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x10(%rsp), %rdx
leaq 0xe86(%rip), %rsi       # 0x2443
xorl %eax, %eax
callq 0x10a0
pushq $0x5
popq %rbx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $-0x1, %eax
jne 0x17bc
movq 0x2ae8(%rip), %rsi      # 0x40d0
leaq 0x2af1(%rip), %rdi      # 0x40e0
callq 0x19f4
cmpl $-0x1, %eax
pushq $0x1
popq %rbp
je 0x1798
cmpl $-0x3, %eax
je 0x1821
cmpl $-0x2, %eax
je 0x17fe
movq 0x2abf(%rip), %rsi      # 0x40d8
leaq 0x2ac8(%rip), %rdi      # 0x40e8
callq 0x1b99
cmpl $-0x5, %eax
je 0x17c1
cmpl $-0x4, %eax
je 0x17e4
leaq 0x2c2a(%rip), %r15      # 0x4268
cmpl $0x64, %ebp
jbe 0x164f
callq 0x195d
cmpl $-0x1, %eax
jne 0x163e
jmp 0x16cb
movl %ebp, %eax
imulq $0x158, %rax, %rax      # imm = 0x158
leaq (%r14,%rax), %r12
movq 0x10(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x1670
movl 0x8(%r12), %esi
callq 0x1848
xorl %r13d, %r13d
cmpq $0x140, %r13            # imm = 0x140
je 0x1695
movq (%r15,%r13), %rdi
testq %rdi, %rdi
je 0x168f
movl -0x8(%r15,%r13), %esi
callq 0x1848
addq $0x10, %r13
jmp 0x1673
cmpl $0x0, 0x4(%r12)
jne 0x16a5
cmpq $0x0, 0x10(%r12)
je 0x16c4
movq 0x2a34(%rip), %rdi      # 0x40e0
callq 0x1e42
cmpl $-0x1, %eax
je 0x16cb
incl %ebp
addq $0x158, %r15            # imm = 0x158
jmp 0x163e
callq 0x195d
jmp 0x16b1
pushq $0x20
popq %r14
cmpq $0x160, %r14            # imm = 0x160
je 0x16f1
movq (%r14,%rbx), %rdi
testq %rdi, %rdi
je 0x16eb
movl -0x8(%r14,%rbx), %esi
callq 0x1848
addq $0x10, %r14
jmp 0x16cf
movq 0x29f0(%rip), %rdi      # 0x40e8
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x1ec2
movq 0x29d8(%rip), %rdi      # 0x40e0
callq 0x1b8f
movq 0x29d4(%rip), %rdi      # 0x40e8
callq 0x1b8f
jmp 0x15c7
movq 0x28d3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0xcf6(%rip), %rsi       # 0x2429
jmp 0x174a
movq 0x28bc(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r13), %rdx
leaq 0xd1b(%rip), %rsi       # 0x2465
xorl %eax, %eax
callq 0x10a0
pushq $0x7
jmp 0x15c6
movq 0x2899(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0xd3a(%rip), %rsi       # 0x24a3
movl %eax, %edx
xorl %eax, %eax
callq 0x10a0
pushq $0x6
jmp 0x15c6
movq 0x2878(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcf5(%rip), %rsi       # 0x247f
xorl %eax, %eax
callq 0x10a0
pushq $0x8
jmp 0x15c6
movq 0x2859(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x2927(%rip), %rdx      # 0x40d0
leaq 0xd10(%rip), %rsi       # 0x24c0
xorl %eax, %eax
callq 0x10a0
jmp 0x1612
callq 0x124c
movq 0x2830(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x2906(%rip), %rdx      # 0x40d8
leaq 0xd47(%rip), %rsi       # 0x2520
xorl %eax, %eax
callq 0x10a0
pushq $0x5
jmp 0x1842
movq 0x280d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x28e3(%rip), %rdx      # 0x40d8
leaq 0xd4c(%rip), %rsi       # 0x2548
jmp 0x1839
movq 0x27f3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x28c1(%rip), %rdx      # 0x40d0
leaq 0xcd4(%rip), %rsi       # 0x24ea
xorl %eax, %eax
callq 0x10a0
pushq $0x2
jmp 0x1842
movq 0x27d0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x289e(%rip), %rdx      # 0x40d0
leaq 0xcd7(%rip), %rsi       # 0x2510
xorl %eax, %eax
callq 0x10a0
pushq $0x3
popq %rdi
callq 0x1120