// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl -0x18(%rbp), %eax
movl $0xd, %ecx
xorl %edx, %edx
divq %rcx
imulq $0xd, %rax, %rax
movl %eax, -0x24(%rbp)
movl -0x18(%rbp), %eax
movl $0xd, %ecx
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x12d76
leaq 0xc497(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
movl -0x24(%rbp), %r8d
movl -0x28(%rbp), %r9d
leaq 0x7bd7(%rip), %rsi      # 0x1a946
movb $0x0, %al
callq 0x31c0
movl -0x24(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
jge 0x12fad
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
shll %ecx
addl -0x34(%rbp), %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0xc31b(%rip), %rcx      # 0x1f0c0
movzwl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x12e48
leaq 0xc41f(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x158(%rbp)
movzbl -0x11(%rbp), %ecx
movl -0x44(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl -0x28(%rbp), %eax
shll %eax
addl -0x34(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
leaq 0xc2c6(%rip), %rax      # 0x1f0c0
movzwl (%rax,%rdx,2), %eax
addl $0x1, %eax
movl $0x100, %esi            # imm = 0x100
cltd
idivl %esi
addl %eax, %r9d
movl -0x28(%rbp), %eax
shll %eax
addl -0x34(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
leaq 0xc2a1(%rip), %rax      # 0x1f0c0
movzwl (%rax,%rdx,2), %eax
addl $0x1, %eax
movl $0x100, %esi            # imm = 0x100
cltd
idivl %esi
movl %edx, %eax
movq -0x158(%rbp), %rdx
leaq 0x7a78(%rip), %rsi      # 0x1a8b6
movl %eax, (%rsp)
movb $0x0, %al
callq 0x31c0
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
leaq -0x150(%rbp), %rcx
movl $0x100, %edx            # imm = 0x100
callq 0x5620
movl %eax, -0x40(%rbp)
cmpl $0x0, %eax
jne 0x12edf
movq 0xb189(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x719f(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
leaq 0xc355(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x12ed3
leaq 0xc348(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0xb116(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x12ed3
leaq 0xc332(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x7158(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x12fb4
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x80, -0x38(%rbp)
jge 0x12f91
movslq -0x3c(%rbp), %rax
movzbl -0x150(%rbp,%rax), %eax
shll $0x4, %eax
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzbl -0x150(%rbp,%rcx), %ecx
sarl $0x4, %ecx
andl $0xf, %ecx
orl %ecx, %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, %dx
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cltq
movzbl -0x150(%rbp,%rax), %eax
shll $0x8, %eax
andl $0xf00, %eax            # imm = 0xF00
movl -0x3c(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movzbl -0x150(%rbp,%rcx), %ecx
orl %ecx, %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, %dx
movq -0x30(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movl -0x38(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x3c(%rbp)
jmp 0x12eed
movq -0x30(%rbp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x12d85
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq