// File: /workspace/github_cpp_makefile_star2_O3/hyp1231[P]FMSS/filesystem.cpp
// Function: FileSystem::DeleteFile(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>>)
bool FileSystem::DeleteFile(const string &filepath, const string param) {
    /* 
        step 1: find the right directory
    */

	string filename;
    int dir_inodeNum = Analysis_path(filepath, filename);
    if(dir_inodeNum == -1) {
    	return false;
    }

	if(filename == "." || filename == "..") {
        cout << "[Error] '.' or '..' cannot be removed" << endl;
        return false;
    }

    /*
        step 2: make sure file exists; otherwise, return FALSE
    */

    int file_inodeNum = Get_file_inodeNum_from_dir(dir_inodeNum, filename, false);
    if (file_inodeNum == -1) {
        cout << "[Error] File doesn't exist (cannot delete file \""
        << filename << "\")" << endl;
        return false;
    }

    /*
        step 3: delete data in file's data_BLK
        (set corresponding bit in data bitmap to 0)
    */

    bool isFile;
    char file_buf[64];
    Get_inode_from_inodeNum(file_buf, file_inodeNum);
    // judge if inode is a file_inode
    if (byte2int(file_buf, 0, 2) == 0)
        isFile = true;
    else 
        isFile = false;

    if (!isFile) {  // if it's dir, data should be deleted recursively
        if(param != "-r") {
            cout << "[Error] Cannot delete directory" << endl;
            return false;
        }

        char fileBLK[BLKsize];
        int dataNum = byte2int(file_buf, 42, 44);
        Fill_data_block_bitmap(dataNum, false);
        D.Getblk(fileBLK, Get_actual_dataBLKnumber(dataNum));
        for (int i = 24; i + 12 < BLKsize; i += 12) {
            if (byte2int(fileBLK, i + 8, i + 10) == 0) 
                continue;
            string tmp_filename = byte2string(fileBLK, i, i + 8);
            // delete all files in dir recursively
            DeleteFile(filepath + '/' + tmp_filename, param);
        }
    } else {    // if it's a file, just delete data block one by one
        for (int i = 42; i < 58; i += 2) {
            char fileBLK[BLKsize];
            int dataNum = byte2int(file_buf, i, i + 2);
            Fill_data_block_bitmap(dataNum, false);
            // if EOF, break
            D.Getblk(fileBLK, Get_actual_dataBLKnumber(dataNum));
            if (FindEOF(fileBLK) != -1)
                break;
        }
    }

    /*
        step 4: modify dir's data block to remove this file's inode
        (set valid to 0x0)
     */
    Get_file_inodeNum_from_dir(dir_inodeNum, filename, true);

    /*
        step 5: remove file's inode
        (set corresponding bit in inode bitmap to 0)
    */

    Fill_inode_bitmap(file_inodeNum, false);

    /*
        step 6: save the super block
    */

    Save_superBLK();

    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
leaq -0x40(%rbp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
leaq -0x50(%rbp), %rdx
callq 0x4e5e
cmpl $-0x1, %eax
je 0x4a1a
movl %eax, -0x2c(%rbp)
leaq 0x4c90(%rip), %rsi      # 0x955b
leaq -0x50(%rbp), %rdi
callq 0x2090
testl %eax, %eax
je 0x49d4
leaq 0x4c77(%rip), %rsi      # 0x955a
leaq -0x50(%rbp), %rdi
callq 0x2090
testl %eax, %eax
je 0x49d4
leaq -0x50(%rbp), %rdx
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
xorl %ecx, %ecx
callq 0x35dc
cmpl $-0x1, %eax
je 0x4a44
leaq -0x120(%rbp), %rsi
movq %rbx, %rdi
movl %eax, -0x30(%rbp)
movl %eax, %edx
callq 0x3d48
movsbl -0x120(%rbp), %eax
movsbl -0x11f(%rbp), %ecx
shll $0x8, %ecx
addl %eax, %ecx
jne 0x4ab3
leaq 0x8(%rbx), %r15
movl $0x2a, %esi
cmpl $0x39, %esi
ja 0x4d36
movq %rsp, %r12
movl (%rbx), %eax
movq %rsp, %r14
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
leal 0x2(%rsi), %r13d
leaq -0x120(%rbp), %rdi
movl %r13d, %edx
callq 0x2a36
movslq %eax, %rdx
imulq $0x66666667, %rdx, %rax # imm = 0x66666667
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x25, %rax
addl %ecx, %eax
movslq %eax, %rsi
shll $0x4, %eax
leal (%rax,%rax,4), %eax
movl %edx, %ecx
subl %eax, %ecx
movl $0x1, %eax
shll %cl, %eax
notb %al
andb %al, 0x5a(%rbx,%rsi)
addl $0x6, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x26e0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f56
movq %r12, %rsp
movl %r13d, %esi
cmpl $-0x1, %eax
je 0x4944
jmp 0x4d36
movq 0x85f5(%rip), %rbx      # 0xcfd0
leaq 0x4bae(%rip), %rsi      # 0x9590
movl $0x25, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq 0x85c3(%rip), %rdi      # 0xcfd0
callq 0x2030
movq %rax, %rdi
callq 0x2120
xorl %r14d, %r14d
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x4a32
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2200
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8585(%rip), %rdi      # 0xcfd0
leaq 0x4b64(%rip), %rsi      # 0x95b6
movl $0x30, %edx
callq 0x2240
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x8565(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x4b6d(%rip), %rsi      # 0x95e7
movl $0x2, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
jmp 0x4a1a
leaq 0x4b30(%rip), %rsi      # 0x95ea
movq %r12, %rdi
callq 0x2090
testl %eax, %eax
je 0x4b11
movq 0x8503(%rip), %rbx      # 0xcfd0
leaq 0x4b19(%rip), %rsi      # 0x95ed
movl $0x1f, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq 0x84d1(%rip), %rdi      # 0xcfd0
callq 0x2030
movq %rax, %rdi
callq 0x2120
jmp 0x4a1a
movq %rsp, -0xe0(%rbp)
movl (%rbx), %eax
movq %rsp, %rsi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
movsbl -0xf6(%rbp), %eax
movsbl -0xf5(%rbp), %edx
shll $0x8, %edx
leal (%rdx,%rax), %ecx
movslq %ecx, %rcx
imulq $0x66666667, %rcx, %r8  # imm = 0x66666667
movq %r8, %rdi
shrq $0x3f, %rdi
sarq $0x25, %r8
addl %edi, %r8d
movslq %r8d, %rdi
shll $0x4, %r8d
leal (%r8,%r8,4), %r8d
subl %r8d, %ecx
movl $0x1, %r8d
shll %cl, %r8d
notb %r8b
andb %r8b, 0x5a(%rbx,%rdi)
leaq 0x8(%rbx), %rdi
addl %eax, %edx
addl $0x6, %edx
movq %rsi, -0x78(%rbp)
callq 0x26e0
cmpl $0x25, (%rbx)
jl 0x4d2f
movl $0x24, %r15d
leal -0x4(%r15), %r14d
leal -0x2(%r15), %edx
movq -0x78(%rbp), %rdi
movl %r14d, %esi
callq 0x2a36
testl %eax, %eax
je 0x4d22
leal -0xc(%r15), %edx
leaq -0xd8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl %r14d, %ecx
callq 0x2ab4
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x7268
movq -0x68(%rbp), %rsi
movl $0x1, %ecx
movq %r14, %rdi
xorl %edx, %edx
movl $0x2f, %r8d
callq 0x2380
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
movq %r14, %rdi
callq 0x20a0
leaq -0x88(%rbp), %rsi
movq %rsi, -0x98(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x4c48
movq %rdx, -0x98(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x88(%rbp)
jmp 0x4c4e
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, -0x90(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq -0xb8(%rbp), %r14
movq %r14, %rdi
callq 0x7268
movq %rbx, %rdi
leaq -0x98(%rbp), %rsi
movq %r14, %rdx
callq 0x487e
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x4cc5
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x4ce7
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x4d00
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x4d22
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x2200
addl $0xc, %r15d
cmpl (%rbx), %r15d
jl 0x4b97
movq -0xe0(%rbp), %rsp
leaq -0x50(%rbp), %rdx
movl $0x1, %r14d
movq %rbx, %rdi
movl -0x2c(%rbp), %esi
movl $0x1, %ecx
leaq -0x40(%rbp), %r15
callq 0x35dc
movl -0x30(%rbp), %ecx
movslq %ecx, %rax
imulq $0x66666667, %rax, %rax # imm = 0x66666667
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rax
addl %edx, %eax
leal (%rax,%rax), %edx
leal (%rdx,%rdx,4), %edx
subl %edx, %ecx
shll %cl, %r14d
cltq
notb %r14b
andb %r14b, 0x50(%rbx,%rax)
movb $0x1, %r14b
movq %rbx, %rdi
callq 0x3ed6
jmp 0x4a1d
jmp 0x4e3a
jmp 0x4e3a
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x4dd3
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x4dd3
movq %rax, %rbx
jmp 0x4e0e
jmp 0x4dcb
movq %rax, %rbx
jmp 0x4df5
movq %rax, %rbx
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x4df5
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x4e0e
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x4e3d
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x4e3d
jmp 0x4e3a
jmp 0x4e3a
jmp 0x4e3a
jmp 0x4e3a
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x4e56
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2350