// File: /workspace/github_c_makefile_star2_O0/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
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 0x13046
leaq 0xc1c7(%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 0x78a8(%rip), %rsi      # 0x1a8e7
movb $0x0, %al
callq 0x31c0
movl -0x24(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x2, -0x34(%rbp)
jge 0x13280
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
shll %ecx
addl -0x34(%rbp), %ecx
movl %ecx, %ecx
movl %ecx, %edx
leaq 0xc04b(%rip), %rcx      # 0x1f0c0
movzwl (%rcx,%rdx,2), %ecx
addl %ecx, %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x80, -0x38(%rbp)
jge 0x13136
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, -0x150(%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, -0x150(%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, -0x150(%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 0x1308c
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x131d0
leaq 0xc097(%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 0xbf3e(%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 0xbf19(%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 0x774f(%rip), %rsi      # 0x1a915
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 0x56a0
movl %eax, -0x40(%rbp)
cmpl $0x0, %eax
jne 0x13264
movq 0xae01(%rip), %rax      # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x6e17(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
leaq 0xbfcd(%rip), %rax      # 0x1f1e8
cmpq $0x0, (%rax)
je 0x1325b
leaq 0xbfc0(%rip), %rax      # 0x1f1e8
movq (%rax), %rax
movq 0xad8e(%rip), %rcx      # 0x1dfc0
cmpq (%rcx), %rax
je 0x1325b
leaq 0xbfaa(%rip), %rax      # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0x6dd0(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x13287
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 0x13055
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
nopw %cs:(%rax,%rax)