// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11WriteBytes
int dos11WriteBytes(
  struct dos11OpenFile *file,
  char *buf,
  int len
)
{
  int count = 0;
  uint16_t next;

  if (file->current == 0) {
    file->eob = file->mount->blocksz;

    if ((le16toh(file->creation) & UFD_TYPE) == 0) {
      /*
       * Linked file - allocate initial block
       */
      if ((next = bitmapAllocBlock(file->mount)) == 0) {
        ERROR("Free disk space exhausted\n");
        return 0;
      }
      file->current = next;
      file->start = file->last = htole16(next);
      file->length = htole16(1);
      *((uint16_t *)(file->buffer)) = 0;
      file->nab = 2;
    } else {
      file->current = le16toh(file->start);
      file->last = file->start;
      file->nab = 0;
    }
  }

  while (len) {
    if (file->nab == file->eob) {
      if ((le16toh(file->creation) & UFD_TYPE) == 0) {
        /*
         * Linked file - extend it by 1 block
         */
        if ((next = bitmapAllocBlock(file->mount)) == 0) {
          ERROR("Free disk space exhausted extending file\n");
          return count;
        }
        *((uint16_t *)(file->buffer)) = htole16(next);

        if (dos11WriteBlock(file->mount, file->current, file->buffer) == 0)
          return 0;

        file->current = next;
        file->last = htole16(next);
        file->length = htole16(le16toh(file->length) + 1);
        file->nab = 2;
      } else {
        if (dos11WriteBlock(file->mount, file->current, file->buffer) == 0)
          return 0;

        if (++file->current == (le16toh(file->last) + le16toh(file->length))) {
          ERROR("Contiguous disk space allocation exceeded\n");
          return 0;
        }
        file->nab = 0;
        file->last = htole16(file->current);
      }
    }

    file->buffer[file->nab++] = *buf++;
    len--;
    count++;
  }
  return count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
cmpw $0x0, 0x30(%rdi)
jne 0x80df
movq 0x20(%r14), %rdi
movzwl 0x28(%rdi), %eax
movw %ax, 0x34(%r14)
cmpw $0x0, 0x6(%r14)
js 0x80c9
callq 0x7ac2
testw %ax, %ax
je 0x81c3
movw %ax, 0x30(%r14)
movw %ax, 0xe(%r14)
movw %ax, 0xa(%r14)
movw $0x1, 0xc(%r14)
movq 0x28(%r14), %rax
movw $0x0, (%rax)
movw $0x2, %ax
jmp 0x80da
movzwl 0xa(%r14), %eax
movw %ax, 0x30(%r14)
movw %ax, 0xe(%r14)
xorl %eax, %eax
movw %ax, 0x32(%r14)
testl %ebp, %ebp
je 0x81b0
movl %ebp, %r15d
xorl %r12d, %r12d
movzwl 0x32(%r14), %eax
cmpw 0x34(%r14), %ax
jne 0x818c
cmpw $0x0, 0x6(%r14)
movq 0x20(%r14), %rdi
js 0x814e
callq 0x7ac2
testw %ax, %ax
je 0x81d2
movl %eax, %ebp
movq 0x28(%r14), %rdx
movw %ax, (%rdx)
movq 0x20(%r14), %rdi
movzwl 0x30(%r14), %esi
callq 0x7700
testl %eax, %eax
je 0x81b0
movw %bp, 0x30(%r14)
movw %bp, 0xe(%r14)
incw 0xc(%r14)
movw $0x2, 0x32(%r14)
movw $0x2, %ax
jmp 0x818c
movzwl 0x30(%r14), %esi
movq 0x28(%r14), %rdx
callq 0x7700
testl %eax, %eax
je 0x81b0
movl 0x30(%r14), %eax
incl %eax
movw %ax, 0x30(%r14)
movzwl %ax, %ecx
movzwl 0xe(%r14), %edx
movzwl 0xc(%r14), %esi
addl %edx, %esi
cmpl %ecx, %esi
je 0x81e4
movw $0x0, 0x32(%r14)
movw %ax, 0xe(%r14)
xorl %eax, %eax
movb (%rbx,%r12), %cl
movq 0x28(%r14), %rdx
leal 0x1(%rax), %esi
movw %si, 0x32(%r14)
movzwl %ax, %eax
movb %cl, (%rdx,%rax)
incq %r12
cmpl %r12d, %r15d
jne 0x80ed
jmp 0x81b3
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc(%rsp), %rbx
movq %rbx, %rdi
callq 0x3a6c
jmp 0x81f1
leaq 0x8(%rsp), %rbx
movl %r12d, %edi
movq %rbx, %rsi
callq 0x3a12
jmp 0x81f1
leaq 0x4(%rsp), %rbx
movq %rbx, %rdi
callq 0x39c0
movl (%rbx), %r15d
jmp 0x81b3