// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x37b9(%rip), %rbx      # 0x50b0
movq (%rbx), %rdx
movl 0x800(%rdx), %r14d
movl 0x804(%rdx), %eax
shll $0xa, %eax
addq %rdx, %rax
leal -0x1(%rdi), %esi
movl %esi, %ecx
andb $0x7, %cl
movl $0x1, %r8d
shll %cl, %r8d
shll $0xa, %r14d
addq %rdx, %r14
leal 0x6(%rdi), %ecx
testl %esi, %esi
cmovnsl %esi, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
notb %r8b
andb %r8b, (%rcx,%rax)
movq (%rbx), %r13
incw 0x80e(%r13)
movl 0x808(%r13), %r15d
shll $0xa, %r15d
leaq (%r15,%r13), %rax
movslq %edi, %r12
shlq $0x7, %r12
addq %r12, %rax
addq $-0x80, %rax
movq %rax, -0x30(%rbp)
xorl %edi, %edi
callq 0x10d0
movq -0x30(%rbp), %r9
movl %eax, 0x14(%r9)
addq %r13, %r12
addq %r15, %r12
movq $-0x16, %rax
movl (%r12,%rax,4), %edx
testl %edx, %edx
je 0x19b8
leal -0x1(%rdx), %esi
movl %esi, %ecx
andb $0x7, %cl
movl $0x1, %edi
shll %cl, %edi
addl $0x6, %edx
testl %esi, %esi
cmovnsl %esi, %edx
sarl $0x3, %edx
movslq %edx, %rcx
notb %dil
andb %dil, (%r14,%rcx)
movq (%rbx), %rcx
incw 0x80c(%rcx)
incq %rax
cmpq $-0xa, %rax
jne 0x1982
movslq 0x58(%r9), %rax
testq %rax, %rax
je 0x1a47
shlq $0xa, %rax
addq (%rbx), %rax
xorl %edx, %edx
movl (%rax,%rdx,4), %esi
testl %esi, %esi
je 0x1a0a
leal -0x1(%rsi), %edi
movl %edi, %ecx
andb $0x7, %cl
movl $0x1, %r8d
shll %cl, %r8d
addl $0x6, %esi
testl %edi, %edi
cmovnsl %edi, %esi
sarl $0x3, %esi
movslq %esi, %rcx
notb %r8b
andb %r8b, (%r14,%rcx)
movq (%rbx), %rcx
incw 0x80c(%rcx)
incq %rdx
cmpq $0x100, %rdx            # imm = 0x100
jne 0x19d3
movl 0x58(%r9), %eax
leal -0x1(%rax), %edx
movl %edx, %ecx
andb $0x7, %cl
movl $0x1, %esi
shll %cl, %esi
addl $0x6, %eax
testl %edx, %edx
cmovnsl %edx, %eax
sarl $0x3, %eax
cltq
notb %sil
andb %sil, (%r14,%rax)
movq (%rbx), %rax
incw 0x80c(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq