// File: /workspace/github_c_makefile_star2_O3/simh[P]simtools/converters/fsio/fsio.c
// Function: doCopy
static void doCopy(void)
{
  struct mountedFS *mountSrc, *mountDest;
  struct FSdef *fsSrc, *fsDest;
  uint8_t unitSrc, unitDest, presentSrc, presentDest;
  char *fnameSrc, *fnameDest;
  void *fileSrc, *fileDest;
  char *endptr;

  mountSrc = findMount("copy", words[0], &unitSrc, &presentSrc, &fnameSrc);
  mountDest = findMount("copy", words[1], &unitDest, &presentDest, &fnameDest);

  if ((mountSrc != NULL) && (mountDest != NULL)) {
    fsSrc = mountSrc->filesys;
    fsDest = mountDest->filesys;

    if (mountSrc == mountDest) {
      if ((fsDest->flags & FS_1OPENFILE) != 0) {
        fprintf(stderr,
                "copy: \"%s\" does not allow simultaneous read/write access\n",
                mountDest->name);
        return;
      }
    }

    if ((mountDest->flags & FS_READONLY) != 0) {
      fprintf(stderr, "copy: \"%s\" is mounted read-only\n", mountDest->name);
      return;
    }
    fsSrc = mountSrc->filesys;
    fsDest = mountDest->filesys;

    if ((fileSrc = (*fsSrc->openFileR)(mountSrc, unitSrc, fnameSrc)) != NULL) {
      off_t size = (*fsSrc->fileSize)(fileSrc);
      unsigned long contig;

      if (SWISSET('c')) {
        contig = strtoul(SWGETVAL('c'), &endptr, 10);
        if (*endptr != '\0') {
          fprintf(stderr, "copy: Invalid character in '-c' argument\n");
          (*fsSrc->closeFile)(fileSrc);
          return;
        }
        if (contig != 0)
          size = MAX(size, (long)(contig * mountDest->blocksz));
      }

      if ((fileDest = (*fsDest->openFileW)(mountDest, unitDest, fnameDest, size)) != NULL) {
        char buf[BFRSIZ];
        size_t len;

        while ((len = (*fsSrc->readFile)(fileSrc, buf, BFRSIZ)) != 0) {
          if ((*fsDest->writeFile)(fileDest, buf, len) == 0) {
            fprintf(stderr, "copy: Error writing \"%s\"\n", fnameDest);
            break;
          }
        }
        (*fsDest->closeFile)(fileDest);
        (*fsSrc->closeFile)(fileSrc);
      } else {
        fprintf(stderr,
                "copy: failed to open \"%s\" for write\n", fnameDest);
        (*fsSrc->closeFile)(fileSrc);
      }
    } else fprintf(stderr,
                   "copy: failed to open \"%s\" for read\n", fnameSrc);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq 0x13bcc(%rip), %rax     # 0x190b8
movq (%rax), %rsi
leaq 0xab67(%rip), %r14      # 0x1005d
leaq 0x5(%rsp), %rdx
leaq 0x7(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r14, %rdi
callq 0x636e
movq %rax, %rbx
movq 0x13ba1(%rip), %rax     # 0x190b8
movq 0x8(%rax), %rsi
leaq 0x4(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %r14, %rdi
callq 0x636e
movq %rax, %r14
testq %rbx, %rbx
setne %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x565b
cmpq %r14, %rbx
jne 0x555f
movq 0x20(%r14), %rax
testb $0x8, 0x18(%rax)
jne 0x56df
testb $0x1, 0x30(%r14)
jne 0x566d
movq 0x20(%rbx), %r12
movq 0x20(%r14), %r13
movzbl 0x5(%rsp), %esi
movq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rdx
callq *0x60(%r12)
testq %rax, %rax
je 0x5684
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rbp
callq *0x70(%r12)
movq %rax, %r12
testb $0x4, 0x13a30(%rip)     # 0x18fd8
je 0x55e0
movq 0x13a3f(%rip), %rdi     # 0x18ff0
leaq 0x18(%rsp), %r15
movq %r15, %rsi
movl $0xa, %edx
callq 0x32a0
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x56cf
testq %rax, %rax
je 0x55e0
imulq 0x28(%r14), %rax
cmpq %rax, %r12
cmovleq %rax, %r12
movzbl 0x4(%rsp), %esi
movq 0x8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq *0x68(%r13)
testq %rax, %rax
je 0x569a
movq %rax, %r14
leaq 0x20(%rsp), %r12
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
movq %r12, %rsi
callq *0x80(%rbp)
testq %rax, %rax
je 0x564e
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq *0x88(%r13)
testq %rax, %rax
jne 0x5608
movq 0x129b6(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xaf38(%rip), %rsi      # 0x1057c
movq %r15, %rdx
xorl %eax, %eax
callq 0x31e0
movq %r14, %rdi
callq *0x78(%r13)
movq %rbx, %rdi
callq *0x78(%rbp)
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1297c(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
addq $0x8, %r14
leaq 0xaeaf(%rip), %rsi      # 0x10531
jmp 0x56f4
movq 0x12965(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xaf26(%rip), %rsi      # 0x105bb
movq %r15, %rdx
jmp 0x56f7
movq 0x1294f(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xaeeb(%rip), %rsi      # 0x10596
movq %r15, %rdx
xorl %eax, %eax
callq 0x31e0
movq %rbx, %rdi
movq %rbp, %rax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *0x78(%rax)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3480
jmp 0x565b
movq 0x1290a(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
addq $0x8, %r14
leaq 0xae03(%rip), %rsi      # 0x104f7
movq %r14, %rdx
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31e0