// File: /workspace/github_c_makefile_star2_O3/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 0x1848
movq %rsi, %r14
cmpq $0x0, (%rsi)
je 0x1848
movl %edi, %r15d
leal -0x1(%r15), %r13d
leaq 0x8(%r14), %rbx
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x1529
cmpb $0x2d, (%rax)
jne 0x1529
addq $0x10, %r14
addl $-0x2, %r15d
movzbl 0x1(%rax), %eax
cmpl $0x61, %eax
je 0x1516
cmpl $0x65, %eax
je 0x150d
cmpl $0x63, %eax
jne 0x186d
movl $0x1, 0x2bc5(%rip)      # 0x40d0
jmp 0x1538
leaq 0x2bc4(%rip), %rax      # 0x40d8
jmp 0x151d
leaq 0x2bb7(%rip), %rax      # 0x40d4
movl $0x1, (%rax)
movq %r14, %rbx
movl %r15d, %r13d
cmpl $0x0, 0x2ba0(%rip)      # 0x40d0
je 0x156c
movl %r13d, %r15d
movq %rbx, %r14
cmpl $0x1, %r15d
jbe 0x1848
movq (%r14), %rdi
callq 0x1a92
movl %eax, %ebp
testl %eax, %eax
je 0x15b5
cmpl $-0x5, %ebp
je 0x185d
cmpl $-0x4, %ebp
je 0x1865
xorl %ebp, %ebp
xorl %r14d, %r14d
jmp 0x180c
cmpl $0x0, 0x2b61(%rip)      # 0x40d4
je 0x16ca
cmpl $0x1, %r13d
jbe 0x1848
movq (%rbx), %rdi
callq 0x1b21
xorl %ebp, %ebp
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0x15bb
cmpl $-0x3, %eax
je 0x1875
movl $0x0, %r14d
cmpl $-0x2, %eax
jne 0x180c
movq %rbx, %rdi
callq 0x11e7
movl %r15d, %r13d
movq %r14, %rbx
leaq 0x2b1e(%rip), %r12      # 0x40e0
movl %ebp, %r14d
addq $0x8, %rbx
incl %r13d
movq (%rbx), %rdi
cmpb $0x2d, (%rdi)
jne 0x162b
cmpb $0x72, 0x1(%rdi)
jne 0x162b
cmpb $0x0, 0x2(%rdi)
jne 0x162b
cmpl $0x3, %r13d
je 0x1807
movq 0x8(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10b0
cmpl $0x2, %eax
jge 0x1604
movl $0x1, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmpl %ecx, %eax
jl 0x1612
movl $0x10000, %eax          # imm = 0x10000
movl %eax, 0x2a98(%rip)      # 0x40b0
addq $0x10, %rbx
addl $-0x2, %r13d
cmpl $0x2, %r13d
jg 0x15cc
jmp 0x1807
movq %r13, (%rsp)
leaq 0xc4f(%rip), %rsi       # 0x2285
callq 0x10f0
testq %rax, %rax
je 0x1804
movq %rax, %r13
movq (%rsp), %rax
addl $-0x2, %eax
movq %rax, (%rsp)
movslq 0x2a57(%rip), %rdx      # 0x40b0
movl $0x1, %esi
movq %r12, %rdi
movq %r13, %rcx
callq 0x1060
testq %rax, %rax
je 0x1693
movq %rax, %r15
movq %r13, %rdi
callq 0x1050
testl %eax, %eax
jne 0x1691
movq %r12, %rdi
movl %r15d, %esi
callq 0x1ce9
testl %eax, %eax
je 0x1652
incl %r14d
jmp 0x1693
incl %ebp
movq %r13, %rdi
callq 0x1070
xorl %edi, %edi
callq 0x1d71
testl %eax, %eax
jne 0x16b4
movl $0x1, %edi
callq 0x1d71
testl %eax, %eax
je 0x16b7
incl %r14d
movq (%rsp), %r13
cmpl $0x1, %r13d
jg 0x15c5
jmp 0x1807
cmpl $0x0, 0x2a07(%rip)      # 0x40d8
je 0x1562
testl %r13d, %r13d
je 0x1848
leaq 0x29f9(%rip), %r15      # 0x40e0
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx), %rdi
callq 0x1880
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x17e3
movq %r13, (%rsp)
movq %rbx, 0x8(%rsp)
xorl %ebx, %ebx
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1b6f
testl %eax, %eax
je 0x17b8
movl %eax, %r12d
cmpl $-0x1, %eax
je 0x17d3
testl %r12d, %r12d
js 0x17c0
movl 0x2979(%rip), %edx      # 0x40b4
leal 0x1(%rdx), %eax
movl %eax, 0x2970(%rip)      # 0x40b4
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
leaq 0xbb5(%rip), %rsi       # 0x2308
xorl %eax, %eax
callq 0x1100
movq %rbx, %rdi
leaq 0xbad(%rip), %rsi       # 0x2311
callq 0x10f0
testq %rax, %rax
je 0x17ce
movq %rax, %r13
movl %r12d, %ebx
andl $0xffffff, %ebx         # imm = 0xFFFFFF
shrl $0x1f, %r12d
addl %r12d, %ebp
movl $0x1, %esi
movq %r15, %rdi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x1120
xorl %ecx, %ecx
cmpq %rbx, %rax
setne %cl
addl %ecx, %r14d
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1b6f
movl %eax, %r12d
decl %eax
cmpl $-0x2, %eax
jb 0x1771
jmp 0x17c0
xorl %r12d, %r12d
testb $0x1, %bl
jne 0x17d3
movb $0x1, %bl
cmpl $-0x1, %r12d
jne 0x170b
jmp 0x17d3
incl %r14d
jmp 0x17c0
callq 0x1a70
movq 0x8(%rsp), %rbx
movq (%rsp), %r13
jmp 0x17ed
cmpl $-0x3, %eax
je 0x1855
cmpl $-0x2, %eax
je 0x184d
leal -0x1(%r13), %eax
addq $0x8, %rbx
cmpl $0x1, %r13d
movl %eax, %r13d
jg 0x16ec
jmp 0x180c
incl %r14d
callq 0x1a70
xorl %eax, %eax
movl %ebp, %ecx
orl %r14d, %ecx
jne 0x1824
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x27cd(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xadf(%rip), %rsi       # 0x2314
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1090
movl $0x4, %eax
jmp 0x1815
callq 0x134c
movq %rbx, %rdi
callq 0x1235
movq %rbx, %rdi
callq 0x120e
movq %r14, %rdi
callq 0x1199
movq %r14, %rdi
callq 0x1172
movzbl %al, %edi
callq 0x1150
movq %rbx, %rdi
callq 0x11c0
nopl (%rax)