// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, %r14
callq 0x1b42
movq %rax, %rbx
movzwl 0x4(%rax), %r15d
movslq (%rax,%r15), %rdi
leaq 0x312d(%rip), %rax      # 0x50b0
movq (%rax), %rax
movl 0x808(%rax), %ecx
shll $0xa, %ecx
addq %rax, %rcx
movq %rdi, %rdx
shlq $0x7, %rdx
leaq (%rcx,%rdx), %r8
movzbl 0x7(%rbx,%r15), %esi
cmpl $0x1, %esi
je 0x212a
cmpl $0x2, %esi
je 0x205b
cmpl $0x7, %esi
jne 0x2143
cmpl $0x3d, -0x7c(%r8)
jb 0x200c
movl 0x800(%rax), %edx
movl -0x58(%r8), %esi
leal -0x1(%rsi), %edi
movl %edi, %ecx
andb $0x7, %cl
movl $0x1, %r8d
shll %cl, %r8d
shll $0xa, %edx
addq %rax, %rdx
addl $0x6, %esi
testl %edi, %edi
cmovnsl %edi, %esi
sarl $0x3, %esi
movslq %esi, %rax
notb %r8b
andb %r8b, (%rax,%rdx)
leaq 0x30ae(%rip), %rax      # 0x50b0
movq (%rax), %rax
incw 0x80c(%rax)
leaq 0x309d(%rip), %r9       # 0x50b0
movq (%r9), %rdx
movl 0x804(%rdx), %eax
movl (%rbx,%r15), %esi
leal -0x1(%rsi), %edi
movl %edi, %ecx
andb $0x7, %cl
movl $0x1, %r8d
shll %cl, %r8d
shll $0xa, %eax
addq %rdx, %rax
addl $0x6, %esi
testl %edi, %edi
cmovnsl %edi, %esi
sarl $0x3, %esi
movslq %esi, %rcx
notb %r8b
andb %r8b, (%rcx,%rax)
movq (%r9), %rax
incw 0x80e(%rax)
jmp 0x2143
movq %r15, -0x30(%rbp)
movq %rbx, -0x38(%rbp)
decw -0x66(%r8)
decw 0x1a(%r14)
leaq (%rdx,%rcx), %rax
addq $-0x80, %rax
movq %rax, -0x48(%rbp)
xorl %ebx, %ebx
leaq 0x1140(%rip), %r15      # 0x31c2
movq %r8, -0x40(%rbp)
movslq -0x58(%r8,%rbx,4), %r12
testq %r12, %r12
je 0x20f1
shlq $0xa, %r12
leaq 0x3015(%rip), %rax      # 0x50b0
addq (%rax), %r12
xorl %r14d, %r14d
cmpb $0x0, 0x6(%r12)
je 0x20dc
leaq 0x8(%r12), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x10c0
testl %eax, %eax
je 0x20dc
movq %r13, %rdi
leaq 0x10fa(%rip), %rsi      # 0x31c1
callq 0x10c0
testl %eax, %eax
je 0x20dc
movq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x1f57
movzwl 0x4(%r12), %eax
addl %eax, %r14d
addq %rax, %r12
cmpl $0x400, %r14d           # imm = 0x400
jb 0x20a1
incq %rbx
cmpq $0xc, %rbx
movq -0x40(%rbp), %r8
jne 0x2086
cmpw $0x1, -0x66(%r8)
movq -0x38(%rbp), %rbx
movq -0x30(%rbp), %r15
jne 0x2143
movl (%rbx,%r15), %edi
callq 0x18e2
leaq 0x2f92(%rip), %rax      # 0x50b0
movq (%rax), %rax
decw 0x810(%rax)
jmp 0x2143
movzwl -0x66(%r8), %eax
cmpw $0x1, %ax
jne 0x213c
callq 0x18e2
jmp 0x2143
decl %eax
movw %ax, -0x66(%r8)
movzwl 0x4(%rbx,%r15), %eax
addw %ax, 0x4(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq