// File: /workspace/github_c_makefile_star2_O2/simh[P]simtools/converters/fsio/os8.c
// Function: os8WriteBytes
int os8WriteBytes(
  struct os8OpenFile *file,
  char *buf,
  int len
)
{
  struct mountedFS *mount = file->mount;
  int count = 0;

  while (len) {
    switch (file->bytepos) {
      case OS8_CHECK:
        if (file->wordpos == OS8_BLOCKSIZE) {
          if (file->current == file->last)
            return count;

          if (file->current != 0) {
            if (os8WriteBlock(mount, file->unit, file->current, file->buffer) == 0)
              return 0;

            file->current++;
          } else file->current = file->start;

          file->wordpos = 0;
          memset(file->buffer, 0, OS8_BLOCKSIZE * sizeof(uint16_t));
        }
        file->bytepos = OS8_BYTE0;
        /* FALLTHROUGH */

      case OS8_BYTE0:
        file->buffer[file->wordpos++] = os8Value(*buf++);
        file->bytepos = OS8_BYTE1;
        break;

      case OS8_BYTE1:
        file->buffer[file->wordpos++] = os8Value(*buf++);
        file->bytepos = OS8_BYTE2;
        break;

      case OS8_BYTE2:
        file->buffer[file->wordpos - 2] =
          os8Value(file->buffer[file->wordpos - 2] | ((*buf & 0xF0) << 4));
        file->buffer[file->wordpos - 1] =
          os8Value(file->buffer[file->wordpos - 1] | ((*buf++ & 0xF) << 8));
        file->bytepos = OS8_CHECK;
        break;
    }
    len--;
    count++;
    file->written++;
  }
  return count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq %rax, (%rsp)
xorl %ebp, %ebp
leaq 0x4182(%rip), %r13      # 0x11770
movl $0xfff, %r12d           # imm = 0xFFF
cmpl %ebp, %ebx
je 0xd713
movzbl 0x40(%r15), %eax
cmpq $0x3, %rax
ja 0xd702
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movsbl (%r14), %eax
andl $0xfff, %eax            # imm = 0xFFF
movq 0x30(%r15), %rcx
movzwl 0x3e(%r15), %edx
leal 0x1(%rdx), %esi
movw %si, 0x3e(%r15)
movw %ax, (%rcx,%rdx,2)
movb $0x2, %al
jmp 0xd6fb
movq 0x30(%r15), %rax
movzwl 0x3e(%r15), %ecx
movzwl -0x4(%rax,%rcx,2), %edx
movzbl (%r14), %esi
andl $-0x10, %esi
shll $0x4, %esi
andl %r12d, %edx
orl %esi, %edx
movw %dx, -0x4(%rax,%rcx,2)
movzwl 0x3e(%r15), %ecx
movzwl -0x2(%rax,%rcx,2), %edx
movzbl (%r14), %esi
andl $0xf, %esi
shll $0x8, %esi
andl %r12d, %edx
orl %esi, %edx
movw %dx, -0x2(%rax,%rcx,2)
movb $0x3, %al
jmp 0xd6fb
cmpw $0x100, 0x3e(%r15)      # imm = 0x100
jne 0xd6d6
movzwl 0x3a(%r15), %edx
cmpw 0x3c(%r15), %dx
je 0xd70d
testl %edx, %edx
je 0xd6b6
movzbl 0x28(%r15), %esi
movq 0x30(%r15), %rcx
movq (%rsp), %rdi
callq 0xc6a8
testl %eax, %eax
je 0xd711
movzwl 0x3a(%r15), %eax
incl %eax
jmp 0xd6bb
movzwl 0x38(%r15), %eax
movw %ax, 0x3a(%r15)
andw $0x0, 0x3e(%r15)
movq 0x30(%r15), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3170
movb $0x0, 0x40(%r15)
movsbl (%r14), %eax
andl $0xfff, %eax            # imm = 0xFFF
movq 0x30(%r15), %rcx
movzwl 0x3e(%r15), %edx
leal 0x1(%rdx), %esi
movw %si, 0x3e(%r15)
movw %ax, (%rcx,%rdx,2)
movb $0x1, %al
incq %r14
movb %al, 0x40(%r15)
incl %ebp
incq 0x48(%r15)
jmp 0xd5f4
movl %ebp, %ebx
jmp 0xd713
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq