// File: /workspace/github_c_makefile_star2_O1/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 0xc0bc
testl %eax, %eax
je 0xcbfd
leaq 0xc622(%rip), %rax      # 0x18fc8
testb $0x20, 0x1(%rax)
jne 0xc9b5
movq 0x38(%rbx), %rdi
callq 0x6e22
leaq 0x48(%rbx), %r14
movb 0x30(%rsp), %r12b
movb 0x3a(%rsp), %al
movb %al, 0x17(%rsp)
movb 0x3b(%rsp), %al
movb %al, 0x16(%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 0x6b76
movl %eax, %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leal 0x1(%r15), %ecx
cmpl $0x2, %ecx
jb 0xcbdc
cmpl $0xfefefefe, %r15d      # imm = 0xFEFEFEFE
je 0xcbee
testl %r15d, %r15d
js 0xcbcd
cmpl $0xe, %r15d
jne 0xcbd4
xorl %r13d, %r13d
movq 0x38(%rbx), %rdi
callq 0x69ec
leal 0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0xca65
cmpl $0xfefefefe, %eax       # imm = 0xFEFEFEFE
je 0xcb7b
xorl %ecx, %ecx
testl %eax, %eax
setns %cl
addl %ecx, %r13d
decl %eax
cmpl $-0x2, %eax
jb 0xca3f
testb $0x4, %r12b
jne 0xca84
movl $0x3, %ebp
movb 0x17(%rsp), %al
cmpb 0x4f(%rbx), %al
jne 0xcbbf
testb $0x8, %r12b
jne 0xca9c
movl $0x3, %ebp
movb 0x16(%rsp), %al
cmpb 0x4e(%rbx), %al
jne 0xcbbf
testb $0x1, %r12b
jne 0xcada
movl $0x3, %ebp
movzwl 0x1e(%rsp), %eax
cmpw (%r14), %ax
jne 0xcbbf
movzwl 0x1a(%rsp), %eax
cmpw 0x4a(%rbx), %ax
jne 0xcbbf
testb $0x1, 0x31(%rbx)
je 0xcada
movzwl 0x18(%rsp), %eax
cmpw 0x54(%rbx), %ax
jne 0xcbbf
testb $0x2, %r12b
jne 0xcaf4
movl $0x3, %ebp
movzwl 0x1c(%rsp), %eax
cmpw 0x4c(%rbx), %ax
jne 0xcbbf
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x26(%rsp)
movq %rax, 0x20(%rsp)
movb $0x0, 0x2d(%rsp)
movzwl 0x48(%rbx), %edi
leaq 0x20(%rsp), %rsi
callq 0x65fc
movzwl 0x4a(%rbx), %edi
leaq 0x23(%rsp), %rsi
callq 0x65fc
testb $0x1, 0x31(%rbx)
je 0xcb3d
movzwl 0x54(%rbx), %edi
leaq 0x26(%rsp), %rsi
callq 0x65fc
movb $0x2e, 0x29(%rsp)
movzwl 0x4c(%rbx), %edi
leaq 0x2a(%rsp), %rsi
callq 0x65fc
leaq 0xc471(%rip), %rax      # 0x18fc8
testb $0x20, (%rax)
jne 0xcb82
movzbl 0x4f(%rbx), %edx
movzbl 0x4e(%rbx), %ecx
xorl %ebp, %ebp
leaq 0x5a81(%rip), %rdi      # 0x125ee
leaq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x3160
jmp 0xcbbf
movl $0x1, %ebp
jmp 0xcbbf
movzwl 0x52(%rbx), %edi
leaq 0x3c(%rsp), %rsi
callq 0x6787
movzwl %r13w, %edx
movzwl 0x50(%rbx), %r8d
movzbl 0x4f(%rbx), %r9d
movzbl 0x4e(%rbx), %eax
movl %eax, (%rsp)
xorl %ebp, %ebp
leaq 0x5a23(%rip), %rdi      # 0x125d1
leaq 0x20(%rsp), %rsi
leaq 0x3c(%rsp), %rcx
xorl %eax, %eax
callq 0x3160
testl %ebp, %ebp
je 0xcbd9
movl %r15d, %eax
cmpl $0x3, %ebp
je 0xcbdc
jmp 0xcbee
callq 0x4363
jmp 0xcbd9
callq 0x4385
movl %r15d, %eax
cmpl $-0x1, %eax
jne 0xc9f6
movq 0x38(%rbx), %rdi
callq 0x6e22
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb3ec(%rip), %rax      # 0x17ff0
movq (%rax), %rdi
leaq 0x5174(%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 0x31d0