// File: /workspace/github_c_makefile_star2_O0/JavieW[P]CSCC69_Ext2_File-System/utilities.c
// Function: allocateNewDirent
struct ext2_dir_entry_2 *allocateNewDirent(struct ext2_inode *parent_inode, int size) {
    struct ext2_dir_entry_2 *new_dir_entry = NULL;
    // search in all used direct block
    for(int i = 0; i<12;i++) {
        if (parent_inode->i_block[i] != 0) {
            new_dir_entry = allocateDirentHelper(parent_inode->i_block[i], size);
            if (new_dir_entry!=NULL)
                return new_dir_entry;
        }
    }

    // if we cannot find a space, try to allocate a new block
    int newBlockNum = 0;
    for(int i = 0; i<12;i++) {
        if (parent_inode->i_block[i] != 0) continue;
        newBlockNum = allocateNewBlock();
        parent_inode->i_block[i] = newBlockNum;

        // increse parentdir size
        parent_inode->i_blocks+=(EXT2_BLOCK_SIZE+511)/512;
        parent_inode->i_size+=EXT2_BLOCK_SIZE;

        // insert dummy head
        new_dir_entry = (struct ext2_dir_entry_2 *)getBlock(newBlockNum);
        new_dir_entry->file_type = EXT2_FT_UNKNOWN;
        new_dir_entry->inode = 0;
        new_dir_entry->name_len = 0;
        new_dir_entry->rec_len = ((sizeof(struct ext2_dir_entry_2)+3)/4)*4;

        // return new setted dir_entry
        new_dir_entry = (void *)new_dir_entry + new_dir_entry->rec_len;
        new_dir_entry->rec_len=EXT2_BLOCK_SIZE-((sizeof(struct ext2_dir_entry_2)+3)/4)*4;
        return new_dir_entry;
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0xc, -0x24(%rbp)
jge 0x1fee
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpl $0x0, 0x28(%rax,%rcx,4)
je 0x1fe1
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %edi
movl -0x14(%rbp), %esi
callq 0x20d0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x1fdf
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x20be
jmp 0x1fe1
jmp 0x1fe3
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1f9e
movl $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0xc, -0x2c(%rbp)
jge 0x20b6
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpl $0x0, 0x28(%rax,%rcx,4)
je 0x201a
jmp 0x20a8
callq 0x1d80
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x1c(%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x400, %ecx            # imm = 0x400
movl %ecx, 0x4(%rax)
movl -0x28(%rbp), %edi
callq 0x1d60
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, 0x7(%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x6(%rax)
movq -0x20(%rbp), %rax
movw $0x8, 0x4(%rax)
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movw $0x3f8, 0x4(%rax)       # imm = 0x3F8
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x20be
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1ffc
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)