// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x2, -0x8(%rbp)
jl 0x13d4
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x13d9
callq 0x1250
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x14a2
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x20(%rbp)
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x14a2
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
movl -0x8(%rbp), %eax
decl %eax
movl %eax, -0x8(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x5c(%rbp)
subl $0x61, %eax
je 0x1464
jmp 0x1444
movl -0x5c(%rbp), %eax
subl $0x63, %eax
je 0x1458
jmp 0x144e
movl -0x5c(%rbp), %eax
subl $0x65, %eax
je 0x1470
jmp 0x147c
movl $0x1, 0x3c6e(%rip)      # 0x50d0
jmp 0x14a0
movl $0x1, 0x3c66(%rip)      # 0x50d4
jmp 0x14a0
movl $0x1, 0x3c5e(%rip)      # 0x50d8
jmp 0x14a0
movq 0x3b75(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movsbl (%rax), %edx
leaq 0x1de1(%rip), %rsi      # 0x3275
movb $0x0, %al
callq 0x10a0
callq 0x1250
jmp 0x14a2
cmpl $0x0, 0x3c27(%rip)      # 0x50d0
je 0x16db
cmpl $0x1, -0x8(%rbp)
jg 0x14ba
callq 0x1250
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1c80
movl %eax, %ecx
movl %ecx, -0x60(%rbp)
subl $-0x5, %eax
je 0x1684
jmp 0x14d6
movl -0x60(%rbp), %eax
subl $-0x4, %eax
je 0x16ad
jmp 0x14e4
movl -0x60(%rbp), %eax
testl %eax, %eax
jne 0x16d6
jmp 0x14f1
jmp 0x14f3
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jl 0x167d
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x1d65(%rip), %rsi      # 0x3285
callq 0x1090
cmpl $0x0, %eax
jne 0x15a3
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jl 0x159e
movq -0x10(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x10c0
movl %eax, 0x3b58(%rip)      # 0x50b8
cmpl $0x1, 0x3b51(%rip)      # 0x50b8
jge 0x1573
movl $0x1, 0x3b45(%rip)      # 0x50b8
cmpl $0x10000, 0x3b3b(%rip)  # imm = 0x10000
jle 0x1589
movl $0x10000, 0x3b2f(%rip)  # imm = 0x10000
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x1508
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0x1cd7(%rip), %rsi      # 0x3288
callq 0x1100
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x1658
jmp 0x15c6
movslq 0x3aeb(%rip), %rdx      # 0x50b8
movq -0x40(%rbp), %rcx
leaq 0x3b08(%rip), %rdi      # 0x50e0
movl $0x1, %esi
callq 0x1060
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x1629
movq -0x40(%rbp), %rdi
callq 0x1050
cmpl $0x0, %eax
je 0x1605
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1629
movq -0x50(%rbp), %rax
movl %eax, %esi
leaq 0x3ace(%rip), %rdi      # 0x50e0
callq 0x1f70
cmpl $0x0, %eax
je 0x1627
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1629
jmp 0x15c6
movq -0x40(%rbp), %rdi
callq 0x1070
xorl %edi, %edi
callq 0x2050
cmpl $0x0, %eax
jne 0x164d
movl $0x1, %edi
callq 0x2050
cmpl $0x0, %eax
je 0x1656
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1663
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x167d
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x1508
callq 0x1c50
jmp 0x16d6
movq 0x396d(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x1bef(%rip), %rsi      # 0x328b
movb $0x0, %al
callq 0x10a0
movl $0x5, %edi
callq 0x1120
movq 0x3944(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x1bea(%rip), %rsi      # 0x32af
movb $0x0, %al
callq 0x10a0
movl $0x3, %edi
callq 0x1120
jmp 0x198b
cmpl $0x0, 0x39f2(%rip)      # 0x50d4
je 0x177d
cmpl $0x1, -0x8(%rbp)
jg 0x16f3
callq 0x1250
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1d40
movl %eax, %ecx
movl %ecx, -0x64(%rbp)
subl $-0x3, %eax
je 0x174f
jmp 0x170b
movl -0x64(%rbp), %eax
subl $-0x2, %eax
je 0x1726
jmp 0x1715
movl -0x64(%rbp), %eax
incl %eax
subl $0x1, %eax
ja 0x1778
jmp 0x1721
jmp 0x14f3
movq 0x38cb(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x1b97(%rip), %rsi      # 0x32d5
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x1120
movq 0x38a2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x1b94(%rip), %rsi      # 0x32fb
movb $0x0, %al
callq 0x10a0
movl $0x3, %edi
callq 0x1120
jmp 0x1989
cmpl $0x0, 0x3954(%rip)      # 0x50d8
je 0x1987
cmpl $0x0, -0x8(%rbp)
jne 0x1795
callq 0x1250
jmp 0x1797
cmpl $0x1, -0x8(%rbp)
jl 0x1985
movb $0x1, -0x31(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x19d0
movl %eax, %ecx
movl %ecx, -0x68(%rbp)
subl $-0x3, %eax
je 0x1942
jmp 0x17c1
movl -0x68(%rbp), %eax
subl $-0x2, %eax
je 0x1919
jmp 0x17cf
movl -0x68(%rbp), %eax
incl %eax
subl $0x1, %eax
ja 0x196b
jmp 0x17df
jmp 0x17e1
leaq 0x38f8(%rip), %rdi      # 0x50e0
movl $0x10000, %esi          # imm = 0x10000
callq 0x1da0
movl %eax, -0x44(%rbp)
movl %eax, %ecx
movl %ecx, -0x6c(%rbp)
subl $-0x1, %eax
je 0x180a
jmp 0x1801
movl -0x6c(%rbp), %eax
testl %eax, %eax
je 0x180f
jmp 0x1821
jmp 0x1904
cmpb $0x0, -0x31(%rbp)
jne 0x181c
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
jmp 0x1904
movl -0x44(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
jne 0x1902
leaq -0x30(%rbp), %rdi
movl 0x3880(%rip), %edx      # 0x50bc
movl %edx, %eax
addl $0x1, %eax
movl %eax, 0x3875(%rip)      # 0x50bc
leaq 0x1abd(%rip), %rsi      # 0x330b
movb $0x0, %al
callq 0x1110
leaq -0x30(%rbp), %rdi
leaq 0x1ab4(%rip), %rsi      # 0x3314
callq 0x1100
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
je 0x18f7
jmp 0x1875
xorl %eax, %eax
cmpl $-0x1, -0x44(%rbp)
movb %al, -0x6d(%rbp)
je 0x188a
cmpl $0x0, -0x44(%rbp)
setne %al
movb %al, -0x6d(%rbp)
movb -0x6d(%rbp), %al
testb $0x1, %al
jne 0x1893
jmp 0x18f5
movl -0x44(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
movl %eax, %eax
movq %rax, -0x58(%rbp)
movl -0x44(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x18b7
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x58(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq 0x381a(%rip), %rdi      # 0x50e0
movl $0x1, %esi
callq 0x1130
cmpq -0x58(%rbp), %rax
je 0x18df
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
leaq 0x37fa(%rip), %rdi      # 0x50e0
movl $0x10000, %esi          # imm = 0x10000
callq 0x1da0
movl %eax, -0x44(%rbp)
jmp 0x1875
jmp 0x1900
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x1902
jmp 0x1904
movb $0x0, -0x31(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x17e1
callq 0x1c50
jmp 0x196b
movq 0x36d8(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x19a4(%rip), %rsi      # 0x32d5
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x1120
movq 0x36af(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x19a1(%rip), %rsi      # 0x32fb
movb $0x0, %al
callq 0x10a0
movl $0x3, %edi
callq 0x1120
movl -0x8(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x8(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x10(%rbp)
jmp 0x1797
jmp 0x1987
jmp 0x1989
jmp 0x198b
cmpl $0x0, -0x14(%rbp)
jne 0x1997
cmpl $0x0, -0x18(%rbp)
je 0x19be
movq 0x365a(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
movl -0x14(%rbp), %edx
movl -0x18(%rbp), %ecx
leaq 0x1969(%rip), %rsi      # 0x3317
movb $0x0, %al
callq 0x10a0
movl $0x4, -0x4(%rbp)
jmp 0x19c5
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nop