// File: /workspace/github_c_makefile_star2_O0/JavieW[P]CSCC69_Ext2_File-System/utilities.c
// Function: deleteInode
void deleteInode(int inodeNum) {
    
    char unsigned *inode_bitmap = getBitmap(INODE_BITMAP);
    char unsigned *block_bitmap = getBitmap(BLOCK_BITMAP);
    
    // delete inode
    changeBitmap(inode_bitmap, inodeNum-1, 'd');
    getGroupDesc()->bg_free_inodes_count++;
    struct ext2_inode *inode_table = getInodeTable();
    struct ext2_inode *target = &inode_table[inodeNum-1];
    target->i_dtime = time(NULL);

    // delete block
    int i;
    for(i = 0; i<12;i++) {
        if (target->i_block[i] != 0) {
            changeBitmap(block_bitmap, target->i_block[i]-1, 'd');
            getGroupDesc()->bg_free_blocks_count++;
        }
    }

    // delete single indirect
    int bp = target->i_block[12];
    if (bp != 0)
    {
        // delete blocks in single
        unsigned int *single = (unsigned int*)getBlock(bp);
        for (int i=0; i<EXT2_BLOCK_SIZE/4; i++) {
            if (single[i] != 0) {
                changeBitmap(block_bitmap, single[i]-1, 'd');
                getGroupDesc()->bg_free_blocks_count++;
            }
        }   
        // delte single itself
        changeBitmap(block_bitmap, target->i_block[12]-1, 'd');
        getGroupDesc()->bg_free_blocks_count++;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x4(%rbp)
movl $0x1, %edi
callq 0x1810
movq %rax, -0x10(%rbp)
xorl %edi, %edi
callq 0x1810
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movl -0x4(%rbp), %esi
subl $0x1, %esi
movl $0x64, %edx
callq 0x1970
callq 0x17f0
movw 0xe(%rax), %cx
addw $0x1, %cx
movw %cx, 0xe(%rax)
callq 0x1a70
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl -0x4(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x10b0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movl $0x0, -0x2c(%rbp)
cmpl $0xc, -0x2c(%rbp)
jge 0x1cb3
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpl $0x0, 0x28(%rax,%rcx,4)
je 0x1ca6
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %esi
subl $0x1, %esi
movl $0x64, %edx
callq 0x1970
callq 0x17f0
movw 0xc(%rax), %cx
addw $0x1, %cx
movw %cx, 0xc(%rax)
jmp 0x1ca8
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1c63
movq -0x28(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x1d54
movl -0x30(%rbp), %edi
callq 0x1d60
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x100, -0x3c(%rbp)     # imm = 0x100
jge 0x1d2b
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x1d1e
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %esi
subl $0x1, %esi
movl $0x64, %edx
callq 0x1970
callq 0x17f0
movw 0xc(%rax), %cx
addw $0x1, %cx
movw %cx, 0xc(%rax)
jmp 0x1d20
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1cda
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x58(%rax), %esi
subl $0x1, %esi
movl $0x64, %edx
callq 0x1970
callq 0x17f0
movw 0xc(%rax), %cx
addw $0x1, %cx
movw %cx, 0xc(%rax)
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)