// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/rt11.c
// Function: validate
static int validate(
  struct mountedFS *mount,
  uint8_t unit,
  uint16_t *maxblk,
  uint16_t *first
)
{
  struct RT11data *data = &mount->rt11data;
  int type = RT11_SINGLE;
  uint16_t entrysz, position, dsseg = 1;
  uint16_t seg_count, seg_highest, highest = 0;
  uint8_t seen[RT11_DS_MAX + 1];

  if (rt11ReadBlock(mount, unit, RT11_HOME, NULL) == 0)
    return RT11_NOPART;

  if (!SWISSET('f')) {
    if ((type = partitionType(mount, unit)) == RT11_NOPART)
      return RT11_NOPART;

    *first = le16toh(data->buf[RT11_HB_FIRST]);
  } else *first = RT11_DSSTART;

  memset(seen, 0, sizeof(seen));

  /*
   * Now walk the directory to validate it's integrity.
   */
  do {
    uint16_t off = RT11_DH_SIZE;

    if (rt11ReadDirSegment(mount, unit, dsseg) == 0)
      return RT11_NOPART;

    /*
     * Make sure we only look at each directory segment once - we want to
     * avoid looping up with invalid input.
     */
    if (seen[dsseg]++ != 0)
      return RT11_NOPART;

    entrysz = RT11_DI_SIZE + (le16toh(data->buf[RT11_DH_EXTRA]) >> 1);
    position = le16toh(data->buf[RT11_DH_START]);
 
    if (dsseg == 1) {
      seg_count = le16toh(data->buf[RT11_DH_COUNT]);
      seg_highest = le16toh(data->buf[RT11_DH_HIGHEST]);
      if ((seg_highest > RT11_DS_MAX) || (seg_highest > seg_count))
        return RT11_NOPART;
    }

    if (seg_count != le16toh(data->buf[RT11_DH_COUNT]))
      return RT11_NOPART;

    /*
     * Loop until we see and end-of-segment marker or there is no room for
     * another directory entry.
     */
    while (!RT11EOS(le16toh(data->buf[off + RT11_DI_STATUS])) &&
           ((RT11_DS_SIZE - off) >= entrysz)) {
      uint16_t status = le16toh(data->buf[off + RT11_DI_STATUS]);

      position += le16toh(data->buf[off + RT11_DI_LENGTH]);

      if ((status & RT11_E_MPTY) != 0)
        break;

      /*
       * Within each directory segment the base address should never
       * decrease.
       */
      if (((position + le16toh(data->buf[off + RT11_DI_LENGTH])) & 0xFFFF) < position)
        return RT11_NOPART;

      off += entrysz;
    }
    if (position > highest)
      highest = position;

    dsseg = le16toh(data->buf[RT11_DH_NEXT]);

    if (dsseg > seg_highest)
      return RT11_NOPART;
   } while (dsseg != 0);

  *maxblk = highest - 1;
  return type;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %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)
movw $0x1, -0x3a(%rbp)
movw $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
movzbl -0x11(%rbp), %esi
callq 0xbdc0
cmpl $0x0, %eax
jne 0xf6d0
movl $0x0, -0x4(%rbp)
jmp 0xf99f
leaq 0xfb21(%rip), %rax      # 0x1f1f8
movl (%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0xf71e
movq -0x10(%rbp), %rdi
movzbl -0x11(%rbp), %esi
callq 0xf9b0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jne 0xf702
movl $0x0, -0x4(%rbp)
jmp 0xf99f
movq -0x30(%rbp), %rax
movzwl 0x5fe(%rax), %edi
callq 0xc6b0
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, (%rax)
jmp 0xf727
movq -0x28(%rbp), %rax
movw $0x6, (%rax)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3170
movw $0x5, -0x62(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %al
movzbl %al, %esi
movzwl -0x3a(%rbp), %edx
callq 0xc3e0
cmpl $0x0, %eax
jne 0xf761
movl $0x0, -0x4(%rbp)
jmp 0xf99f
movzwl -0x3a(%rbp), %eax
movl %eax, %ecx
movb -0x60(%rbp,%rcx), %al
movb %al, %dl
addb $0x1, %dl
movb %dl, -0x60(%rbp,%rcx)
movzbl %al, %eax
cmpl $0x0, %eax
je 0xf788
movl $0x0, -0x4(%rbp)
jmp 0xf99f
movq -0x30(%rbp), %rax
movzwl 0x430(%rax), %edi
callq 0xc6b0
movzwl %ax, %eax
sarl %eax
addl $0x7, %eax
movw %ax, -0x36(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x432(%rax), %edi
callq 0xc6b0
movw %ax, -0x38(%rbp)
movzwl -0x3a(%rbp), %eax
cmpl $0x1, %eax
jne 0xf80c
movq -0x30(%rbp), %rax
movzwl 0x42a(%rax), %edi
callq 0xc6b0
movw %ax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x42e(%rax), %edi
callq 0xc6b0
movw %ax, -0x3e(%rbp)
movzwl -0x3e(%rbp), %eax
cmpl $0x1f, %eax
jg 0xf7fe
movzwl -0x3e(%rbp), %eax
movzwl -0x3c(%rbp), %ecx
cmpl %ecx, %eax
jle 0xf80a
movl $0x0, -0x4(%rbp)
jmp 0xf99f
jmp 0xf80c
movzwl -0x3c(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x42a(%rax), %edi
callq 0xc6b0
movw %ax, %cx
movl -0x68(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
je 0xf83c
movl $0x0, -0x4(%rbp)
jmp 0xf99f
jmp 0xf83e
movq -0x30(%rbp), %rax
movzwl -0x62(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
andl $0x800, %ecx            # imm = 0x800
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x69(%rbp)
jne 0xf883
movzwl -0x62(%rbp), %ecx
movl $0x200, %eax            # imm = 0x200
subl %ecx, %eax
movzwl -0x36(%rbp), %ecx
cmpl %ecx, %eax
setge %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0xf88f
jmp 0xf93c
movq -0x30(%rbp), %rax
movzwl -0x62(%rbp), %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movw %ax, -0x64(%rbp)
movq -0x30(%rbp), %rax
movzwl -0x62(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movzwl %ax, %ecx
movzwl -0x38(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x38(%rbp)
movzwl -0x64(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xf8e6
jmp 0xf93c
movzwl -0x38(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movzwl -0x62(%rbp), %ecx
addl $0x4, %ecx
movslq %ecx, %rcx
movzwl 0x42a(%rax,%rcx,2), %edi
callq 0xc6b0
movw %ax, %cx
movl -0x70(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
andl $0xffff, %eax           # imm = 0xFFFF
movzwl -0x38(%rbp), %ecx
cmpl %ecx, %eax
jge 0xf929
movl $0x0, -0x4(%rbp)
jmp 0xf99f
movzwl -0x36(%rbp), %ecx
movzwl -0x62(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x62(%rbp)
jmp 0xf83e
movzwl -0x38(%rbp), %eax
movzwl -0x40(%rbp), %ecx
cmpl %ecx, %eax
jle 0xf950
movw -0x38(%rbp), %ax
movw %ax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x42c(%rax), %edi
callq 0xc6b0
movw %ax, -0x3a(%rbp)
movzwl -0x3a(%rbp), %eax
movzwl -0x3e(%rbp), %ecx
cmpl %ecx, %eax
jle 0xf979
movl $0x0, -0x4(%rbp)
jmp 0xf99f
jmp 0xf97b
movzwl -0x3a(%rbp), %eax
cmpl $0x0, %eax
jne 0xf737
movzwl -0x40(%rbp), %eax
subl $0x1, %eax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)