// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x2, -0x34(%rbp)
movq -0x18(%rbp), %rax
addq $0x1400, %rax           # imm = 0x1400
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rsi
movl -0x34(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x7, %rax
addq %rax, %rsi
leaq -0xc0(%rbp), %rdi
movl $0x80, %edx
callq 0x10a0
movq -0x28(%rbp), %rdi
callq 0x1060
cmpl $0x1, %eax
jne 0x22eb
movq -0x28(%rbp), %rdi
leaq 0xd77(%rip), %rsi       # 0x3040
callq 0x1080
cmpl $0x0, %eax
jne 0x22eb
movl -0x98(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x4(%rbp)
jmp 0x25f2
movl $0x4, %edi
callq 0x10d0
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl $0x0, (%rax)
movq -0x28(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x2160
movq %rax, -0xd0(%rbp)
movl $0x0, -0xd4(%rbp)
movl $0x0, -0xd8(%rbp)
movq -0xc8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
jle 0x25da
movq -0xd0(%rbp), %rax
movl -0xdc(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xe8(%rbp)
movl $0x0, -0xec(%rbp)
movl $0x0, -0xf0(%rbp)
movslq -0xf0(%rbp), %rax
movl -0x98(%rbp,%rax,4), %eax
movl %eax, -0xf4(%rbp)
cmpl $0x0, -0xf4(%rbp)
je 0x25af
movl $0x0, -0xd4(%rbp)
movq -0x18(%rbp), %rax
movl -0xf4(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0xd4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
cmpl $0x400, -0xd4(%rbp)     # imm = 0x400
jge 0x2572
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %ecx
movl %ecx, %eax
incl %eax
movq %rsp, %rdx
movq %rdx, -0x108(%rbp)
addl $0x10, %ecx
andl $-0x10, %ecx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x118(%rbp)
movq %rdi, %rsp
movq %rax, -0x110(%rbp)
movq -0x100(%rbp), %rsi
addq $0x8, %rsi
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %edx
callq 0x1040
movq -0x118(%rbp), %rdi
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
movb $0x0, (%rdi,%rax)
movq -0xe8(%rbp), %rsi
callq 0x1080
cmpl $0x0, %eax
jne 0x2528
cmpl $0x1, -0xdc(%rbp)
jle 0x24e5
movq -0x100(%rbp), %rax
movzbl 0x7(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x24e5
movl $0x1, -0xec(%rbp)
movq -0x100(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x40(%rbp), %rsi
movl -0x34(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x7, %rax
addq %rax, %rsi
leaq -0xc0(%rbp), %rdi
movl $0x80, %edx
callq 0x10a0
movq -0x18(%rbp), %rax
movl -0xf4(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0xd4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movl $0x401, -0xd4(%rbp)     # imm = 0x401
jmp 0x2526
cmpl $0x1, -0xdc(%rbp)
jne 0x2524
movl -0xf4(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0xec(%rbp)
movq -0x100(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movl $0x1, -0xd8(%rbp)
movl $0x401, -0xd4(%rbp)     # imm = 0x401
jmp 0x2526
jmp 0x2528
movq -0x100(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0xd4(%rbp), %eax
movl %eax, -0xd4(%rbp)
movq -0x18(%rbp), %rax
movl -0xf4(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0xd4(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movq -0x108(%rbp), %rax
movq %rax, %rsp
jmp 0x23d1
cmpl $0x0, -0xec(%rbp)
je 0x2587
movl $0x0, -0xf4(%rbp)
jmp 0x25aa
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
movslq -0xf0(%rbp), %rax
movl -0x98(%rbp,%rax,4), %eax
movl %eax, -0xf4(%rbp)
jmp 0x2396
cmpl $0x0, -0xec(%rbp)
jne 0x25c4
movl $0x0, -0xd8(%rbp)
jmp 0x25da
jmp 0x25c6
movl -0xdc(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x2343
cmpl $0x0, -0xd8(%rbp)
je 0x25eb
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25f2
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)