// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11ReadBlock
int rt11ReadBlock(
  struct mountedFS *mount,
  uint8_t unit,
  unsigned int block,
  void *buf
)
{
  struct RT11data *data = &mount->rt11data;
  char *buffer = buf == NULL ? data->buf : buf;
  int status = 0;

  if (RT11_PARTITIONVALID(data, unit)) {
    if (block > data->maxblk[unit]) {
      ERROR("Attempt to read block (%u) outside file system \"%s%o:\"\n",
            block, mount->name, unit);
      return 0;
    }

#ifdef DEBUG
    if ((mount->flags & FS_DEBUG) != 0)
      fprintf(DEBUGout, ">> %s%o: (rt11) Reading logical block %o\n",
              mount->name, unit, block);
#endif

    if (data->sectorsz != 0) {
      /*
       * The sectors making up the disk are sector-interleaved. The algorithm
       * below is for RX01/RX02 interleave. Note that all such devices are
       * small enough that there can only be a single file system present.
       */
      unsigned int count = RT11_BLOCKSIZE / data->sectorsz;
      unsigned int sectno = block * count;

      do {
        unsigned int sector = MapLogToPhys(sectno);

        status = FSioReadSector(mount, sector, data->sectorsz, buffer);

        count--;
        buffer += data->sectorsz;
        sectno++;
      } while ((count != 0) && (status != 0));
    } else status = FSioReadBlock(mount, (unit << 16) | block, buffer);

    if (status == 0)
      ERROR("I/O error on \"%s%o:\"\n", mount->name, unit);
  } else ERROR("Invalid device \"%s%o:\"\n", mount->name, unit);

  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r14
leaq 0x472(%rdi), %r15
testq %rcx, %rcx
cmovneq %rcx, %r15
movl %esi, %eax
shrl $0x4, %eax
movzwl 0x52(%rdi,%rax,2), %eax
movl %esi, %ecx
andl $0xf, %ecx
btl %ecx, %eax
jae 0x9dfc
movl %edx, %ebp
movzbl %bl, %eax
movzwl 0x72(%r14,%rax,2), %eax
cmpl %edx, %eax
jb 0x9e10
testb $0x2, 0x30(%r14)
je 0x9d4a
leaq 0xf288(%rip), %rax      # 0x18fb8
movq (%rax), %rdi
leaq 0x8(%r14), %rdx
leaq 0x826e(%rip), %rsi      # 0x11fac
movl %ebx, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x31d0
movl 0x4c(%r14), %ecx
testl %ecx, %ecx
je 0x9dcc
movl $0x200, %eax            # imm = 0x200
xorl %edx, %edx
divl %ecx
movl %eax, %r12d
imull %eax, %ebp
movl %ebp, %eax
imulq $0x4ec4ec4f, %rax, %rcx # imm = 0x4EC4EC4F
shrq $0x23, %rcx
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %ecx, %eax
movl %ebp, %edx
subl %eax, %edx
leal (%rdx,%rdx), %esi
xorl %edi, %edi
cmpl $0xd, %edx
setae %dil
leal (%rcx,%rcx,2), %ecx
leal (%rsi,%rcx,2), %esi
orl %edi, %esi
imulq $0x4ec4ec4f, %rsi, %rcx # imm = 0x4EC4EC4F
shrq $0x23, %rcx
leal (%rcx,%rcx,4), %edx
leal (%rdx,%rdx,4), %edx
addl %ecx, %edx
subl %edx, %esi
addl %eax, %esi
movl 0x4c(%r14), %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x62c6
movl %eax, %r13d
decl %r12d
je 0x9de1
movl 0x4c(%r14), %eax
addq %rax, %r15
incl %ebp
testl %r13d, %r13d
jne 0x9d61
jmp 0x9de1
movl %ebx, %esi
shll $0x10, %esi
orl %ebp, %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x6234
movl %eax, %r13d
testl %r13d, %r13d
je 0x9e70
movl %r13d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4(%rsp), %r15
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x3d7d
jmp 0x9e80
movq 0xe1d9(%rip), %rax      # 0x17ff0
movq (%rax), %rdi
addq $0x8, %r14
leaq 0x814f(%rip), %rsi      # 0x11f74
xorl %r13d, %r13d
movl %ebp, %edx
movq %r14, %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x31d0
leaq 0xf17a(%rip), %rax      # 0x18fb8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x9dea
movq 0xe16b(%rip), %rax      # 0x17fb8
cmpq (%rax), %rdi
je 0x9dea
leaq 0x811b(%rip), %rsi      # 0x11f74
xorl %r13d, %r13d
movl %ebp, %edx
movq %r14, %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x31d0
jmp 0x9dea
movq %rsp, %r15
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x3d1a
movl (%r15), %r13d
jmp 0x9dea