// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/extracters/dbtap/dos11.c
// Function: extractFiles
void extractFiles(
  char *ascii,
  int ppn
)
{
  unsigned int status;

  do {
    switch (status = ReadTapeRecord(record, sizeof(record))) {
      case ST_EOM:
        break;

      case ST_TM:
        /* Second tape mark in a row - treat as end of medium */
        status = ST_EOM;
        break;

      default:
        if ((status & ST_ERROR) == 0) {
          status &= ST_LENGTH;

          if ((status == sizeof(struct dos11hdr1)) ||
              (status == sizeof(struct dos11hdr2))) {
            char filename[32];
            struct dos11hdr2 *hdr = (struct dos11hdr2 *)record;
            int offset = 0, extension;
            char *useAscii;
            uint32 errorCount = 0, length, i;

            /*
             * Construct the output filename
             */
            offset += r50AscNoSpace(hdr->fname[0], &filename[offset]);
            offset += r50AscNoSpace(hdr->fname[1], &filename[offset]);
            if (status == sizeof(struct dos11hdr2))
              offset += r50AscNoSpace(hdr->fname3, &filename[offset]);
            if (ppn) {
              sprintf(&filename[offset], "_%o_%o", hdr->prog, hdr->proj);
              offset = strlen(filename);
            }
            extension = offset;
            filename[offset++] = '.';
            offset += r50AscNoSpace(hdr->ext, &filename[offset]);
            filename[offset++] = '\0';

            useAscii = strstr(ascii, &filename[extension]);

            printf("   Extracting: %s ", filename);

            initAscii();

            if ((file = fopen(filename, "w")) != NULL) {
              do {
                switch (status = ReadTapeRecord(record, sizeof(record))) {
                  case ST_EOM:
                  case ST_TM:
                    break;

                  default:
                    if ((status & ST_ERROR) != 0)
                      errorCount++;
                    length = status & ST_LENGTH;

                    if (useAscii != NULL) {
                      /*
                       * Process each character separately in order to map
                       * CRLF -> LF.
                       */
                      for (i = 0; i < length; i++)
                        xferAscii(record[i]);
                    } else {
                      if (fwrite(record, sizeof(char), length, file) != length)
                        errorCount++;
                    }
                }
              } while ((status != ST_EOM) && (status != ST_TM));
              flushAscii();
              printf("%s \n", errorCount ? "Errors detected" : "Done");
              fclose(file);
              break;
            } else printf("*** File open failure\n");
          } else fprintf(stderr, "   *** Unexpected record size\n");
        } else fprintf(stderr, "   *** Directory entry contains error\n");

        /*
         * Scan forward to the end of this file
         */
        do {
          status = ReadTapeRecordLength();
        } while ((status != ST_EOM) && (status != ST_TM));
    }
  } while (status != ST_EOM);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, 0xc(%rsp)
movq %rdi, 0x38(%rsp)
leaq 0x12d61(%rip), %r15     # 0x15240
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1e57
movl %eax, %ebp
leal 0x1(%rbp), %eax
cmpl $0x2, %eax
jb 0x279e
testl %ebp, %ebp
js 0x2790
andl $0xffffff, %ebp         # imm = 0xFFFFFF
movl %ebp, %eax
orl $0x2, %eax
cmpl $0xe, %eax
jne 0x2797
movzwl 0x12d1e(%rip), %edi     # 0x15240
movq %r12, %rsi
callq 0x27ad
movzwl 0x12d11(%rip), %edi     # 0x15242
movslq %eax, %r13
leaq (%rsp,%r13), %rsi
addq $0x10, %rsi
callq 0x27ad
addl %eax, %r13d
cmpl $0xe, %ebp
jne 0x2563
movzwl 0x12cfc(%rip), %edi     # 0x1524c
movslq %r13d, %r13
leaq (%rsp,%r13), %rsi
addq $0x10, %rsi
callq 0x27ad
addl %eax, %r13d
cmpl $0x0, 0xc(%rsp)
je 0x259c
movslq %r13d, %rax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
movzbl 0x12ccb(%rip), %edx     # 0x15247
movzbl 0x12cc3(%rip), %ecx     # 0x15246
leaq 0xf56(%rip), %rsi       # 0x34e0
xorl %eax, %eax
callq 0x11c0
movq %r12, %rdi
callq 0x10c0
movq %rax, %r13
movslq %r13d, %r14
leaq (%rsp,%r14), %rbx
addq $0x10, %rbx
movb $0x2e, (%rbx)
movzwl 0x12c93(%rip), %edi     # 0x15244
leaq (%rsp,%r14), %rsi
addq $0x11, %rsi
callq 0x27ad
addl %r14d, %eax
incl %eax
cltq
movb $0x0, 0x10(%rsp,%rax)
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x1200
movq %rax, %rbp
leaq 0xf06(%rip), %rdi       # 0x34e7
movq %r12, %rsi
xorl %eax, %eax
callq 0x10e0
movl $0x0, 0x12c43(%rip)     # 0x15238
movq %r12, %rdi
leaq 0xefb(%rip), %rsi       # 0x34fa
callq 0x1190
movq %rax, 0x12c25(%rip)     # 0x15230
testq %rax, %rax
je 0x2767
xorl %ebx, %ebx
movq %rbp, 0x40(%rsp)
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1e57
movl %eax, %r13d
leal 0x1(%r13), %eax
cmpl $0x2, %eax
jb 0x2714
movl %r13d, %r14d
shrl $0x1f, %r14d
addl %ebx, %r14d
movl %r13d, %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
testq %rbp, %rbp
je 0x26de
testl %eax, %eax
je 0x2702
movl %eax, %r12d
movl 0x12bd4(%rip), %eax     # 0x15238
xorl %ebp, %ebp
movsbl (%rbp,%r15), %ebx
cmpl $0xd, %ebx
jne 0x2697
testl %eax, %eax
je 0x2686
movq 0x12bb4(%rip), %rsi     # 0x15230
movl $0xd, %edi
callq 0x1110
movl $0x1, 0x12ba8(%rip)     # 0x15238
movl $0x1, %eax
jmp 0x26cf
testl %eax, %eax
je 0x26bb
movl $0x0, 0x12b93(%rip)     # 0x15238
cmpb $0xa, %bl
je 0x26bb
movq 0x12b7f(%rip), %rsi     # 0x15230
movl $0xd, %edi
callq 0x1110
movq 0x12b6e(%rip), %rsi     # 0x15230
movl %ebx, %edi
callq 0x1110
movl 0x12b69(%rip), %eax     # 0x15238
incq %rbp
cmpq %rbp, %r12
jne 0x2666
movq 0x40(%rsp), %rbp
jmp 0x2702
movl %eax, %ebx
movq 0x12b49(%rip), %rcx     # 0x15230
movl $0x1, %esi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x11e0
xorl %ecx, %ecx
cmpq %rbx, %rax
setne %cl
addl %ecx, %r14d
leal -0x1(%r13), %eax
movl %r14d, %ebx
cmpl $-0x2, %eax
jb 0x261b
jmp 0x2717
movl %ebx, %r14d
cmpl $0x0, 0x12b1a(%rip)     # 0x15238
je 0x2731
movq 0x12b09(%rip), %rsi     # 0x15230
movl $0xd, %edi
callq 0x1110
testl %r14d, %r14d
leaq 0xdc6(%rip), %rsi       # 0x3501
leaq 0xdcf(%rip), %rax       # 0x3511
cmoveq %rax, %rsi
leaq 0xdaf(%rip), %rdi       # 0x34fc
xorl %eax, %eax
callq 0x10e0
movq 0x12ad5(%rip), %rdi     # 0x15230
callq 0x10b0
leaq 0x10(%rsp), %r12
jmp 0x2784
leaq 0xe2d(%rip), %rdi       # 0x359b
callq 0x1080
callq 0x1f2d
movl %eax, %r13d
leal -0x1(%r13), %eax
cmpl $-0x2, %eax
jb 0x2773
cmpl $-0x1, %r13d
jne 0x24e4
jmp 0x279e
callq 0x1418
jmp 0x2773
callq 0x143a
jmp 0x2773
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq