// File: /workspace/github_c_makefile_star2_O3/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, %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 0xd3c8
movzbl %sil, %eax
movl %eax, 0x14(%rsp)
jmp 0xd3f2
leaq 0xbbf9(%rip), %rax      # 0x18fc8
movq (%rax), %rdi
leaq 0x8(%r15), %rdx
movzbl %sil, %ecx
leaq 0x5665(%rip), %rsi      # 0x12a46
movl %ecx, 0x14(%rsp)
movl %r12d, %r8d
movl %r14d, %r9d
xorl %eax, %eax
callq 0x31e0
addl %r12d, %r12d
addl %r14d, %r14d
leaq 0x8(%r15), %rax
movq %rax, 0x18(%rsp)
movb $0x1, %bpl
xorl %eax, %eax
orq %r14, %rax
leaq 0x5400(%rip), %rcx      # 0x12810
movzwl (%rcx,%rax,2), %eax
leal (%r12,%rax), %r13d
testb $0x2, 0x30(%r15)
je 0xd455
leaq 0xbba2(%rip), %rcx      # 0x18fc8
movq (%rcx), %rdi
incl %eax
movl %eax, %r9d
shrl $0x8, %r9d
addl %r12d, %r9d
movzbl %al, %eax
movl %eax, (%rsp)
leaq 0x5574(%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 $0x100, %edx            # imm = 0x100
leaq 0x20(%rsp), %rcx
callq 0x6264
testl %eax, %eax
je 0xd570
leaq 0x2b(%rsp), %rax
xorl %ecx, %ecx
pxor %xmm3, %xmm3
movdqa 0x52db(%rip), %xmm4     # 0x12760
movdqa 0x52e3(%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 $0x40, %rcx
jne 0xd48d
addq $0x100, %rbx            # imm = 0x100
movl $0x1, %eax
testb $0x1, %bpl
movl $0x0, %ebp
jne 0xd406
movl $0x1, %eax
addq $0x128, %rsp            # imm = 0x128
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 0x4697
xorl %eax, %eax
jmp 0xd55e