// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/os8.c
// Function: os8LookupFile
int os8LookupFile(
  struct mountedFS *mount,
  uint8_t unit,
  struct os8FileSpec *spec,
  struct os8OpenFile *file
)
{
  struct OS8data *data = &mount->os8data;
  unsigned int dirblk = OS8_DSSTART;

  if (os8UnitValid(data, unit)) {
    do {
      uint16_t i, entrysz, entries, extra, startblk, off = OS8_DH_SIZE;

      if (os8ReadBlock(mount, unit, dirblk, NULL) == 0)
        return 0;

      entrysz = OS8_DI_SIZE + (-os8SXT(le16toh(data->buf[OS8_DH_EXTRA])));
      entries = os8Neg(le16toh(data->buf[OS8_DH_ENTRIES]));
      extra = os8Neg(le16toh(data->buf[OS8_DH_EXTRA]));

      startblk = os8Word(le16toh(data->buf[OS8_DH_START]));

      for (i = 0; i < entries; i++) {
        if (os8Word(le16toh(data->buf[off + OS8_DI_FNAME1])) != 0) {
          if ((os8Word(le16toh(data->buf[off + OS8_DI_FNAME1])) == spec->name[0]) &&
              (os8Word(le16toh(data->buf[off + OS8_DI_FNAME2])) == spec->name[1]) &&
              (os8Word(le16toh(data->buf[off + OS8_DI_FNAME3])) == spec->name[2]) &&
              (os8Word(le16toh(data->buf[off + OS8_DI_EXT])) == spec->ext)) {
            file->name[0] = spec->name[0];
            file->name[1] = spec->name[1];
            file->name[2] = spec->name[2];
            file->ext = spec->ext;
            file->creation =
              extra != 0 ? os8Word(le16toh(data->buf[off + OS8_DI_DATE])) : 0;
            file->length = os8Neg(le16toh(data->buf[off + extra + OS8_DI_LENGTH]));

            file->segment = dirblk;
            file->offset = off;
            file->entrysz = entrysz;
            file->extra = extra;
            file->remain = entries - (i + 1);

            file->mount = mount;
            file->unit = unit;

            file->current = 0;
            if (file->length != 0) {
              file->start = startblk;
              file->last = startblk + file->length - 1;
            } else file->start = file->last = 0;
            
            file->wordpos = OS8_BLOCKSIZE;
            file->bytepos = OS8_CHECK;
            file->written = 0;

            return 1;
          }
          startblk += os8Neg(le16toh(data->buf[off + extra + OS8_DI_LENGTH]));
          off += entrysz;
        } else {
          startblk += os8Neg(le16toh(data->buf[off + OS8_ED_LENGTH]));
          off += OS8_ED_SIZE;
        }
      }
      dirblk = os8Word(le16toh(data->buf[OS8_DH_NEXT]));
    } while (dirblk != 0);
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r13
movq 0x50(%rdi), %rdx
xorl %eax, %eax
testq %rdx, %rdx
je 0xe455
cmpb %sil, 0x8(%rdx)
jbe 0xe455
movq %rdi, %r14
movzbl 0x59(%rdi), %edx
movzbl %sil, %ebp
btl %ebp, %edx
jae 0xe455
movl %esi, 0xc(%rsp)
movq %rcx, 0x10(%rsp)
movl $0x1, %r15d
movw $0xfff, %bx             # imm = 0xFFF
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0xd707
testl %eax, %eax
je 0xe38f
xorl %esi, %esi
subw 0x5c(%r14), %si
andw $0xfff, %si             # imm = 0xFFF
je 0xe37d
movzwl 0x64(%r14), %eax
movl %eax, %ecx
movl $0xfff, %r8d            # imm = 0xFFF
andl %r8d, %ecx
movl %eax, %edx
shll $0x4, %edx
movswl %dx, %edi
sarl $0xf, %edi
andl $0xfffff000, %edi       # imm = 0xFFFFF000
orl %ecx, %edi
movl $0x5, %edx
subl %edi, %edx
negl %eax
andl %r8d, %eax
movzwl 0x5e(%r14), %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movzwl %ax, %edi
addl $0x4, %edi
movzwl %si, %r8d
movl $0x1, %esi
subl %r8d, %esi
movw $0x5, %r9w
movzwl %r9w, %r8d
movzwl 0x5c(%r14,%r8,2), %r11d
andw %bx, %r11w
je 0xe349
cmpw 0x2(%r13), %r11w
jne 0xe341
movl %r8d, %r10d
movzwl 0x5e(%r14,%r10,2), %r12d
andl $0xfff, %r12d           # imm = 0xFFF
cmpw 0x4(%r13), %r12w
jne 0xe341
movzwl 0x60(%r14,%r10,2), %r12d
andl $0xfff, %r12d           # imm = 0xFFF
cmpw 0x6(%r13), %r12w
jne 0xe341
movzwl 0x62(%r14,%r10,2), %r12d
andl $0xfff, %r12d           # imm = 0xFFF
cmpw 0x8(%r13), %r12w
je 0xe396
addl %edi, %r8d
movl %edx, %r10d
jmp 0xe351
incl %r8d
movw $0x2, %r10w
movl %r8d, %r8d
xorl %r11d, %r11d
subw 0x5c(%r14,%r8,2), %r11w
andl $0xfff, %r11d           # imm = 0xFFF
movswl %r10w, %r8d
addl %r9d, %r8d
addl %r11d, %ecx
incl %esi
movl %r8d, %r9d
cmpw $0x1, %si
jne 0xe2eb
movzwl 0x60(%r14), %r15d
andl $0xfff, %r15d           # imm = 0xFFF
jne 0xe271
xorl %eax, %eax
jmp 0xe455
movq 0x10(%rsp), %r9
movw %r11w, (%r9)
movq %r9, %r11
movzwl 0x4(%r13), %r9d
movw %r9w, 0x2(%r11)
movzwl 0x6(%r13), %r9d
movw %r9w, 0x4(%r11)
movzwl 0x8(%r13), %r9d
movw %r9w, 0x6(%r11)
testw %ax, %ax
je 0xe3d4
movzwl 0x64(%r14,%r10,2), %r9d
andl $0xfff, %r9d            # imm = 0xFFF
jmp 0xe3d7
xorl %r9d, %r9d
movl 0xc(%rsp), %r10d
movw %r9w, 0x8(%r11)
addl %r8d, %edi
xorl %r9d, %r9d
subw 0x5c(%r14,%rdi,2), %r9w
xorl %edi, %edi
andl $0xfff, %r9d            # imm = 0xFFF
movw %r9w, 0xa(%r11)
movb %r15b, 0xc(%r11)
movw %r8w, 0xe(%r11)
movw %dx, 0x10(%r11)
movw %ax, 0x12(%r11)
negl %esi
movw %si, 0x14(%r11)
movq %r14, 0x20(%r11)
movb %r10b, 0x28(%r11)
movw %di, 0x3a(%r11)
testl %r9d, %r9d
leal -0x1(%rcx,%r9), %eax
cmovel %r9d, %eax
cmovnel %ecx, %r9d
movw %ax, 0x3c(%r11)
movw %r9w, 0x38(%r11)
movw $0x100, 0x3e(%r11)      # imm = 0x100
movb $0x3, 0x40(%r11)
movq $0x0, 0x48(%r11)
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq