// File: hyp1231[P]FMSS/filesystem.cpp
// Function: FileSystem::CopyFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool FileSystem::CopyFile(string& S_filepath, string& D_filepath) {
    if(isOutside_path(S_filepath)) {        // souce is outside path
        if(isOutside_path(D_filepath)) {    // both outside path
            cout << "[Error] Cannot move an outside file to another outside place" << endl;
            return false;
        } else {    // move from outside to inside
            /*
                step 1: test if can open the outside file, and change file to string (attention length)
            */

            S_filepath = S_filepath.substr(1);

            ifstream ifile(S_filepath);
            if(!ifile) {    // cannot open
                cout << "[Error] Illegal outside path:" << S_filepath << endl;
                return false;
            }

            // store the data in outside file
            string file_data;

            if(!Outside_file2str(ifile, file_data)) {
                ifile.close();
                return false;
            }

            ifile.close();

            if(!str2Inside_file(file_data, D_filepath)) {
                return false;
            }
        }
    } else {        // souce is inside path
        if(isOutside_path(D_filepath)) {    // move from inside to outside
            D_filepath = D_filepath.substr(1);

            // convert inside file to string
            string file_data;
            if(!Inside_file2str(S_filepath, file_data)) {
                return false;
            }

            ofstream ofile(D_filepath);
            if(!ofile) {
                cout << "[Error] Cannot open file: " << D_filepath << endl;
                return false;
            }

            ofile << file_data;
            ofile.close();
        } else {    // both inside
            string filename;
            int dir_inodeNum = Analysis_path(S_filepath, filename);
            if(dir_inodeNum == -1) {
                return false;
            }
            int file_inodeNum = Get_file_inodeNum_from_dir(dir_inodeNum, filename);
            if(file_inodeNum == -1) {
                return false;
            }
            if(isDir(file_inodeNum)) {
                cout << "[Error] Source cannot be a directory" << endl;
                return false;
            }

            // convert inside file to string
            string file_data;
            if(!Inside_file2str(S_filepath, file_data)) {
                return false;
            }

            str2Inside_file(file_data, D_filepath);
        }
    }

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5a0, %rsp            # imm = 0x5A0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x508(%rbp)
movq -0x18(%rbp), %rsi
callq 0x9960
testb $0x1, %al
jne 0x8244
jmp 0x8489
movq -0x508(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x9960
testb $0x1, %al
jne 0x825a
jmp 0x8285
movq 0xcd6f(%rip), %rdi      # 0x14fd0
leaq 0x7405(%rip), %rsi      # 0xf66d
callq 0x3250
movq %rax, %rdi
movq 0xcd49(%rip), %rsi      # 0x14fc0
callq 0x32a0
movb $0x0, -0x1(%rbp)
jmp 0x8865
movq -0x18(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x518(%rbp)
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x3280
movq -0x518(%rbp), %rsi
movq -0x18(%rbp), %rdi
callq 0x3330
movq -0x518(%rbp), %rdi
callq 0x3160
movq -0x18(%rbp), %rsi
leaq -0x248(%rbp), %rdi
movl $0x8, %edx
callq 0x3450
movq -0x248(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x248(%rbp,%rax), %rdi
callq 0x3120
movb %al, -0x509(%rbp)
jmp 0x82f6
movb -0x509(%rbp), %al
testb $0x1, %al
jne 0x8302
jmp 0x8376
movq 0xccc7(%rip), %rdi      # 0x14fd0
leaq 0x739a(%rip), %rsi      # 0xf6aa
callq 0x3250
movq %rax, -0x520(%rbp)
jmp 0x831e
movq -0x520(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3230
movq %rax, -0x528(%rbp)
jmp 0x8337
movq -0x528(%rbp), %rdi
movq 0xcc7b(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x834c
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8454
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
jmp 0x8473
leaq -0x278(%rbp), %rdi
movq %rdi, -0x538(%rbp)
callq 0x33e0
movq -0x508(%rbp), %rdi
movq -0x538(%rbp), %rdx
leaq -0x248(%rbp), %rsi
callq 0x6ea0
movb %al, -0x529(%rbp)
jmp 0x83ab
movb -0x529(%rbp), %al
testb $0x1, %al
jne 0x83f5
jmp 0x83b7
leaq -0x248(%rbp), %rdi
callq 0x3080
jmp 0x83c5
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x3160
jmp 0x8473
leaq -0x248(%rbp), %rdi
callq 0x3080
jmp 0x8403
movq -0x508(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq -0x278(%rbp), %rsi
callq 0x7c00
movb %al, -0x539(%rbp)
jmp 0x8422
movb -0x539(%rbp), %al
testb $0x1, %al
jne 0x843e
jmp 0x842e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8448
movl $0x0, -0x258(%rbp)
leaq -0x278(%rbp), %rdi
callq 0x3160
leaq -0x248(%rbp), %rdi
callq 0x3090
movl -0x258(%rbp), %eax
testl %eax, %eax
je 0x8471
jmp 0x846c
jmp 0x8865
jmp 0x8484
leaq -0x248(%rbp), %rdi
callq 0x3090
jmp 0x8873
jmp 0x8861
movq -0x508(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x9960
testb $0x1, %al
jne 0x84a2
jmp 0x8672
movq -0x20(%rbp), %rsi
leaq -0x298(%rbp), %rdi
movq %rdi, -0x550(%rbp)
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x3280
movq -0x550(%rbp), %rsi
movq -0x20(%rbp), %rdi
callq 0x3330
movq -0x550(%rbp), %rdi
callq 0x3160
leaq -0x2b8(%rbp), %rdi
movq %rdi, -0x548(%rbp)
callq 0x33e0
movq -0x508(%rbp), %rdi
movq -0x548(%rbp), %rdx
movq -0x18(%rbp), %rsi
callq 0x8880
movb %al, -0x53a(%rbp)
jmp 0x8513
movb -0x53a(%rbp), %al
testb $0x1, %al
jne 0x8549
jmp 0x851f
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x863f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
jmp 0x8661
movq -0x20(%rbp), %rsi
leaq -0x4b8(%rbp), %rdi
movl $0x10, %edx
callq 0x34c0
jmp 0x8560
movq -0x4b8(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x4b8(%rbp,%rax), %rdi
callq 0x3120
movb %al, -0x551(%rbp)
jmp 0x8580
movb -0x551(%rbp), %al
testb $0x1, %al
jne 0x858c
jmp 0x8606
movq 0xca3d(%rip), %rdi      # 0x14fd0
leaq 0x712e(%rip), %rsi      # 0xf6c8
callq 0x3250
movq %rax, -0x560(%rbp)
jmp 0x85a8
movq -0x560(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3230
movq %rax, -0x568(%rbp)
jmp 0x85c1
movq -0x568(%rbp), %rdi
movq 0xc9f1(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x85d6
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8633
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
leaq -0x4b8(%rbp), %rdi
callq 0x3410
jmp 0x8661
leaq -0x4b8(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x3230
jmp 0x861b
leaq -0x4b8(%rbp), %rdi
callq 0x33f0
jmp 0x8629
movl $0x0, -0x258(%rbp)
leaq -0x4b8(%rbp), %rdi
callq 0x3410
leaq -0x2b8(%rbp), %rdi
callq 0x3160
movl -0x258(%rbp), %eax
testl %eax, %eax
je 0x865c
jmp 0x8657
jmp 0x8865
jmp 0x885f
leaq -0x2b8(%rbp), %rdi
callq 0x3160
jmp 0x8873
leaq -0x4d8(%rbp), %rdi
movq %rdi, -0x578(%rbp)
callq 0x33e0
movq -0x508(%rbp), %rdi
movq -0x578(%rbp), %rdx
movq -0x18(%rbp), %rsi
callq 0x65b0
movl %eax, -0x56c(%rbp)
jmp 0x86a4
movl -0x56c(%rbp), %eax
movl %eax, -0x4dc(%rbp)
cmpl $-0x1, -0x4dc(%rbp)
jne 0x86e3
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8835
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
jmp 0x8851
movq -0x508(%rbp), %rdi
movl -0x4dc(%rbp), %esi
leaq -0x4d8(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4d30
movl %eax, -0x57c(%rbp)
jmp 0x8706
movl -0x57c(%rbp), %eax
movl %eax, -0x4e0(%rbp)
cmpl $-0x1, -0x4e0(%rbp)
jne 0x872e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8835
movq -0x508(%rbp), %rdi
movl -0x4e0(%rbp), %esi
callq 0x6800
movb %al, -0x57d(%rbp)
jmp 0x8748
movb -0x57d(%rbp), %al
testb $0x1, %al
jne 0x8754
jmp 0x8798
movq 0xc875(%rip), %rdi      # 0x14fd0
leaq 0x6f81(%rip), %rsi      # 0xf6e3
callq 0x3250
movq %rax, -0x588(%rbp)
jmp 0x8770
movq -0x588(%rbp), %rdi
movq 0xc842(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x8785
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8835
leaq -0x500(%rbp), %rdi
movq %rdi, -0x598(%rbp)
callq 0x33e0
movq -0x508(%rbp), %rdi
movq -0x598(%rbp), %rdx
movq -0x18(%rbp), %rsi
callq 0x8880
movb %al, -0x589(%rbp)
jmp 0x87ca
movb -0x589(%rbp), %al
testb $0x1, %al
jne 0x8806
jmp 0x87d6
movb $0x0, -0x1(%rbp)
movl $0x1, -0x258(%rbp)
jmp 0x8829
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x250(%rbp)
movl %eax, -0x254(%rbp)
leaq -0x500(%rbp), %rdi
callq 0x3160
jmp 0x8851
movq -0x508(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq -0x500(%rbp), %rsi
callq 0x7c00
jmp 0x881f
movl $0x0, -0x258(%rbp)
leaq -0x500(%rbp), %rdi
callq 0x3160
leaq -0x4d8(%rbp), %rdi
callq 0x3160
movl -0x258(%rbp), %eax
testl %eax, %eax
je 0x884f
jmp 0x884d
jmp 0x8865
jmp 0x885f
leaq -0x4d8(%rbp), %rdi
callq 0x3160
jmp 0x8873
jmp 0x8861
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x5a0, %rsp            # imm = 0x5A0
popq %rbp
retq
movq -0x250(%rbp), %rdi
callq 0x3490
nop