// File: /workspace/github_c_makefile_star2_O2/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
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r14
cmpw $0x0, 0x30(%rdi)
jne 0x6b5c
movq 0x20(%r14), %rdi
movzwl 0x28(%rdi), %eax
movw %ax, 0x34(%r14)
cmpw $0x0, 0x6(%r14)
js 0x6b46
callq 0x6597
testw %ax, %ax
je 0x6c43
movw %ax, 0x30(%r14)
movw %ax, 0xe(%r14)
movw %ax, 0xa(%r14)
movw $0x1, 0xc(%r14)
movq 0x28(%r14), %rax
andw $0x0, (%rax)
movw $0x2, %ax
jmp 0x6b57
movzwl 0xa(%r14), %eax
movw %ax, 0x30(%r14)
movw %ax, 0xe(%r14)
xorl %eax, %eax
movw %ax, 0x32(%r14)
movl %ebp, %r12d
xorl %r15d, %r15d
cmpl %r15d, %r12d
je 0x6c31
movzwl 0x32(%r14), %eax
cmpw 0x34(%r14), %ax
jne 0x6c13
cmpw $0x0, 0x6(%r14)
movq 0x20(%r14), %rdi
js 0x6bd0
callq 0x6597
testw %ax, %ax
je 0x6c8a
movl %eax, %ebp
movq 0x28(%r14), %rdx
movw %ax, (%rdx)
movq 0x20(%r14), %rdi
movzwl 0x30(%r14), %esi
callq 0x616b
testl %eax, %eax
je 0x6c36
movw %bp, 0x30(%r14)
movw %bp, 0xe(%r14)
incw 0xc(%r14)
movw $0x2, 0x32(%r14)
movw $0x2, %ax
jmp 0x6c13
movzwl 0x30(%r14), %esi
movq 0x28(%r14), %rdx
callq 0x616b
xorl %ebp, %ebp
testl %eax, %eax
je 0x6c38
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 0x6cdb
andw $0x0, 0x32(%r14)
movw %ax, 0xe(%r14)
xorl %eax, %eax
movb (%rbx,%r15), %cl
movq 0x28(%r14), %rdx
leal 0x1(%rax), %esi
movw %si, 0x32(%r14)
movzwl %ax, %eax
movb %cl, (%rdx,%rax)
incq %r15
jmp 0x6b62
movl %r12d, %ebp
jmp 0x6c38
xorl %ebp, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x103ae(%rip), %rax     # 0x16ff8
movq (%rax), %rcx
leaq 0x9d8d(%rip), %rdi      # 0x109e1
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
leaq 0x11362(%rip), %rax     # 0x17fc8
movq (%rax), %rcx
xorl %ebp, %ebp
testq %rcx, %rcx
je 0x6c38
movq 0x10349(%rip), %rax     # 0x16fc0
cmpq (%rax), %rcx
je 0x6c38
leaq 0x9d5e(%rip), %rdi      # 0x109e1
pushq $0x1a
jmp 0x6d23
movq 0x10367(%rip), %rax     # 0x16ff8
movq (%rax), %rcx
leaq 0x9d61(%rip), %rdi      # 0x109fc
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
leaq 0x1131b(%rip), %rax     # 0x17fc8
movq (%rax), %rcx
testq %rcx, %rcx
je 0x6cd3
movq 0x10304(%rip), %rax     # 0x16fc0
cmpq (%rax), %rcx
je 0x6cd3
leaq 0x9d34(%rip), %rdi      # 0x109fc
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
movl %r15d, %ebp
jmp 0x6c38
movq 0x10316(%rip), %rax     # 0x16ff8
movq (%rax), %rcx
leaq 0x9d3a(%rip), %rdi      # 0x10a26
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
leaq 0x112ca(%rip), %rax     # 0x17fc8
movq (%rax), %rcx
testq %rcx, %rcx
je 0x6c38
movq 0x102af(%rip), %rax     # 0x16fc0
cmpq (%rax), %rcx
je 0x6c38
leaq 0x9d05(%rip), %rdi      # 0x10a26
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
jmp 0x6c38