// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11Dir
static void dos11Dir(
  struct mountedFS *mount,
  uint8_t UNUSED(unit),
  char *fname
)
{
  struct DOS11data *data = &mount->dos11data;
  uint8_t user = data->user;
  uint8_t group = data->group;
  struct dos11FileSpec spec;
  unsigned int mfdblk, ufdblk, i, j;
  uint16_t buf[512];
  uint8_t found = 0;

  if (dos11ParseFilespec(fname, &spec, user, group, DOS11_M_NONAME) == 0) {
    fprintf(stderr, "dir: syntax error in file spec \"%s\"\n", fname);
    return;
  }

  if (dos11ReadBlock(mount, MFD1_BLOCK, NULL) == 0)
    return;

  /*
   * Search the MFD for matching UIC entries.
   */
  mfdblk = le16toh(data->buf[MFD1_MFD2BLOCK]);
  do {
    if (dos11ReadBlock(mount, mfdblk, NULL) == 0)
      return;

    for (i = MFD2_HEADER; i < EODIR(mount, MFD2_SIZE); i += MFD2_SIZE) {
      uint16_t uic = le16toh(data->buf[i + MFD2_UFDUIC]);
      uint16_t entrysz = le16toh(data->buf[i + MFD2_UFDSIZE]);

      if (uic == 0)
        continue;

      if ((spec.flags & DOS11_WC_GROUP) == 0)
        if (spec.group != ((uic >> 8) & 0377))
          continue;

      if ((spec.flags & DOS11_WC_USER) == 0)
        if (spec.user != (uic & 0377))
          continue;

      found = 1;

      if ((ufdblk = le16toh(data->buf[i + MFD2_UFDSTART])) != 0) {
        unsigned int header = 0;

        do {
          if (dos11ReadBlock(mount, ufdblk, buf) == 0)
            return;

          for (j = UFD_HEADER; j < EODIR(mount, entrysz); j += entrysz) {
            if ((le16toh(buf[j + UFD_FILENAME]) == 0) &&
                (le16toh(buf[j + UFD_FILENAME + 1]) == 0) &&
                (le16toh(buf[j + UFD_EXTENSION]) == 0))
              continue;

            if ((spec.flags & DOS11_WC_NAME) == 0)
              if ((le16toh(buf[j + UFD_FILENAME]) != spec.name[0]) ||
                  (le16toh(buf[j + UFD_FILENAME + 1]) != spec.name[1]))
                continue;

            if ((spec.flags & DOS11_WC_EXT) == 0)
              if (le16toh(buf[j + UFD_EXTENSION]) != spec.ext)
                  continue;

            if (!header) {
              printf("%s:[%u, %u]\n\n",
                     mount->name, (uic >> 8) & 0377, uic & 0377);
              header = 1;
            }

            dos11DisplayDir(&buf[j], SWISSET('f'));
          }
          ufdblk = le16toh(buf[UFD_LINK]);
        } while (ufdblk != 0);

        if (header)
          printf("\n");
      }
    }
    mfdblk = le16toh(data->buf[MFD2_LINK]);
  } while (mfdblk != 0);

  if (!found && ((spec.flags & (DOS11_WC_GROUP | DOS11_WC_USER)) == 0))
    ERROR("dir: Directory [%o,%o] not found\n", spec.group, spec.user);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rdx, %r14
movq %rdi, %rbx
movzbl 0x955(%rdi), %edx
movzbl 0x954(%rdi), %ecx
leaq 0x1a(%rsp), %rsi
movq %r14, %rdi
movl $0x2, %r8d
callq 0x7c06
testl %eax, %eax
je 0x916b
movq %rbx, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x9159
movzwl 0x554(%rbx), %eax
movb 0x1a(%rsp), %dil
movzbl 0x23(%rsp), %ecx
movl %ecx, 0x14(%rsp)
movb 0x22(%rsp), %cl
movb %cl, 0xb(%rsp)
movzwl 0x1c(%rsp), %ecx
movw %cx, 0x12(%rsp)
movzwl 0x1e(%rsp), %ecx
movw %cx, 0xe(%rsp)
movzwl 0x20(%rsp), %ecx
movw %cx, 0x10(%rsp)
leaq 0x8(%rbx), %rcx
movq %rcx, 0x48(%rsp)
movl %edi, %ecx
andb $0x8, %cl
shrb $0x3, %cl
movb %cl, 0xc(%rsp)
movl %edi, %ecx
andb $0x4, %cl
shrb $0x2, %cl
movb %cl, 0xd(%rsp)
movq $0x0, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
movb %dil, 0xa(%rsp)
movzwl %ax, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x9159
movq 0x28(%rbx), %rax
movq %rax, %rcx
shrq %rcx
addq $-0x5, %rcx
cmpq $-0x3, %rcx
ja 0x9136
movl $0x1, %r9d
movl $0x1, %ecx
movb 0xa(%rsp), %dil
movzwl 0x554(%rbx,%rcx,2), %ecx
testl %ecx, %ecx
je 0x911a
movl %ecx, %edx
shrl $0x8, %edx
movl %edx, 0x2c(%rsp)
cmpl 0x14(%rsp), %edx
sete %sil
orb 0xd(%rsp), %sil
cmpb %cl, 0xb(%rsp)
sete %dl
cmpb $0x1, %sil
jne 0x911a
orb 0xc(%rsp), %dl
je 0x911a
movl %r9d, %esi
movzwl 0x556(%rbx,%rsi,2), %edx
movb $0x1, %r8b
movq %r8, 0x38(%rsp)
testw %dx, %dx
je 0x911a
movq %r9, 0x30(%rsp)
movzwl 0x558(%rbx,%rsi,2), %r14d
leaq -0x1(%r14), %rbp
movzbl %cl, %eax
movl %eax, 0x28(%rsp)
leal 0x1(%r14), %eax
movl %eax, 0x24(%rsp)
xorl %r13d, %r13d
movzwl %dx, %esi
movq %rbx, %rdi
leaq 0x50(%rsp), %rdx
callq 0x7621
testl %eax, %eax
je 0x9159
movq 0x28(%rbx), %rax
movq %rax, %rcx
shrq %rcx
subq %rbp, %rcx
cmpq $0x2, %rcx
jb 0x90ea
movl $0x1, %r15d
movl $0x3, %r12d
movl 0x24(%rsp), %ebx
movb 0xa(%rsp), %sil
movzwl 0x50(%rsp,%r15,2), %ecx
testw %cx, %cx
jne 0x904c
leal -0x1(%r12), %edx
cmpw $0x0, 0x50(%rsp,%rdx,2)
jne 0x904c
movl %r12d, %edx
cmpw $0x0, 0x50(%rsp,%rdx,2)
je 0x90cf
testb $0x1, %sil
jne 0x906a
cmpw 0x12(%rsp), %cx
jne 0x90cf
leal -0x1(%r12), %ecx
movzwl 0xe(%rsp), %edx
cmpw %dx, 0x50(%rsp,%rcx,2)
jne 0x90cf
testb $0x2, %sil
jne 0x907f
movl %r12d, %ecx
movzwl 0x10(%rsp), %edx
cmpw %dx, 0x50(%rsp,%rcx,2)
jne 0x90cf
testl %r13d, %r13d
jne 0x909f
leaq 0x8d1c(%rip), %rdi      # 0x11da7
movq 0x48(%rsp), %rsi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %ecx
xorl %eax, %eax
callq 0x3160
leaq (%rsp,%r15,2), %rdi
addq $0x50, %rdi
leaq 0xff2a(%rip), %rax      # 0x18fd8
movl (%rax), %esi
shrl $0x5, %esi
andl $0x1, %esi
callq 0x6ed0
movq 0x40(%rsp), %rax
movq 0x28(%rax), %rax
movl $0x1, %r13d
movb 0xa(%rsp), %sil
movl %ebx, %r15d
movq %rax, %rcx
shrq %rcx
subq %rbp, %rcx
addl %r14d, %ebx
addl %r14d, %r12d
cmpq %r15, %rcx
ja 0x9025
movzwl 0x50(%rsp), %edx
testw %dx, %dx
movq 0x40(%rsp), %rbx
jne 0x8fe1
testl %r13d, %r13d
je 0x9110
movl $0xa, %edi
callq 0x3060
movq 0x28(%rbx), %rax
movb 0xa(%rsp), %dil
movq 0x30(%rsp), %r9
addl $0x4, %r9d
movq %rax, %rdx
shrq %rdx
addq $-0x3, %rdx
movq %r9, %rcx
cmpq %r9, %rdx
ja 0x8f60
jmp 0x913b
movb 0xa(%rsp), %dil
movzwl 0x554(%rbx), %eax
testw %ax, %ax
jne 0x8f23
andb $0xc, %dil
movq 0x38(%rsp), %rax
orb %dil, %al
je 0x9197
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xee7e(%rip), %rax      # 0x17ff0
movq (%rax), %rdi
leaq 0x8c06(%rip), %rsi      # 0x11d82
movq %r14, %rdx
xorl %eax, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31e0
movzbl 0xb(%rsp), %edi
movl 0x14(%rsp), %esi
callq 0x3c0c
jmp 0x9159