// File: /workspace/github_c_makefile_star2_O1/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 0x1870
cmpq $0x0, (%rsi)
je 0x1870
leal -0x1(%rdi), %r12d
leaq 0x8(%rsi), %rbx
movq 0x8(%rsi), %rax
testq %rax, %rax
je 0x152f
cmpb $0x2d, (%rax)
jne 0x152f
movzbl 0x1(%rax), %eax
cmpl $0x65, %eax
je 0x1515
cmpl $0x63, %eax
je 0x150c
cmpl $0x61, %eax
jne 0x188d
leaq 0x2bca(%rip), %rax      # 0x40d4
jmp 0x151c
leaq 0x2bbd(%rip), %rax      # 0x40d0
jmp 0x151c
leaq 0x2bbc(%rip), %rax      # 0x40d8
addq $0x10, %rsi
addl $-0x2, %edi
movl $0x1, (%rax)
movq %rsi, %rbx
movl %edi, %r12d
cmpl $0x0, 0x2b9a(%rip)      # 0x40d0
je 0x1568
cmpl $0x1, %r12d
jle 0x1870
movq (%rbx), %rdi
callq 0x1ae0
testl %eax, %eax
je 0x1593
cmpl $-0x5, %eax
je 0x1885
cmpl $-0x4, %eax
jne 0x1825
movq %rbx, %rdi
callq 0x1182
cmpl $0x0, 0x2b65(%rip)      # 0x40d4
je 0x16cf
cmpl $0x1, %r12d
jle 0x1870
movq (%rbx), %rdi
callq 0x1b6f
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x181b
xorl %ebp, %ebp
movl $0x0, %r14d
cmpl $0x2, %r12d
jl 0x182f
xorl %r14d, %r14d
leaq 0xcd6(%rip), %r13       # 0x2285
xorl %ebp, %ebp
decl %r12d
addq $0x8, %rbx
movq %r12, %r15
movq (%rbx), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x1090
testl %eax, %eax
jne 0x161f
movq %r15, %r12
cmpl $0x1, %r12d
jne 0x15df
addq $0x8, %rbx
xorl %r12d, %r12d
jmp 0x1615
movq 0x8(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10c0
cmpl $0x2, %eax
jge 0x15f9
movl $0x1, %eax
movl $0x10000, %ecx          # imm = 0x10000
cmpl %ecx, %eax
jl 0x1607
movl $0x10000, %eax          # imm = 0x10000
movl %eax, 0x2aab(%rip)      # 0x40b8
addl $-0x2, %r12d
addq $0x10, %rbx
testl %r12d, %r12d
jg 0x15b8
jmp 0x182f
movq %r12, %rdi
leaq 0xc5f(%rip), %rsi       # 0x2288
callq 0x1100
testq %rax, %rax
je 0x182c
movq %rax, (%rsp)
leaq 0x2a9e(%rip), %r12      # 0x40e0
movslq 0x2a6f(%rip), %rdx      # 0x40b8
movl $0x1, %esi
movq %r12, %rdi
movq (%rsp), %r12
movq %r12, %rcx
callq 0x1060
testq %rax, %rax
je 0x1692
movq %rax, %r13
movq %r12, %rdi
callq 0x1050
testl %eax, %eax
jne 0x1690
leaq 0x2a68(%rip), %r12      # 0x40e0
movq %r12, %rdi
movl %r13d, %esi
callq 0x1d4c
testl %eax, %eax
je 0x1642
incl %r14d
movq (%rsp), %r12
jmp 0x1692
incl %ebp
movq %r12, %rdi
callq 0x1070
xorl %edi, %edi
callq 0x1dd4
testl %eax, %eax
leaq 0xbdb(%rip), %r13       # 0x2285
jne 0x16ba
movl $0x1, %edi
callq 0x1dd4
testl %eax, %eax
je 0x16bd
incl %r14d
movq %r15, %r12
cmpl $0x1, %r12d
jg 0x15b1
jmp 0x182f
xorl %ebp, %ebp
cmpl $0x0, 0x2a00(%rip)      # 0x40d8
je 0x1827
testl %r12d, %r12d
je 0x1870
movl $0x0, %r14d
jle 0x1834
leaq 0x29e6(%rip), %r15      # 0x40e0
xorl %r14d, %r14d
xorl %ebp, %ebp
movq (%rbx), %rdi
callq 0x18a8
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x17f9
movq %r12, 0x8(%rsp)
movq %rbx, (%rsp)
xorl %ebx, %ebx
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1bbd
movl %eax, %r12d
cmpl $-0x1, %eax
je 0x17da
testl %r12d, %r12d
jne 0x1744
movl %ebx, %r12d
jmp 0x17da
js 0x17da
movl 0x296c(%rip), %edx      # 0x40bc
leal 0x1(%rdx), %eax
movl %eax, 0x2963(%rip)      # 0x40bc
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
leaq 0xba3(%rip), %rsi       # 0x230b
xorl %eax, %eax
callq 0x1110
movq %rbx, %rdi
leaq 0xb9b(%rip), %rsi       # 0x2314
callq 0x1100
testq %rax, %rax
je 0x17d7
movq %rax, %r13
movl %r12d, %eax
decl %eax
cmpl $-0x3, %eax
ja 0x17da
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 0x1130
xorl %ecx, %ecx
cmpq %rbx, %rax
setne %cl
addl %ecx, %r14d
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1bbd
movl %eax, %r12d
decl %eax
cmpl $-0x2, %eax
jb 0x1790
jmp 0x17da
incl %r14d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %r12d
jne 0x171e
callq 0x1abe
movq (%rsp), %rbx
movq 0x8(%rsp), %r12
jmp 0x1803
cmpl $-0x3, %eax
je 0x187d
cmpl $-0x2, %eax
je 0x1875
leal -0x1(%r12), %eax
addq $0x8, %rbx
cmpl $0x1, %r12d
movl %eax, %r12d
jg 0x16ff
jmp 0x1834
cmpl $-0x3, %eax
je 0x1895
cmpl $-0x2, %eax
je 0x189d
xorl %ebp, %ebp
xorl %r14d, %r14d
jmp 0x1834
incl %r14d
callq 0x1abe
xorl %eax, %eax
movl %ebp, %ecx
orl %r14d, %ecx
jne 0x184c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x27a5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xaba(%rip), %rsi       # 0x2317
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10a0
movl $0x4, %eax
jmp 0x183d
callq 0x135c
movq %rbx, %rdi
callq 0x1245
movq %rbx, %rdi
callq 0x121e
movq %rbx, %rdi
callq 0x11a9
movzbl %al, %edi
callq 0x1160
movq %rbx, %rdi
callq 0x11d0
movq %rbx, %rdi
callq 0x11f7
nopl (%rax)