// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rdx, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %r15
xorl %ebx, %ebx
movl $0x1, %esi
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
je 0x7c53
movzwl 0x554(%r15), %eax
movzwl %ax, %esi
xorl %ebx, %ebx
movq %r15, %rdi
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
je 0x7c53
movq 0x28(%r15), %rax
shrq %rax
addq $-0x5, %rax
cmpq $-0x3, %rax
ja 0x7c38
movl $0x1, %r13d
movl $0x1, %eax
movzwl 0x554(%r15,%rax,2), %ecx
movb $0x1, %al
testl %ecx, %ecx
je 0x7c19
movzwl 0x8(%r14), %edx
cmpl %ecx, %edx
jne 0x7c19
movl %r13d, %ecx
movzwl 0x556(%r15,%rcx,2), %ebp
testw %bp, %bp
je 0x7c19
movzwl 0x558(%r15,%rcx,2), %ebx
leaq -0x1(%rbx), %rax
movq %rax, 0x10(%rsp)
movzwl %bp, %esi
xorl %r12d, %r12d
movq %r15, %rdi
xorl %edx, %edx
callq 0x78a2
testl %eax, %eax
je 0x7c17
movq 0x28(%r15), %rcx
shrq %rcx
subq 0x10(%rsp), %rcx
cmpq $0x2, %rcx
jb 0x7b5b
movl $0x1, %edx
movl $0x9, %eax
movzwl 0x554(%r15,%rdx,2), %edx
testw %dx, %dx
jne 0x7b21
leal -0x7(%rax), %esi
cmpw $0x0, 0x554(%r15,%rsi,2)
jne 0x7b21
leal -0x6(%rax), %esi
cmpw $0x0, 0x554(%r15,%rsi,2)
je 0x7b4e
cmpw 0x2(%r14), %dx
jne 0x7b4e
leal -0x7(%rax), %esi
movzwl 0x554(%r15,%rsi,2), %edi
cmpw 0x4(%r14), %di
jne 0x7b4e
leal -0x6(%rax), %edi
movzwl 0x554(%r15,%rdi,2), %r8d
cmpw 0x6(%r14), %r8w
je 0x7b76
leal (%rbx,%rax), %edx
addl $-0x8, %edx
addl %ebx, %eax
cmpq %rdx, %rcx
ja 0x7af5
movzwl 0x554(%r15), %ebp
testw %bp, %bp
jne 0x7ac1
xorl %eax, %eax
xorl %r12d, %r12d
jmp 0x7c19
movq 0x8(%rsp), %r8
movw %dx, (%r8)
movzwl 0x554(%r15,%rsi,2), %ecx
movw %cx, 0x2(%r8)
movzwl 0x554(%r15,%rdi,2), %ecx
movw %cx, 0x4(%r8)
leal -0x5(%rax), %ecx
movzwl 0x554(%r15,%rcx,2), %ecx
movw %cx, 0x6(%r8)
leal -0x4(%rax), %ecx
movzwl 0x554(%r15,%rcx,2), %ecx
movw %cx, 0x8(%r8)
leal -0x3(%rax), %ecx
movzwl 0x554(%r15,%rcx,2), %ecx
movw %cx, 0xa(%r8)
leal -0x2(%rax), %ecx
movzwl 0x554(%r15,%rcx,2), %ecx
movw %cx, 0xc(%r8)
leal -0x1(%rax), %ecx
movzwl 0x554(%r15,%rcx,2), %ecx
movw %cx, 0xe(%r8)
movl %eax, %ecx
movzwl 0x554(%r15,%rcx,2), %ecx
movw %cx, 0x10(%r8)
movw %bp, 0x12(%r8)
addl $-0x8, %eax
movw %ax, 0x14(%r8)
movq %r15, 0x20(%r8)
movl $0x1, %r12d
xorl %eax, %eax
testb %al, %al
je 0x7c50
addl $0x4, %r13d
movq 0x28(%r15), %rcx
shrq %rcx
addq $-0x3, %rcx
movq %r13, %rax
cmpq %r13, %rcx
ja 0x7a7a
movzwl 0x554(%r15), %eax
testw %ax, %ax
movl $0x0, %ebx
jne 0x7a43
jmp 0x7c53
movl %r12d, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq