// File: /workspace/github_c_makefile_star2_O2/sheheryarbutt[P]Ext2-File-System/ext2_utils.c
// Function: pathTraversal
unsigned int pathTraversal(char *path, unsigned char *disk, unsigned int *parent_block){
	char *absPath=path; unsigned int * temp_parent_block=parent_block;

	int cur_inode=EXT2_ROOT_INO;//start off with root node 2!
	struct ext2_inode *inode_table = (struct ext2_inode *)(disk + EXT2_BLOCK_SIZE*5);//pointer to first inode!
	struct ext2_inode inode = inode_table[cur_inode-1];// this gets the root node!

	if((unsigned int) strlen(absPath)==1 && strcmp(absPath,"/")==0){
		*temp_parent_block=inode.i_block[0];
		return 2; }
	int *path_length=malloc(sizeof(int));
    *path_length=0;
	char ** path_names=all_names(absPath,path_length);

	int size=0;
	int valid_path=0;

	 for(int i=*path_length; i>0;i--){
	 	char * cur_name= path_names[i-1]; 
	 	int found_or_not=0;
	 	int current_i_block_index=0;
		int current_i_block=inode.i_block[current_i_block_index];
		while (current_i_block){
			size=0;
			struct ext2_dir_entry *direntry = (struct ext2_dir_entry *)(disk + current_i_block*EXT2_BLOCK_SIZE +size); //first directory!

		 	while(size<EXT2_BLOCK_SIZE){
				char name [direntry->name_len+1]; strncpy(name,direntry->name,direntry->name_len);
				name[direntry->name_len]='\0';

				if(strcmp(name,cur_name)==0){//if the names match! file/directory found!
					if(i>1 && direntry->file_type & EXT2_FT_DIR){
						found_or_not=1;
						cur_inode=(int)direntry->inode;	//update cur_inode to new one!
						inode=inode_table[cur_inode-1];
						direntry=(struct ext2_dir_entry *)(disk + current_i_block*EXT2_BLOCK_SIZE +size);
						size=EXT2_BLOCK_SIZE+1; // to braek out of inner loop
					}
					else if (i==1){// end of path so it can be dir or file!
						*temp_parent_block=current_i_block;
						found_or_not=1;
						cur_inode=(int)direntry->inode;
						valid_path=1;
						size=EXT2_BLOCK_SIZE+1;
					}
				}
				size+=direntry->rec_len;
				direntry = (struct ext2_dir_entry *)(disk + current_i_block*EXT2_BLOCK_SIZE +size);
			}// end of inner while
			if(found_or_not){
				current_i_block=0; 
			}
			else{
				//since we havent found what we want, it may be in next block
				current_i_block_index++;
				current_i_block=inode.i_block[current_i_block_index];
			}
		}//end of outer while
		if(found_or_not==0){ //since we didnt find the current file/dir, break out of for loop
			valid_path=0;
			break; 
		}
	 }// end of for loop 
	 if(valid_path){
			return cur_inode;
		}
		else {
			return 0;
		}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, -0x58(%rbp)
movq %rdi, %r14
movq %rsi, -0x50(%rbp)
addq $0x1480, %rsi           # imm = 0x1480
pushq $0x20
popq %rcx
leaq -0x100(%rbp), %rdi
rep  movsl (%rsi), %es:(%rdi)
movq %r14, %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x1abd
cmpb $0x2f, (%r14)
jne 0x1abd
cmpb $0x0, 0x1(%r14)
je 0x1c57
pushq $0x4
popq %rdi
callq 0x10d0
movq %rax, %r15
andl $0x0, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x19c6
movq %rax, -0x70(%rbp)
movl (%r15), %ecx
movq -0x50(%rbp), %rax
addq $0x1380, %rax           # imm = 0x1380
movq %rax, -0x78(%rbp)
pushq $0x2
popq %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
testl %ecx, %ecx
jle 0x1c36
movq -0x70(%rbp), %rax
movq -0x8(%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0xd8(%rbp), %eax
movl $0x0, -0x3c(%rbp)
movq %rcx, -0x60(%rbp)
testl %eax, %eax
je 0x1c27
movl %eax, -0x44(%rbp)
shll $0xa, %eax
movslq %eax, %rbx
addq -0x50(%rbp), %rbx
xorl %r15d, %r15d
movq %rbx, -0x68(%rbp)
cmpl $0x3ff, %r15d           # imm = 0x3FF
jg 0x1c01
movq %rsp, %r14
movzbl 0x6(%rbx), %eax
movq %rsp, %r13
addl $0x10, %eax
andl $-0x10, %eax
subq %rax, %r13
movq %r13, %rsp
leaq 0x8(%rbx), %rsi
movzbl 0x6(%rbx), %r12d
movq %r13, %rdi
movq %r12, %rdx
callq 0x1040
movb $0x0, (%r13,%r12)
movq %r13, %rdi
movq -0x80(%rbp), %rsi
callq 0x1080
testl %eax, %eax
jne 0x1be7
cmpq $0x1, -0x60(%rbp)
jne 0x1bb3
movq -0x58(%rbp), %rax
movl -0x44(%rbp), %ecx
movl %ecx, (%rax)
movl (%rbx), %eax
movq %rax, -0x38(%rbp)
movl $0x401, %r15d           # imm = 0x401
pushq $0x1
popq %rax
movl %eax, -0x40(%rbp)
jmp 0x1be4
testb $0x2, 0x7(%rbx)
je 0x1be7
movslq (%rbx), %rsi
movq %rsi, -0x38(%rbp)
shlq $0x7, %rsi
addq -0x78(%rbp), %rsi
pushq $0x20
popq %rcx
leaq -0x100(%rbp), %rdi
rep  movsl (%rsi), %es:(%rdi)
movl %r15d, %ebx
addq -0x68(%rbp), %rbx
movl $0x401, %r15d           # imm = 0x401
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
movq %r14, %rsp
movzwl 0x4(%rbx), %eax
addl %r15d, %eax
movq -0x68(%rbp), %rcx
leaq (%rcx,%rax), %rbx
movl %eax, %r15d
jmp 0x1b42
xorl %eax, %eax
cmpl $0x0, -0x2c(%rbp)
movq -0x60(%rbp), %rcx
jne 0x1b26
movl -0x3c(%rbp), %edx
movl %edx, %eax
incl %edx
movl %edx, -0x3c(%rbp)
movl -0xd4(%rbp,%rax,4), %eax
jmp 0x1b26
decq %rcx
cmpl $0x0, -0x2c(%rbp)
jne 0x1af9
jmp 0x1c3c
cmpl $0x0, -0x40(%rbp)
jne 0x1c44
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0xd8(%rbp), %eax
movq -0x58(%rbp), %rcx
movl %eax, (%rcx)
pushq $0x2
popq %rax
movq %rax, -0x38(%rbp)
jmp 0x1c44