// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x15786
movq -0x10(%rbp), %rax
movzbl 0x40(%rax), %eax
movq %rax, -0x38(%rbp)
subq $0x3, %rax
ja 0x1575f
movq -0x38(%rbp), %rax
leaq 0x526f(%rip), %rcx      # 0x1a7c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movzwl 0x3e(%rax), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x1560d
movq -0x10(%rbp), %rax
movzwl 0x3a(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x3c(%rcx), %ecx
cmpl %ecx, %eax
jne 0x1558c
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1578c
movq -0x10(%rbp), %rax
movzwl 0x3a(%rax), %eax
cmpl $0x0, %eax
je 0x155df
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movb 0x28(%rax), %al
movq -0x10(%rbp), %rcx
movzwl 0x3a(%rcx), %edx
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movzbl %al, %esi
callq 0x13450
cmpl $0x0, %eax
jne 0x155cd
movl $0x0, -0x4(%rbp)
jmp 0x1578c
movq -0x10(%rbp), %rax
movw 0x3a(%rax), %cx
addw $0x1, %cx
movw %cx, 0x3a(%rax)
jmp 0x155ef
movq -0x10(%rbp), %rax
movw 0x38(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x3a(%rax)
movq -0x10(%rbp), %rax
movw $0x0, 0x3e(%rax)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3170
movq -0x10(%rbp), %rax
movb $0x0, 0x40(%rax)
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movsbl (%rax), %eax
movzwl %ax, %edi
callq 0x148e0
movw %ax, %dx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rsi
movw 0x3e(%rsi), %cx
movw %cx, %di
addw $0x1, %di
movw %di, 0x3e(%rsi)
movzwl %cx, %ecx
movw %dx, (%rax,%rcx,2)
movq -0x10(%rbp), %rax
movb $0x1, 0x40(%rax)
jmp 0x1575f
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movsbl (%rax), %eax
movzwl %ax, %edi
callq 0x148e0
movw %ax, %dx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rsi
movw 0x3e(%rsi), %cx
movw %cx, %di
addw $0x1, %di
movw %di, 0x3e(%rsi)
movzwl %cx, %ecx
movw %dx, (%rax,%rcx,2)
movq -0x10(%rbp), %rax
movb $0x2, 0x40(%rax)
jmp 0x1575f
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
movq -0x18(%rbp), %rcx
movsbl (%rcx), %ecx
andl $0xf0, %ecx
shll $0x4, %ecx
orl %ecx, %eax
movzwl %ax, %edi
callq 0x148e0
movw %ax, %dx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
movq -0x18(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movsbl (%rcx), %ecx
andl $0xf, %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzwl %ax, %edi
callq 0x148e0
movw %ax, %dx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3e(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movq -0x10(%rbp), %rax
movb $0x3, 0x40(%rax)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
jmp 0x15526
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)