// File: /workspace/github_c_makefile_star2_O3/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 0x19ab
movq %rsi, %r15
movl %edi, %ebp
movq 0x8(%rsi), %rax
movq %rax, 0x3ad6(%rip)      # 0x50d0
movq 0x10(%rsi), %rax
addq $0x10, %r15
movq %rax, 0x3acf(%rip)      # 0x50d8
addl $-0x2, %ebp
leaq 0x3add(%rip), %r14      # 0x50f0
movl $0x87b8, %edx           # imm = 0x87B8
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
leaq 0xc27f(%rip), %rbx      # 0xd8a8
movl $0x158, %edx            # imm = 0x158
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1090
leaq 0x1dbd(%rip), %r12      # 0x33fc
movq 0x3992(%rip), %r13      # 0x4fd8
movl %ebp, %edi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1100
cmpl $0x51, %eax
jle 0x169d
cmpl $0x52, %eax
je 0x1752
cmpl $0x53, %eax
je 0x1719
cmpl $0x72, %eax
jne 0x19ab
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, 0x3a1d(%rip)      # 0x50b8
jmp 0x1646
cmpl $0x41, %eax
je 0x16f8
cmpl $0x49, %eax
jne 0x17fc
movq (%r13), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1577
testl %eax, %eax
jne 0x1955
movslq 0xc(%rsp), %rax
imulq $0x158, %rax, %rax      # imm = 0x158
movl $0x20, %ecx
leaq (%rax,%rcx), %rdx
cmpq $0x0, (%r14,%rdx)
je 0x17c3
addq $0x10, %rcx
cmpq $0x160, %rcx            # imm = 0x160
jne 0x16d7
jmp 0x17e8
movl $0x20, %eax
cmpq $0x0, (%rax,%rbx)
je 0x17a2
addq $0x10, %rax
cmpq $0x160, %rax            # imm = 0x160
jne 0x16fd
jmp 0x17de
movq (%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1110
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x1961
cmpl $0x65, %eax
jge 0x1961
cltq
imulq $0x158, %rax, %rax      # imm = 0x158
movl $0x1, 0x4(%rax,%r14)
jmp 0x1646
movq (%r13), %rdi
leaq 0xc(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x1577
testl %eax, %eax
jne 0x194e
movslq 0xc(%rsp), %rdx
imulq $0x158, %rdx, %rax      # imm = 0x158
cmpq $0x0, 0x10(%rax,%r14)
jne 0x1982
addq %r14, %rax
addq $0x8, %rax
movl 0x3926(%rip), %ecx      # 0x50b8
movl %ecx, (%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x1646
movl 0x3910(%rip), %ecx      # 0x50b8
movl %ecx, -0x8(%rax,%rbx)
movq 0x381d(%rip), %rcx      # 0x4fd0
movslq (%rcx), %rcx
movq (%r15,%rcx,8), %rcx
movq %rcx, (%rax,%rbx)
jmp 0x1646
movl 0x38ef(%rip), %edx      # 0x50b8
addq %r14, %rax
movl %edx, -0x8(%rcx,%rax)
movq 0x10(%rsp), %rdx
movq %rdx, (%rcx,%rax)
jmp 0x1646
movq %r15, %rdi
callq 0x11c2
jmp 0x17f2
leaq 0x10(%rsp), %rdi
callq 0x11a4
movl $0x5, %ebx
jmp 0x193d
cmpl $-0x1, %eax
jne 0x19ab
movq 0x38c4(%rip), %rsi      # 0x50d0
leaq 0x38cd(%rip), %rdi      # 0x50e0
callq 0x1ad8
cmpl $-0x1, %eax
je 0x19a1
cmpl $-0x3, %eax
je 0x19bf
cmpl $-0x2, %eax
je 0x19ba
movq 0x389e(%rip), %rsi      # 0x50d8
leaq 0x38a7(%rip), %rdi      # 0x50e8
callq 0x1cc7
cmpl $-0x5, %eax
je 0x19b0
cmpl $-0x4, %eax
je 0x19b5
movl $0x1, %ebp
leaq 0x3a04(%rip), %r15      # 0x5268
cmpl $0x64, %ebp
jbe 0x1875
callq 0x1a6f
cmpl $-0x1, %eax
jne 0x1864
jmp 0x18ef
movl %ebp, %eax
imulq $0x158, %rax, %rax      # imm = 0x158
leaq (%r14,%rax), %r12
movq 0x10(%rax,%r14), %rdi
testq %rdi, %rdi
je 0x1896
movl 0x8(%r12), %esi
callq 0x19c4
xorl %r13d, %r13d
movq (%r15,%r13), %rdi
testq %rdi, %rdi
je 0x18ac
movl -0x8(%r15,%r13), %esi
callq 0x19c4
addq $0x10, %r13
cmpq $0x140, %r13            # imm = 0x140
jne 0x1899
cmpl $0x0, 0x4(%r12)
jne 0x18c9
cmpq $0x0, 0x10(%r12)
je 0x18e8
movq 0x3810(%rip), %rdi      # 0x50e0
callq 0x1f97
cmpl $-0x1, %eax
je 0x18ef
incl %ebp
addq $0x158, %r15            # imm = 0x158
jmp 0x1864
callq 0x1a6f
jmp 0x18d5
movl $0x20, %r14d
movq (%r14,%rbx), %rdi
testq %rdi, %rdi
je 0x1908
movl -0x8(%r14,%rbx), %esi
callq 0x19c4
addq $0x10, %r14
cmpq $0x160, %r14            # imm = 0x160
jne 0x18f5
movq 0x37cc(%rip), %rdi      # 0x50e8
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x200d
movq 0x37b4(%rip), %rdi      # 0x50e0
callq 0x1cbd
movq 0x37b0(%rip), %rdi      # 0x50e8
callq 0x1cbd
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1160
jmp 0x195a
callq 0x1182
movl $0x7, %ebx
jmp 0x193d
movq 0x3690(%rip), %rcx      # 0x4ff8
movq (%rcx), %rdi
leaq 0x1b31(%rip), %rsi      # 0x34a3
movl %eax, %edx
xorl %eax, %eax
callq 0x10a0
movl $0x6, %ebx
jmp 0x193d
movq 0x366f(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1aec(%rip), %rsi      # 0x347f
xorl %eax, %eax
callq 0x10a0
movl $0x8, %ebx
jmp 0x193d
callq 0x1213
jmp 0x1833
callq 0x147c
callq 0x125a
callq 0x1282
callq 0x11eb
callq 0x1232