// File: /workspace/github_c_makefile_star2_O0/sheheryarbutt[P]Ext2-File-System/ext2_restore.c
// Function: file_in_gap
int file_in_gap(char * directory_path,char *file_namee, int*file_in_dir_blockk,int *file_offsett, int * file_inodee, int * entry_offsett){

	unsigned int * parent_block= malloc(sizeof(unsigned int *));
	int cur_inode = pathTraversal(directory_path, disk, parent_block); //get the inode of parent directory

	char *file_name=malloc((unsigned int) strlen(file_namee));strcpy(file_name,file_namee);
	int file_found=0; int *file_inode=file_inodee;
	int *file_in_dir_block=file_in_dir_blockk;
	int *entry_offset=entry_offsett;// offset of the entry that has the gap!
	int *file_offset=file_offsett;// when file is found what's the offset

	if(cur_inode>EXT2_GOOD_OLD_FIRST_INO || cur_inode==2){
		struct ext2_inode inode = inode_table[cur_inode-1];// represents the parent directories inode struct
		if((inode.i_mode & EXT2_S_IFDIR) == EXT2_S_IFDIR){//file containing the removed fill is a directory!

			int i=0; // represents the block # in which directory entries are
			while(inode.i_block[i]){//loop through all directory enteries blocks essentially!
				int size=0;
				struct ext2_dir_entry *direntry = (struct ext2_dir_entry *)(disk + inode.i_block[i]*EXT2_BLOCK_SIZE +size);// first i_block[0] = some # 
				int current_rec_len; int not_in_gap=1; //1 for not in gap, 0 for in gap!
				int offset_of_next_entry=0; // represents the offset orignally (with gaps)
				//int size_of_current_entry=0; // represents the size orignally (with gaps)
				int left_over_size; // size still left over
				while(size<EXT2_BLOCK_SIZE){// going through whole block
					char name2 [direntry->name_len+1]; strcpy(name2,direntry->name); name2[direntry->name_len]='\0';
					if(not_in_gap){//When im not in the gaps, update these values!
						*entry_offset=size;
						offset_of_next_entry+=direntry->rec_len; 
						left_over_size=direntry->rec_len; 
					}
					current_rec_len=direntry->rec_len;

					int real_rec_len; // represents the real rec_len (w/o gaps) 
					if(direntry->name_len % 4 == 0){ // if name_len is a multiple of 4
						real_rec_len=direntry->name_len + 8; // dont need to add padding
					}else {
						real_rec_len=direntry->name_len + 12 - (direntry->name_len % 4);
					}

					if(i>0 && size==0 && direntry->inode==0){
						direntry=(struct ext2_dir_entry *)(disk +  inode.i_block[i]*EXT2_BLOCK_SIZE +size); //update direntry +44+16 (instead of 44+36)
						char name [direntry->name_len+1]; strcpy(name,direntry->name); name[direntry->name_len]='\0';
						if(strcmp(name,file_name)==0){// means we found a gap with same name! 
							break; 
						}
					} 
					// if the current is greater then what it's suppose to be, clearly GAP!
					if(left_over_size>real_rec_len){
						left_over_size-=real_rec_len; 
						not_in_gap=0; // change to 0 because we found a gap  
						//meaning we are getting a corrupted file/no more gaps
						if(left_over_size<=0 || current_rec_len==0 || current_rec_len>EXT2_BLOCK_SIZE){
							size=offset_of_next_entry;// get out of gaps and look at the next directory entry
							not_in_gap=1;
						}
						direntry=(struct ext2_dir_entry *)(disk +  inode.i_block[i]*EXT2_BLOCK_SIZE +size+ real_rec_len); 
						char name [direntry->name_len+1]; strcpy(name,direntry->name); name[direntry->name_len]='\0';
						if(strcmp(name,file_name)==0 && not_in_gap==0){// means we found a gap with same name! 
							file_found=1; 
							size+=real_rec_len; //now size represents where gap was found!
							*file_offset=size; *file_in_dir_block=inode.i_block[i];
							*file_inode=direntry->inode;
							break; 
						}
						else {
							size+=real_rec_len; 
						}		
				    }//end of if (for being inside the gap!)
				    else{
				    	size+=real_rec_len;
				    	direntry = (struct ext2_dir_entry *)(disk + inode.i_block[i]*EXT2_BLOCK_SIZE +size);
				    	if(not_in_gap==0){
					    	direntry=(struct ext2_dir_entry *)(disk +  inode.i_block[i]*EXT2_BLOCK_SIZE +size); 
							char name2 [direntry->name_len+1]; strcpy(name2,direntry->name); name2[direntry->name_len]='\0';
							if(strcmp(name2,file_name)==0){// means we found a gap with same name! 
								file_found=1; 
								size+=real_rec_len; //now size represents where gap was found!
								*file_offset=size; *file_in_dir_block=inode.i_block[i];
								*file_inode=direntry->inode;
								break; 
							}
				    	}
				    }			   
					if(size==offset_of_next_entry){
						not_in_gap=1;
					}
				}// end of while loop (iterating in a block)			
				if (file_found){
					*file_offset=size;
					*file_in_dir_block=inode.i_block[i];
					*file_inode=direntry->inode;
					break; // break out of outer loop!
				}
				else{
					i++;// update block counter to next!	
				}

			}//end of while loop iterating over all blocks
		}// end of if (checking if its a directory)
		else {//not a directory
			fprintf(stderr, "ERROR: %s\n", strerror(ENOTDIR)); 
			exit(ENOTDIR);
		}
	}//end of it (checking inode of directory>11 || ==2)!

	return file_found;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x8, %edi
callq 0x10d0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq 0x371c(%rip), %rsi      # 0x50a8
movq -0x38(%rbp), %rdx
callq 0x2250
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1060
movl %eax, %eax
movl %eax, %edi
callq 0x10d0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1050
movl $0x0, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpl $0xb, -0x3c(%rbp)
jg 0x19f2
cmpl $0x2, -0x3c(%rbp)
jne 0x206c
movq 0x36c7(%rip), %rsi      # 0x50c0
movl -0x3c(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x7, %rax
addq %rax, %rsi
leaq -0xf0(%rbp), %rdi
movl $0x80, %edx
callq 0x10a0
movzwl -0xf0(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x202d
movl $0x0, -0xf4(%rbp)
movslq -0xf4(%rbp), %rax
cmpl $0x0, -0xc8(%rbp,%rax,4)
je 0x202b
movl $0x0, -0xf8(%rbp)
movq 0x3648(%rip), %rax      # 0x50a8
movslq -0xf4(%rbp), %rcx
movl -0xc8(%rbp,%rcx,4), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movslq -0xf8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movl $0x1, -0x108(%rbp)
movl $0x0, -0x10c(%rbp)
cmpl $0x400, -0xf8(%rbp)     # imm = 0x400
jge 0x1fe0
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %ecx
movl %ecx, %eax
incl %eax
movq %rsp, %rdx
movq %rdx, -0x118(%rbp)
addl $0x10, %ecx
andl $-0x10, %ecx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x168(%rbp)
movq %rdi, %rsp
movq %rax, -0x120(%rbp)
movq -0x100(%rbp), %rsi
addq $0x8, %rsi
callq 0x1050
movq -0x168(%rbp), %rax
movq -0x100(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
movb $0x0, (%rax,%rcx)
cmpl $0x0, -0x108(%rbp)
je 0x1b44
movl -0xf8(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, (%rax)
movq -0x100(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x10c(%rbp), %eax
movl %eax, -0x10c(%rbp)
movq -0x100(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, -0x110(%rbp)
movq -0x100(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, -0x104(%rbp)
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1b83
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
addl $0x8, %eax
movl %eax, -0x124(%rbp)
jmp 0x1bb8
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
addl $0xc, %eax
movl %eax, -0x16c(%rbp)
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl -0x16c(%rbp), %eax
subl %edx, %eax
movl %eax, -0x124(%rbp)
cmpl $0x0, -0xf4(%rbp)
jle 0x1caa
cmpl $0x0, -0xf8(%rbp)
jne 0x1caa
movq -0x100(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x1caa
movq 0x34bf(%rip), %rax      # 0x50a8
movslq -0xf4(%rbp), %rcx
movl -0xc8(%rbp,%rcx,4), %ecx
shll $0xa, %ecx
addq %rcx, %rax
movslq -0xf8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %ecx
movl %ecx, %eax
incl %eax
movq %rsp, %rdx
movq %rdx, -0x130(%rbp)
addl $0x10, %ecx
andl $-0x10, %ecx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x178(%rbp)
movq %rdi, %rsp
movq %rax, -0x138(%rbp)
movq -0x100(%rbp), %rsi
addq $0x8, %rsi
callq 0x1050
movq -0x178(%rbp), %rdi
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
movb $0x0, (%rdi,%rax)
movq -0x48(%rbp), %rsi
callq 0x1080
cmpl $0x0, %eax
jne 0x1c84
movl $0x5, -0x13c(%rbp)
jmp 0x1c8e
movl $0x0, -0x13c(%rbp)
movq -0x130(%rbp), %rax
movq %rax, %rsp
movl -0x13c(%rbp), %eax
testl %eax, %eax
jne 0x1fc3
jmp 0x1ca8
jmp 0x1caa
movl -0x110(%rbp), %eax
cmpl -0x124(%rbp), %eax
jle 0x1e46
movl -0x124(%rbp), %ecx
movl -0x110(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x110(%rbp)
movl $0x0, -0x108(%rbp)
cmpl $0x0, -0x110(%rbp)
jle 0x1cf8
cmpl $0x0, -0x104(%rbp)
je 0x1cf8
cmpl $0x400, -0x104(%rbp)    # imm = 0x400
jle 0x1d0e
movl -0x10c(%rbp), %eax
movl %eax, -0xf8(%rbp)
movl $0x1, -0x108(%rbp)
movq 0x3393(%rip), %rax      # 0x50a8
movslq -0xf4(%rbp), %rcx
movl -0xc8(%rbp,%rcx,4), %ecx
shll $0xa, %ecx
addq %rcx, %rax
movslq -0xf8(%rbp), %rcx
addq %rcx, %rax
movslq -0x124(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %ecx
movl %ecx, %eax
incl %eax
movq %rsp, %rdx
movq %rdx, -0x148(%rbp)
addl $0x10, %ecx
andl $-0x10, %ecx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x180(%rbp)
movq %rdi, %rsp
movq %rax, -0x150(%rbp)
movq -0x100(%rbp), %rsi
addq $0x8, %rsi
callq 0x1050
movq -0x180(%rbp), %rdi
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
movb $0x0, (%rdi,%rax)
movq -0x48(%rbp), %rsi
callq 0x1080
cmpl $0x0, %eax
jne 0x1e0b
cmpl $0x0, -0x108(%rbp)
jne 0x1e0b
movl $0x1, -0x4c(%rbp)
movl -0x124(%rbp), %eax
addl -0xf8(%rbp), %eax
movl %eax, -0xf8(%rbp)
movl -0xf8(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movslq -0xf4(%rbp), %rax
movl -0xc8(%rbp,%rax,4), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movq -0x100(%rbp), %rax
movl (%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl $0x5, -0x13c(%rbp)
jmp 0x1e27
movl -0x124(%rbp), %eax
addl -0xf8(%rbp), %eax
movl %eax, -0xf8(%rbp)
movl $0x0, -0x13c(%rbp)
movq -0x148(%rbp), %rax
movq %rax, %rsp
movl -0x13c(%rbp), %eax
testl %eax, %eax
jne 0x1fc3
jmp 0x1e41
jmp 0x1fa1
movl -0x124(%rbp), %eax
addl -0xf8(%rbp), %eax
movl %eax, -0xf8(%rbp)
movq 0x3249(%rip), %rax      # 0x50a8
movslq -0xf4(%rbp), %rcx
movl -0xc8(%rbp,%rcx,4), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movslq -0xf8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
cmpl $0x0, -0x108(%rbp)
jne 0x1f9f
movq 0x320e(%rip), %rax      # 0x50a8
movslq -0xf4(%rbp), %rcx
movl -0xc8(%rbp,%rcx,4), %ecx
shll $0xa, %ecx
addq %rcx, %rax
movslq -0xf8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %ecx
movl %ecx, %eax
incl %eax
movq %rsp, %rdx
movq %rdx, -0x158(%rbp)
addl $0x10, %ecx
andl $-0x10, %ecx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x188(%rbp)
movq %rdi, %rsp
movq %rax, -0x160(%rbp)
movq -0x100(%rbp), %rsi
addq $0x8, %rsi
callq 0x1050
movq -0x188(%rbp), %rdi
movq -0x100(%rbp), %rax
movzbl 0x6(%rax), %eax
movb $0x0, (%rdi,%rax)
movq -0x48(%rbp), %rsi
callq 0x1080
cmpl $0x0, %eax
jne 0x1f7d
movl $0x1, -0x4c(%rbp)
movl -0x124(%rbp), %eax
addl -0xf8(%rbp), %eax
movl %eax, -0xf8(%rbp)
movl -0xf8(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movslq -0xf4(%rbp), %rax
movl -0xc8(%rbp,%rax,4), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movq -0x100(%rbp), %rax
movl (%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
movl $0x5, -0x13c(%rbp)
jmp 0x1f87
movl $0x0, -0x13c(%rbp)
movq -0x158(%rbp), %rax
movq %rax, %rsp
movl -0x13c(%rbp), %eax
testl %eax, %eax
jne 0x1fc3
jmp 0x1f9d
jmp 0x1f9f
jmp 0x1fa1
movl -0xf8(%rbp), %eax
cmpl -0x10c(%rbp), %eax
jne 0x1fb9
movl $0x1, -0x108(%rbp)
movl $0x0, -0x13c(%rbp)
movq -0x118(%rbp), %rax
movq %rax, %rsp
movl -0x13c(%rbp), %eax
testl %eax, %eax
je 0x1fdb
jmp 0x1fd9
jmp 0x1fe0
jmp 0x1a9b
cmpl $0x0, -0x4c(%rbp)
je 0x2017
movl -0xf8(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movslq -0xf4(%rbp), %rax
movl -0xc8(%rbp,%rax,4), %ecx
movq -0x60(%rbp), %rax
movl %ecx, (%rax)
movq -0x100(%rbp), %rax
movl (%rax), %ecx
movq -0x58(%rbp), %rax
movl %ecx, (%rax)
jmp 0x202b
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x1a3a
jmp 0x206a
movq 0x2fc4(%rip), %rax      # 0x4ff8
movq (%rax), %rax
movq %rax, -0x190(%rbp)
movl $0x14, %edi
callq 0x1110
movq -0x190(%rbp), %rdi
movq %rax, %rdx
leaq 0xfe9(%rip), %rsi       # 0x3042
movb $0x0, %al
callq 0x1090
movl $0x14, %edi
callq 0x1100
jmp 0x206c
movl -0x4c(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop