// File: /workspace/github_c_makefile_star2_O1/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
movq %rdx, -0x30(%rbp)
decl %esi
shlq $0x7, %rsi
movq %rdi, -0x48(%rbp)
movl 0x4(%rdi,%rsi), %r13d
testl %r13d, %r13d
je 0x17fd
movq -0x48(%rbp), %rax
leaq (%rax,%rsi), %rcx
addq %rsi, %rax
addq $0x28, %rax
movq %rax, -0x50(%rbp)
leaq 0x3989(%rip), %rax      # 0x50b8
movq (%rax), %rbx
movq %rcx, -0x58(%rbp)
movl 0x28(%rcx), %r15d
shlq $0xa, %r15
addq %rbx, %r15
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq %rdi, -0x38(%rbp)
callq 0x1070
movq %rbx, %rsi
movq %rax, %rbx
xorl %edx, %edx
xorl %r14d, %r14d
movl %r13d, -0x3c(%rbp)
movq %rsi, -0x60(%rbp)
movl %r14d, %eax
andl $0x3ff, %eax            # imm = 0x3FF
leaq (%r15,%rax), %r12
movzbl 0x6(%r15,%rax), %eax
cmpq %rax, %rbx
jne 0x17a2
leaq 0x8(%r12), %rsi
movq -0x38(%rbp), %rdi
movq %rdx, %r13
movq %rbx, %rdx
callq 0x1060
movq %r13, %rdx
movq -0x60(%rbp), %rsi
movl -0x3c(%rbp), %r13d
testl %eax, %eax
je 0x185a
movzwl 0x4(%r12), %eax
addl %eax, %r14d
cmpl %r13d, %r14d
setae %al
testl $0x3ff, %r14d           # imm = 0x3FF
setne %cl
orb %al, %cl
jne 0x17f4
leal 0x1(%rdx), %eax
cmpl $0xb, %eax
ja 0x17d3
movl %eax, %ecx
movq -0x50(%rbp), %rdx
leaq (%rdx,%rcx,4), %rcx
jmp 0x17e8
movq -0x58(%rbp), %rcx
movl 0x58(%rcx), %ecx
shlq $0xa, %rcx
addq %rsi, %rcx
addl $-0xb, %edx
leaq (%rcx,%rdx,4), %rcx
movl (%rcx), %r15d
shlq $0xa, %r15
addq %rsi, %r15
movl %eax, %edx
cmpl %r13d, %r14d
jb 0x1764
movq -0x30(%rbp), %rbx
movq (%rbx), %rdi
movq 0x8(%rbx), %r14
callq 0x1030
movq %rbx, %rdi
callq 0x1030
testq %r14, %r14
je 0x1836
movq (%r14), %rdi
movq 0x8(%r14), %rbx
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq %rbx, %r14
testq %rbx, %rbx
jne 0x181a
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %eax, %eax
shlq $0x20, %rbx
orq %rax, %rbx
movzbl %r14b, %eax
orq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %r15
movq 0x8(%r15), %rbx
testq %rbx, %rbx
je 0x1899
movq -0x38(%rbp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movl (%r12), %esi
movq -0x48(%rbp), %rdi
movq %rbx, %rdx
callq 0x16e8
movq %rax, %r14
movq %rax, %rbx
shrq $0x20, %rbx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
jmp 0x183d
movb 0x7(%r12), %al
decb %al
movl $0x3f, %r14d
cmpb $0x6, %al
ja 0x18b9
movzbl %al, %eax
leaq 0x17ac(%rip), %rcx      # 0x3060
movzbl (%rcx,%rax,8), %r14d
movl (%r12), %ebx
movq -0x38(%rbp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
jmp 0x183b