// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x17d0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jbe 0x2028
leaq 0x31c9(%rip), %rax      # 0x50b0
movq (%rax), %rdi
movl -0x34(%rbp), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17f0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x38(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2026
movq -0x48(%rbp), %rax
movl -0x38(%rbp), %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x50(%rbp), %rax
movzbl 0x6(%rax), %ecx
addl $0x8, %ecx
movq -0x50(%rbp), %rax
movzbl 0x6(%rax), %eax
addl $0x8, %eax
movl $0x4, %esi
cltd
idivl %esi
movl $0x4, %eax
subl %edx, %eax
movl $0x4, %esi
cltd
idivl %esi
movl -0x64(%rbp), %eax
addl %edx, %ecx
subl %ecx, %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movq -0x20(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jl 0x1fde
movl -0x54(%rbp), %edx
movq -0x50(%rbp), %rax
movzwl 0x4(%rax), %ecx
subl %edx, %ecx
movw %cx, 0x4(%rax)
movq -0x50(%rbp), %rdi
movq -0x50(%rbp), %rax
movzwl 0x4(%rax), %eax
cltq
addq %rax, %rdi
movq -0x20(%rbp), %rsi
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, %edx
callq 0x10e0
movq -0x50(%rbp), %rax
movq -0x50(%rbp), %rcx
movzwl 0x4(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl -0x54(%rbp), %eax
movw %ax, %cx
movq -0x50(%rbp), %rax
movw %cx, 0x4(%rax)
movl -0x18(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x209e
movq -0x50(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x2021
movl -0x38(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl $0x0, %eax
jne 0x2021
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x1890
movq %rax, -0x48(%rbp)
jmp 0x1f03
jmp 0x2028
cmpl $0x0, -0x18(%rbp)
jle 0x2097
movq -0x30(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x3c(%rbp), %edx
callq 0x1930
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jg 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x209e
leaq 0x3059(%rip), %rax      # 0x50b0
movq (%rax), %rdi
movl -0x58(%rbp), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17f0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x20(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, %edx
callq 0x10e0
movq -0x60(%rbp), %rax
movw $0x400, 0x4(%rax)       # imm = 0x400
movl -0x18(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x209e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)