// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movq %rsi, %r15
movq %rdi, %r14
xorl %r12d, %r12d
movl $0x1, %esi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x7990
movzwl 0x554(%r14), %eax
movzwl %ax, %esi
xorl %r12d, %r12d
movq %r14, %rdi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x7990
movq 0x28(%r14), %rax
shrq %rax
addq $-0x3, %rax
cmpq $0x2, %rax
jb 0x781e
movl $0x1, %edx
movl $0x5, %ecx
movzwl 0x554(%r14,%rdx,2), %edx
testw %dx, %dx
je 0x7814
cmpw %dx, 0x8(%r15)
jne 0x7814
leal -0x3(%rcx), %edx
movzwl 0x554(%r14,%rdx,2), %ebp
testw %bp, %bp
jne 0x7830
movl %ecx, %edx
addl $0x4, %ecx
cmpq %rdx, %rax
ja 0x77ee
movzwl 0x554(%r14), %eax
testw %ax, %ax
jne 0x77bb
jmp 0x7990
addl $-0x2, %ecx
movzwl 0x554(%r14,%rcx,2), %r13d
leaq -0x1(%r13), %rbx
movzwl %bp, %esi
xorl %r12d, %r12d
movq %r14, %rdi
xorl %edx, %edx
callq 0x7621
testl %eax, %eax
je 0x7990
movq 0x28(%r14), %rcx
shrq %rcx
subq %rbx, %rcx
cmpq $0x2, %rcx
jb 0x78da
movl $0x1, %edx
movl $0x9, %eax
movzwl 0x554(%r14,%rdx,2), %edx
testw %dx, %dx
jne 0x789e
leal -0x7(%rax), %esi
cmpw $0x0, 0x554(%r14,%rsi,2)
jne 0x789e
leal -0x6(%rax), %esi
cmpw $0x0, 0x554(%r14,%rsi,2)
je 0x78cb
cmpw 0x2(%r15), %dx
jne 0x78cb
leal -0x7(%rax), %esi
movzwl 0x554(%r14,%rsi,2), %edi
cmpw 0x4(%r15), %di
jne 0x78cb
leal -0x6(%rax), %edi
movzwl 0x554(%r14,%rdi,2), %r8d
cmpw 0x6(%r15), %r8w
je 0x78f0
leal (%rax,%r13), %edx
addl $-0x8, %edx
addl %r13d, %eax
cmpq %rdx, %rcx
ja 0x7872
movzwl 0x554(%r14), %ebp
testw %bp, %bp
jne 0x7840
jmp 0x7990
movq (%rsp), %r8
movw %dx, (%r8)
movzwl 0x554(%r14,%rsi,2), %ecx
movw %cx, 0x2(%r8)
movzwl 0x554(%r14,%rdi,2), %ecx
movw %cx, 0x4(%r8)
leal -0x5(%rax), %ecx
movzwl 0x554(%r14,%rcx,2), %ecx
movw %cx, 0x6(%r8)
leal -0x4(%rax), %ecx
movzwl 0x554(%r14,%rcx,2), %ecx
movw %cx, 0x8(%r8)
leal -0x3(%rax), %ecx
movzwl 0x554(%r14,%rcx,2), %ecx
movw %cx, 0xa(%r8)
leal -0x2(%rax), %ecx
movzwl 0x554(%r14,%rcx,2), %ecx
movw %cx, 0xc(%r8)
leal -0x1(%rax), %ecx
movzwl 0x554(%r14,%rcx,2), %ecx
movw %cx, 0xe(%r8)
movl %eax, %ecx
movzwl 0x554(%r14,%rcx,2), %ecx
movw %cx, 0x10(%r8)
movw %bp, 0x12(%r8)
addl $-0x8, %eax
movw %ax, 0x14(%r8)
movq %r14, 0x20(%r8)
movl $0x1, %r12d
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq