// File: /workspace/github_c_makefile_star2_O2/JavieW[P]CSCC69_Ext2_File-System/ext2_rm_bonus.c
// Function: main
int main(int argc, char **argv) {
    char parentDirPath[EXT2_NAME_LEN];
    char parentDirPathCopy[EXT2_NAME_LEN];
    char fileName[EXT2_NAME_LEN];
    int parentInodeNum, childInodeNum;
    struct ext2_inode *inodeTable;
    struct ext2_inode *parentInode, *childInode;
    int flagged = 0;

    if(argc!=3 && argc!=4) {
        fprintf(stderr, "Usage: ext2_rm <image file name> <optional flag -r> <absolute path>\n");
        exit(1);
    }

    // read disk and get inode table
    int fd = open(argv[1], O_RDWR);
    disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    inodeTable = getInodeTable();

    // read other arguments
    if(argc == 3) {
        strcpy(parentDirPath, argv[2]);
    } else {
        if (strcmp(argv[2], "-r")!=0) {
            fprintf(stderr, "Invalid Flag\n");
            exit(1);
        }
        flagged = 1;
        strcpy(parentDirPath, argv[3]);
    }
    strcpy(parentDirPathCopy, parentDirPath);

    // get the parent directory inode
    if (parentDirPath[0]!='/') {
        fprintf(stderr, "Must be an absolute path\n");
        return ENOENT;
    } else if (parentDirPath[1]=='\0'){
        fprintf(stderr, "Cannot remove root directory\n");
        return ENOENT;
    } else {
        getParentDirPath(parentDirPath);
    }
    parentInodeNum = getInodeFromPath(parentDirPath);
    if (parentInodeNum == 0) {
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    parentInode = &inodeTable[parentInodeNum-1];

    // check file exist
    getFileNameFromPath(fileName, parentDirPathCopy);
    if (strcmp(fileName, ".") == 0 || strcmp(fileName, "..") == 0){
        fprintf(stderr, "Cannot remove . or ..\n");
        return ENOENT;
    }
    childInodeNum = searchFileInDir(parentInode, fileName);
    if (childInodeNum == 0) {
        fprintf(stderr, "No such file or directory\n");
        return ENOENT;
    }
    childInode = &inodeTable[childInodeNum-1];

    // if the file is a directory
    if ((childInode->i_mode & EXT2_S_IFDIR) && !flagged){
        fprintf(stderr, "Cannot remove a directory without -r flag\n");
        return ENOENT;
    }

    rm(parentInode, fileName);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movl %edi, %r14d
leal -0x5(%r14), %eax
cmpl $-0x3, %eax
jbe 0x1401
movq %rsi, %r12
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10b0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1050
movq %rax, 0x2e46(%rip)      # 0x4098
cmpq $-0x1, %rax
je 0x141f
callq 0x154f
movq %rax, %r15
movq 0x10(%r12), %rsi
cmpl $0x3, %r14d
je 0x1291
cmpb $0x2d, (%rsi)
jne 0x13ec
cmpb $0x72, 0x1(%rsi)
jne 0x13ec
cmpb $0x0, 0x2(%rsi)
jne 0x13ec
movq 0x18(%r12), %rsi
leaq -0x230(%rbp), %r12
movq %r12, %rdi
callq 0x1030
leaq -0x330(%rbp), %rdi
movq %r12, %rsi
callq 0x1030
cmpb $0x2f, (%r12)
jne 0x1392
cmpb $0x0, -0x22f(%rbp)
je 0x13a7
movq %r12, %rdi
callq 0x1b77
movq %r12, %rdi
callq 0x1a7a
testl %eax, %eax
je 0x1368
movl %eax, %r12d
leaq -0x130(%rbp), %r13
leaq -0x330(%rbp), %rsi
movq %r13, %rdi
callq 0x1b42
cmpw $0x2e, (%r13)
je 0x137d
leaq 0xda8(%rip), %rsi       # 0x20af
leaq -0x130(%rbp), %rdi
pushq $0x3
popq %rdx
callq 0x10a0
testl %eax, %eax
je 0x137d
movslq %r12d, %rax
shlq $0x7, %rax
leaq (%r15,%rax), %r12
addq $-0x80, %r12
leaq -0x130(%rbp), %rsi
movq %r12, %rdi
callq 0x1808
testl %eax, %eax
je 0x1368
cmpl $0x3, %r14d
jne 0x1355
cltq
shlq $0x7, %rax
movzwl -0x80(%r15,%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
jne 0x13bc
leaq -0x130(%rbp), %rsi
movq %r12, %rdi
callq 0x1bd6
xorl %ebx, %ebx
jmp 0x13d8
movq 0x2c89(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xd1b(%rip), %rdi       # 0x2094
pushq $0x1a
jmp 0x13cf
movq 0x2c74(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xd24(%rip), %rdi       # 0x20b2
pushq $0x16
jmp 0x13cf
movq 0x2c5f(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xcb9(%rip), %rdi       # 0x205c
pushq $0x19
jmp 0x13cf
movq 0x2c4a(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xcbe(%rip), %rdi       # 0x2076
pushq $0x1d
jmp 0x13cf
movq 0x2c35(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xcfc(%rip), %rdi       # 0x20c9
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
movl %ebx, %eax
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c05(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc51(%rip), %rdi       # 0x204e
pushq $0xd
jmp 0x1414
movq 0x2bf0(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xbf2(%rip), %rdi       # 0x2004
pushq $0x44
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x142b
leaq 0xc23(%rip), %rdi       # 0x2049
callq 0x10c0
pushq $0x1
popq %rdi
callq 0x10e0
nop