// File: /workspace/github_c_makefile_star2_O2/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 0x142b9(%rip), %rax     # 0x180b8
movq (%rax), %rsi
leaq 0xb254(%rip), %r14      # 0xf05d
leaq 0x5(%rsp), %rdx
leaq 0x7(%rsp), %rcx
leaq 0x10(%rsp), %r8
movq %r14, %rdi
callq 0x4e24
movq %rax, %rbx
movq 0x1428e(%rip), %rax     # 0x180b8
movq 0x8(%rax), %rsi
leaq 0x4(%rsp), %rdx
leaq 0x6(%rsp), %rcx
leaq 0x8(%rsp), %r8
movq %r14, %rdi
callq 0x4e24
movq %rax, %r14
testq %rbx, %rbx
setne %al
testq %r14, %r14
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x3f6c
cmpq %r14, %rbx
jne 0x3e72
movq 0x20(%r14), %rax
testb $0x8, 0x18(%rax)
jne 0x3ffe
testb $0x1, 0x30(%r14)
jne 0x3f7e
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 0x3f95
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rbp
callq *0x70(%r12)
movq %rax, %r12
testb $0x4, 0x1411d(%rip)     # 0x17fd8
je 0x3ef1
movq 0x1412c(%rip), %rdi     # 0x17ff0
leaq 0x18(%rsp), %r15
pushq $0xa
popq %rdx
movq %r15, %rsi
callq 0x32a0
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x3fc8
testq %rax, %rax
je 0x3ef1
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 0x3fab
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 0x3f5f
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq *0x88(%r13)
testq %rax, %rax
jne 0x3f19
movq 0x130ad(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xb627(%rip), %rsi      # 0xf57c
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 0x13073(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
addq $0x8, %r14
leaq 0xb59e(%rip), %rsi      # 0xf531
jmp 0x4013
movq 0x1305c(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xb615(%rip), %rsi      # 0xf5bb
movq %r15, %rdx
jmp 0x4016
movq 0x13046(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
leaq 0xb5da(%rip), %rsi      # 0xf596
movq %r15, %rdx
xorl %eax, %eax
callq 0x31e0
jmp 0x3fe4
movq 0x13029(%rip), %rax     # 0x16ff8
movq (%rax), %rcx
leaq 0xb579(%rip), %rdi      # 0xf552
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x32d0
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 0x12ff3(%rip), %rax     # 0x16ff8
movq (%rax), %rdi
addq $0x8, %r14
leaq 0xb4e4(%rip), %rsi      # 0xf4f7
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