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

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

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

    /*
     * Pack 64 words into the temporary buffer
     */
    for (j = 0, k = 0; j < OS8_RX01SS_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 + ((rx01interleave[(offset * 4) + i] + 1) / OS8_RX01SS),
              (rx01interleave[(offset * 4) + i] + 1) % OS8_RX01SS);
#endif

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
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 0x12a66
leaq 0xc7a7(%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 0x7e88(%rip), %rsi      # 0x1a8e7
movb $0x0, %al
callq 0x31c0
movl -0x24(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x4, -0x34(%rbp)
jge 0x12ca1
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
shll $0x2, %ecx
addl -0x34(%rbp), %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0xc5b9(%rip), %rcx      # 0x1f050
movzwl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x40, -0x38(%rbp)
jge 0x12b55
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
sarl $0x4, %eax
andl $0xff, %eax
movb %al, %cl
movslq -0x3c(%rbp), %rax
movb %cl, -0xd0(%rbp,%rax)
movq -0x30(%rbp), %rax
movslq -0x38(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
shll $0x4, %eax
andl $0xf0, %eax
movq -0x30(%rbp), %rcx
movl -0x38(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzwl (%rcx,%rdx,2), %ecx
sarl $0x8, %ecx
andl $0xf, %ecx
orl %ecx, %eax
movb %al, %cl
movl -0x3c(%rbp), %eax
addl $0x1, %eax
cltq
movb %cl, -0xd0(%rbp,%rax)
movq -0x30(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0xff, %eax
movb %al, %cl
movl -0x3c(%rbp), %eax
addl $0x2, %eax
cltq
movb %cl, -0xd0(%rbp,%rax)
movl -0x38(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x3, %eax
movl %eax, -0x3c(%rbp)
jmp 0x12aae
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x12bf1
leaq 0xc678(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xd8(%rbp)
movzbl -0x11(%rbp), %ecx
movl -0x44(%rbp), %r8d
movl -0x24(%rbp), %r9d
movl -0x28(%rbp), %eax
shll $0x2, %eax
addl -0x34(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
leaq 0xc4ae(%rip), %rax      # 0x1f050
movzwl (%rax,%rdx,2), %eax
addl $0x1, %eax
movl $0x80, %esi
cltd
idivl %esi
addl %eax, %r9d
movl -0x28(%rbp), %eax
shll $0x2, %eax
addl -0x34(%rbp), %eax
movl %eax, %eax
movl %eax, %edx
leaq 0xc488(%rip), %rax      # 0x1f050
movzwl (%rax,%rdx,2), %eax
addl $0x1, %eax
movl $0x80, %esi
cltd
idivl %esi
movl %edx, %eax
movq -0xd8(%rbp), %rdx
leaq 0x7d2e(%rip), %rsi      # 0x1a915
movl %eax, (%rsp)
movb $0x0, %al
callq 0x31c0
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %esi
leaq -0xd0(%rbp), %rcx
movl $0x80, %edx
callq 0x56a0
movl %eax, -0x40(%rbp)
cmpl $0x0, %eax
jne 0x12c85
movq 0xb3e0(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x73f6(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
leaq 0xc5ac(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x12c7c
leaq 0xc59f(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0xb36d(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x12c7c
leaq 0xc589(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x73af(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x12ca8
movq -0x30(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x12a76
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)