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

    if(argc!=3) {
        fprintf(stderr, "Usage: ext2_rm <image file name> <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();

    // get the parent directory inode
    strcpy(parentDirPath, argv[2]);
    // if it's not a absolute path
    if (parentDirPath[0]!='/') {
        fprintf(stderr, "Must be an absolute path\n");
        return ENOENT;
    // if it's a root directory
    } else if (parentDirPath[1]=='\0'){
        fprintf(stderr, "Cannot remove root directory\n");
        return ENOENT;
    // general case
    } 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, argv[2]);
    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) {
        fprintf(stderr, "Cannot remove a directory\n");
        return ENOENT;
    }

    rm(parentInode, fileName);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1231
movq 0x3de2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1de4(%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, -0x234(%rbp)
movl -0x234(%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, 0x3e1b(%rip)      # 0x5090
movq $-0x1, %rax
cmpq %rax, 0x3e0d(%rip)      # 0x5090
jne 0x129b
leaq 0x1daa(%rip), %rdi      # 0x3036
callq 0x10c0
movl $0x1, %edi
callq 0x10e0
callq 0x1750
movq %rax, -0x220(%rbp)
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1030
movsbl -0x110(%rbp), %eax
cmpl $0x2f, %eax
je 0x12eb
movq 0x3d2a(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d63(%rip), %rsi      # 0x303b
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x149d
movsbl -0x10f(%rbp), %eax
cmpl $0x0, %eax
jne 0x131b
movq 0x3cfa(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d4d(%rip), %rsi      # 0x3055
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x149d
leaq -0x110(%rbp), %rdi
callq 0x20f0
jmp 0x1329
leaq -0x110(%rbp), %rdi
callq 0x1f60
movl %eax, -0x214(%rbp)
cmpl $0x0, -0x214(%rbp)
jne 0x1368
movq 0x3cad(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1d1e(%rip), %rsi      # 0x3073
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x149d
movq -0x220(%rbp), %rax
movl -0x214(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x228(%rbp)
leaq -0x210(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x2090
leaq -0x210(%rbp), %rdi
leaq 0x1ce4(%rip), %rsi      # 0x308f
callq 0x1080
cmpl $0x0, %eax
je 0x13cd
leaq -0x210(%rbp), %rdi
leaq 0x1ccb(%rip), %rsi      # 0x308e
callq 0x1080
cmpl $0x0, %eax
jne 0x13f1
movq 0x3c24(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1cb3(%rip), %rsi      # 0x3091
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x149d
movq -0x228(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x1ab0
movl %eax, -0x218(%rbp)
cmpl $0x0, -0x218(%rbp)
jne 0x1434
movq 0x3bde(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c4f(%rip), %rsi      # 0x3073
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x149d
movq -0x220(%rbp), %rax
movl -0x218(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x230(%rbp)
movq -0x230(%rbp), %rax
movzwl (%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x148a
movq 0x3b88(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1c2e(%rip), %rsi      # 0x30a8
movb $0x0, %al
callq 0x1090
movl $0x2, -0x4(%rbp)
jmp 0x149d
movq -0x228(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x2190
movl -0x4(%rbp), %eax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopl (%rax)