// File: /workspace/github_c_makefile_star2_O3/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 0xa797
movl %esi, %ebp
movq %rdi, %r14
movw $0x1, %r15w
movzbl %bpl, %r13d
movzwl %r15w, %edx
movq %r14, %rdi
movl %r13d, %esi
callq 0x9d20
testl %eax, %eax
je 0xa72a
movzwl 0x478(%r14), %edx
movzwl 0x47c(%r14), %ecx
btl $0xb, %ecx
setae %al
cmpl $0x3ea, %edx            # imm = 0x3EA
setb %sil
andb %al, %sil
cmpb $0x1, %sil
jne 0xa718
shrl %edx
movl $0x1f9, %esi            # imm = 0x1F9
subl %edx, %esi
movl %edx, %edi
addl $0x7, %edi
movzwl 0x47a(%r14), %eax
movzwl %dx, %r8d
addl $0xc, %r8d
movw $0x5, %dx
movl $0x5, %r10d
btl $0x9, %ecx
movl %r10d, %r9d
jb 0xa6f1
movzwl 0x474(%r14,%r9,2), %r10d
cmpw 0x2(%r12), %r10w
jne 0xa6f1
movzwl 0x476(%r14,%r9,2), %r10d
cmpw 0x4(%r12), %r10w
jne 0xa6f1
movzwl 0x478(%r14,%r9,2), %r10d
cmpw 0x6(%r12), %r10w
je 0xa72e
movzwl %r8w, %r10d
movzwl 0x472(%r14,%r10,2), %ecx
btl $0xb, %ecx
jb 0xa718
addw 0x47a(%r14,%r9,2), %ax
addl %edi, %edx
leal (%r10,%rdi), %r8d
cmpl %r10d, %esi
jge 0xa6b5
movzwl 0x474(%r14), %r15d
testw %r15w, %r15w
jne 0xa648
xorl %eax, %eax
jmp 0xa797
leaq 0x1(%r9), %rsi
leaq 0x2(%r9), %rdi
leaq 0x3(%r9), %r8
movw %cx, (%rbx)
movzwl 0x472(%r14,%rsi,2), %ecx
movw %cx, 0x2(%rbx)
movzwl 0x472(%r14,%rdi,2), %ecx
movw %cx, 0x4(%rbx)
movzwl 0x472(%r14,%r8,2), %ecx
movw %cx, 0x6(%rbx)
movzwl 0x47a(%r14,%r9,2), %ecx
movw %cx, 0x8(%rbx)
movzwl 0x47e(%r14,%r9,2), %ecx
movw %cx, 0xa(%rbx)
movb %r15b, 0xc(%rbx)
movw %dx, 0xe(%rbx)
movq %r14, 0x18(%rbx)
movb %bpl, 0x20(%rbx)
movw %ax, 0x36(%rbx)
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq