// File: /workspace/github_cpp_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movq %rdx, -0x30(%rbp)
movq %rsi, %r13
movq %rdi, %rbx
leaq -0x50(%rbp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq -0x60(%rbp), %rdx
callq 0x48f6
movl %eax, %r14d
cmpl $-0x1, %eax
je 0x4460
leaq 0x518b(%rip), %rsi      # 0x94ca
leaq -0x60(%rbp), %rdi
callq 0x2090
testl %eax, %eax
je 0x441a
leaq 0x5172(%rip), %rsi      # 0x94c9
leaq -0x60(%rbp), %rdi
callq 0x2090
testl %eax, %eax
je 0x441a
leaq -0x60(%rbp), %rdx
movq %rbx, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0x35de
cmpl $-0x1, %eax
je 0x448a
leaq -0x130(%rbp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, -0x3c(%rbp)
movl %eax, %edx
callq 0x37c4
xorl %ecx, %ecx
xorl %eax, %eax
movsbl (%r15), %edx
shll %cl, %edx
addl %edx, %eax
addq $0x8, %rcx
incq %r15
cmpq $0x10, %rcx
jne 0x439d
testl %eax, %eax
je 0x44f9
leaq 0x5198(%rip), %rsi      # 0x9559
movq -0x30(%rbp), %rdi
callq 0x2090
testl %eax, %eax
je 0x4596
movq 0x8bf7(%rip), %rbx      # 0xcfd0
leaq 0x517c(%rip), %rsi      # 0x955c
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 0x8bc5(%rip), %rdi      # 0xcfd0
callq 0x2030
movq %rax, %rdi
callq 0x2120
jmp 0x4460
movq 0x8baf(%rip), %rbx      # 0xcfd0
leaq 0x50d7(%rip), %rsi      # 0x94ff
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 0x8b7d(%rip), %rdi      # 0xcfd0
callq 0x2030
movq %rax, %rdi
callq 0x2120
xorl %r14d, %r14d
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
je 0x4478
movq -0x50(%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 0x8b3f(%rip), %rdi      # 0xcfd0
leaq 0x508d(%rip), %rsi      # 0x9525
movl $0x30, %edx
callq 0x2240
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x8b1f(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x5096(%rip), %rsi      # 0x9556
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 0x4460
movl %r14d, -0x30(%rbp)
leaq 0x8(%rbx), %r15
movl $0x2a, %esi
cmpl $0x39, %esi
ja 0x47ce
movq %rsp, %r14
movl (%rbx), %eax
movq %rsp, %r12
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
leal 0x2(%rsi), %r13d
leaq -0x130(%rbp), %rdi
movl %r13d, %edx
callq 0x2a38
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 %r12, %rsi
callq 0x26d0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3a16
movq %r14, %rsp
movl %r13d, %esi
cmpl $-0x1, %eax
je 0x4506
jmp 0x47ce
movq %rsp, -0xe8(%rbp)
movl (%rbx), %eax
movq %rsp, %rcx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
xorl %ecx, %ecx
leaq -0x106(%rbp), %rax
xorl %edx, %edx
movsbl (%rax), %esi
shll %cl, %esi
addl %esi, %edx
incq %rax
addq $0x8, %rcx
cmpq $0x10, %rcx
jne 0x45bf
movslq %edx, %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)
leaq 0x8(%rbx), %rdi
addl $0x6, %edx
movq -0x38(%rbp), %rsi
callq 0x26d0
cmpl $0x25, (%rbx)
jl 0x47c3
movl $0x24, %r12d
leal -0x4(%r12), %r15d
leal -0x2(%r12), %edx
movq -0x38(%rbp), %rdi
movl %r15d, %esi
callq 0x2a38
testl %eax, %eax
je 0x47b6
leal -0xc(%r12), %edx
leaq -0xe0(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %ecx
callq 0x2ab6
leaq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %r15
movq %r15, %rdi
callq 0x6c9e
movq -0x78(%rbp), %rsi
movl $0x1, %ecx
movq %r15, %rdi
xorl %edx, %edx
movl $0x2f, %r8d
callq 0x2380
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
movq %r15, %rdi
callq 0x20a0
leaq -0x90(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
movq -0x30(%rbp), %rsi
je 0x46db
movq %rdx, -0xa0(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x90(%rbp)
jmp 0x46e1
movups (%rcx), %xmm0
movups %xmm0, (%rdi)
movq 0x8(%rax), %rdx
movq %rdx, -0x98(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq %rsi, %rax
movq (%rsi), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %r15
movq %r15, %rdi
callq 0x6c9e
movq %rbx, %rdi
leaq -0xa0(%rbp), %rsi
movq %r15, %rdx
callq 0x42ee
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x4759
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0x477b
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x4794
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x47b6
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x2200
addl $0xc, %r12d
cmpl (%rbx), %r12d
jl 0x4623
movl %r14d, -0x30(%rbp)
movq -0xe8(%rbp), %rsp
leaq -0x60(%rbp), %rdx
movl $0x1, %r14d
movq %rbx, %rdi
movl -0x30(%rbp), %esi
movl $0x1, %ecx
leaq -0x50(%rbp), %r12
callq 0x35de
movl -0x3c(%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 0x3996
jmp 0x4463
jmp 0x48d2
jmp 0x48d2
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x486b
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x486b
movq %rax, %rbx
jmp 0x48a6
jmp 0x4863
movq %rax, %rbx
jmp 0x488d
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0x488d
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x48a6
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x48d5
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x48d5
jmp 0x48d2
jmp 0x48d2
jmp 0x48d2
jmp 0x48d2
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x48ee
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2350