// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
decl %esi
shlq $0x7, %rsi
movq %rdi, -0x40(%rbp)
movl 0x4(%rdi,%rsi), %r13d
movq %rdx, -0x48(%rbp)
movq (%rdx), %rax
movq %rax, -0x30(%rbp)
testl %r13d, %r13d
je 0x17c3
movq -0x40(%rbp), %rax
leaq (%rax,%rsi), %rcx
addq %rsi, %rax
addq $0x28, %rax
movq %rax, -0x50(%rbp)
leaq 0x39bc(%rip), %rax      # 0x50b8
movq (%rax), %rbx
movq %rcx, -0x58(%rbp)
movl 0x28(%rcx), %r14d
shlq $0xa, %r14
addq %rbx, %r14
movq -0x30(%rbp), %rdi
callq 0x1070
movq %rbx, %rsi
movq %rax, %rbx
xorl %edx, %edx
xorl %r15d, %r15d
movl %r13d, -0x34(%rbp)
movq %rsi, -0x60(%rbp)
movl %r15d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
leaq (%r14,%rax), %r12
movzbl 0x6(%r14,%rax), %eax
cmpq %rax, %rbx
jne 0x1768
leaq 0x8(%r12), %rsi
movq -0x30(%rbp), %rdi
movq %rdx, %r13
movq %rbx, %rdx
callq 0x1060
movq %r13, %rdx
movq -0x60(%rbp), %rsi
movl -0x34(%rbp), %r13d
testl %eax, %eax
je 0x1821
movzwl 0x4(%r12), %eax
addl %eax, %r15d
cmpl %r13d, %r15d
setae %al
testl $0x3ff, %r15d           # imm = 0x3FF
setne %cl
orb %al, %cl
jne 0x17ba
leal 0x1(%rdx), %eax
cmpl $0xb, %eax
ja 0x1799
movl %eax, %ecx
movq -0x50(%rbp), %rdx
leaq (%rdx,%rcx,4), %rcx
jmp 0x17ae
movq -0x58(%rbp), %rcx
movl 0x58(%rcx), %ecx
shlq $0xa, %rcx
addq %rsi, %rcx
addl $-0xb, %edx
leaq (%rcx,%rdx,4), %rcx
movl (%rcx), %r14d
shlq $0xa, %r14
addq %rsi, %r14
movl %eax, %edx
cmpl %r13d, %r15d
jb 0x172a
movq -0x48(%rbp), %rbx
movq 0x8(%rbx), %r15
movq -0x30(%rbp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
testq %r15, %r15
je 0x17fd
movq (%r15), %rdi
movq 0x8(%r15), %rbx
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbx, %r15
testq %rbx, %rbx
jne 0x17e1
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %eax, %eax
shlq $0x20, %rbx
orq %rax, %rbx
movzbl %r15b, %eax
orq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x48(%rbp), %r14
movq 0x8(%r14), %rbx
testq %rbx, %rbx
je 0x1860
movq -0x30(%rbp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movl (%r12), %esi
movq -0x40(%rbp), %rdi
movq %rbx, %rdx
callq 0x16ae
movq %rax, %r15
movq %rax, %rbx
shrq $0x20, %rbx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
jmp 0x1804
movb 0x7(%r12), %al
decb %al
movl $0x3f, %r15d
cmpb $0x6, %al
ja 0x1880
movzbl %al, %eax
leaq 0x17e5(%rip), %rcx      # 0x3060
movzbl (%rcx,%rax,8), %r15d
movl (%r12), %ebx
movq -0x30(%rbp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
jmp 0x1802