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

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

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

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

#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 + ((rx01interleave[(offset * 4) + i] + 1) / OS8_RX01SS),
              (rx01interleave[(offset * 4) + i] + 1) % OS8_RX01SS);
#endif

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

    /*
     * Unpack 64 words from the temporary buffer
     */
    for (j = 0, k = 0; j < OS8_RX01SS_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_RX01SS_W;
  }
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %rbx
movl %edx, %r14d
movq %rdi, %r15
movl %edx, %eax
imulq $0x4ec4ec4f, %rax, %rax # imm = 0x4EC4EC4F
shrq $0x22, %rax
leal (%rax,%rax,2), %ecx
leal (%rax,%rcx,4), %r12d
subl %r12d, %r14d
testb $0x2, 0x30(%rdi)
jne 0xd05d
movzbl %sil, %eax
movl %eax, 0x14(%rsp)
jmp 0xd087
leaq 0xbf64(%rip), %rax      # 0x18fc8
movq (%rax), %rdi
leaq 0x8(%r15), %rdx
movzbl %sil, %ecx
leaq 0x5913(%rip), %rsi      # 0x12989
movl %ecx, 0x14(%rsp)
movl %r12d, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x31e0
shll $0x2, %r12d
shll $0x2, %r14d
leaq 0x8(%r15), %rax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
leaq (%r14,%rbp), %rax
leaq 0x56fb(%rip), %rcx      # 0x127a0
movzwl (%rcx,%rax,2), %eax
leal (%r12,%rax), %r13d
testb $0x2, 0x30(%r15)
je 0xd0ea
leaq 0xbf0d(%rip), %rcx      # 0x18fc8
movq (%rcx), %rdi
incl %eax
movl %eax, %r9d
shrl $0x7, %r9d
addl %r12d, %r9d
andl $0x7f, %eax
movl %eax, (%rsp)
leaq 0x58df(%rip), %rsi      # 0x129b6
movq 0x18(%rsp), %rdx
movl 0x14(%rsp), %ecx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x31e0
movq %r15, %rdi
movl %r13d, %esi
movl $0x80, %edx
leaq 0x20(%rsp), %rcx
callq 0x6264
testl %eax, %eax
je 0xd1fb
leaq 0x2b(%rsp), %rax
xorl %ecx, %ecx
pxor %xmm3, %xmm3
movdqa 0x5646(%rip), %xmm4     # 0x12760
movdqa 0x564e(%rip), %xmm5     # 0x12770
movzbl -0xb(%rax), %edx
movzbl -0x8(%rax), %esi
movzbl -0x5(%rax), %edi
shll $0x8, %esi
orl %edx, %esi
shll $0x10, %edi
orl %esi, %edi
movzbl -0x2(%rax), %edx
shll $0x18, %edx
orl %edi, %edx
movd %edx, %xmm0
punpcklbw %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1],xmm0[2],xmm3[2],xmm0[3],xmm3[3],xmm0[4],xmm3[4],xmm0[5],xmm3[5],xmm0[6],xmm3[6],xmm0[7],xmm3[7]
psllw $0x4, %xmm0
movzbl -0xa(%rax), %edx
movzbl -0x7(%rax), %esi
movzbl -0x4(%rax), %edi
shll $0x8, %esi
orl %edx, %esi
shll $0x10, %edi
orl %esi, %edi
movzbl -0x1(%rax), %edx
shll $0x18, %edx
orl %edi, %edx
movd %edx, %xmm1
pxor %xmm2, %xmm2
punpcklbw %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1],xmm2[2],xmm1[2],xmm2[3],xmm1[3],xmm2[4],xmm1[4],xmm2[5],xmm1[5],xmm2[6],xmm1[6],xmm2[7],xmm1[7]
psrlw $0x4, %xmm1
pand %xmm4, %xmm1
punpcklbw %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1],xmm1[2],xmm3[2],xmm1[3],xmm3[3],xmm1[4],xmm3[4],xmm1[5],xmm3[5],xmm1[6],xmm3[6],xmm1[7],xmm3[7]
pand %xmm5, %xmm2
punpcklwd %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1],xmm0[2],xmm2[2],xmm0[3],xmm2[3]
movzbl -0x9(%rax), %edx
movzbl -0x6(%rax), %esi
movzbl -0x3(%rax), %edi
shll $0x8, %esi
orl %edx, %esi
shll $0x10, %edi
orl %esi, %edi
movzbl (%rax), %edx
shll $0x18, %edx
orl %edi, %edx
movd %edx, %xmm2
punpcklbw %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1],xmm2[2],xmm3[2],xmm2[3],xmm3[3],xmm2[4],xmm3[4],xmm2[5],xmm3[5],xmm2[6],xmm3[6],xmm2[7],xmm3[7]
punpcklwd %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
por %xmm0, %xmm1
movdqu %xmm1, (%rbx,%rcx,4)
addq $0x4, %rcx
addq $0xc, %rax
cmpq $0x20, %rcx
jne 0xd122
subq $-0x80, %rbx
incq %rbp
cmpq $0x4, %rbp
jne 0xd09a
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x45df
xorl %eax, %eax
jmp 0xd1e9