// File: /workspace/github_c_makefile_star2_O1/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 0x83c9
movq 0x20(%r14), %rdi
movzwl 0x28(%rdi), %eax
movw %ax, 0x34(%r14)
cmpw $0x0, 0x6(%r14)
js 0x83b3
callq 0x7d7f
testw %ax, %ax
je 0x84ad
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 0x83c4
movzwl 0xa(%r14), %eax
movw %ax, 0x30(%r14)
movw %ax, 0xe(%r14)
xorl %eax, %eax
movw %ax, 0x32(%r14)
testl %ebp, %ebp
je 0x849a
movl %ebp, %r15d
xorl %r12d, %r12d
movzwl 0x32(%r14), %eax
cmpw 0x34(%r14), %ax
jne 0x8474
cmpw $0x0, 0x6(%r14)
js 0x8434
movq 0x20(%r14), %rdi
callq 0x7d7f
testw %ax, %ax
je 0x84bc
movl %eax, %ebp
movq 0x28(%r14), %rdx
movw %ax, (%rdx)
movq 0x20(%r14), %rdi
movzwl 0x30(%r14), %esi
callq 0x7983
testl %eax, %eax
je 0x849a
movw %bp, 0x30(%r14)
movw %bp, 0xe(%r14)
incw 0xc(%r14)
movw $0x2, 0x32(%r14)
jmp 0x8474
movzwl 0x30(%r14), %esi
movq 0x20(%r14), %rdi
movq 0x28(%r14), %rdx
callq 0x7983
testl %eax, %eax
je 0x849a
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 0x84ce
movw $0x0, 0x32(%r14)
movw %ax, 0xe(%r14)
movb (%rbx,%r12), %al
movq 0x28(%r14), %rcx
movzwl 0x32(%r14), %edx
leal 0x1(%rdx), %esi
movw %si, 0x32(%r14)
movb %al, (%rcx,%rdx)
incq %r12
cmpl %r12d, %r15d
jne 0x83d7
jmp 0x849d
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 0x3994
jmp 0x84db
leaq 0x8(%rsp), %rbx
movl %r12d, %edi
movq %rbx, %rsi
callq 0x393a
jmp 0x84db
leaq 0x4(%rsp), %rbx
movq %rbx, %rdi
callq 0x38e8
movl (%rbx), %r15d
jmp 0x849d