// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x200, %rsp            # imm = 0x200
cmpl $0x3, %edi
jne 0x1394
movq %rsi, %r15
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, 0x2e4f(%rip)      # 0x4098
cmpq $-0x1, %rax
je 0x13b2
callq 0x14e3
movq %rax, %r14
movq 0x10(%r15), %rsi
leaq -0x220(%rbp), %r12
movq %r12, %rdi
callq 0x1030
cmpb $0x2f, (%r12)
jne 0x133c
cmpb $0x0, -0x21f(%rbp)
je 0x1351
movq %r12, %rdi
callq 0x1b0b
movq %r12, %rdi
callq 0x1a0e
testl %eax, %eax
je 0x1314
movl %eax, %r12d
movq 0x10(%r15), %rsi
leaq -0x120(%rbp), %r15
movq %r15, %rdi
callq 0x1ad6
cmpw $0x2e, (%r15)
je 0x1327
leaq 0xdd0(%rip), %rsi       # 0x208e
leaq -0x120(%rbp), %rdi
pushq $0x3
popq %rdx
callq 0x10a0
testl %eax, %eax
je 0x1327
movslq %r12d, %rax
shlq $0x7, %rax
leaq (%r14,%rax), %r15
addq $-0x80, %r15
leaq -0x120(%rbp), %rsi
movq %r15, %rdi
callq 0x179c
testl %eax, %eax
je 0x1314
cltq
shlq $0x7, %rax
testb $0x40, -0x7f(%r14,%rax)
jne 0x1366
leaq -0x120(%rbp), %rsi
movq %r15, %rdi
callq 0x1b6a
xorl %ebx, %ebx
jmp 0x1382
movq 0x2cdd(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xd4e(%rip), %rdi       # 0x2073
jmp 0x1377
movq 0x2cca(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xd59(%rip), %rdi       # 0x2091
pushq $0x16
jmp 0x1379
movq 0x2cb5(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xcee(%rip), %rdi       # 0x203b
pushq $0x19
jmp 0x1379
movq 0x2ca0(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xcf3(%rip), %rdi       # 0x2055
pushq $0x1d
jmp 0x1379
movq 0x2c8b(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xd31(%rip), %rdi       # 0x20a8
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
movl %ebx, %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c5d(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xc5f(%rip), %rdi       # 0x2004
pushq $0x31
popq %rsi
pushq $0x1
popq %rdx
callq 0x10f0
jmp 0x13be
leaq 0xc7d(%rip), %rdi       # 0x2036
callq 0x10c0
pushq $0x1
popq %rdi
callq 0x10e0
nop