// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/os8.c
// Function: rx02WriteBlock
static int rx02WriteBlock(
  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: rx01WriteBlock, base %u, offset %u\n",
            mount->name, unit, base, offset);
#endif

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

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

    /*
     * Pack 128 words into the temporary buffer
     */
    for (j = 0, k = 0; j < OS8_RX02SS_W; j += 2, k += 3) {
      temp[k] = (buffer[j] >> 4) & 0xFF;
      temp[k + 1] = ((buffer[j] << 4) & 0xF0) | ((buffer[j + 1] >> 8) & 0xF);
      temp[k + 2] = buffer[j + 1] & 0xFF;
    }

#ifdef DEBUG
    if ((mount->flags & FS_DEBUG) != 0)
      fprintf(DEBUGout, "  >> %s%o: (os8) Writing %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 = FSioWriteSector(mount, sector, OS8_RX02SS, temp)) == 0) {
      ERROR("I/O error on \"%s%o:\"\n", mount->name, unit);
      return 0;
    }
    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 0xd5c8
movzbl %sil, %eax
movl %eax, 0x14(%rsp)
jmp 0xd5f2
leaq 0xb9f9(%rip), %rax      # 0x18fc8
movq (%rax), %rdi
leaq 0x8(%r15), %rdx
movzbl %sil, %ecx
leaq 0x5406(%rip), %rsi      # 0x129e7
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 0x5200(%rip), %rcx      # 0x12810
movzwl (%rcx,%rax,2), %eax
movq $-0x2, %rcx
leaq 0x22(%rsp), %rdx
movzwl 0x4(%rbx,%rcx,2), %esi
movl %esi, %edi
shrl $0x4, %edi
movb %dil, -0x2(%rdx)
shlb $0x4, %sil
movzwl 0x6(%rbx,%rcx,2), %edi
movl %edi, %r8d
shrl $0x8, %r8d
andb $0xf, %r8b
orb %sil, %r8b
movb %r8b, -0x1(%rdx)
movb %dil, (%rdx)
addq $0x2, %rcx
addq $0x3, %rdx
cmpq $0x7e, %rcx
jb 0xd620
leal (%r12,%rax), %r13d
testb $0x2, 0x30(%r15)
je 0xd69b
leaq 0xb95c(%rip), %rcx      # 0x18fc8
movq (%rcx), %rdi
incl %eax
movl %eax, %r9d
shrl $0x8, %r9d
addl %r12d, %r9d
movzbl %al, %eax
movl %eax, (%rsp)
leaq 0x538d(%rip), %rsi      # 0x12a15
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 0x62b0
testl %eax, %eax
je 0xd6e6
addq $0x100, %rbx            # imm = 0x100
movl $0x1, %eax
testb $0x1, %bpl
movl $0x0, %ebp
jne 0xd606
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 0x46f3
xorl %eax, %eax
jmp 0xd6d4