// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11LookupFile
int rt11LookupFile(
  struct mountedFS *mount,
  uint8_t unit,
  struct rt11FileSpec *spec,
  struct rt11OpenFile *file
)
{
  struct RT11data *data = &mount->rt11data;

  if (RT11_PARTITIONVALID(data, unit)) {
    uint16_t entrysz, position, dsseg = 1;

    do {
      uint16_t off = RT11_DH_SIZE;

      if (rt11ReadDirSegment(mount, unit, dsseg) == 0)
        return 0;

      entrysz = RT11_DI_SIZE + (le16toh(data->buf[RT11_DH_EXTRA]) >> 1);
      position = le16toh(data->buf[RT11_DH_START]);

      /*
       * Loop until we see and end-of-segment marker or there is no room for
       * another directory entry.
       */
      while (!RT11EOS(le16toh(data->buf[off + RT11_DI_STATUS])) &&
             ((RT11_DS_SIZE - off) >= entrysz)) {
        uint16_t status = le16toh(data->buf[off + RT11_DI_STATUS]);
        
        if ((status & RT11_E_MPTY) == 0) {
          if ((le16toh(data->buf[off + RT11_DI_FNAME1]) == spec->name[0]) &&
              (le16toh(data->buf[off + RT11_DI_FNAME2]) == spec->name[1]) &&
              (le16toh(data->buf[off + RT11_DI_FTYPE]) == spec->type)) {

            /*
             * Save directory entry and it's location in the open file
             * descriptor.
             */
            file->status = data->buf[off + RT11_DI_STATUS];
            file->name[0] = data->buf[off + RT11_DI_FNAME1];
            file->name[1] = data->buf[off + RT11_DI_FNAME2];
            file->type = data->buf[off + RT11_DI_FTYPE];
            file->length = data->buf[off + RT11_DI_LENGTH];
            file->creation = data->buf[off + RT11_DI_CREATE];

            file->segment = dsseg;
            file->offset = off;

            file->mount = mount;
            file->unit = unit;

            file->start = position;

            return 1;
          }
        }
        position += le16toh(data->buf[off + RT11_DI_LENGTH]);

        off += entrysz;
      }
      dsseg = le16toh(data->buf[RT11_DH_NEXT]);
    } while (dsseg != 0);
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movl %esi, %eax
shrl $0x4, %eax
movzwl 0x52(%rdi,%rax,2), %eax
movl %esi, %ecx
andl $0xf, %ecx
btl %ecx, %eax
jae 0xaa82
movq %rdx, %r14
movq %rdi, %r15
movw $0x1, %bp
movl %esi, (%rsp)
movzbl %sil, %eax
movl %eax, 0x4(%rsp)
movzwl %bp, %edx
movq %r15, %rdi
movl 0x4(%rsp), %esi
callq 0xa037
testl %eax, %eax
je 0xaa60
movzwl 0x478(%r15), %eax
shrl %eax
movzwl 0x47a(%r15), %ecx
movl $0x1f9, %edx            # imm = 0x1F9
subl %eax, %edx
addl $0x7, %eax
movw $0x5, %si
movzwl %si, %edi
movzwl 0x472(%r15,%rdi,2), %r9d
btl $0xb, %r9d
jb 0xaa67
cmpl %edi, %edx
jl 0xaa67
btl $0x9, %r9d
movl %edi, %r8d
jb 0xaa45
leal 0x1(%rdi), %r10d
movzwl 0x472(%r15,%r10,2), %r11d
cmpw 0x2(%r14), %r11w
jne 0xaa45
leal 0x2(%rdi), %r11d
movzwl 0x472(%r15,%r11,2), %r12d
cmpw 0x4(%r14), %r12w
jne 0xaa45
addl $0x3, %edi
movzwl 0x472(%r15,%rdi,2), %r12d
cmpw 0x6(%r14), %r12w
jne 0xaa45
movw %r9w, (%rbx)
movzwl 0x472(%r15,%r10,2), %r9d
movw %r9w, 0x2(%rbx)
movzwl 0x472(%r15,%r11,2), %r9d
movw %r9w, 0x4(%rbx)
movzwl 0x472(%r15,%rdi,2), %edi
movw %di, 0x6(%rbx)
movzwl 0x47a(%r15,%r8,2), %edi
movw %di, 0x8(%rbx)
movzwl 0x47e(%r15,%r8,2), %edi
movw %di, 0xa(%rbx)
movb %bpl, 0xc(%rbx)
movw %si, 0xe(%rbx)
movq %r15, 0x18(%rbx)
movl (%rsp), %edi
movb %dil, 0x20(%rbx)
movw %cx, 0x36(%rbx)
movl $0x1, %r13d
xorl %edi, %edi
jmp 0xaa53
addw 0x47a(%r15,%r8,2), %cx
addl %eax, %esi
movb $0x1, %dil
testb %dil, %dil
jne 0xa971
xorl %eax, %eax
jmp 0xaa71
xorl %eax, %eax
xorl %r13d, %r13d
jmp 0xaa71
movzwl 0x474(%r15), %ebp
movb $0x1, %al
testb %al, %al
je 0xaa85
testw %bp, %bp
jne 0xa93a
testb %al, %al
je 0xaa85
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq