// File: demon386[P]git-store/./libs/filesystem/src/operations.cpp
// Function: (anonymous namespace)::copy_file_api(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool)
bool // true if ok
  copy_file_api(const std::string& from_p,
    const std::string& to_p, bool fail_if_exists)
  {
    const std::size_t buf_sz = 32768;
    boost::scoped_array<char> buf(new char [buf_sz]);
    int infile=-1, outfile=-1;  // -1 means not open

    // bug fixed: code previously did a stat()on the from_file first, but that
    // introduced a gratuitous race condition; the stat()is now done after the open()

    if ((infile = ::open(from_p.c_str(), O_RDONLY))< 0)
      { return false; }

    struct stat from_stat;
    if (::stat(from_p.c_str(), &from_stat)!= 0)
    { 
      ::close(infile);
      return false;
    }

    int oflag = O_CREAT | O_WRONLY | O_TRUNC;
    if (fail_if_exists)
      oflag |= O_EXCL;
    if ((outfile = ::open(to_p.c_str(), oflag, from_stat.st_mode))< 0)
    {
      int open_errno = errno;
      BOOST_ASSERT(infile >= 0);
      ::close(infile);
      errno = open_errno;
      return false;
    }

    ssize_t sz, sz_read=1, sz_write;
    while (sz_read > 0
      && (sz_read = ::read(infile, buf.get(), buf_sz)) > 0)
    {
      // Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
      // Marc Rochkind, Addison-Wesley, 2004, page 94
      sz_write = 0;
      do
      {
        BOOST_ASSERT(sz_read - sz_write > 0);  // #1
          // ticket 4438 claimed possible infinite loop if write returns 0. My analysis
          // is that POSIX specifies 0 return only if 3rd arg is 0, and that will never
          // happen due to loop entry and coninuation conditions. BOOST_ASSERT #1 above
          // and #2 below added to verify that analysis.
        if ((sz = ::write(outfile, buf.get() + sz_write,
          sz_read - sz_write)) < 0)
        { 
          sz_read = sz; // cause read loop termination
          break;        //  and error reported after closes
        }
        BOOST_ASSERT(sz > 0);                  // #2
        sz_write += sz;
      } while (sz_write < sz_read);
    }

    if (::close(infile)< 0)
      sz_read = -1;
    if (::close(outfile)< 0)
      sz_read = -1;

    return sz_read >= 0;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq $0x8000, -0x28(%rbp)    # imm = 0x8000
movl $0x8000, %edi           # imm = 0x8000
callq 0xd030
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x4dd00
movl $0xffffffff, -0x34(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
callq 0xd210
movq %rax, %rdi
xorl %esi, %esi
movb %sil, %al
callq 0xdb00
movl %eax, -0xfc(%rbp)
jmp 0x49164
movl -0xfc(%rbp), %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jge 0x4919c
movb $0x0, -0x1(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x49462
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4dd70
jmp 0x49479
movq -0x10(%rbp), %rdi
callq 0xd210
movq %rax, %rdi
leaq -0xd8(%rbp), %rsi
callq 0xdae0
cmpl $0x0, %eax
je 0x491d3
movl -0x34(%rbp), %edi
callq 0xdc20
jmp 0x491c3
movb $0x0, -0x1(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x49462
movl $0x241, -0xdc(%rbp)     # imm = 0x241
testb $0x1, -0x19(%rbp)
je 0x491f4
movl -0xdc(%rbp), %eax
orl $0x80, %eax
movl %eax, -0xdc(%rbp)
movq -0x18(%rbp), %rdi
callq 0xd210
movq %rax, %rdi
movl -0xdc(%rbp), %esi
movl -0xc0(%rbp), %edx
xorl %eax, %eax
callq 0xdb00
movl %eax, -0x100(%rbp)
jmp 0x4921b
movl -0x100(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jge 0x49290
callq 0xd070
movl (%rax), %eax
movl %eax, -0xe0(%rbp)
cmpl $0x0, -0x34(%rbp)
jl 0x4923e
jmp 0x4925d
leaq 0x1a0ef(%rip), %rdi     # 0x63334
leaq 0x198db(%rip), %rsi     # 0x62b27
movl $0x1b1, %edx            # imm = 0x1B1
leaq 0x1a0e8(%rip), %rcx     # 0x63340
callq 0xd400
movl -0x34(%rbp), %edi
callq 0xdc20
jmp 0x49267
movl -0xe0(%rbp), %eax
movl %eax, -0x104(%rbp)
callq 0xd070
movl -0x104(%rbp), %ecx
movl %ecx, (%rax)
movb $0x0, -0x1(%rbp)
movl $0x1, -0x48(%rbp)
jmp 0x49462
movq $0x1, -0xf0(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xf0(%rbp)
movb %al, -0x105(%rbp)
jle 0x492f6
movl -0x34(%rbp), %eax
movl %eax, -0x114(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4dd20
movl -0x114(%rbp), %edi
movq %rax, %rsi
movl $0x8000, %edx           # imm = 0x8000
callq 0xd940
movq %rax, -0x110(%rbp)
jmp 0x492db
movq -0x110(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
setg %al
movb %al, -0x105(%rbp)
movb -0x105(%rbp), %al
testb $0x1, %al
jne 0x49305
jmp 0x493ff
movq $0x0, -0xf8(%rbp)
movq -0xf0(%rbp), %rax
subq -0xf8(%rbp), %rax
cmpq $0x0, %rax
jle 0x49326
jmp 0x49345
leaq 0x1a06d(%rip), %rdi     # 0x6339a
leaq 0x197f3(%rip), %rsi     # 0x62b27
movl $0x1c0, %edx            # imm = 0x1C0
leaq 0x1a000(%rip), %rcx     # 0x63340
callq 0xd400
movl -0x38(%rbp), %eax
movl %eax, -0x124(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4dd20
movl -0x124(%rbp), %edi
movq %rax, %rsi
movq -0xf8(%rbp), %rax
addq %rax, %rsi
movq -0xf0(%rbp), %rdx
subq %rax, %rdx
callq 0xd1a0
movq %rax, -0x120(%rbp)
jmp 0x49382
movq -0x120(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
jge 0x493a6
movq -0xe8(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x493fa
cmpq $0x0, -0xe8(%rbp)
jle 0x493b2
jmp 0x493d1
leaq 0x19ff8(%rip), %rdi     # 0x633b1
leaq 0x19767(%rip), %rsi     # 0x62b27
movl $0x1cb, %edx            # imm = 0x1CB
leaq 0x19f74(%rip), %rcx     # 0x63340
callq 0xd400
movq -0xe8(%rbp), %rax
addq -0xf8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
cmpq -0xf0(%rbp), %rax
jl 0x49310
jmp 0x4929b
movl -0x34(%rbp), %edi
callq 0xdc20
movl %eax, -0x128(%rbp)
jmp 0x4940f
movl -0x128(%rbp), %eax
cmpl $0x0, %eax
jge 0x49425
movq $-0x1, -0xf0(%rbp)
movl -0x38(%rbp), %edi
callq 0xdc20
movl %eax, -0x12c(%rbp)
jmp 0x49435
movl -0x12c(%rbp), %eax
cmpl $0x0, %eax
jge 0x4944b
movq $-0x1, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
setge %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0x48(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4dd70
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0xdb30
nopw %cs:(%rax,%rax)