// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movq %rcx, %r14
movl %edx, -0x2c(%rbp)
movq %rdi, %rax
decl %esi
shlq $0x7, %rsi
addq %rsi, %rdi
movl 0x4(%rax,%rsi), %eax
testl %eax, %eax
je 0x1d4f
leaq 0x28(%rdi), %rcx
leaq 0x340b(%rip), %rdx      # 0x50b8
movq (%rdx), %rdx
movl 0x28(%rdi), %r15d
shlq $0xa, %r15
addq %rdx, %r15
movzwl 0x4(%r14), %esi
xorl %r9d, %r9d
xorl %r10d, %r10d
movl %r9d, %r11d
andl $0x3ff, %r11d           # imm = 0x3FF
movzwl 0x4(%r15,%r11), %r13d
movzbl 0x6(%r15,%r11), %ebx
movl %ebx, %r12d
negl %r12d
andl $0x3, %r12d
addl %r12d, %ebx
addl $0x8, %ebx
movl %r13d, %r12d
subl %ebx, %r12d
cmpl %esi, %r12d
jge 0x1d9a
addl %r13d, %r9d
cmpl %eax, %r9d
setae %r11b
testl $0x3ff, %r9d            # imm = 0x3FF
setne %bl
orb %r11b, %bl
jne 0x1d46
leal 0x1(%r10), %r11d
cmpl $0xb, %r11d
ja 0x1d27
movl %r11d, %r10d
leaq (%rcx,%r10,4), %r10
jmp 0x1d39
movl 0x58(%rdi), %ebx
shlq $0xa, %rbx
addq %rdx, %rbx
addl $-0xb, %r10d
leaq (%rbx,%r10,4), %r10
movl (%r10), %r15d
shlq $0xa, %r15
addq %rdx, %r15
movl %r11d, %r10d
cmpl %eax, %r9d
jb 0x1cc6
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpl $0x0, -0x2c(%rbp)
jle 0x1dc3
movq %r8, %rsi
callq 0x1923
testl %eax, %eax
jle 0x1dc3
leaq 0x334a(%rip), %rcx      # 0x50b8
movq (%rcx), %rbx
movl %eax, %r15d
shlq $0xa, %r15
leaq (%rbx,%r15), %rdi
movzwl 0x4(%r14), %edx
movq %r14, %rsi
callq 0x10e0
movw $0x400, 0x4(%rbx,%r15)  # imm = 0x400
movl -0x2c(%rbp), %r15d
decl %r15d
jmp 0x1dc3
addq %r11, %r15
movw %bx, 0x4(%r15)
movl %ebx, %edi
addq %r15, %rdi
movzwl 0x4(%r14), %edx
movq %r14, %rsi
callq 0x10e0
movzwl 0x4(%r15), %eax
movw %r12w, 0x4(%r15,%rax)
movl -0x2c(%rbp), %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq