// File: /workspace/github_c_makefile_star2_O1/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, -0x60(%rbp)
movq %rdi, %r15
movq %rsi, -0x58(%rbp)
addq $0x1480, %rsi           # imm = 0x1480
leaq -0x100(%rbp), %rdi
movl $0x20, %ecx
rep  movsl (%rsi), %es:(%rdi)
movq %r15, %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x1c68
leaq 0x13e8(%rip), %rsi      # 0x3040
movq %r15, %rdi
callq 0x1080
testl %eax, %eax
je 0x1e22
movl $0x4, %edi
callq 0x10d0
movq %rax, %r12
movl $0x0, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x1b3f
movq %rax, -0x70(%rbp)
movslq (%r12), %rcx
testq %rcx, %rcx
jle 0x1e1e
movq -0x58(%rbp), %rax
addq $0x1380, %rax           # imm = 0x1380
movq %rax, -0x78(%rbp)
movl $0x2, %eax
movq %rax, -0x40(%rbp)
movl $0x0, -0x2c(%rbp)
movq %rcx, -0x48(%rbp)
movl -0xd8(%rbp), %edx
testl %edx, %edx
je 0x1de9
movq -0x70(%rbp), %rax
movq -0x48(%rbp), %rcx
movq -0x8(%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl %edx, -0x4c(%rbp)
movl %edx, %eax
shll $0xa, %eax
movslq %eax, %rbx
addq -0x58(%rbp), %rbx
movq %rbx, -0x68(%rbp)
xorl %r12d, %r12d
movq %rsp, %r15
movzbl 0x6(%rbx), %eax
movq %rsp, %r14
addl $0x10, %eax
andl $-0x10, %eax
subq %rax, %r14
movq %r14, %rsp
leaq 0x8(%rbx), %rsi
movzbl 0x6(%rbx), %r13d
movq %r14, %rdi
movq %r13, %rdx
callq 0x1040
movb $0x0, (%r14,%r13)
movq %r14, %rdi
movq -0x80(%rbp), %rsi
callq 0x1080
testl %eax, %eax
jne 0x1d9e
cmpq $0x1, -0x48(%rbp)
jne 0x1d67
movq -0x60(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, (%rax)
movl (%rbx), %eax
movq %rax, -0x40(%rbp)
movl $0x1, -0x2c(%rbp)
movl $0x401, %r12d           # imm = 0x401
movl $0x1, -0x30(%rbp)
jmp 0x1d9e
testb $0x2, 0x7(%rbx)
je 0x1d9e
movslq (%rbx), %rsi
movq %rsi, -0x40(%rbp)
shlq $0x7, %rsi
addq -0x78(%rbp), %rsi
movl $0x20, %ecx
leaq -0x100(%rbp), %rdi
rep  movsl (%rsi), %es:(%rdi)
movl %r12d, %ebx
addq -0x68(%rbp), %rbx
movl $0x1, -0x30(%rbp)
movl $0x401, %r12d           # imm = 0x401
movq %r15, %rsp
movzwl 0x4(%rbx), %eax
addl %r12d, %eax
movq -0x68(%rbp), %rcx
leaq (%rcx,%rax), %rbx
movl %eax, %r12d
cmpl $0x400, %eax            # imm = 0x400
jl 0x1cfc
xorl %edx, %edx
cmpl $0x0, -0x30(%rbp)
jne 0x1dd8
movl -0x34(%rbp), %ecx
movslq %ecx, %rax
incl %ecx
movl %ecx, -0x34(%rbp)
movl -0xd4(%rbp,%rax,4), %edx
testl %edx, %edx
jne 0x1ce6
cmpl $0x0, -0x30(%rbp)
sete %al
jmp 0x1deb
movb $0x1, %al
testb %al, %al
movl $0x0, %ecx
movl -0x2c(%rbp), %edx
cmovnel %ecx, %edx
movl %edx, -0x2c(%rbp)
movq -0x48(%rbp), %rcx
cmpq $0x2, %rcx
jl 0x1e10
decq %rcx
testb %al, %al
je 0x1cb5
movl -0x2c(%rbp), %ecx
testl %ecx, %ecx
movq -0x40(%rbp), %rax
cmovel %ecx, %eax
jmp 0x1e33
xorl %eax, %eax
jmp 0x1e33
movl -0xd8(%rbp), %eax
movq -0x60(%rbp), %rcx
movl %eax, (%rcx)
movl $0x2, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq