// File: /workspace/github_c_makefile_star2_O1/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 0x13b56(%rip), %rax     # 0x190a8
movq (%rax), %rsi
leaq 0xab01(%rip), %r14      # 0x1005d
leaq 0x5(%rsp), %rdx
leaq 0x7(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r14, %rdi
callq 0x63c4
movq %rax, %rbx
movq 0x13b2b(%rip), %rax     # 0x190a8
movq 0x8(%rax), %rsi
leaq 0x4(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %r14, %rdi
callq 0x63c4
movq %rax, %r14
testq %rbx, %rbx
setne %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x56c1
cmpq %r14, %rbx
jne 0x55c5
movq 0x20(%r14), %rax
testb $0x8, 0x18(%rax)
jne 0x5745
testb $0x1, 0x30(%r14)
jne 0x56d3
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 0x56ea
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rbp
callq *0x70(%r12)
movq %rax, %r12
testb $0x4, 0x139ba(%rip)     # 0x18fc8
je 0x5646
movq 0x139c9(%rip), %rdi     # 0x18fe0
leaq 0x18(%rsp), %r15
movq %r15, %rsi
movl $0xa, %edx
callq 0x3290
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x5735
testq %rax, %rax
je 0x5646
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 0x5700
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 0x56b4
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq *0x88(%r13)
testq %rax, %rax
jne 0x566e
movq 0x12950(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xaed2(%rip), %rsi      # 0x1057c
movq %r15, %rdx
xorl %eax, %eax
callq 0x31d0
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 0x12916(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
addq $0x8, %r14
leaq 0xae49(%rip), %rsi      # 0x10531
jmp 0x575a
movq 0x128ff(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xaec0(%rip), %rsi      # 0x105bb
movq %r15, %rdx
jmp 0x575d
movq 0x128e9(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
leaq 0xae85(%rip), %rsi      # 0x10596
movq %r15, %rdx
xorl %eax, %eax
callq 0x31d0
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 0x3470
jmp 0x56c1
movq 0x128a4(%rip), %rax     # 0x17ff0
movq (%rax), %rdi
addq $0x8, %r14
leaq 0xad9d(%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 0x31d0