// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq 0x50(%rdi), %rdx
xorl %eax, %eax
testq %rdx, %rdx
je 0xe743
cmpb %sil, 0x8(%rdx)
jbe 0xe743
movq %rdi, %r15
movzbl 0x59(%rdi), %edi
movzbl %sil, %edx
movl %edx, 0xc(%rsp)
btl %edx, %edi
jae 0xe743
movl %esi, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
movl $0x1, %r13d
movw $0xfff, %bx             # imm = 0xFFF
movq %r15, %rdi
movl 0xc(%rsp), %esi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0xd979
testl %eax, %eax
je 0xe64b
xorl %esi, %esi
subw 0x5c(%r15), %si
andw $0xfff, %si             # imm = 0xFFF
je 0xe63b
movzwl 0x64(%r15), %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(%r15), %ecx
andl $0xfff, %ecx            # imm = 0xFFF
movzwl %ax, %edi
leal 0x4(%rdi), %r8d
movzwl %si, %r9d
movl $0x1, %esi
subl %r9d, %esi
movw $0x5, %r10w
movzwl %r10w, %r9d
movzwl 0x5c(%r15,%r9,2), %r11d
andw %bx, %r11w
je 0xe60a
movzwl 0x2(%r14), %r12d
cmpw %r12w, %r11w
jne 0xe602
movl %r9d, %r11d
movzwl 0x5e(%r15,%r11,2), %ebp
andl $0xfff, %ebp            # imm = 0xFFF
cmpw 0x4(%r14), %bp
jne 0xe602
movzwl 0x60(%r15,%r11,2), %ebp
andl $0xfff, %ebp            # imm = 0xFFF
cmpw 0x6(%r14), %bp
jne 0xe602
movzwl 0x62(%r15,%r11,2), %ebp
andl $0xfff, %ebp            # imm = 0xFFF
cmpw 0x8(%r14), %bp
je 0xe66b
addl %r8d, %r9d
movl %edx, %r11d
jmp 0xe612
incl %r9d
movw $0x2, %r11w
movl %r9d, %r9d
xorl %ebp, %ebp
subw 0x5c(%r15,%r9,2), %bp
andl $0xfff, %ebp            # imm = 0xFFF
movswl %r11w, %r9d
addl %r10d, %r9d
addl %ebp, %ecx
incl %esi
movl %r9d, %r10d
cmpw $0x1, %si
jne 0xe5ab
movzwl 0x60(%r15), %r13d
andl $0xfff, %r13d           # imm = 0xFFF
movb $0x1, %al
jmp 0xe655
movl $0x0, 0x4(%rsp)
xorl %eax, %eax
testb %al, %al
je 0xe73b
testl %r13d, %r13d
jne 0xe52e
jmp 0xe741
movq 0x10(%rsp), %r10
movw %r12w, (%r10)
movzwl 0x4(%r14), %r8d
movw %r8w, 0x2(%r10)
movzwl 0x6(%r14), %r8d
movw %r8w, 0x4(%r10)
movzwl 0x8(%r14), %r8d
movw %r8w, 0x6(%r10)
testw %ax, %ax
je 0xe6a6
movzwl 0x64(%r15,%r11,2), %r8d
andl $0xfff, %r8d            # imm = 0xFFF
jmp 0xe6a9
xorl %r8d, %r8d
movq 0x10(%rsp), %r10
movw %r8w, 0x8(%r10)
addl %r9d, %edi
xorl %r8d, %r8d
subw 0x64(%r15,%rdi,2), %r8w
negl %esi
andw $0xfff, %r8w            # imm = 0xFFF
movw %r8w, 0xa(%r10)
movb %r13b, 0xc(%r10)
movw %r9w, 0xe(%r10)
movw %dx, 0x10(%r10)
movw %ax, 0x12(%r10)
movw %si, 0x14(%r10)
movq %r15, 0x20(%r10)
movl 0x8(%rsp), %eax
movb %al, 0x28(%r10)
movl $0x0, %eax
movw %ax, 0x3a(%r10)
je 0xe710
movw %cx, 0x38(%r10)
movzwl %r8w, %eax
addl %ecx, %eax
decl %eax
movw %ax, 0x3c(%r10)
jmp 0xe71a
movw %ax, 0x3c(%r10)
movw %ax, 0x38(%r10)
movw $0x100, 0x3e(%r10)      # imm = 0x100
movb $0x3, 0x40(%r10)
movq $0x0, 0x48(%r10)
movl $0x1, 0x4(%rsp)
jmp 0xe653
movl 0x4(%rsp), %eax
jmp 0xe743
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq