// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x7ab3
movl $0x0, -0x4(%rbp)
jmp 0x7e78
movq -0x28(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x7aea
movl $0x0, -0x4(%rbp)
jmp 0x7e78
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
shrq %rcx
subq $0x3, %rcx
cmpq %rcx, %rax
jae 0x7e51
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0x3a(%rbp)
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movw %ax, -0x3c(%rbp)
movzwl -0x3a(%rbp), %eax
cmpl $0x0, %eax
je 0x7e41
movzwl -0x3a(%rbp), %eax
movq -0x18(%rbp), %rcx
movzbl 0x9(%rcx), %ecx
shll $0x8, %ecx
movq -0x18(%rbp), %rdx
movzbl 0x8(%rdx), %edx
orl %edx, %ecx
cmpl %ecx, %eax
jne 0x7e3f
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x7e3d
jmp 0x7b9f
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x7380
cmpl $0x0, %eax
jne 0x7bc0
movl $0x0, -0x4(%rbp)
jmp 0x7e78
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
shrq %rcx
movzwl -0x3c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x7e14
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x7c56
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x0, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x7c56
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x7c56
jmp 0x7e05
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
movq -0x18(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jne 0x7cd1
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x0, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
movq -0x18(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jne 0x7cd1
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzwl 0x50c(%rax,%rcx,2), %edi
callq 0x6c50
movzwl %ax, %eax
movq -0x18(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
cmpl %ecx, %eax
je 0x7cd6
jmp 0x7e05
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x0, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x0, %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x6(%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x4, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x5, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0xa(%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x6, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0xc(%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x7, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0xe(%rax)
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x8, %ecx
movl %ecx, %ecx
movw 0x50c(%rax,%rcx,2), %cx
movq -0x20(%rbp), %rax
movw %cx, 0x10(%rax)
movl -0x30(%rbp), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x12(%rax)
movl -0x38(%rbp), %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x7e78
movzwl -0x3c(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x7bc7
movq -0x28(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x7b9f
movl $0x0, -0x4(%rbp)
jmp 0x7e78
jmp 0x7e3f
jmp 0x7e41
jmp 0x7e43
movl -0x34(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x34(%rbp)
jmp 0x7af1
movq -0x28(%rbp), %rax
movzwl 0x50c(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x7ac9
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)