// File: /workspace/github_c_makefile_star2_O3/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
testl %edx, %edx
je 0xe82b
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %r15
movq 0x20(%rdi), %rax
movq %rax, (%rsp)
movb 0x40(%rdi), %al
xorl %ebx, %ebx
leaq 0x4087(%rip), %r13      # 0x12790
movl $0xfff, %r12d           # imm = 0xFFF
cmpb $0x3, %al
ja 0xe819
movzbl %al, %eax
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 0xe812
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 0xe812
cmpw $0x100, 0x3e(%r15)      # imm = 0x100
jne 0xe7ed
movzwl 0x3a(%r15), %edx
cmpw 0x3c(%r15), %dx
je 0xe82d
testl %edx, %edx
je 0xe7cc
movzbl 0x28(%r15), %esi
movq 0x30(%r15), %rcx
movq (%rsp), %rdi
callq 0xd79b
testl %eax, %eax
je 0xe82b
movzwl 0x3a(%r15), %eax
incl %eax
jmp 0xe7d1
movzwl 0x38(%r15), %eax
movw %ax, 0x3a(%r15)
movw $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 %ebx
incq 0x48(%r15)
cmpl %ebx, %ebp
jne 0xe70f
movl %ebp, %ebx
jmp 0xe82d
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq