// File: /workspace/github_c_makefile_star2_O3/JavieW[P]CSCC69_Ext2_File-System/utilities.c
// Function: rm
void rm(struct ext2_inode *parentInode, char *childFileName) {
    struct ext2_dir_entry_2 *pre_dir_entry = NULL;
    struct ext2_dir_entry_2 *cur_dir_entry = NULL;
    struct ext2_dir_entry_2 *child_dir_entry = NULL;
    struct ext2_inode *childInode = NULL;
    int total_rec_len;
    
    // delete childFile from parentDir and get childInode
    pre_dir_entry = getPreDirent(parentInode, childFileName);
    cur_dir_entry = (void *)pre_dir_entry + pre_dir_entry->rec_len;
    childInode = getInode(cur_dir_entry->inode);

    // base case1, if childInode is Symbolic link
    if (cur_dir_entry->file_type == EXT2_FT_SYMLINK)
    {
        if (childInode->i_size > 60) {
            changeBitmap(getBitmap(BLOCK_BITMAP), childInode->i_block[0]-1, 'd');
            getGroupDesc()->bg_free_blocks_count++;
        }
        changeBitmap(getBitmap(INODE_BITMAP), cur_dir_entry->inode-1, 'd');
        getGroupDesc()->bg_free_inodes_count++;
    }
    // base case2, if childInode is a file
    else if (cur_dir_entry->file_type == EXT2_FT_REG_FILE)
    {
        // if link count == 0 remove inode
        if (childInode->i_links_count == 1) {
            deleteInode(cur_dir_entry->inode);
        } else {
            childInode->i_links_count--;
        }
    }
    // recursive case, if childInode is a dir
    else if (cur_dir_entry->file_type == EXT2_FT_DIR)
    {
        // reduce link count for self and parent (. and ..)
        childInode->i_links_count--;
        parentInode->i_links_count--;

        // for each file name (other than . and ..) in child dir, call recursion
        for (int i=0; i<12; i++) {
            if (childInode->i_block[i] == 0)
                continue;
            child_dir_entry = (struct ext2_dir_entry_2 *)getBlock(childInode->i_block[i]);
            total_rec_len = 0;
            // for each dir entry in the block
            while (total_rec_len < EXT2_BLOCK_SIZE) {
                if (child_dir_entry->name_len != 0 &&
                    strcmp(child_dir_entry->name, ".")!=0 &&
                    strcmp(child_dir_entry->name, "..")!=0) {
                    rm(childInode, child_dir_entry->name);
                }
                total_rec_len = total_rec_len + child_dir_entry->rec_len;
                child_dir_entry = (void *) child_dir_entry + child_dir_entry->rec_len;
            }
        }
        
        // if child link count == 0 remove inode
        if (childInode->i_links_count == 1) {
            deleteInode(cur_dir_entry->inode);
            getGroupDesc()->bg_used_dirs_count--;
        }
    }
    pre_dir_entry->rec_len += cur_dir_entry->rec_len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r15
callq 0x1b30
movq %rax, %rbx
movzwl 0x4(%rax), %r14d
movslq (%rax,%r14), %rdi
leaq 0x3143(%rip), %r9       # 0x50b0
movq (%r9), %rax
movl 0x808(%rax), %ecx
shll $0xa, %ecx
addq %rax, %rcx
movq %rdi, %rdx
shlq $0x7, %rdx
leaq (%rcx,%rdx), %r12
movzbl 0x7(%rbx,%r14), %esi
cmpl $0x1, %esi
je 0x20e2
cmpl $0x2, %esi
je 0x2033
cmpl $0x7, %esi
jne 0x20fd
cmpl $0x3d, -0x7c(%r12)
jb 0x1ff2
movl 0x800(%rax), %edx
shll $0xa, %edx
addq %rax, %rdx
movl -0x58(%r12), %eax
leal -0x1(%rax), %esi
movl %esi, %ecx
andb $0x7, %cl
movl $0x1, %edi
shll %cl, %edi
addl $0x6, %eax
testl %esi, %esi
cmovnsl %esi, %eax
sarl $0x3, %eax
cltq
notb %dil
andb %dil, (%rax,%rdx)
movq (%r9), %rax
incw 0x80c(%rax)
movl (%rbx,%r14), %edi
movl 0x804(%rax), %edx
leal -0x1(%rdi), %esi
movl %esi, %ecx
andb $0x7, %cl
movl $0x1, %r8d
shll %cl, %r8d
shll $0xa, %edx
addq %rax, %rdx
addl $0x6, %edi
testl %esi, %esi
cmovnsl %esi, %edi
sarl $0x3, %edi
movslq %edi, %rax
notb %r8b
andb %r8b, (%rax,%rdx)
movq (%r9), %rax
incw 0x80e(%rax)
jmp 0x20fd
movq %r14, -0x30(%rbp)
movq %rbx, -0x38(%rbp)
leaq (%rdx,%rcx), %r14
addq $-0x80, %r14
decw -0x66(%r12)
decw 0x1a(%r15)
xorl %r15d, %r15d
movslq -0x58(%r12,%r15,4), %r13
testq %r13, %r13
je 0x20ac
shlq $0xa, %r13
leaq 0x304a(%rip), %rax      # 0x50b0
addq (%rax), %r13
xorl %ebx, %ebx
cmpb $0x0, 0x6(%r13)
je 0x209a
cmpb $0x2e, 0x8(%r13)
jne 0x208e
cmpb $0x0, 0x9(%r13)
je 0x209a
cmpb $0x2e, 0x9(%r13)
jne 0x208e
cmpb $0x0, 0xa(%r13)
je 0x209a
leaq 0x8(%r13), %rsi
movq %r14, %rdi
callq 0x1f41
movzwl 0x4(%r13), %eax
addl %eax, %ebx
addq %rax, %r13
cmpl $0x400, %ebx            # imm = 0x400
jb 0x206b
incq %r15
cmpq $0xc, %r15
jne 0x2051
cmpw $0x1, -0x66(%r12)
movq -0x38(%rbp), %rbx
movq -0x30(%rbp), %r14
leaq 0x2fe5(%rip), %r15      # 0x50b0
jne 0x20fd
movl (%rbx,%r14), %edi
callq 0x18e2
movq (%r15), %rax
decw 0x810(%rax)
jmp 0x20fd
movzwl -0x66(%r12), %eax
cmpw $0x1, %ax
jne 0x20f5
callq 0x18e2
jmp 0x20fd
decl %eax
movw %ax, -0x66(%r12)
movzwl 0x4(%rbx,%r14), %eax
addw %ax, 0x4(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq