// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %rcx
movq %r14, %rsi
callq 0xaa88
testl %eax, %eax
je 0xb688
leaq 0xcb8f(%rip), %rax      # 0x17fd8
testb $0x20, 0x1(%rax)
jne 0xb458
movq 0x38(%rbx), %rdi
callq 0x57e5
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 0x5568
movl %eax, %r15d
leal 0x1(%r15), %eax
cmpl $0x2, %eax
jb 0xb670
cmpl $0xfefefefe, %r15d      # imm = 0xFEFEFEFE
je 0xb679
testl %r15d, %r15d
js 0xb635
cmpl $0xe, %r15d
jne 0xb64a
xorl %r13d, %r13d
movq 0x38(%rbx), %rdi
callq 0x53fc
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0xb502
cmpl $0xfefefefe, %eax       # imm = 0xFEFEFEFE
je 0xb679
xorl %ecx, %ecx
testl %eax, %eax
setns %cl
addl %ecx, %r13d
jmp 0xb4da
testb $0x4, %r12b
jne 0xb512
cmpb 0x4f(%rbx), %bpl
jne 0xb666
testb $0x8, %r12b
jne 0xb525
movb 0x17(%rsp), %al
cmpb 0x4e(%rbx), %al
jne 0xb666
testb $0x1, %r12b
jne 0xb55e
movzwl 0x1e(%rsp), %eax
cmpw (%r14), %ax
jne 0xb666
movzwl 0x1a(%rsp), %eax
cmpw 0x4a(%rbx), %ax
jne 0xb666
testb $0x1, 0x31(%rbx)
je 0xb55e
movzwl 0x18(%rsp), %eax
cmpw 0x54(%rbx), %ax
jne 0xb666
testb $0x2, %r12b
jne 0xb573
movzwl 0x1c(%rsp), %eax
cmpw 0x4c(%rbx), %ax
jne 0xb666
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 0x5068
movzwl 0x4a(%rbx), %edi
leaq 0x23(%rsp), %rsi
callq 0x5068
testb $0x1, 0x31(%rbx)
je 0xb5bc
movzwl 0x54(%rbx), %edi
leaq 0x26(%rsp), %rsi
callq 0x5068
movb $0x2e, 0x29(%rsp)
movzwl 0x4c(%rbx), %edi
leaq 0x2a(%rsp), %rsi
callq 0x5068
leaq 0xca02(%rip), %rax      # 0x17fd8
testb $0x20, (%rax)
jne 0xb5f8
movzbl 0x4f(%rbx), %edx
movzbl 0x4e(%rbx), %ecx
leaq 0x6004(%rip), %rdi      # 0x115ee
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x3160
jmp 0xb666
movzwl 0x52(%rbx), %edi
leaq 0x3c(%rsp), %rsi
callq 0x51da
movzwl %r13w, %edx
movzwl 0x50(%rbx), %r8d
movzbl 0x4f(%rbx), %r9d
movzbl 0x4e(%rbx), %eax
movl %eax, (%rsp)
leaq 0x5faf(%rip), %rdi      # 0x115d1
leaq 0x20(%rsp), %rsi
leaq 0x3c(%rsp), %rcx
xorl %eax, %eax
callq 0x3160
jmp 0xb666
movq 0xb9bc(%rip), %rax      # 0x16ff8
movq (%rax), %rcx
leaq 0x5fd5(%rip), %rdi      # 0x1161b
pushq $0x25
jmp 0xb65d
movq 0xb9a7(%rip), %rax      # 0x16ff8
movq (%rax), %rcx
leaq 0x5fa1(%rip), %rdi      # 0x115fc
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
cmpl $-0x1, %r15d
jne 0xb496
movq 0x38(%rbx), %rdi
callq 0x57e5
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb969(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x56e9(%rip), %rsi      # 0x10d82
movq %r14, %rdx
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31e0