// File: /workspace/github_c_makefile_star2_O2/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 0x1a62
movq %rax, %rbx
movzwl 0x4(%rax), %r14d
movslq (%rax,%r14), %rax
leaq 0x2296(%rip), %rcx      # 0x40a8
movq (%rcx), %rdx
movl 0x808(%rdx), %ecx
shll $0xa, %ecx
addq %rdx, %rcx
movq %rax, %rsi
shlq $0x7, %rsi
leaq (%rcx,%rsi), %r13
movzbl 0x7(%rbx,%r14), %edi
cmpl $0x1, %edi
je 0x1f57
cmpl $0x2, %edi
je 0x1e9f
cmpl $0x7, %edi
jne 0x1fa8
cmpl $0x3d, -0x7c(%r13)
pushq $0x1
popq %rsi
pushq $0x8
popq %rdi
jb 0x1f74
movl 0x800(%rdx), %r8d
movl -0x58(%r13), %eax
decl %eax
movl %eax, %ecx
andb $0x7, %cl
movl %esi, %r9d
shll %cl, %r9d
shll $0xa, %r8d
addq %rdx, %r8
cltd
idivl %edi
cltq
notb %r9b
andb %r9b, (%rax,%r8)
leaq 0x221c(%rip), %r9       # 0x40a8
movq (%r9), %rdx
incw 0x80c(%rdx)
movl (%rbx,%r14), %eax
jmp 0x1f7b
movq %r14, -0x30(%rbp)
movq %rbx, -0x38(%rbp)
leaq (%rsi,%rcx), %r14
addq $-0x80, %r14
decw -0x66(%r13)
decw 0x1a(%r15)
xorl %r15d, %r15d
cmpq $0xc, %r15
je 0x1f2b
movslq -0x58(%r13,%r15,4), %r12
testq %r12, %r12
je 0x1f26
shlq $0xa, %r12
leaq 0x21d1(%rip), %rax      # 0x40a8
addq (%rax), %r12
xorl %ebx, %ebx
cmpl $0x3ff, %ebx            # imm = 0x3FF
ja 0x1f26
cmpb $0x0, 0x6(%r12)
je 0x1f19
cmpb $0x2e, 0x8(%r12)
jne 0x1f0c
cmpb $0x0, 0x9(%r12)
je 0x1f19
cmpb $0x2e, 0x9(%r12)
jne 0x1f0c
cmpb $0x0, 0xa(%r12)
je 0x1f19
leaq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0x1de6
movzwl 0x4(%r12), %eax
addl %eax, %ebx
addq %rax, %r12
jmp 0x1edc
incq %r15
jmp 0x1ebc
cmpw $0x1, -0x66(%r13)
movq -0x38(%rbp), %rbx
movq -0x30(%rbp), %r14
jne 0x1fa8
movl (%rbx,%r14), %edi
callq 0x1856
leaq 0x215d(%rip), %rax      # 0x40a8
movq (%rax), %rax
decw 0x810(%rax)
jmp 0x1fa8
movzwl -0x66(%r13), %ecx
cmpw $0x1, %cx
jne 0x1f6b
movl %eax, %edi
callq 0x1856
jmp 0x1fa8
decl %ecx
movw %cx, -0x66(%r13)
jmp 0x1fa8
leaq 0x212d(%rip), %r9       # 0x40a8
movl 0x804(%rdx), %r8d
shll $0xa, %r8d
addq %rdx, %r8
decl %eax
movl %eax, %ecx
andb $0x7, %cl
shll %cl, %esi
cltd
idivl %edi
cltq
notb %sil
andb %sil, (%rax,%r8)
movq (%r9), %rax
incw 0x80e(%rax)
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