// File: /workspace/github_c_makefile_star2_O2/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 %rcx, %rbx
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r14
addq $0x48, %rdi
callq 0xd345
testl %eax, %eax
je 0xd27f
movq %rbx, 0x10(%rsp)
pushq $0x1
popq %r15
movl %ebp, 0x8(%rsp)
movzbl %bpl, %eax
movl %eax, 0xc(%rsp)
movl $0xfff, %r13d           # imm = 0xFFF
movw $0xfff, %bx             # imm = 0xFFF
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl %r15d, %edx
xorl %ecx, %ecx
callq 0xc5d7
testl %eax, %eax
je 0xd27f
movzwl 0x64(%r14), %eax
movl %eax, %ecx
andl %r13d, %ecx
movl %eax, %edx
shll $0x4, %edx
movswl %dx, %esi
sarl $0xf, %esi
andl $0xfffff000, %esi       # imm = 0xFFFFF000
orl %ecx, %esi
pushq $0x5
popq %rdx
subl %esi, %edx
xorl %r8d, %r8d
subw 0x5c(%r14), %r8w
andl %r13d, %r8d
negl %eax
andl %r13d, %eax
movzwl 0x5e(%r14), %ecx
andl %r13d, %ecx
movl %eax, %edi
addl $0x4, %edi
pushq $0x1
popq %rsi
subl %r8d, %esi
movw $0x5, %r9w
cmpw $0x1, %si
je 0xd26d
movzwl %r9w, %r8d
movzwl 0x5c(%r14,%r8,2), %r11d
andw %bx, %r11w
je 0xd23e
cmpw 0x2(%r12), %r11w
jne 0xd236
movl %r8d, %r10d
movzwl 0x5e(%r14,%r10,2), %ebp
andl $0xfff, %ebp            # imm = 0xFFF
cmpw 0x4(%r12), %bp
jne 0xd236
movzwl 0x60(%r14,%r10,2), %ebp
andl $0xfff, %ebp            # imm = 0xFFF
cmpw 0x6(%r12), %bp
jne 0xd236
movzwl 0x62(%r14,%r10,2), %ebp
andl $0xfff, %ebp            # imm = 0xFFF
cmpw 0x8(%r12), %bp
je 0xd286
addl %edi, %r8d
movl %edx, %r10d
jmp 0xd246
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
jmp 0xd1d5
movzwl 0x60(%r14), %r15d
andl $0xfff, %r15d           # imm = 0xFFF
jne 0xd172
xorl %eax, %eax
jmp 0xd336
movq 0x10(%rsp), %rbx
movw %r11w, (%rbx)
movzwl 0x4(%r12), %r9d
movw %r9w, 0x2(%rbx)
movzwl 0x6(%r12), %r9d
movw %r9w, 0x4(%rbx)
movzwl 0x8(%r12), %r9d
movw %r9w, 0x6(%rbx)
testw %ax, %ax
je 0xd2c4
movzwl 0x64(%r14,%r10,2), %r9d
andl $0xfff, %r9d            # imm = 0xFFF
jmp 0xd2c7
xorl %r9d, %r9d
movw %r9w, 0x8(%rbx)
addl %r8d, %edi
xorl %r9d, %r9d
subw 0x5c(%r14,%rdi,2), %r9w
andl $0xfff, %r9d            # imm = 0xFFF
movw %r9w, 0xa(%rbx)
movb %r15b, 0xc(%rbx)
movw %r8w, 0xe(%rbx)
movw %dx, 0x10(%rbx)
movw %ax, 0x12(%rbx)
negl %esi
movw %si, 0x14(%rbx)
movq %r14, 0x20(%rbx)
movl 0x8(%rsp), %eax
movb %al, 0x28(%rbx)
andw $0x0, 0x3a(%rbx)
testl %r9d, %r9d
leal -0x1(%rcx,%r9), %eax
cmovel %r9d, %eax
cmovnel %ecx, %r9d
movw %ax, 0x3c(%rbx)
movw %r9w, 0x38(%rbx)
movw $0x100, 0x3e(%rbx)      # imm = 0x100
movb $0x3, 0x40(%rbx)
andq $0x0, 0x48(%rbx)
pushq $0x1
popq %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq