// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xc10c
movq -0x28(%rbp), %rax
addq $0x42a, %rax            # imm = 0x42A
movq %rax, -0x48(%rbp)
jmp 0xc114
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movzbl -0x11(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq %ecx, %rcx
movzwl 0xa(%rax,%rcx,2), %eax
movl %eax, -0x4c(%rbp)
movzbl -0x11(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl -0x4c(%rbp), %eax
movl %edx, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xc359
movl -0x18(%rbp), %eax
movq -0x28(%rbp), %rcx
movzbl -0x11(%rbp), %edx
movzwl 0x2a(%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jbe 0xc200
movq 0x11e70(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movq -0x10(%rbp), %rcx
addq $0x8, %rcx
movzbl -0x11(%rbp), %r8d
leaq 0xdeb0(%rip), %rsi      # 0x1a052
movb $0x0, %al
callq 0x31c0
leaq 0x13038(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xc1f4
leaq 0x1302b(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x11df9(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xc1f4
leaq 0x13015(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movq -0x10(%rbp), %rcx
addq $0x8, %rcx
movzbl -0x11(%rbp), %r8d
leaq 0xde65(%rip), %rsi      # 0x1a052
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0xc3cc
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xc238
leaq 0x12fd1(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
movl -0x18(%rbp), %r8d
leaq 0xde5a(%rip), %rsi      # 0x1a08b
movb $0x0, %al
callq 0x31c0
movq -0x28(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0xc2ca
movq -0x28(%rbp), %rcx
movl $0x200, %eax            # imm = 0x200
xorl %edx, %edx
divl 0x4(%rcx)
movl %eax, -0x38(%rbp)
movl -0x18(%rbp), %eax
imull -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %edi
callq 0xbd60
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %esi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x30(%rbp), %rcx
callq 0x56a0
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x38(%rbp)
movb %al, -0x59(%rbp)
je 0xc2c1
cmpl $0x0, -0x34(%rbp)
setne %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0xc261
jmp 0xc2e4
movq -0x10(%rbp), %rdi
movzbl -0x11(%rbp), %esi
shll $0x10, %esi
orl -0x18(%rbp), %esi
movq -0x30(%rbp), %rdx
callq 0x55a0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0xc357
movq 0x11d07(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xdd1d(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
leaq 0x12ed3(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xc355
leaq 0x12ec6(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x11c94(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xc355
leaq 0x12eb0(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xdcd6(%rip), %rsi      # 0x1a024
movb $0x0, %al
callq 0x31c0
jmp 0xc357
jmp 0xc3c6
movq 0x11c98(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xdcc4(%rip), %rsi      # 0x1a03a
movb $0x0, %al
callq 0x31c0
leaq 0x12e64(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0xc3c4
leaq 0x12e57(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x11c25(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0xc3c4
leaq 0x12e41(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
movzbl -0x11(%rbp), %ecx
leaq 0xdc7d(%rip), %rsi      # 0x1a03a
movb $0x0, %al
callq 0x31c0
jmp 0xc3c6
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)