// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movzbl -0x11(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
movzwl 0xa(%rax,%rcx,2), %eax
movl %eax, -0x40(%rbp)
movzbl -0x11(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl -0x40(%rbp), %eax
movl %edx, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xd883
movw $0x1, -0x36(%rbp)
movw $0x5, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x36(%rbp), %edx
callq 0xc3e0
cmpl $0x0, %eax
jne 0xd5f0
movl $0x0, -0x4(%rbp)
jmp 0xd88a
movq -0x30(%rbp), %rax
movzwl 0x430(%rax), %edi
callq 0xc6b0
movzwl %ax, %eax
sarl %eax
addl $0x7, %eax
movw %ax, -0x32(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x432(%rax), %edi
callq 0xc6b0
movw %ax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
andl $0x800, %ecx            # imm = 0x800
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x49(%rbp)
jne 0xd665
movzwl -0x38(%rbp), %ecx
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movzwl -0x32(%rbp), %ecx
cmpl %ecx, %eax
setge %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0xd671
jmp 0xd860
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movw %ax, -0x3a(%rbp)
movzwl -0x3a(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
jne 0xd825
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
movq -0x20(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jne 0xd823
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
movq -0x20(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jne 0xd823
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %eax
movq -0x20(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
cmpl %ecx, %eax
jne 0xd823
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x6(%rax)
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x6, %ecx
movslq %ecx, %rcx
movw 0x42a(%rax,%rcx,2), %cx
movq -0x28(%rbp), %rax
movw %cx, 0xa(%rax)
movw -0x36(%rbp), %ax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0xc(%rax)
movw -0x38(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0xe(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movb -0x11(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x20(%rax)
movw -0x34(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x36(%rax)
movl $0x1, -0x4(%rbp)
jmp 0xd88a
jmp 0xd825
movq -0x30(%rbp), %rax
movzwl -0x38(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
movzwl -0x34(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x34(%rbp)
movzwl -0x32(%rbp), %ecx
movzwl -0x38(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x38(%rbp)
jmp 0xd620
movq -0x30(%rbp), %rax
movzwl 0x42c(%rax), %edi
callq 0xc6b0
movw %ax, -0x36(%rbp)
movzwl -0x36(%rbp), %eax
cmpl $0x0, %eax
jne 0xd5c6
jmp 0xd883
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)