// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, -0x90(%rbp)
movq %r8, -0x70(%rbp)
movq %rcx, -0x68(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, %r14
movq %rdi, %r13
movl $0x8, %edi
callq 0x10d0
movq 0x39ae(%rip), %rsi      # 0x50b0
movq %r13, %rdi
movq %rax, %rdx
callq 0x1c10
movl %eax, %ebx
movq %r14, %rdi
callq 0x1060
movl %eax, %edi
callq 0x10d0
movq %rax, -0x78(%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
cmpl $0xc, %ebx
setl %al
cmpl $0x2, %ebx
setne %cl
xorl %edx, %edx
testb %cl, %al
jne 0x1a9a
movq 0x397e(%rip), %rax      # 0x50c8
movl %ebx, %ecx
shlq $0x7, %rcx
leaq (%rax,%rcx), %rsi
addq $-0x80, %rsi
leaq -0x110(%rbp), %rax
movl $0x20, %ecx
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
testb $0x40, 0x1(%rax)
je 0x1aab
movl $0x0, -0x3c(%rbp)
xorl %edx, %edx
movslq -0x3c(%rbp), %rax
movl -0xe8(%rbp,%rax,4), %eax
testl %eax, %eax
je 0x1a9a
movl %edx, -0x34(%rbp)
movl %eax, -0x30(%rbp)
movl %eax, %ecx
shll $0xa, %ecx
movq 0x390f(%rip), %r12      # 0x50b0
movq %rcx, -0x58(%rbp)
addq %rcx, %r12
movl $0x1, %r8d
xorl %edx, %edx
movl %r9d, %r14d
xorl %r13d, %r13d
cmpl $0x3ff, %r13d           # imm = 0x3FF
jg 0x1a69
movq %rsp, %rsi
testl %r8d, %r8d
je 0x17de
movq -0x90(%rbp), %rax
movl %r13d, (%rax)
movzwl 0x4(%r12), %r14d
addl %r14d, %edx
movzbl 0x6(%r12), %eax
leal 0x8(%rax), %ecx
movl %eax, %ebx
addl $0xc, %ebx
andl $-0x4, %ebx
testb $0x3, %al
cmovel %ecx, %ebx
movzwl 0x4(%r12), %r15d
cmpl $0x0, -0x3c(%rbp)
jle 0x189e
testl %r13d, %r13d
jne 0x189e
cmpl $0x0, (%r12)
jne 0x189e
movq %rsp, -0x80(%rbp)
movq 0x388d(%rip), %rcx      # 0x50b0
movq %rcx, -0x50(%rbp)
movq -0x58(%rbp), %rax
addq %rax, %rcx
addq $0x8, %rcx
movzbl -0x2(%rcx), %eax
movq %rcx, -0x88(%rbp)
addl $0x10, %eax
andl $-0x10, %eax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %r12, %rdi
movq %rsi, -0x48(%rbp)
movq %rcx, %rsi
movl %edx, -0x2c(%rbp)
movl %r8d, -0x38(%rbp)
callq 0x1050
movq -0x88(%rbp), %rax
movzbl -0x2(%rax), %eax
movb $0x0, (%r12,%rax)
movq %r12, %rdi
movq -0x78(%rbp), %rsi
callq 0x1080
movl -0x38(%rbp), %r8d
movq -0x50(%rbp), %r12
movq -0x48(%rbp), %rsi
movl -0x2c(%rbp), %edx
xorl %ecx, %ecx
testl %eax, %eax
sete %al
movq -0x80(%rbp), %rsp
je 0x19ff
movl %r14d, %eax
subl %ebx, %eax
jle 0x1989
movl %eax, -0x38(%rbp)
movq %rsi, -0x48(%rbp)
movq %rsp, -0x80(%rbp)
setle %al
addl $0xfffffbff, %r15d      # imm = 0xFFFFFBFF
movzwl %r15w, %ecx
cmpl $0xfc00, %ecx           # imm = 0xFC00
setb %r14b
orb %al, %r14b
movl %edx, -0x2c(%rbp)
cmovnel %edx, %r13d
movq 0x37d3(%rip), %rax      # 0x50b0
addq -0x58(%rbp), %rax
movslq %r13d, %r13
addq %r13, %rax
movl %ebx, %ecx
leaq (%rax,%rcx), %rdx
movq %rdx, -0x50(%rbp)
leaq (%rcx,%rax), %r15
addq $0x8, %r15
movzbl -0x2(%r15), %eax
addl $0x10, %eax
andl $-0x10, %eax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %r12, %rdi
movq %r15, %rsi
callq 0x1050
movzbl -0x2(%r15), %eax
movb $0x0, (%r12,%rax)
movq %r12, %rdi
movq -0x78(%rbp), %rsi
callq 0x1080
movq -0x50(%rbp), %r12
testl %eax, %eax
setne %al
orb %r14b, %al
addl %ebx, %r13d
xorl %ecx, %ecx
testb %al, %al
jne 0x1969
movq -0x68(%rbp), %rcx
movl %r13d, (%rcx)
movq -0x60(%rbp), %rcx
movl -0x30(%rbp), %edx
movl %edx, (%rcx)
movl (%r12), %ecx
movq -0x70(%rbp), %rdx
movl %ecx, (%rdx)
movl $0x1, -0x34(%rbp)
movl $0x5, %ecx
movzbl %r14b, %r8d
movq -0x80(%rbp), %rsp
testb %al, %al
movl -0x2c(%rbp), %edx
movq -0x48(%rbp), %rsi
movl -0x38(%rbp), %r9d
jne 0x1a4b
jmp 0x1a59
addl %ebx, %r13d
movq 0x371d(%rip), %rax      # 0x50b0
addq -0x58(%rbp), %rax
movslq %r13d, %r12
addq %rax, %r12
testl %r8d, %r8d
movl $0x1, %r8d
je 0x19b0
movl %r14d, %r9d
jmp 0x1a4b
movq %rsi, -0x48(%rbp)
movl %edx, -0x2c(%rbp)
movq %rsp, -0x50(%rbp)
movzbl 0x6(%r12), %eax
movq %rsp, %r15
addl $0x10, %eax
andl $-0x10, %eax
subq %rax, %r15
movq %r15, %rsp
leaq 0x8(%r12), %rsi
movq %r15, %rdi
callq 0x1050
movzbl 0x6(%r12), %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
movq -0x78(%rbp), %rsi
callq 0x1080
xorl %r8d, %r8d
testl %eax, %eax
je 0x1a10
xorl %ecx, %ecx
jmp 0x1a39
addq -0x58(%rbp), %r12
xorl %r13d, %r13d
movb %al, %cl
leal (%rcx,%rcx,4), %ecx
movl %r14d, %r9d
jmp 0x1a59
addl %ebx, %r13d
movq -0x68(%rbp), %rcx
movl %r13d, (%rcx)
movq -0x60(%rbp), %rcx
movl -0x30(%rbp), %edx
movl %edx, (%rcx)
movl (%r12), %ecx
movq -0x70(%rbp), %rdx
movl %ecx, (%rdx)
movl $0x1, -0x34(%rbp)
movl $0x5, %ecx
movl -0x2c(%rbp), %edx
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rsp
movl %r14d, %r9d
testl %eax, %eax
je 0x1a59
cmpl %edx, %r13d
movl $0x1, %eax
cmovel %eax, %r8d
xorl %ecx, %ecx
movq %rsi, %rsp
movl %r9d, %r14d
testl %ecx, %ecx
je 0x17b6
jmp 0x1a6c
movl %r14d, %r9d
movl -0x34(%rbp), %edx
testl %edx, %edx
je 0x1a8f
movq -0x68(%rbp), %rax
movl %r13d, (%rax)
movq -0x60(%rbp), %rax
movl -0x30(%rbp), %ecx
movl %ecx, (%rax)
movl (%r12), %eax
movq -0x70(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x1a92
incl -0x3c(%rbp)
testl %edx, %edx
je 0x177c
movl %edx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1264