// File: /workspace/github_c_makefile_star2_O0/Sidd0107[P]EXT2-File-System/a3helper.c
// Function: search_directory
struct basic_fileinfo search_directory(char *inode_base, unsigned int inode_num, struct parsed_filename *filename){
	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;
	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));
		if (strlen(filename -> file_name) == current -> name_len && !strncmp(filename -> file_name, current -> name, strlen(filename -> file_name))){
			if (!filename -> next){
			struct basic_fileinfo current_file;
				current_file.inode = current -> inode;
				current_file.type = type_to_char(current -> file_type);
				free(filename -> file_name);
				free(filename);
				return current_file;
			}
			else{
				struct parsed_filename *next = filename -> next;
				free(filename -> file_name);
				free(filename);
				return search_directory(inode_base, current -> inode, next);
			}
		}
		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);
		}
	}
	struct parsed_filename *current_filename = filename -> next, *prev = filename;
	free(prev -> file_name);
	free(prev);
	while(current_filename){
		prev = current_filename;
		current_filename = current_filename -> next;
		free(prev -> file_name);
		free(prev);
	}
	struct basic_fileinfo null_file;
	null_file.inode = 0;
	null_file.type = '\0';
	return null_file;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x17d0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
leaq 0x3aae(%rip), %rax      # 0x50b0
movq (%rax), %rdi
movl -0x2c(%rbp), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17f0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x30(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x175c
movq -0x40(%rbp), %rax
movl -0x30(%rbp), %ecx
andl $0x3ff, %ecx            # imm = 0x3FF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x1060
movq -0x48(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
cmpq %rcx, %rax
jne 0x1714
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x1060
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq %rax, %rdx
callq 0x1050
cmpl $0x0, %eax
jne 0x1714
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x16d7
movq -0x48(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4(%rbp)
movq -0x48(%rbp), %rax
movzbl 0x7(%rax), %edi
callq 0x1820
movb %al, -0x8(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x1030
jmp 0x17c2
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x1030
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rax
movl (%rax), %esi
movq -0x50(%rbp), %rdx
callq 0x15c0
movq %rax, -0x8(%rbp)
jmp 0x17c2
movq -0x48(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x1757
movl -0x30(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl $0x0, %eax
jne 0x1757
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x34(%rbp), %edx
callq 0x1890
movq %rax, -0x40(%rbp)
jmp 0x161e
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rdi
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1030
cmpq $0x0, -0x58(%rbp)
je 0x17b7
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rdi
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1030
jmp 0x1785
movl $0x0, -0x4(%rbp)
movb $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)