// File: /workspace/github_c_makefile_star2_O0/simh[P]simtools/converters/fsio/dos11.c
// Function: dos11ReadBytes
int dos11ReadBytes(
  struct dos11OpenFile *file,
  char *buf,
  int len
)
{
  int count = 0;

  if (file->current == 0) {
    file->current = le16toh(file->start);

    if (dos11ReadBlock(file->mount, file->current, file->buffer) == 0)
      return 0;

    file->nab = (le16toh(file->creation) & UFD_TYPE) == 0 ? 2 : 0;
    file->eob = file->current == le16toh(file->last) ?
      le16toh(file->nfb) : file->mount->blocksz;
  }

  while (len) {
    if (file->nab == file->eob) {
      if (file->current == le16toh(file->last))
        break;

      if ((le16toh(file->creation) & UFD_TYPE) == 0)
        file->current = le16toh(*((uint16_t *)(file->buffer)));
      else file->current++;

      if (dos11ReadBlock(file->mount, file->current, file->buffer) == 0)
        return 0;

      file->nab = (le16toh(file->creation) & UFD_TYPE) == 0 ? 2 : 0;
      file->eob = file->current == le16toh(file->last) ?
        le16toh(file->nfb) : file->mount->blocksz;
      continue;
    }
    *buf++ = file->buffer[file->nab++];
    len--;
    count++;
  }
  return count;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %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 0x8919
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
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x7380
cmpl $0x0, %eax
jne 0x8891
movl $0x0, -0x4(%rbp)
jmp 0x8ab8
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x6c50
movzwl %ax, %edx
andl $0x8000, %edx           # imm = 0x8000
xorl %eax, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x32(%rax)
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %edi
callq 0x6c50
movw %ax, %cx
movl -0x24(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
jne 0x88fa
movq -0x10(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
movq %rax, -0x30(%rbp)
jmp 0x890a
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x34(%rax)
jmp 0x891b
cmpl $0x0, -0x1c(%rbp)
je 0x8ab2
movq -0x10(%rbp), %rax
movzwl 0x32(%rax), %eax
movq -0x10(%rbp), %rcx
movzwl 0x34(%rcx), %ecx
cmpl %ecx, %eax
jne 0x8a69
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %edi
callq 0x6c50
movw %ax, %cx
movl -0x34(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
jne 0x8967
jmp 0x8ab2
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
jne 0x899e
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movzwl (%rax), %edi
callq 0x6c50
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x30(%rax)
jmp 0x89ae
movq -0x10(%rbp), %rax
movw 0x30(%rax), %cx
addw $0x1, %cx
movw %cx, 0x30(%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 0x7380
cmpl $0x0, %eax
jne 0x89dc
movl $0x0, -0x4(%rbp)
jmp 0x8ab8
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %edi
callq 0x6c50
movzwl %ax, %edx
andl $0x8000, %edx           # imm = 0x8000
xorl %eax, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x32(%rax)
movq -0x10(%rbp), %rax
movzwl 0x30(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %edi
callq 0x6c50
movw %ax, %cx
movl -0x38(%rbp), %eax
movzwl %cx, %ecx
cmpl %ecx, %eax
jne 0x8a45
movq -0x10(%rbp), %rax
movzwl 0x8(%rax), %edi
callq 0x6c50
movzwl %ax, %eax
movq %rax, -0x40(%rbp)
jmp 0x8a55
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x34(%rax)
jmp 0x891b
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rdx
movw 0x32(%rdx), %cx
movw %cx, %si
addw $0x1, %si
movw %si, 0x32(%rdx)
movzwl %cx, %ecx
movb (%rax,%rcx), %cl
movq -0x18(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x18(%rbp)
movb %cl, (%rax)
movl -0x1c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x891b
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)