// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/extracters/rawtap/rawtap.c
// Function: main
int main(
  int argc,
  char *argv[]
)
{
  int readError = 0, writeError = 0;
  char *s, filename[16], bot;
  FILE *file;
  unsigned int status;

  if ((argc < 2) || (argv[0] == NULL))
    usage();

  argc--, argv++;

  s = argv[0];
  if ((s != NULL) && (*s++ == '-')) {
    ++argv, --argc;

    switch (*s) {
      case 'c':
        create = 1;
        break;

      case 'a':
        append = 1;
        break;

      case 'e':
        extract = 1;
        break;

      default:
        fprintf(stderr, "Bad option: %c\n", *s);
        usage();
    }
  }

  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:
    appendFiles:
        argc--, argv++;

        while (argc >= 1) {
          if (strcmp(argv[0], "-r") == 0) {
            argc--, argv++;

            if (argc >= 1) {
              reclen = strtol(argv[0], NULL, 10);
              if (reclen < MINRECLEN)
                reclen = MINRECLEN;
              if (reclen > MAXRECLEN)
                reclen = MAXRECLEN;
              argc--, argv++;
            }
            continue;
          }
          if ((file = fopen(argv[0], "rb")) != NULL) {
            size_t datalen;
            
            while ((datalen = fread(record, sizeof(char), reclen, file)) != 0) {
              if (ferror(file)) {
                readError++;
                break;
              }
              if (WriteTapeRecord(record, datalen) != 0) {
                writeError++;
                break;
              }
            }
            fclose(file);
            if ((WriteTapeMark(0) != 0) || (WriteTapeMark(1) != 0))
              writeError++;
          } else {
            writeError++;
            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:
        goto appendFiles;

      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) {
      bot = 1;

      switch (OpenTapeForRead(argv[0])) {
        case TIO_SUCCESS:
        case TIO_ERROR:
          /*
           * Extract files from the container file.
           */
          do {
            switch (status = ReadTapeRecord(record, sizeof(record))) {
              case ST_EOM:
                break;

              case ST_TM:
                /* Ignore tape mark at the beginning pf a container */
                if (!bot) {
                  /* Second tape mark in a row - treat as end of medium */
                  status = ST_EOM;
                }
                break;

              default:
                if ((status & ST_ERROR) == 0) {
                  sprintf(filename, "%05u.dat", seqno++);
                  if ((file = fopen(filename, "wb")) != NULL) {
                    while ((status != ST_EOM) && (status != ST_TM)) {
                      size_t length = status & ST_LENGTH;

                      if ((status & ST_ERROR) != 0)
                        readError++;

                      if (fwrite(record, sizeof(char), length, file) != length)
                        writeError++;

                      status = ReadTapeRecord(record, sizeof(record));
                    }
                  } else writeError++;
                }
                break;
            }
            bot = 0;
          } while (status != ST_EOM);
          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++;
    }
  }

  if ((readError != 0) || (writeError != 0)) {
    fprintf(stderr, "Read Errors: %d, Write Errors: %d\n",
            readError, writeError);
    return 4;
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, %edi
jl 0x1735
movq %rsi, %r14
cmpq $0x0, (%rsi)
je 0x1735
movl %edi, %r15d
leal -0x1(%r15), %ecx
leaq 0x8(%r14), %r12
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x13f7
cmpb $0x2d, (%rax)
jne 0x13f7
addq $0x10, %r14
addl $-0x2, %r15d
movzbl 0x1(%rax), %eax
cmpl $0x61, %eax
je 0x13e4
cmpl $0x65, %eax
je 0x13db
cmpl $0x63, %eax
jne 0x1793
movl $0x1, 0x2cf7(%rip)      # 0x40d0
jmp 0x1406
leaq 0x2cf6(%rip), %rax      # 0x40d8
jmp 0x13eb
leaq 0x2ce9(%rip), %rax      # 0x40d4
movl $0x1, (%rax)
movq %r14, %r12
movl %r15d, %ecx
cmpl $0x0, 0x2cd2(%rip)      # 0x40d0
je 0x1439
movl %ecx, %r15d
movq %r12, %r14
cmpl $0x1, %r15d
jbe 0x1735
movq (%r14), %rdi
callq 0x1978
movl %eax, %ebx
testl %eax, %eax
je 0x146e
cmpl $-0x5, %ebx
je 0x1751
cmpl $-0x4, %ebx
je 0x1770
xorl %ebx, %ebx
xorl %ebp, %ebp
jmp 0x16fd
cmpl $0x0, 0x2c94(%rip)      # 0x40d4
je 0x1585
cmpl $0x1, %ecx
jbe 0x1735
movl %ecx, %ebp
movq (%r12), %rdi
callq 0x19ec
xorl %ebx, %ebx
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x16c4
movq %r12, %r14
movl %ebp, %r15d
pushq $0xa
popq %r12
pushq $0x1
popq %r13
movl %ebx, %ebp
decl %r15d
addq $0x8, %r14
movq %r14, %rax
movl %r15d, %ecx
testl %ecx, %ecx
jle 0x16f8
movl %ecx, %r15d
movq %rax, %r14
movq (%rax), %rdi
cmpb $0x2d, (%rdi)
jne 0x14e7
cmpb $0x72, 0x1(%rdi)
jne 0x14e7
cmpb $0x0, 0x2(%rdi)
jne 0x14e7
leaq 0x8(%r14), %rax
xorl %ecx, %ecx
cmpl $0x1, %r15d
je 0x1485
movq 0x8(%r14), %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x10b0
cmpl $0x2, %eax
jge 0x14c9
movl %r13d, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmpl %ecx, %eax
jl 0x14d7
movl $0x10000, %eax          # imm = 0x10000
movl %eax, 0x2bd3(%rip)      # 0x40b0
addl $-0x2, %r15d
addq $0x10, %r14
jmp 0x147f
leaq 0xd97(%rip), %rsi       # 0x2285
callq 0x10f0
testq %rax, %rax
je 0x16f6
movq %rax, %r12
movq %r15, (%rsp)
leaq 0x2bd6(%rip), %r15      # 0x40e0
movslq 0x2b9f(%rip), %rdx      # 0x40b0
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %r12, %rcx
callq 0x1060
testq %rax, %rax
je 0x154f
movq %rax, %r13
movq %r12, %rdi
callq 0x1050
testl %eax, %eax
jne 0x154d
leaq 0x2ba6(%rip), %r15      # 0x40e0
movq %r15, %rdi
movl %r13d, %esi
callq 0x1b8d
testl %eax, %eax
je 0x150a
incl %ebp
jmp 0x154f
incl %ebx
movq %r12, %rdi
callq 0x1070
xorl %edi, %edi
callq 0x1c09
testl %eax, %eax
pushq $0xa
popq %r12
pushq $0x1
popq %r13
movq (%rsp), %r15
jne 0x157e
movl %r13d, %edi
callq 0x1c09
testl %eax, %eax
je 0x1478
incl %ebp
jmp 0x1478
cmpl $0x0, 0x2b4c(%rip)      # 0x40d8
je 0x1430
testl %ecx, %ecx
je 0x1735
leaq 0x2b3f(%rip), %r14      # 0x40e0
xorl %ebx, %ebx
xorl %ebp, %ebp
testl %ecx, %ecx
jle 0x16fd
movl %ecx, (%rsp)
movq (%r12), %rdi
callq 0x17b4
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x16a8
movq %r12, 0x8(%rsp)
xorl %r12d, %r12d
movq %r14, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1a21
testl %eax, %eax
je 0x167d
movl %eax, %r15d
cmpl $-0x1, %eax
je 0x1699
testl %r15d, %r15d
js 0x1686
movl 0x2ab7(%rip), %edx      # 0x40b4
leal 0x1(%rdx), %eax
movl %eax, 0x2aae(%rip)      # 0x40b4
leaq 0x10(%rsp), %r12
movq %r12, %rdi
leaq 0xcf3(%rip), %rsi       # 0x2308
xorl %eax, %eax
callq 0x1100
movq %r12, %rdi
leaq 0xceb(%rip), %rsi       # 0x2311
callq 0x10f0
testq %rax, %rax
je 0x1695
movq %rax, %r12
leal -0x1(%r15), %eax
cmpl $-0x3, %eax
ja 0x1686
movl %r15d, %r13d
andl $0xffffff, %r13d        # imm = 0xFFFFFF
shrl $0x1f, %r15d
addl %ebx, %r15d
movq %r14, %rdi
pushq $0x1
popq %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x1120
xorl %ecx, %ecx
cmpq %r13, %rax
setne %cl
addl %ecx, %ebp
movq %r14, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1a21
movl %r15d, %ebx
movl %eax, %r15d
jmp 0x1633
xorl %r15d, %r15d
testb $0x1, %r12b
jne 0x1699
movb $0x1, %r12b
cmpl $-0x1, %r15d
jne 0x15cd
jmp 0x1699
incl %ebp
jmp 0x1686
callq 0x1959
movq 0x8(%rsp), %r12
movl (%rsp), %ecx
jmp 0x16b9
cmpl $-0x3, %eax
je 0x173a
cmpl $-0x2, %eax
movl (%rsp), %ecx
je 0x16d3
decl %ecx
addq $0x8, %r12
jmp 0x15a5
cmpl $-0x3, %eax
je 0x173a
movl $0x0, %ebp
cmpl $-0x2, %eax
jne 0x16fd
movq 0x291e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0xbea(%rip), %rsi       # 0x22d2
xorl %eax, %eax
callq 0x1090
pushq $0x2
jmp 0x178d
incl %ebp
callq 0x1959
xorl %eax, %eax
movl %ebx, %ecx
orl %ebp, %ecx
jne 0x1714
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28dd(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbef(%rip), %rsi       # 0x2314
movl %ebx, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1090
pushq $0x4
popq %rax
jmp 0x1705
callq 0x123c
movq 0x28b7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0xba9(%rip), %rsi       # 0x22f8
jmp 0x1784
movq 0x28a0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r14), %rdx
leaq 0xb23(%rip), %rsi       # 0x2288
xorl %eax, %eax
callq 0x1090
pushq $0x5
jmp 0x178d
movq 0x2881(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%r14), %rdx
leaq 0xb28(%rip), %rsi       # 0x22ac
xorl %eax, %eax
callq 0x1090
pushq $0x3
popq %rdi
callq 0x1110
movsbl %al, %edx
movq 0x285b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xace(%rip), %rsi       # 0x2275
xorl %eax, %eax
callq 0x1090
callq 0x123c
nop