// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1, -0x1c(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x1408
callq 0x1250
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x3cb9(%rip)      # 0x50d0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x3cb2(%rip)      # 0x50d8
movl -0x8(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x10(%rbp)
leaq 0x3cae(%rip), %rdi      # 0x50f0
xorl %esi, %esi
movl $0x87b8, %edx           # imm = 0x87B8
callq 0x1090
leaq 0xc453(%rip), %rdi      # 0xd8a8
xorl %esi, %esi
movl $0x158, %edx            # imm = 0x158
callq 0x1090
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x206d(%rip), %rdx      # 0x34dc
callq 0x1100
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x1807
movl -0x14(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0x38(%rbp)
subl $0x33, %eax
ja 0x17fd
movq -0x38(%rbp), %rax
leaq 0x1b74(%rip), %rcx      # 0x3014
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x3b28(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1110
movl %eax, 0x3bf4(%rip)      # 0x50b8
cmpl $0x1, 0x3bed(%rip)      # 0x50b8
jge 0x14d7
movl $0x1, 0x3be1(%rip)      # 0x50b8
cmpl $0x10000, 0x3bd7(%rip)  # imm = 0x10000
jle 0x14ed
movl $0x10000, 0x3bcb(%rip)  # imm = 0x10000
jmp 0x14ef
jmp 0x1802
movl $0x0, -0x18(%rbp)
cmpl $0x14, -0x18(%rbp)
jge 0x1577
movslq -0x18(%rbp), %rcx
leaq 0xc39c(%rip), %rax      # 0xd8a8
addq $0x18, %rax
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0x156a
movl 0x3b94(%rip), %ecx      # 0x50b8
movslq -0x18(%rbp), %rdx
leaq 0xc379(%rip), %rax      # 0xd8a8
addq $0x18, %rax
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq 0x3a89(%rip), %rcx      # 0x4fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x18(%rbp), %rdx
leaq 0xc34f(%rip), %rax      # 0xd8a8
addq $0x18, %rax
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x14ef
jmp 0x156c
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x14fb
movq 0x3a7a(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x3a44(%rip), %rcx      # 0x4fd0
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x1f4d(%rip), %rsi      # 0x34e7
movb $0x0, %al
callq 0x10a0
movl $0x5, -0x4(%rbp)
jmp 0x1ad5
movq 0x3a24(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0x1350
cmpl $0x0, %eax
je 0x15f7
movq 0x3a28(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0x39fe(%rip), %rax      # 0x4fd8
movq (%rax), %rdx
leaq 0x1f25(%rip), %rsi      # 0x3509
movb $0x0, %al
callq 0x10a0
movl $0x7, -0x4(%rbp)
jmp 0x1ad5
movl $0x0, -0x18(%rbp)
cmpl $0x14, -0x18(%rbp)
jge 0x16a0
movslq -0x20(%rbp), %rcx
leaq 0x3add(%rip), %rax      # 0x50f0
imulq $0x158, %rcx, %rcx      # imm = 0x158
addq %rcx, %rax
addq $0x18, %rax
movslq -0x18(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0x1690
movl 0x3a7f(%rip), %ecx      # 0x50b8
movslq -0x20(%rbp), %rdx
leaq 0x3aac(%rip), %rax      # 0x50f0
imulq $0x158, %rdx, %rdx      # imm = 0x158
addq %rdx, %rax
addq $0x18, %rax
movslq -0x18(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rcx
movslq -0x20(%rbp), %rdx
leaq 0x3a82(%rip), %rax      # 0x50f0
imulq $0x158, %rdx, %rdx      # imm = 0x158
addq %rdx, %rax
addq $0x18, %rax
movslq -0x18(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x14ef
jmp 0x1692
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x15fe
movq 0x3951(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rdx
leaq 0x1e6e(%rip), %rsi      # 0x3523
movb $0x0, %al
callq 0x10a0
movl $0x5, -0x4(%rbp)
jmp 0x1ad5
movq 0x3909(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x28(%rbp), %rdx
callq 0x1350
cmpl $0x0, %eax
je 0x1712
movq 0x390d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0x38e3(%rip), %rax      # 0x4fd8
movq (%rax), %rdx
leaq 0x1e46(%rip), %rsi      # 0x3545
movb $0x0, %al
callq 0x10a0
movl $0x7, -0x4(%rbp)
jmp 0x1ad5
movslq -0x20(%rbp), %rcx
leaq 0x39d3(%rip), %rax      # 0x50f0
imulq $0x158, %rcx, %rcx      # imm = 0x158
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
je 0x1755
movq 0x38c3(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
leaq 0x1e1d(%rip), %rsi      # 0x355f
movb $0x0, %al
callq 0x10a0
movl $0x8, -0x4(%rbp)
jmp 0x1ad5
movl 0x395d(%rip), %ecx      # 0x50b8
movslq -0x20(%rbp), %rdx
leaq 0x398a(%rip), %rax      # 0x50f0
imulq $0x158, %rdx, %rdx      # imm = 0x158
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movslq -0x20(%rbp), %rdx
leaq 0x396e(%rip), %rax      # 0x50f0
imulq $0x158, %rdx, %rdx      # imm = 0x158
addq %rdx, %rax
movq %rcx, 0x10(%rax)
jmp 0x1802
movq 0x383f(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1110
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x17b6
cmpl $0x64, -0x20(%rbp)
jle 0x17dd
movq 0x383b(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
leaq 0x1db9(%rip), %rsi      # 0x3583
movb $0x0, %al
callq 0x10a0
movl $0x6, -0x4(%rbp)
jmp 0x1ad5
movslq -0x20(%rbp), %rcx
leaq 0x3908(%rip), %rax      # 0x50f0
imulq $0x158, %rcx, %rcx      # imm = 0x158
addq %rcx, %rax
movl $0x1, 0x4(%rax)
jmp 0x1802
jmp 0x17fd
callq 0x1250
jmp 0x1461
movq 0x38c2(%rip), %rsi      # 0x50d0
leaq 0x38cb(%rip), %rdi      # 0x50e0
callq 0x1d00
addl $0x3, %eax
movl %eax, %ecx
movq %rcx, -0x40(%rbp)
subl $0x3, %eax
ja 0x18b5
movq -0x40(%rbp), %rax
leaq 0x17cd(%rip), %rcx      # 0x3004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x18b5
movq 0x37af(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0x387d(%rip), %rdx      # 0x50d0
leaq 0x1d46(%rip), %rsi      # 0x35a0
movb $0x0, %al
callq 0x10a0
jmp 0x18b5
movq 0x378e(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0x385c(%rip), %rdx      # 0x50d0
leaq 0x1d4f(%rip), %rsi      # 0x35ca
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x1120
movq 0x3765(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0x3833(%rip), %rdx      # 0x50d0
leaq 0x1d4c(%rip), %rsi      # 0x35f0
movb $0x0, %al
callq 0x10a0
movl $0x3, %edi
callq 0x1120
movq 0x381c(%rip), %rsi      # 0x50d8
leaq 0x3825(%rip), %rdi      # 0x50e8
callq 0x1fc0
movl %eax, %ecx
movl %ecx, -0x44(%rbp)
subl $-0x5, %eax
je 0x18e9
jmp 0x18d4
movl -0x44(%rbp), %eax
subl $-0x4, %eax
je 0x1912
jmp 0x18de
movl -0x44(%rbp), %eax
testl %eax, %eax
jne 0x193b
jmp 0x18e7
jmp 0x193b
movq 0x3708(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0x37de(%rip), %rdx      # 0x50d8
leaq 0x1cff(%rip), %rsi      # 0x3600
movb $0x0, %al
callq 0x10a0
movl $0x5, %edi
callq 0x1120
movq 0x36df(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq 0x37b5(%rip), %rdx      # 0x50d8
leaq 0x1cfe(%rip), %rsi      # 0x3628
movb $0x0, %al
callq 0x10a0
movl $0x3, %edi
callq 0x1120
jmp 0x193d
cmpl $0x64, -0x1c(%rbp)
jg 0x1a27
movslq -0x1c(%rbp), %rcx
leaq 0x379e(%rip), %rax      # 0x50f0
imulq $0x158, %rcx, %rcx      # imm = 0x158
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x197f
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x1ae0
movl $0x0, -0x18(%rbp)
cmpl $0x14, -0x18(%rbp)
jge 0x19e4
movq -0x30(%rbp), %rax
addq $0x18, %rax
movslq -0x18(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x19d7
movq -0x30(%rbp), %rax
addq $0x18, %rax
movslq -0x18(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x30(%rbp), %rax
addq $0x18, %rax
movslq -0x18(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %esi
callq 0x1ae0
jmp 0x19d9
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1986
movq -0x30(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x19f9
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1a0e
movq 0x36e0(%rip), %rdi      # 0x50e0
callq 0x23a0
cmpl $-0x1, %eax
jne 0x1a0c
jmp 0x1a3a
jmp 0x1a1c
callq 0x1c20
cmpl $-0x1, %eax
jne 0x1a1a
jmp 0x1a3a
jmp 0x1a1c
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1a35
callq 0x1c20
cmpl $-0x1, %eax
jne 0x1a33
jmp 0x1a3a
jmp 0x1a35
jmp 0x193d
movl $0x0, -0x18(%rbp)
cmpl $0x14, -0x18(%rbp)
jge 0x1aa8
movslq -0x18(%rbp), %rcx
leaq 0xbe56(%rip), %rax      # 0xd8a8
addq $0x18, %rax
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1a9b
movslq -0x18(%rbp), %rcx
leaq 0xbe39(%rip), %rax      # 0xd8a8
addq $0x18, %rax
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movslq -0x18(%rbp), %rcx
leaq 0xbe1f(%rip), %rax      # 0xd8a8
addq $0x18, %rax
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %esi
callq 0x1ae0
jmp 0x1a9d
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1a41
movq 0x3639(%rip), %rdi      # 0x50e8
xorl %esi, %esi
callq 0x2450
movq 0x3623(%rip), %rdi      # 0x50e0
callq 0x1f90
movq 0x361f(%rip), %rdi      # 0x50e8
callq 0x1f90
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop