// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movl %edx, %r9d
leal -0x1(%rsi), %eax
shlq $0x7, %rax
leaq (%rdi,%rax), %r11
movq %rdi, -0x48(%rbp)
movl 0x4(%rdi,%rax), %r14d
testl %r14d, %r14d
je 0x1a51
movl 0x28(%r11), %ebx
leaq 0x370a(%rip), %rax      # 0x50b8
shlq $0xa, %rbx
addq (%rax), %rbx
movzwl 0x4(%rcx), %r13d
movl $0x0, -0x2c(%rbp)
xorl %r12d, %r12d
cmpl %r14d, %r12d
jae 0x1a51
movl %r12d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
movzwl 0x4(%rbx,%rax), %edi
movzbl 0x6(%rbx,%rax), %edx
movl %edx, %r10d
negl %r10d
andl $0x3, %r10d
addl %r10d, %edx
addl $0x8, %edx
movl %edi, %r15d
subl %edx, %r15d
cmpl %r13d, %r15d
jge 0x1aa0
addl %edi, %r12d
cmpl %r14d, %r12d
setae %al
testw $0x3ff, %r12w           # imm = 0x3FF
setne %dl
orb %al, %dl
jne 0x19c4
movl -0x2c(%rbp), %edx
incl %edx
movq -0x48(%rbp), %rdi
movl %edx, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq %rcx, %r15
movq %r8, -0x40(%rbp)
movq %rsi, %rbx
movq %r11, -0x38(%rbp)
callq 0x1627
movq -0x38(%rbp), %r11
movq %rbx, %rsi
movq -0x40(%rbp), %r8
movq %r15, %rcx
movl -0x30(%rbp), %r9d
movq %rax, %rbx
jmp 0x19c4
pushq $-0x1
popq %rbx
testl %r9d, %r9d
jle 0x1ac8
movq %rcx, %r12
movl %r9d, %r15d
movq %r11, %rdi
movq %r8, %rsi
callq 0x166b
testl %eax, %eax
jle 0x1ac8
leaq 0x3643(%rip), %rcx      # 0x50b8
movq (%rcx), %rbx
movl %eax, %r14d
shlq $0xa, %r14
leaq (%rbx,%r14), %rdi
movq %r12, %rsi
movzwl 0x4(%r12), %edx
callq 0x10e0
movw $0x400, 0x4(%rbx,%r14)  # imm = 0x400
movl %r15d, %ebx
decl %ebx
jmp 0x1ac8
addq %rax, %rbx
movw %dx, 0x4(%rbx)
movl %edx, %edi
addq %rbx, %rdi
movzwl 0x4(%rcx), %edx
movq %rcx, %rsi
movl %r9d, %r14d
callq 0x10e0
movzwl 0x4(%rbx), %eax
movw %r15w, 0x4(%rbx,%rax)
movl %r14d, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq