// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x30(%rbp)
movl $0x1, -0x34(%rbp)
movq -0x30(%rbp), %rdi
movzbl -0x11(%rbp), %esi
callq 0x14f20
cmpl $0x0, %eax
je 0x14f04
jmp 0x14b15
movw $0x5, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movl -0x34(%rbp), %edx
xorl %ecx, %ecx
movzbl %al, %esi
callq 0x132f0
cmpl $0x0, %eax
jne 0x14b40
movl $0x0, -0x4(%rbp)
jmp 0x14f0b
movq -0x30(%rbp), %rax
movzwl 0x1c(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x148b0
movzwl %ax, %ecx
xorl %eax, %eax
subl %ecx, %eax
addl $0x5, %eax
movw %ax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x14(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, -0x3a(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x1c(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x16(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movw %ax, -0x3e(%rbp)
movw $0x0, -0x36(%rbp)
movzwl -0x36(%rbp), %eax
movzwl -0x3a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x14edd
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
cmpl $0x0, %eax
je 0x14e92
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
movq -0x20(%rbp), %rcx
movzwl 0x2(%rcx), %ecx
cmpl %ecx, %eax
jne 0x14e4f
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
movq -0x20(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jne 0x14e4f
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
movq -0x20(%rbp), %rcx
movzwl 0x6(%rcx), %ecx
cmpl %ecx, %eax
jne 0x14e4f
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
movq -0x20(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jne 0x14e4f
movq -0x20(%rbp), %rax
movw 0x2(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
movq -0x20(%rbp), %rax
movw 0x4(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x20(%rbp), %rax
movw 0x6(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x20(%rbp), %rax
movw 0x8(%rax), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x6(%rax)
movzwl -0x3c(%rbp), %eax
cmpl $0x0, %eax
je 0x14d2c
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
movl %eax, -0x44(%rbp)
jmp 0x14d33
xorl %eax, %eax
movl %eax, -0x44(%rbp)
jmp 0x14d33
movl -0x44(%rbp), %eax
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0x8(%rax)
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
movzwl -0x3c(%rbp), %edx
addl %edx, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0xa(%rax)
movl -0x34(%rbp), %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0xc(%rax)
movw -0x40(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0xe(%rax)
movw -0x38(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x10(%rax)
movw -0x3c(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x12(%rax)
movzwl -0x3a(%rbp), %eax
movzwl -0x36(%rbp), %ecx
addl $0x1, %ecx
subl %ecx, %eax
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0x14(%rax)
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movb -0x11(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x28(%rax)
movq -0x28(%rbp), %rax
movw $0x0, 0x3a(%rax)
movq -0x28(%rbp), %rax
movzwl 0xa(%rax), %eax
cmpl $0x0, %eax
je 0x14e11
movw -0x3e(%rbp), %cx
movq -0x28(%rbp), %rax
movw %cx, 0x38(%rax)
movzwl -0x3e(%rbp), %eax
movq -0x28(%rbp), %rcx
movzwl 0xa(%rcx), %ecx
addl %ecx, %eax
subl $0x1, %eax
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0x3c(%rax)
jmp 0x14e25
movq -0x28(%rbp), %rax
movw $0x0, 0x3c(%rax)
movq -0x28(%rbp), %rax
movw $0x0, 0x38(%rax)
movq -0x28(%rbp), %rax
movw $0x100, 0x3e(%rax)      # imm = 0x100
movq -0x28(%rbp), %rax
movb $0x3, 0x40(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x48(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x14f0b
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
movzwl -0x3c(%rbp), %edx
addl %edx, %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movzwl %ax, %ecx
movzwl -0x3e(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x3e(%rbp)
movzwl -0x38(%rbp), %ecx
movzwl -0x40(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x40(%rbp)
jmp 0x14eca
movq -0x30(%rbp), %rax
movzwl -0x40(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl 0x14(%rax,%rcx,2), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14860
movzwl %ax, %ecx
movzwl -0x3e(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x3e(%rbp)
movzwl -0x40(%rbp), %eax
addl $0x2, %eax
movw %ax, -0x40(%rbp)
jmp 0x14ecc
movw -0x36(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x36(%rbp)
jmp 0x14bb4
movq -0x30(%rbp), %rax
movzwl 0x18(%rax), %edi
callq 0x14890
movzwl %ax, %edi
callq 0x14f90
movzwl %ax, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x14b15
jmp 0x14f04
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)