// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rdx, %r15
movq %rsi, -0x50(%rbp)
leal -0x1(%rsi), %eax
shlq $0x7, %rax
movl 0x4(%rdi,%rax), %ebx
movq %rdi, -0x38(%rbp)
movl 0x28(%rdi,%rax), %ecx
leaq 0x3c22(%rip), %rax      # 0x50b8
shlq $0xa, %rcx
addq (%rax), %rcx
movq %rcx, -0x48(%rbp)
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
movq %rdx, -0x40(%rbp)
movl %ebx, -0x30(%rbp)
cmpl %ebx, %r12d
jae 0x152e
movl %r12d, %r14d
andl $0x3ff, %r14d           # imm = 0x3FF
movq -0x48(%rbp), %r13
leaq (%r14,%r13), %rbx
movq (%r15), %r15
movq %r15, %rdi
callq 0x1070
movzbl 0x6(%r13,%r14), %ecx
cmpq %rcx, %rax
jne 0x14f2
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
movq %rax, %rdx
callq 0x1060
testl %eax, %eax
je 0x156a
movzwl 0x4(%rbx), %eax
addl %eax, %r12d
movl -0x30(%rbp), %ebx
cmpl %ebx, %r12d
setae %al
testw $0x3ff, %r12w           # imm = 0x3FF
setne %cl
orb %al, %cl
movq -0x40(%rbp), %r15
jne 0x14b2
movl -0x2c(%rbp), %edx
incl %edx
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl %edx, -0x2c(%rbp)
callq 0x1627
movq %rax, -0x48(%rbp)
jmp 0x14b2
movq (%r15), %rdi
movq 0x8(%r15), %r14
callq 0x1030
movq %r15, %rdi
callq 0x1030
testq %r14, %r14
je 0x1560
movq (%r14), %rdi
movq 0x8(%r14), %rbx
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq %rbx, %r14
jmp 0x1542
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0x15a4
movq -0x40(%rbp), %r13
movq 0x8(%r13), %r12
testq %r12, %r12
je 0x15c1
movq %r15, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movl (%rbx), %esi
movq -0x38(%rbp), %rdi
movq %r12, %rdx
callq 0x1464
movq %rax, %r14
movq %rax, %r12
shrq $0x20, %r12
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
shlq $0x20, %r12
orq %rax, %r12
movzbl %r14b, %eax
orq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%rbx), %r12d
movzbl 0x7(%rbx), %edi
callq 0x1601
movl %eax, %r14d
movq %r15, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
jmp 0x1566