// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/rt11.c
// Function: rt11WriteBlock
int rt11WriteBlock(
  struct mountedFS *mount,
  uint8_t unit,
  unsigned int block,
  void *buf
)
{
  struct RT11data *data = &mount->rt11data;
  char *buffer = buf == NULL ? data->buf : buf;
  int status = 0;

  if (RT11_PARTITIONVALID(data, unit)) {
    if (block > data->maxblk[unit]) {
      ERROR("Attempt to write block (%u) outside file system \"%s%o:\"\n",
            block, mount->name, unit);
      return 0;
    }

#ifdef DEBUG
    if ((mount->flags & FS_DEBUG) != 0)
      fprintf(DEBUGout, ">> %s%o: (rt11) Writing logical block %o\n",
              mount->name, unit, block);
#endif

    if (data->sectorsz != 0) {
      /*
       * The sectors making up the disk are sector-interleaved. The algorithm
       * below is for RX01/RX02 interleave. Note that all such devices are
       * small enough that there can only be a single file system present.
       */
      unsigned int count = RT11_BLOCKSIZE / data->sectorsz;
      unsigned int sectno = block * count;

      do {
        unsigned int sector = MapLogToPhys(sectno);

        status = FSioWriteSector(mount, sector, data->sectorsz, buffer);

        count--;
        buffer += data->sectorsz;
        sectno++;
      } while ((count != 0) && (status != 0));
    } else status = FSioWriteBlock(mount, (unit << 16) | block, buffer);

    if (status == 0)
      ERROR("I/O error on \"%s%o:\"\n", mount->name, unit);
  } else ERROR("Invalid device \"%s%o:\"\n", mount->name, unit);

  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %r13d
movq %rdi, %r14
leaq 0x472(%rdi), %r12
testq %rcx, %rcx
cmovneq %rcx, %r12
movl %esi, %eax
shrl $0x4, %eax
movzwl 0x52(%rdi,%rax,2), %eax
movl %esi, %ecx
andl $0xf, %ecx
btl %ecx, %eax
jae 0x88a3
movl %edx, %ebp
movzbl %r13b, %eax
movzwl 0x72(%r14,%rax,2), %eax
cmpl %edx, %eax
jb 0x88f8
testb $0x2, 0x30(%r14)
je 0x8826
leaq 0xf7bd(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
leaq 0x8(%r14), %rdx
leaq 0x8824(%rip), %rsi      # 0x1103d
movl %r13d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x31e0
movl 0x4c(%r14), %ebx
testl %ebx, %ebx
movl %r13d, 0x4(%rsp)
je 0x8870
movl $0x200, %eax            # imm = 0x200
xorl %edx, %edx
divl %ebx
movl %eax, %r13d
imull %eax, %ebp
movl %ebp, %edi
callq 0x8578
movq %r14, %rdi
movl %eax, %esi
movl %ebx, %edx
movq %r12, %rcx
callq 0x4cfb
movl %eax, %r15d
decl %r13d
je 0x8888
movl 0x4c(%r14), %ebx
addq %rbx, %r12
incl %ebp
testl %r15d, %r15d
jne 0x8842
jmp 0x8888
shll $0x10, %r13d
orl %ebp, %r13d
movq %r14, %rdi
movl %r13d, %esi
movq %r12, %rdx
callq 0x4c6a
movl %eax, %r15d
testl %r15d, %r15d
je 0x8960
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe74e(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
addq $0x8, %r14
leaq 0x8734(%rip), %rsi      # 0x10fec
xorl %r15d, %r15d
movq %r14, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xf6f9(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8891
movq 0xe6e2(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x8891
leaq 0x8702(%rip), %rsi      # 0x10fec
xorl %r15d, %r15d
movq %r14, %rdx
movl %r13d, %ecx
jmp 0x89ba
movq 0xe6f9(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
addq $0x8, %r14
leaq 0x86f7(%rip), %rsi      # 0x11004
xorl %r15d, %r15d
movl %ebp, %edx
movq %r14, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x31e0
leaq 0xf6a2(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8891
movq 0xe687(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x8891
leaq 0x86bb(%rip), %rsi      # 0x11004
xorl %r15d, %r15d
movl %ebp, %edx
movq %r14, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x31e0
jmp 0x8891
movq 0xe691(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
addq $0x8, %r14
leaq 0x8661(%rip), %rsi      # 0x10fd6
xorl %r15d, %r15d
movq %r14, %rdx
movl 0x4(%rsp), %ebx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x31e0
leaq 0xf639(%rip), %rax      # 0x17fc8
movq (%rax), %rdi
testq %rdi, %rdi
je 0x8891
movq 0xe61e(%rip), %rax      # 0x16fc0
cmpq (%rax), %rdi
je 0x8891
leaq 0x8624(%rip), %rsi      # 0x10fd6
xorl %r15d, %r15d
movq %r14, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x31e0
jmp 0x8891