// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11LookupFile
int dos11LookupFile(
  struct mountedFS *mount,
  struct dos11FileSpec *spec,
  struct dos11OpenFile *file
)
{
  struct DOS11data *data = &mount->dos11data;
  unsigned int mfdblk, ufdblk, i, j;

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

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

    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) {
        if (uic == ((spec->group << 8) | spec->user)) {
          ufdblk = le16toh(data->buf[i + MFD2_UFDSTART]);

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

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

                if ((le16toh(data->buf[j + UFD_FILENAME]) != spec->name[0]) ||
                    (le16toh(data->buf[j + UFD_FILENAME + 1]) != spec->name[1]) ||
                    (le16toh(data->buf[j + UFD_EXTENSION]) != spec->ext))
                  continue;
                /*
                 * Save the directory entry and it's location in the open
                 * file descriptor.
                 */
                file->name[0] = data->buf[j + UFD_FILENAME];
                file->name[1] = data->buf[j + UFD_FILENAME + 1];
                file->ext = data->buf[j + UFD_EXTENSION];
                file->creation = data->buf[j + UFD_CREATION];
                file->nfb = data->buf[j + UFD_NEXTFREEBYTE];
                file->start = data->buf[j + UFD_FILESTART];
                file->length = data->buf[j + UFD_FILELENGTH];
                file->last = data->buf[j + UFD_LASTBLOCKWRITTEN];
                file->lup = data->buf[j + UFD_LUP];

                file->ufdblk = ufdblk;
                file->ufdoffset = j;

                file->mount = mount;
                return 1;
              }
              ufdblk = le16toh(data->buf[UFD_LINK]);
            } while (ufdblk != 0);
            return 0;
          }
        }
      }
    }
    mfdblk = le16toh(data->buf[MFD2_LINK]);
  } while (mfdblk != 0);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
xorl %r12d, %r12d
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x5ffa
testl %eax, %eax
je 0x6469
movzwl 0x554(%r14), %eax
pushq $0x3
popq %rbp
movzwl %ax, %esi
xorl %r12d, %r12d
movq %r14, %rdi
xorl %edx, %edx
callq 0x5ffa
testl %eax, %eax
je 0x6469
movq 0x28(%r14), %rax
shrq %rax
addq $-0x3, %rax
movl %ebp, %ecx
leal -0x2(%rcx), %edx
cmpq %rdx, %rax
jbe 0x6311
movzwl 0x554(%r14,%rdx,2), %edx
testw %dx, %dx
je 0x630c
cmpw %dx, 0x8(%r15)
jne 0x630c
leal -0x1(%rcx), %edx
movzwl 0x554(%r14,%rdx,2), %r13d
testw %r13w, %r13w
jne 0x6323
addl $0x4, %ecx
jmp 0x62dd
movzwl 0x554(%r14), %eax
testw %ax, %ax
jne 0x62b8
jmp 0x6469
movl %ecx, %eax
movzwl 0x554(%r14,%rax,2), %ebp
leaq -0x1(%rbp), %rax
movq %rax, (%rsp)
movzwl %r13w, %esi
xorl %r12d, %r12d
movq %r14, %rdi
xorl %edx, %edx
callq 0x5ffa
testl %eax, %eax
je 0x6469
movq 0x28(%r14), %rdx
shrq %rdx
subq (%rsp), %rdx
pushq $0x9
popq %rax
leal -0x8(%rax), %ecx
cmpq %rcx, %rdx
jbe 0x63c3
movzwl 0x554(%r14,%rcx,2), %esi
testw %si, %si
jne 0x6391
leal -0x7(%rax), %edi
cmpw $0x0, 0x554(%r14,%rdi,2)
jne 0x6391
leal -0x6(%rax), %edi
cmpw $0x0, 0x554(%r14,%rdi,2)
je 0x63bf
cmpw 0x2(%r15), %si
jne 0x63bf
leal -0x7(%rax), %edi
movzwl 0x554(%r14,%rdi,2), %r8d
cmpw 0x4(%r15), %r8w
jne 0x63bf
leal -0x6(%rax), %r8d
movzwl 0x554(%r14,%r8,2), %r9d
cmpw 0x6(%r15), %r9w
je 0x63da
addl %ebp, %eax
jmp 0x635d
movzwl 0x554(%r14), %r13d
testw %r13w, %r13w
jne 0x6336
jmp 0x6469
movw %si, (%rbx)
movzwl 0x554(%r14,%rdi,2), %edx
movw %dx, 0x2(%rbx)
movzwl 0x554(%r14,%r8,2), %edx
movw %dx, 0x4(%rbx)
leal -0x5(%rax), %edx
movzwl 0x554(%r14,%rdx,2), %edx
movw %dx, 0x6(%rbx)
leal -0x4(%rax), %edx
movzwl 0x554(%r14,%rdx,2), %edx
movw %dx, 0x8(%rbx)
leal -0x3(%rax), %edx
movzwl 0x554(%r14,%rdx,2), %edx
movw %dx, 0xa(%rbx)
leal -0x2(%rax), %edx
movzwl 0x554(%r14,%rdx,2), %edx
movw %dx, 0xc(%rbx)
leal -0x1(%rax), %edx
movzwl 0x554(%r14,%rdx,2), %edx
movw %dx, 0xe(%rbx)
movl %eax, %eax
movzwl 0x554(%r14,%rax,2), %eax
movw %ax, 0x10(%rbx)
movw %r13w, 0x12(%rbx)
movw %cx, 0x14(%rbx)
movq %r14, 0x20(%rbx)
pushq $0x1
popq %rax
movl %eax, %r12d
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq