// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/dosmt.c
// Function: dosmtLookupFile
int dosmtLookupFile(
  struct mountedFS *mount,
  struct dosmtFileSpec *spec,
  struct dosmtOpenFile *file
)
{
  struct DOSMTdata *data = &mount->dosmtdata;
  uint32_t status;

  if (!SWISSET('n'))
    tapeRewind(mount->container);

  do {
    switch (status = tapeReadRecord(mount->container, data->buf, DOSMTRCLNT)) {
      case ST_FAIL:
        goto error;

      case ST_EOM:
        break;

      case ST_TM:
        /* Second tape mark in a row - treat as end-of-media */
        status = ST_EOM;
        break;

      default:
        if ((status & ST_ERROR) == 0) {
          if (status == sizeof(struct dosmthdr)) {
            struct dosmthdr *hdr = (struct dosmthdr *)data->buf;

            /*
             * Determine if this file matches the filespec.
             */
            if ((spec->flags & DOSMT_WC_PROJ) == 0)
              if (spec->proj != hdr->proj)
                goto nomatch;

            if ((spec->flags & DOSMT_WC_PROG) == 0)
              if (spec->prog != hdr->prog)
                goto nomatch;

            if ((spec->flags & DOSMT_WC_NAME) == 0) {
              if ((spec->name[0] != le16toh(hdr->fname[0])) ||
                  (spec->name[1] != le16toh(hdr->fname[1])))
                goto nomatch;

              if ((mount->flags & FS_DOSMTEXT) != 0)
                if (spec->name[2] != le16toh(hdr->fname3))
                  goto nomatch;
            }

            if ((spec->flags & DOSMT_WC_EXT) == 0)
              if (spec->ext != le16toh(hdr->ext))
                goto nomatch;

            /*
             * Found a matching file.
             */
            file->mount = mount;

            return 1;
          } else goto error;
        }

    nomatch:
        /*
         * Skip to next file.
         */
        do {
          if ((status = tapeReadRecordLength(mount->container)) == ST_FAIL)
            goto error;
        } while ((status != ST_EOM) && (status != ST_TM));

        if (status == ST_EOM)
          tapeSetPosition(mount->container, data->eot);
    }
  } while (status != ST_EOM);

 error:
  /*
   * Make sure we leave the tape at a valid position.
   */
  tapeSetPosition(mount->container, data->eot);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x28(%rbp)
leaq 0xe8e1(%rip), %rax      # 0x1f1f8
movl (%rax), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
jne 0x10930
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x68d0
jmp 0x10932
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x28(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x64d0
movl %eax, -0x2c(%rbp)
movl %eax, %ecx
movl %ecx, -0x3c(%rbp)
subl $0xfefefefe, %eax       # imm = 0xFEFEFEFE
je 0x1096c
jmp 0x10959
movl -0x3c(%rbp), %eax
subl $-0x1, %eax
je 0x10971
jmp 0x10963
movl -0x3c(%rbp), %eax
testl %eax, %eax
je 0x10976
jmp 0x10982
jmp 0x10b55
jmp 0x10b47
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x10b47
movl -0x2c(%rbp), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
jne 0x10aec
movl -0x2c(%rbp), %eax
cmpq $0xe, %rax
jne 0x10aea
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x109d2
movq -0x18(%rbp), %rax
movzbl 0xa(%rax), %eax
movq -0x38(%rbp), %rcx
movzbl 0x7(%rcx), %ecx
cmpl %ecx, %eax
je 0x109d0
jmp 0x10aee
jmp 0x109d2
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x109fc
movq -0x18(%rbp), %rax
movzbl 0xb(%rax), %eax
movq -0x38(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
cmpl %ecx, %eax
je 0x109fa
jmp 0x10aee
jmp 0x109fc
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x10a9a
movq -0x18(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movzwl (%rax), %edi
callq 0x10b80
movw %ax, %cx
movl -0x40(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
jne 0x10a58
movq -0x18(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x10b80
movw %ax, %cx
movl -0x44(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x10a5d
jmp 0x10aee
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x10a98
movq -0x18(%rbp), %rax
movzwl 0x6(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movzwl 0xc(%rax), %edi
callq 0x10b80
movw %ax, %cx
movl -0x48(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x10a96
jmp 0x10aee
jmp 0x10a98
jmp 0x10a9a
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x10ad2
movq -0x18(%rbp), %rax
movzwl 0x8(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x38(%rbp), %rax
movzwl 0x4(%rax), %edi
callq 0x10b80
movw %ax, %cx
movl -0x4c(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0x10ad0
jmp 0x10aee
jmp 0x10ad2
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x1, -0x4(%rbp)
jmp 0x10b74
jmp 0x10b55
jmp 0x10aee
jmp 0x10af0
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x6290
movl %eax, -0x2c(%rbp)
cmpl $0xfefefefe, %eax       # imm = 0xFEFEFEFE
jne 0x10b09
jmp 0x10b55
jmp 0x10b0b
xorl %eax, %eax
cmpl $-0x1, -0x2c(%rbp)
movb %al, -0x4d(%rbp)
je 0x10b20
cmpl $0x0, -0x2c(%rbp)
setne %al
movb %al, -0x4d(%rbp)
movb -0x4d(%rbp), %al
testb $0x1, %al
jne 0x10af0
cmpl $-0x1, -0x2c(%rbp)
jne 0x10b45
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x200(%rax), %rsi
callq 0x6230
jmp 0x10b47
jmp 0x10b49
cmpl $-0x1, -0x2c(%rbp)
jne 0x10932
jmp 0x10b55
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x200(%rax), %rsi
callq 0x6230
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)