// File: /workspace/github_c_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x300, %rsp            # imm = 0x300
movl %edi, %ebx
leal -0x5(%rbx), %eax
cmpl $-0x3, %eax
jbe 0x14da
movq %rsi, %r15
movq 0x8(%rsi), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10b0
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1050
movq %rax, 0x2d06(%rip)      # 0x4098
cmpq $-0x1, %rax
je 0x14df
callq 0x1615
movq %rax, %r14
movq 0x10(%r15), %rsi
cmpl $0x3, %ebx
je 0x13cb
leaq 0xc9a(%rip), %rax       # 0x204e
movq %rsi, %rdi
movq %rax, %rsi
callq 0x1090
testl %eax, %eax
jne 0x14e4
movq 0x18(%r15), %rsi
leaq -0x220(%rbp), %r15
movq %r15, %rdi
callq 0x1030
leaq -0x320(%rbp), %rdi
movq %r15, %rsi
callq 0x1030
cmpb $0x2f, (%r15)
jne 0x14a4
cmpb $0x0, -0x21f(%rbp)
je 0x14ab
movq %r15, %rdi
callq 0x1d5d
movq %r15, %rdi
callq 0x1c42
testl %eax, %eax
je 0x14b2
movl %eax, %r15d
leaq -0x120(%rbp), %r12
leaq -0x320(%rbp), %rsi
movq %r12, %rdi
callq 0x1d16
cmpw $0x2e, (%r12)
je 0x149d
movl $0x2e2e, %eax           # imm = 0x2E2E
xorl -0x120(%rbp), %eax
movzbl -0x11e(%rbp), %ecx
orw %ax, %cx
je 0x149d
movslq %r15d, %rax
shlq $0x7, %rax
leaq (%r14,%rax), %r15
addq $-0x80, %r15
leaq -0x120(%rbp), %rsi
movq %r15, %rdi
callq 0x1952
testl %eax, %eax
je 0x14b9
cmpl $0x3, %ebx
jne 0x148a
cltq
shlq $0x7, %rax
movzwl -0x80(%r14,%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
jne 0x14c0
leaq -0x120(%rbp), %rsi
movq %r15, %rdi
callq 0x1dc3
xorl %eax, %eax
jmp 0x14ca
callq 0x119e
jmp 0x14c5
callq 0x1138
jmp 0x14c5
callq 0x11e2
jmp 0x14c5
callq 0x11c0
jmp 0x14c5
callq 0x117c
jmp 0x14c5
callq 0x115a
movl $0x2, %eax
addq $0x300, %rsp            # imm = 0x300
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x121c
callq 0x1204
callq 0x1110
nopl (%rax)