// File: /workspace/github_c_makefile_star2_O2/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
movq %rdx, %r12
movl %esi, %eax
shrl $0x4, %eax
movzwl 0x52(%rdi,%rax,2), %edx
movl %esi, %ecx
andl $0xf, %ecx
xorl %eax, %eax
btl %ecx, %edx
jae 0x94e4
movq %rdi, %r14
movw $0x1, %r15w
movl %esi, 0x4(%rsp)
movzbl %sil, %r13d
movzwl %r15w, %edx
movq %r14, %rdi
movl %r13d, %esi
callq 0x89c6
testl %eax, %eax
je 0x9481
movzwl 0x478(%r14), %edx
shrl %edx
movzwl 0x47a(%r14), %eax
movl $0x1f9, %esi            # imm = 0x1F9
subl %edx, %esi
addl $0x7, %edx
movw $0x5, %cx
movzwl %cx, %edi
movzwl 0x472(%r14,%rdi,2), %r9d
btl $0xb, %r9d
jb 0x946f
cmpl %edi, %esi
jl 0x946f
btl $0x9, %r9d
movl %edi, %r8d
jb 0x9462
leal 0x1(%rdi), %r10d
movzwl 0x472(%r14,%r10,2), %r11d
cmpw 0x2(%r12), %r11w
jne 0x9462
leal 0x2(%rdi), %r11d
movzwl 0x472(%r14,%r11,2), %ebp
cmpw 0x4(%r12), %bp
jne 0x9462
addl $0x3, %edi
movzwl 0x472(%r14,%rdi,2), %ebp
cmpw 0x6(%r12), %bp
je 0x9485
addw 0x47a(%r14,%r8,2), %ax
addl %edx, %ecx
jmp 0x9403
movzwl 0x474(%r14), %r15d
testw %r15w, %r15w
jne 0x93cc
xorl %eax, %eax
jmp 0x94e4
movw %r9w, (%rbx)
movzwl 0x472(%r14,%r10,2), %edx
movw %dx, 0x2(%rbx)
movzwl 0x472(%r14,%r11,2), %edx
movw %dx, 0x4(%rbx)
movzwl 0x472(%r14,%rdi,2), %edx
movw %dx, 0x6(%rbx)
movzwl 0x47a(%r14,%r8,2), %edx
movw %dx, 0x8(%rbx)
movzwl 0x47e(%r14,%r8,2), %edx
movw %dx, 0xa(%rbx)
movb %r15b, 0xc(%rbx)
movw %cx, 0xe(%rbx)
movq %r14, 0x18(%rbx)
movl 0x4(%rsp), %ecx
movb %cl, 0x20(%rbx)
movw %ax, 0x36(%rbx)
pushq $0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq