// File: /workspace/github_c_makefile_star2_O1/Sidd0107[P]EXT2-File-System/a3helper.c
// Function: reserve_directory_entry
int reserve_directory_entry(char *inode_base, unsigned int inode_num, int num_free_blocks, struct ext2_dir_entry_2 *new_file, char *block_bitmap){
	struct ext2_inode *current_inode = get_inode(inode_base, inode_num);
	unsigned int block_num = current_inode -> i_block[0];	
	unsigned int cur_index_serviced = 0, cur_inode_index = 0;
	if(current_inode -> i_size > 0){
		char *current_block = get_block(disk, block_num, 1024);	
		struct ext2_dir_entry_2 *current = (struct ext2_dir_entry_2 *) current_block;
		while(cur_index_serviced < current_inode -> i_size){
			current = (struct ext2_dir_entry_2 *) (current_block + (cur_index_serviced % 1024));
			//According to ext2 spec directory entries must be aligned by 4's.
			int leftover_size = current -> rec_len - (((current -> name_len + 8) + (4 - (current -> name_len + 8) % 4) % 4));
			if(leftover_size >= new_file -> rec_len){
				current -> rec_len -= leftover_size;
				memcpy(((char *)current + (current -> rec_len)), new_file, new_file -> rec_len);
				current = (struct ext2_dir_entry_2 *) (((void *)current) + current -> rec_len);
				current -> rec_len = leftover_size;
				return num_free_blocks;
			}
			cur_index_serviced += current -> rec_len;
			if (cur_index_serviced < current_inode->i_size && cur_index_serviced % 1024 == 0){
				cur_inode_index++;
				current_block = get_block_from_inode(inode_base, inode_num, cur_inode_index);
			}
		}
	}
	if(num_free_blocks > 0){
		int new_block = add_block_in_inode(current_inode, block_bitmap, cur_inode_index);
		if(new_block <= 0){
			return -1;
		}	
		char *new_dir_entry_ptr = get_block(disk, new_block, 1024);
		memcpy(new_dir_entry_ptr, new_file, new_file -> rec_len);
		((struct ext2_dir_entry_2 *)new_dir_entry_ptr) -> rec_len = 1024;
		return num_free_blocks - 1;
	}
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r14
movl %edx, -0x2c(%rbp)
decl %esi
shlq $0x7, %rsi
leaq (%rdi,%rsi), %r9
cmpl $0x0, 0x4(%rdi,%rsi)
je 0x1e11
movq %r8, -0x38(%rbp)
leaq 0x28(%r9), %rax
movq %rax, -0x40(%rbp)
movl 0x28(%r9), %edi
leaq 0x33ad(%rip), %rax      # 0x50b8
shlq $0xa, %rdi
addq (%rax), %rdi
xorl %esi, %esi
xorl %eax, %eax
movq %r14, -0x48(%rbp)
movl %eax, %ebx
movl 0x4(%r9), %r8d
cmpl %r8d, %eax
jae 0x1e08
movl %ebx, %r15d
andl $0x3ff, %r15d           # imm = 0x3FF
movzwl 0x4(%rdi,%r15), %eax
movzbl 0x6(%rdi,%r15), %ecx
movl %ecx, %edx
negl %edx
andl $0x3, %edx
addl %edx, %ecx
addl $0x8, %ecx
movl %eax, %r12d
subl %ecx, %r12d
movzwl 0x4(%r14), %r10d
cmpl %r10d, %r12d
jge 0x1d90
addl %ebx, %eax
cmpl %r8d, %eax
setae %cl
testl $0x3ff, %eax            # imm = 0x3FF
setne %dl
orb %cl, %dl
jne 0x1dff
leal 0x1(%rsi), %ecx
leaq 0x333b(%rip), %rdx      # 0x50b8
movq (%rdx), %rdi
cmpl $0xb, %ecx
ja 0x1de5
movl %ecx, %edx
movq -0x40(%rbp), %rsi
movl (%rsi,%rdx,4), %edx
jmp 0x1df6
addq %rdi, %r15
movw %cx, 0x4(%r15)
movq %rdi, -0x58(%rbp)
movl %ecx, %edi
addq %r15, %rdi
movzwl 0x4(%r14), %edx
movq %rsi, -0x50(%rbp)
movq %r14, %rsi
movq %r9, %r13
movl %r8d, -0x30(%rbp)
movl %r10d, %r14d
callq 0x10e0
movl %r14d, %r10d
movq -0x50(%rbp), %rsi
movl -0x30(%rbp), %r8d
movq -0x58(%rbp), %rdi
movq %r13, %r9
movq -0x48(%rbp), %r14
movzwl 0x4(%r15), %eax
movw %r12w, 0x4(%r15,%rax)
movl %ebx, %eax
movl -0x2c(%rbp), %r13d
jmp 0x1dff
movl 0x58(%r9), %edx
shlq $0xa, %rdx
addq %rdi, %rdx
addl $-0xb, %esi
movl (%rdx,%rsi,4), %edx
shlq $0xa, %rdx
addq %rdx, %rdi
movl %ecx, %esi
cmpl %r10d, %r12d
jl 0x1d1a
cmpl %r8d, %ebx
movq -0x38(%rbp), %r8
jb 0x1e5c
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $0x0, -0x2c(%rbp)
jle 0x1e5c
movq %r9, %rdi
movq %r8, %rsi
callq 0x195c
testl %eax, %eax
jle 0x1e5c
movq %r14, %rsi
leaq 0x3282(%rip), %rcx      # 0x50b8
movq (%rcx), %rbx
movl %eax, %r14d
shlq $0xa, %r14
leaq (%rbx,%r14), %rdi
movzwl 0x4(%rsi), %edx
callq 0x10e0
movw $0x400, 0x4(%rbx,%r14)  # imm = 0x400
movl -0x2c(%rbp), %r13d
decl %r13d
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq