// File: /workspace/github_c_makefile_star2_O1/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 0x19b4
movq %rsi, %r15
movl %edi, %ebp
movq 0x8(%rsi), %rax
movq %rax, 0x3acb(%rip)      # 0x50d0
movq 0x10(%rsi), %rax
addq $0x10, %r15
movq %rax, 0x3ac4(%rip)      # 0x50d8
addl $-0x2, %ebp
leaq 0x3ad2(%rip), %r14      # 0x50f0
movl $0x87b8, %edx           # imm = 0x87B8
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
leaq 0xc274(%rip), %rbx      # 0xd8a8
movl $0x158, %edx            # imm = 0x158
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1090
leaq 0x1db2(%rip), %r12      # 0x33fc
movq 0x3987(%rip), %r13      # 0x4fd8
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1100
cmpl $0x51, %eax
jle 0x16a8
cmpl $0x52, %eax
je 0x175d
cmpl $0x53, %eax
je 0x1724
cmpl $0x72, %eax
jne 0x19b4
movq (%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
cmpl $0x2, %eax
movl $0x1, %ecx
cmovll %ecx, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmpl %ecx, %eax
cmovgel %ecx, %eax
movl %eax, 0x3a12(%rip)      # 0x50b8
jmp 0x1651
cmpl $0x41, %eax
je 0x1703
cmpl $0x49, %eax
jne 0x1807
movq (%r13), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1577
testl %eax, %eax
jne 0x195e
movslq 0xc(%rsp), %rax
imulq $0x158, %rax, %rax      # imm = 0x158
movl $0x20, %ecx
leaq (%rax,%rcx), %rdx
cmpq $0x0, (%r14,%rdx)
je 0x17ce
addq $0x10, %rcx
cmpq $0x160, %rcx            # imm = 0x160
jne 0x16e2
jmp 0x17f3
movl $0x20, %eax
cmpq $0x0, (%rax,%rbx)
je 0x17ad
addq $0x10, %rax
cmpq $0x160, %rax            # imm = 0x160
jne 0x1708
jmp 0x17e9
movq (%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x196a
cmpl $0x65, %eax
jge 0x196a
cltq
imulq $0x158, %rax, %rax      # imm = 0x158
movl $0x1, 0x4(%rax,%r14)
jmp 0x1651
movq (%r13), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1577
testl %eax, %eax
jne 0x1957
movslq 0xc(%rsp), %rdx
imulq $0x158, %rdx, %rax      # imm = 0x158
cmpq $0x0, 0x10(%rax,%r14)
jne 0x198b
addq %r14, %rax
addq $0x8, %rax
movl 0x391b(%rip), %ecx      # 0x50b8
movl %ecx, (%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1651
movl 0x3905(%rip), %ecx      # 0x50b8
movl %ecx, -0x8(%rax,%rbx)
movq 0x3812(%rip), %rcx      # 0x4fd0
movslq (%rcx), %rcx
movq (%r15,%rcx,8), %rcx
movq %rcx, (%rax,%rbx)
jmp 0x1651
movl 0x38e4(%rip), %edx      # 0x50b8
addq %r14, %rax
movl %edx, -0x8(%rcx,%rax)
movq 0x10(%rsp), %rdx
movq %rdx, (%rcx,%rax)
jmp 0x1651
movq %r15, %rdi
callq 0x11c2
jmp 0x17fd
leaq 0x10(%rsp), %rdi
callq 0x11a4
movl $0x5, %ebx
jmp 0x1946
cmpl $-0x1, %eax
jne 0x19b4
movq 0x38b9(%rip), %rsi      # 0x50d0
leaq 0x38c2(%rip), %rdi      # 0x50e0
callq 0x1ae0
cmpl $-0x1, %eax
je 0x19aa
cmpl $-0x3, %eax
je 0x19c8
cmpl $-0x2, %eax
je 0x19c3
movq 0x3893(%rip), %rsi      # 0x50d8
leaq 0x389c(%rip), %rdi      # 0x50e8
callq 0x1cf1
cmpl $-0x5, %eax
je 0x19b9
cmpl $-0x4, %eax
je 0x19be
movl $0x1, %ebp
cmpl $0x64, %ebp
jle 0x1879
callq 0x1a78
cmpl $-0x1, %eax
jne 0x1868
jmp 0x18f8
movslq %ebp, %rax
imulq $0x158, %rax, %rax      # imm = 0x158
leaq (%r14,%rax), %r15
movq 0x10(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x189a
movl 0x8(%r15), %esi
callq 0x19cd
movl $0x20, %r12d
movq (%r15,%r12), %rdi
testq %rdi, %rdi
je 0x18b3
movl -0x8(%r15,%r12), %esi
callq 0x19cd
addq $0x10, %r12
cmpq $0x160, %r12            # imm = 0x160
jne 0x18a0
cmpl $0x0, 0x4(%r15)
jne 0x18ce
cmpq $0x0, 0x10(%r15)
je 0x18f1
movq 0x380b(%rip), %rdi      # 0x50e0
callq 0x1fd6
cmpl $-0x1, %eax
je 0x18e5
incl %ebp
movb $0x1, %al
jmp 0x18e7
xorl %eax, %eax
testb %al, %al
jne 0x1868
jmp 0x18f8
callq 0x1a78
jmp 0x18da
movl $0x20, %r14d
movq (%r14,%rbx), %rdi
testq %rdi, %rdi
je 0x1911
movl -0x8(%r14,%rbx), %esi
callq 0x19cd
addq $0x10, %r14
cmpq $0x160, %r14            # imm = 0x160
jne 0x18fe
movq 0x37c3(%rip), %rdi      # 0x50e8
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x204c
movq 0x37ab(%rip), %rdi      # 0x50e0
callq 0x1ce7
movq 0x37a7(%rip), %rdi      # 0x50e8
callq 0x1ce7
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1160
jmp 0x1963
callq 0x1182
movl $0x7, %ebx
jmp 0x1946
movq 0x3687(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0x1b28(%rip), %rsi      # 0x34a3
movl %eax, %edx
xorl %eax, %eax
callq 0x10a0
movl $0x6, %ebx
jmp 0x1946
movq 0x3666(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1ae3(%rip), %rsi      # 0x347f
xorl %eax, %eax
callq 0x10a0
movl $0x8, %ebx
jmp 0x1946
callq 0x1213
jmp 0x183e
callq 0x147c
callq 0x125a
callq 0x1282
callq 0x11eb
callq 0x1232