// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/dosmt.c
// Function: dosmtDir
static void dosmtDir(
  struct mountedFS *mount,
  uint8_t UNUSED(unit),
  char *fname
)
{
  struct DOSMTdata *data = &mount->dosmtdata;
  struct dosmtFileSpec spec;
  uint32_t status;

  if (dosmtParseFilespec(mount, fname, &spec, DOSMT_M_NONAME) == 0) {
    fprintf(stderr, "dir: syntax error in file spec \"%s\"\n", fname);
    return;
  }

  /*
   * Position to beginning-of-tape unless the '-n' switch is specified.
   */
  if (!SWISSET('n'))
    tapeRewind(mount->container);

  do {
    switch (status = tapeReadRecord(mount->container, data->buf, DOSMTRCLNT)) {
      case ST_FAIL:
        return;

      case ST_EOM:
        break;

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

      default:
        if ((status & ST_ERROR) == 0) {
          if (status == sizeof(struct dosmthdr)) {
            char filename[14], sdate[12];
            struct dosmthdr *hdr = (struct dosmthdr *)data->buf;
            uint16_t blocks = 0;
            uint32_t length;

            /*
             * Compute the number of blocks in this file.
             */
            do {
              switch (length = tapeReadRecordLength(mount->container)) {
                case ST_FAIL:
                  return;

                case ST_EOM:
                case ST_TM:
                  break;

                default:
                  if ((length & ST_ERROR) == 0)
                    blocks++;
                  break;
              }
            } while ((length != ST_EOM) && (length != ST_TM));

            /*
             * Determine if this file matches the filespec.
             */
            if ((spec.flags & DOSMT_WC_PROJ) == 0)
              if (spec.proj != hdr->proj)
                continue;

            if ((spec.flags & DOSMT_WC_PROG) == 0)
              if (spec.prog != hdr->prog)
                continue;

            if ((spec.flags & DOSMT_WC_NAME) == 0) {
              if ((spec.name[0] != le16toh(hdr->fname[0])) ||
                  (spec.name[1] != le16toh(hdr->fname[1])))
                continue;

              if ((mount->flags & FS_DOSMTEXT) != 0)
                if (spec.name[2] != le16toh(hdr->fname3))
                  continue;
            }

            if ((spec.flags & DOSMT_WC_EXT) == 0)
              if (spec.ext != le16toh(hdr->ext))
                continue;
              
            memset(filename, ' ', sizeof(filename));
            filename[13] = '\0';

            r50Asc(le16toh(hdr->fname[0]), &filename[0]);
            r50Asc(le16toh(hdr->fname[1]), &filename[3]);
            if ((mount->flags & FS_DOSMTEXT) != 0)
              r50Asc(le16toh(hdr->fname3), &filename[6]);
            filename[9] = '.';
            r50Asc(le16toh(hdr->ext), &filename[10]);

            if (SWISSET('f')) {
              dos11Date(le16toh(hdr->date), sdate);

              printf("%s %5d. %s <%03o> [%3o,%3o]\n",
                     filename, blocks, sdate,
                     le16toh(hdr->prot), hdr->proj, hdr->prog);
            } else printf("%s [%3o,%3o]\n", filename, hdr->proj, hdr->prog);
          } else fprintf(stderr, "   *** Unexpected record size\n");
        } else fprintf(stderr, "  *** Directory entry contains error\n");
    }
  } while (status != ST_EOM);

  tapeRewind(mount->container);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r14
movq %rdi, %rbx
leaq 0x30(%rsp), %rdx
movq %r14, %rsi
movl $0x2, %ecx
callq 0xbd08
testl %eax, %eax
je 0xc8c7
leaq 0xc92b(%rip), %rax      # 0x18fd8
testb $0x20, 0x1(%rax)
jne 0xc6bc
movq 0x38(%rbx), %rdi
callq 0x6dbb
leaq 0x48(%rbx), %r14
movb 0x30(%rsp), %r12b
movb 0x3a(%rsp), %bpl
movb 0x3b(%rsp), %al
movb %al, 0x17(%rsp)
movzwl 0x32(%rsp), %eax
movw %ax, 0x1e(%rsp)
movzwl 0x34(%rsp), %eax
movw %ax, 0x1a(%rsp)
movzwl 0x36(%rsp), %eax
movw %ax, 0x18(%rsp)
movzwl 0x38(%rsp), %eax
movw %ax, 0x1c(%rsp)
movq 0x38(%rbx), %rdi
movq %r14, %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x6b0f
movl %eax, %r15d
leal 0x1(%r15), %eax
cmpl $0x2, %eax
jb 0xc8af
cmpl $0xfefefefe, %r15d      # imm = 0xFEFEFEFE
je 0xc8b8
testl %r15d, %r15d
js 0xc899
cmpl $0xe, %r15d
jne 0xc8a0
xorl %r13d, %r13d
movq 0x38(%rbx), %rdi
callq 0x6985
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0xc766
cmpl $0xfefefefe, %eax       # imm = 0xFEFEFEFE
je 0xc8b8
xorl %ecx, %ecx
testl %eax, %eax
setns %cl
addl %ecx, %r13d
jmp 0xc73e
testb $0x4, %r12b
jne 0xc776
cmpb 0x4f(%rbx), %bpl
jne 0xc8a5
testb $0x8, %r12b
jne 0xc789
movb 0x17(%rsp), %al
cmpb 0x4e(%rbx), %al
jne 0xc8a5
testb $0x1, %r12b
jne 0xc7c2
movzwl 0x1e(%rsp), %eax
cmpw (%r14), %ax
jne 0xc8a5
movzwl 0x1a(%rsp), %eax
cmpw 0x4a(%rbx), %ax
jne 0xc8a5
testb $0x1, 0x31(%rbx)
je 0xc7c2
movzwl 0x18(%rsp), %eax
cmpw 0x54(%rbx), %ax
jne 0xc8a5
testb $0x2, %r12b
jne 0xc7d7
movzwl 0x1c(%rsp), %eax
cmpw 0x4c(%rbx), %ax
jne 0xc8a5
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x25(%rsp)
movq %rax, 0x20(%rsp)
movb $0x0, 0x2d(%rsp)
movzwl 0x48(%rbx), %edi
leaq 0x20(%rsp), %rsi
callq 0x658c
movzwl 0x4a(%rbx), %edi
leaq 0x23(%rsp), %rsi
callq 0x658c
testb $0x1, 0x31(%rbx)
je 0xc820
movzwl 0x54(%rbx), %edi
leaq 0x26(%rsp), %rsi
callq 0x658c
movb $0x2e, 0x29(%rsp)
movzwl 0x4c(%rbx), %edi
leaq 0x2a(%rsp), %rsi
callq 0x658c
leaq 0xc79e(%rip), %rax      # 0x18fd8
testb $0x20, (%rax)
jne 0xc85c
movzbl 0x4f(%rbx), %edx
movzbl 0x4e(%rbx), %ecx
leaq 0x5da0(%rip), %rdi      # 0x125ee
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x3160
jmp 0xc8a5
movzwl 0x52(%rbx), %edi
leaq 0x3c(%rsp), %rsi
callq 0x6729
movzwl %r13w, %edx
movzwl 0x50(%rbx), %r8d
movzbl 0x4f(%rbx), %r9d
movzbl 0x4e(%rbx), %eax
movl %eax, (%rsp)
leaq 0x5d4b(%rip), %rdi      # 0x125d1
leaq 0x20(%rsp), %rsi
leaq 0x3c(%rsp), %rcx
xorl %eax, %eax
callq 0x3160
jmp 0xc8a5
callq 0x43e2
jmp 0xc8a5
callq 0x4404
cmpl $-0x1, %r15d
jne 0xc6fa
movq 0x38(%rbx), %rdi
callq 0x6dbb
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb722(%rip), %rax      # 0x17ff0
movq (%rax), %rdi
leaq 0x54aa(%rip), %rsi      # 0x11d82
movq %r14, %rdx
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31e0