// File: /workspace/github_c_makefile_star2_O1/simh[P]simtools/converters/fsio/os8.c
// Function: rx02ReadBlock
static int rx02ReadBlock(
  struct mountedFS *mount,
  uint8_t unit,
  unsigned int block,
  void *buf
)
{
  unsigned int base = (block / RX02REPEAT) * RX02REPEAT;
  unsigned int offset = block % RX02REPEAT;
  uint16_t *buffer = buf;
  int i, j, k, status;

#ifdef DEBUG
  if ((mount->flags & FS_DEBUG) != 0)
    fprintf(DEBUGout, "  >>%s%o: rx02ReadBlock, base %u, offset %u\n",
            mount->name, unit, base, offset);
#endif

  /*
   * Convert to sector number
   */
  base *= OS8_BLOCKSIZE / OS8_RX02SS_W;

  /*
   * Read 2 sectors into the buffer
   */
  for (i = 0; i < 2; i++) {
    unsigned int sector = base + rx02interleave[(offset * 2) + i];
    unsigned char temp[OS8_RX02SS];

#ifdef DEBUG
    if ((mount->flags & FS_DEBUG) != 0)
      fprintf(DEBUGout, "  >> %s%o: (os8) Reading %u, track %u sector %u\n",
              mount->name, unit, sector,
              base + ((rx02interleave[(offset * 2) + i] + 1) / OS8_RX02SS),
              (rx02interleave[(offset * 2) + i] + 1) % OS8_RX02SS);
#endif

    if ((status = FSioReadSector(mount, sector, OS8_RX02SS, temp)) == 0) {
      ERROR("I/O error on \"%s%o:\"\n", mount->name, unit);
      return 0;
    }

    /*
     * Unpack 128 words from the temporary buffer
     */
    for (j = 0, k = 0; j < OS8_RX02SS_W; j += 2, k += 3) {
      buffer[j] = ((temp[k] << 4) | ((temp[k + 1] >> 4) & 0xF)) & 0xFFF;
      buffer[j + 1] = (((temp[k + 1] << 8) & 0xF00) | temp[k + 2]) & 0xFFF;
    }
    buffer += OS8_RX02SS_W;
  }
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rcx, %r15
movl %edx, %ebx
movq %rdi, %r14
movl %edx, %eax
imulq $0x4ec4ec4f, %rax, %rax # imm = 0x4EC4EC4F
shrq $0x22, %rax
leal (%rax,%rax,2), %ecx
leal (%rax,%rcx,4), %r12d
subl %r12d, %ebx
testb $0x2, 0x30(%rdi)
movzbl %sil, %eax
movl %eax, 0xc(%rsp)
je 0xd6da
leaq 0xb8fd(%rip), %rax      # 0x18fb8
movq (%rax), %rdi
leaq 0x8(%r14), %rdx
leaq 0x535d(%rip), %rsi      # 0x12a26
movl 0xc(%rsp), %ecx
movl %r12d, %r8d
movl %ebx, %r9d
xorl %eax, %eax
callq 0x31d0
addl %r12d, %r12d
addl %ebx, %ebx
leaq 0x8(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movq %r13, %rax
orq %rbx, %rax
leaq 0x50f8(%rip), %rcx      # 0x127f0
movzwl (%rcx,%rax,2), %eax
leal (%r12,%rax), %ebp
testb $0x2, 0x30(%r14)
je 0xd73d
leaq 0xb8aa(%rip), %rcx      # 0x18fb8
movq (%rcx), %rdi
incl %eax
movl %eax, %r9d
shrl $0x8, %r9d
addl %r12d, %r9d
movzbl %al, %eax
movl %eax, (%rsp)
leaq 0x526c(%rip), %rsi      # 0x12996
movq 0x10(%rsp), %rdx
movl 0xc(%rsp), %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x31d0
movq %r14, %rdi
movl %ebp, %esi
movl $0x100, %edx            # imm = 0x100
leaq 0x20(%rsp), %rcx
callq 0x62c6
movl %eax, %ebp
testl %eax, %eax
je 0xd7b9
movq $-0x2, %rax
leaq 0x22(%rsp), %rcx
movzbl -0x2(%rcx), %edx
shll $0x4, %edx
movzbl -0x1(%rcx), %esi
shrl $0x4, %esi
orl %edx, %esi
movw %si, 0x4(%r15,%rax,2)
movzbl -0x1(%rcx), %edx
movzbl (%rcx), %esi
andl $0xf, %edx
shll $0x8, %edx
orl %esi, %edx
movw %dx, 0x6(%r15,%rax,2)
addq $0x2, %rax
addq $0x3, %rcx
cmpq $0x7e, %rax
jb 0xd763
addq $0x100, %r15            # imm = 0x100
testl %ebp, %ebp
je 0xd7d6
leaq 0x1(%r13), %rax
testq %r13, %r13
movq %rax, %r13
je 0xd6eb
jmp 0xd7da
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %esi
movq %r15, %rdx
leaq 0x18(%rsp), %rcx
callq 0x464c
movq 0x18(%rsp), %r15
jmp 0xd7a3
xorl %eax, %eax
jmp 0xd7df
movl $0x1, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq