// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
cmpl $0x0, %eax
jne 0x8c32
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rax), %rax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x34(%rax)
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0x8bfe
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x80a0
movw %ax, -0x22(%rbp)
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x8ba8
movq 0x154a8(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
leaq 0x10ed1(%rip), %rsi     # 0x19a2b
movb $0x0, %al
callq 0x31c0
leaq 0x16680(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x8b9c
leaq 0x16673(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x15441(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x8b9c
leaq 0x1665d(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
leaq 0x10e96(%rip), %rsi     # 0x19a2b
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x8eca
movw -0x22(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x30(%rax)
movzwl -0x22(%rbp), %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xe(%rax)
movq -0x10(%rbp), %rax
movw %cx, 0xa(%rax)
movl $0x1, %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movw $0x0, (%rax)
movq -0x10(%rbp), %rax
movw $0x2, 0x32(%rax)
jmp 0x8c30
movq -0x10(%rbp), %rax
movzwl 0xa(%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x30(%rax)
movq -0x10(%rbp), %rax
movw 0xa(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0xe(%rax)
movq -0x10(%rbp), %rax
movw $0x0, 0x32(%rax)
jmp 0x8c32
jmp 0x8c34
cmpl $0x0, -0x1c(%rbp)
je 0x8ec4
movq -0x10(%rbp), %rax
movzwl 0x32(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x34(%rcx), %ecx
cmpl %ecx, %eax
jne 0x8e7b
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0x8d85
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x80a0
movw %ax, -0x22(%rbp)
movzwl %ax, %eax
cmpl $0x0, %eax
jne 0x8ceb
movq 0x15364(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
leaq 0x10da8(%rip), %rsi     # 0x19a46
movb $0x0, %al
callq 0x31c0
leaq 0x1653c(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x8ce0
leaq 0x1652f(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x152fd(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x8ce0
leaq 0x16519(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
leaq 0x10d6d(%rip), %rsi     # 0x19a46
movb $0x0, %al
callq 0x31c0
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x8eca
movzwl -0x22(%rbp), %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movw %cx, (%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x75a0
cmpl $0x0, %eax
jne 0x8d30
movl $0x0, -0x4(%rbp)
jmp 0x8eca
movw -0x22(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x30(%rax)
movzwl -0x22(%rbp), %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xe(%rax)
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
addl $0x1, %eax
movzwl %ax, %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xc(%rax)
movq -0x10(%rbp), %rax
movw $0x2, 0x32(%rax)
jmp 0x8e79
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x75a0
cmpl $0x0, %eax
jne 0x8db3
movl $0x0, -0x4(%rbp)
jmp 0x8eca
movq -0x10(%rbp), %rcx
movw 0x30(%rcx), %ax
addw $0x1, %ax
movw %ax, 0x30(%rcx)
movzwl %ax, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %edi
callq 0x6c50
movl -0x2c(%rbp), %ecx
movw %ax, %dx
movl -0x28(%rbp), %eax
movzwl %dx, %edx
addl %edx, %ecx
cmpl %ecx, %eax
jne 0x8e57
movq 0x151f6(%rip), %rax     # 0x1dff8
movq (%rax), %rdi
leaq 0x10c64(%rip), %rsi     # 0x19a70
movb $0x0, %al
callq 0x31c0
leaq 0x163ce(%rip), %rax     # 0x1f1e8
cmpq $0x0, (%rax)
je 0x8e4e
leaq 0x163c1(%rip), %rax     # 0x1f1e8
movq (%rax), %rax
movq 0x1518f(%rip), %rcx     # 0x1dfc0
cmpq (%rcx), %rax
je 0x8e4e
leaq 0x163ab(%rip), %rax     # 0x1f1e8
movq (%rax), %rdi
leaq 0x10c29(%rip), %rsi     # 0x19a70
movb $0x0, %al
callq 0x31c0
movl $0x0, -0x4(%rbp)
jmp 0x8eca
movq -0x10(%rbp), %rax
movw $0x0, 0x32(%rax)
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0xe(%rax)
jmp 0x8e7b
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movb (%rax), %dl
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rsi
movw 0x32(%rsi), %cx
movw %cx, %di
addw $0x1, %di
movw %di, 0x32(%rsi)
movzwl %cx, %ecx
movb %dl, (%rax,%rcx)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x8c34
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)