// File: /workspace/github_c_makefile_star2_O2/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 0x13042(%rip), %r15     # 0x15240
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1ba5
movl %eax, %r14d
leal 0x1(%r14), %eax
cmpl $0x2, %eax
jb 0x24b7
testl %r14d, %r14d
js 0x2469
andl $0xffffff, %r14d        # imm = 0xFFFFFF
movl %r14d, %eax
orl $0x2, %eax
cmpl $0xe, %eax
jne 0x247e
movzwl 0x12ffa(%rip), %edi     # 0x15240
movq %r12, %rsi
callq 0x24c6
movzwl 0x12fed(%rip), %edi     # 0x15242
movslq %eax, %rbx
leaq (%rsp,%rbx), %rsi
addq $0x10, %rsi
callq 0x24c6
addl %eax, %ebx
cmpl $0xe, %r14d
jne 0x2286
movzwl 0x12fd8(%rip), %edi     # 0x1524c
movslq %ebx, %rbx
leaq (%rsp,%rbx), %rsi
addq $0x10, %rsi
callq 0x24c6
addl %eax, %ebx
cmpl $0x0, 0xc(%rsp)
je 0x22bf
movslq %ebx, %rax
leaq (%rsp,%rax), %rdi
addq $0x10, %rdi
movzbl 0x12fa8(%rip), %edx     # 0x15247
movzbl 0x12fa0(%rip), %ecx     # 0x15246
leaq 0x1233(%rip), %rsi      # 0x34e0
xorl %eax, %eax
callq 0x11c0
movq %r12, %rdi
callq 0x10c0
movq %rax, %rbx
movslq %ebx, %r14
leaq (%rsp,%r14), %rbx
addq $0x10, %rbx
movb $0x2e, (%rbx)
movzwl 0x12f70(%rip), %edi     # 0x15244
leaq (%rsp,%r14), %rsi
addq $0x11, %rsi
callq 0x24c6
addl %r14d, %eax
incl %eax
cltq
movb $0x0, 0x10(%rsp,%rax)
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x1200
movq %rax, 0x40(%rsp)
leaq 0x11e1(%rip), %rdi      # 0x34e7
movq %r12, %rsi
xorl %eax, %eax
callq 0x10e0
andl $0x0, 0x12f21(%rip)     # 0x15238
movq %r12, %rdi
leaq 0x11d9(%rip), %rsi      # 0x34fa
callq 0x1190
movq %rax, 0x12f03(%rip)     # 0x15230
testq %rax, %rax
je 0x245b
xorl %ebx, %ebx
movq %r15, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x1ba5
movl %eax, %ebp
leal 0x1(%rbp), %eax
cmpl $0x2, %eax
jb 0x240f
movl %ebp, %r14d
shrl $0x1f, %r14d
addl %ebx, %r14d
movl %ebp, %ebx
andl $0xffffff, %ebx         # imm = 0xFFFFFF
cmpq $0x0, 0x40(%rsp)
je 0x23de
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x23fe
movsbl (%r13,%r15), %r12d
movl 0x12eb3(%rip), %eax     # 0x15238
cmpl $0xd, %r12d
jne 0x23aa
testl %eax, %eax
je 0x239e
movq 0x12e9a(%rip), %rsi     # 0x15230
pushq $0xd
popq %rdi
callq 0x1110
movl $0x1, 0x12e90(%rip)     # 0x15238
jmp 0x23d9
testl %eax, %eax
je 0x23ca
andl $0x0, 0x12e83(%rip)     # 0x15238
cmpb $0xa, %r12b
je 0x23ca
movq 0x12e6e(%rip), %rsi     # 0x15230
pushq $0xd
popq %rdi
callq 0x1110
movq 0x12e5f(%rip), %rsi     # 0x15230
movl %r12d, %edi
callq 0x1110
incq %r13
jmp 0x2370
movq 0x12e4b(%rip), %rcx     # 0x15230
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
callq 0x11e0
xorl %ecx, %ecx
cmpq %rbx, %rax
setne %cl
addl %ecx, %r14d
leal -0x1(%rbp), %eax
movl %r14d, %ebx
cmpl $-0x2, %eax
jb 0x2338
jmp 0x2412
movl %ebx, %r14d
cmpl $0x0, 0x12e1f(%rip)     # 0x15238
je 0x242a
movq 0x12e0e(%rip), %rsi     # 0x15230
pushq $0xd
popq %rdi
callq 0x1110
testl %r14d, %r14d
leaq 0x10cd(%rip), %rsi      # 0x3501
leaq 0x10d6(%rip), %rax      # 0x3511
cmoveq %rax, %rsi
leaq 0x10b6(%rip), %rdi      # 0x34fc
xorl %eax, %eax
callq 0x10e0
movq 0x12ddc(%rip), %rdi     # 0x15230
callq 0x10b0
jmp 0x24a9
leaq 0x1139(%rip), %rdi      # 0x359b
callq 0x1080
jmp 0x249a
movq 0x2b88(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x10bb(%rip), %rdi      # 0x3535
pushq $0x26
jmp 0x2491
movq 0x2b73(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1087(%rip), %rdi      # 0x3516
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x11e0
callq 0x1c73
movl %eax, %ebp
leal -0x1(%rbp), %eax
cmpl $-0x2, %eax
jb 0x249a
cmpl $-0x1, %ebp
leaq 0x10(%rsp), %r12
jne 0x2203
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq