// File: /workspace/github_c_makefile_star2_O0/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
subq $0x340, %rsp            # imm = 0x340
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x334(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1241
cmpl $0x4, -0x8(%rbp)
je 0x1241
movq 0x3dd2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1dd4(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10b0
movl %eax, -0x338(%rbp)
movl -0x338(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1050
movq %rax, 0x3e0b(%rip)      # 0x5090
movq $-0x1, %rax
cmpq %rax, 0x3dfd(%rip)      # 0x5090
jne 0x12ab
leaq 0x1dad(%rip), %rdi      # 0x3049
callq 0x10c0
movl $0x1, %edi
callq 0x10e0
callq 0x17e0
movq %rax, -0x320(%rbp)
cmpl $0x3, -0x8(%rbp)
jne 0x12d3
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1030
jmp 0x132c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x1d6c(%rip), %rsi      # 0x304e
callq 0x1080
cmpl $0x0, %eax
je 0x130e
movq 0x3d05(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d54(%rip), %rsi      # 0x3051
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x10e0
movl $0x1, -0x334(%rbp)
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0x1030
leaq -0x210(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x1030
movsbl -0x110(%rbp), %eax
cmpl $0x2f, %eax
je 0x136f
movq 0x3ca6(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d03(%rip), %rsi      # 0x305f
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x1529
movsbl -0x10f(%rbp), %eax
cmpl $0x0, %eax
jne 0x139f
movq 0x3c76(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1ced(%rip), %rsi      # 0x3079
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x1529
leaq -0x110(%rbp), %rdi
callq 0x2180
jmp 0x13ad
leaq -0x110(%rbp), %rdi
callq 0x1ff0
movl %eax, -0x314(%rbp)
cmpl $0x0, -0x314(%rbp)
jne 0x13ec
movq 0x3c29(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1cbe(%rip), %rsi      # 0x3097
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x1529
movq -0x320(%rbp), %rax
movl -0x314(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x328(%rbp)
leaq -0x310(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x2120
leaq -0x310(%rbp), %rdi
leaq 0x1c85(%rip), %rsi      # 0x30b3
callq 0x1080
cmpl $0x0, %eax
je 0x1450
leaq -0x310(%rbp), %rdi
leaq 0x1c6c(%rip), %rsi      # 0x30b2
callq 0x1080
cmpl $0x0, %eax
jne 0x1474
movq 0x3ba1(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c54(%rip), %rsi      # 0x30b5
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x1529
movq -0x328(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x1b40
movl %eax, -0x318(%rbp)
cmpl $0x0, -0x318(%rbp)
jne 0x14b7
movq 0x3b5b(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1bf0(%rip), %rsi      # 0x3097
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x1529
movq -0x320(%rbp), %rax
movl -0x318(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x330(%rbp)
movq -0x330(%rbp), %rax
movzwl (%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x1516
cmpl $0x0, -0x334(%rbp)
jne 0x1516
movq 0x3afc(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1bc6(%rip), %rsi      # 0x30cc
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x1529
movq -0x328(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x2220
movl -0x4(%rbp), %eax
addq $0x340, %rsp            # imm = 0x340
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop