// File: /workspace/github_c_makefile_star2_O3/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, -0x80(%rbp)
movq %rcx, -0x70(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, %rbx
movq %rdi, %r14
movl $0x8, %edi
callq 0x10d0
movq 0x29aa(%rip), %rsi      # 0x40b0
movq %r14, %rdi
movq %rax, %rdx
callq 0x1b98
movl %eax, %r14d
movq %rbx, %rdi
callq 0x1060
movl %eax, %edi
callq 0x10d0
movq %rax, -0x58(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1050
cmpl $0xc, %r14d
setl %cl
cmpl $0x2, %r14d
setne %dl
xorl %eax, %eax
testb %dl, %cl
jne 0x1a3b
movq 0x2977(%rip), %rax      # 0x40c8
movl %r14d, %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 0x1a4a
movl -0xe8(%rbp), %eax
testl %eax, %eax
je 0x1a05
xorl %esi, %esi
movl %eax, -0x64(%rbp)
movl %eax, %ecx
shll $0xa, %ecx
movq 0x2916(%rip), %r15      # 0x40b0
movq %rcx, -0x60(%rbp)
addq %rcx, %r15
xorl %r14d, %r14d
movl $0x1, %ebx
xorl %edx, %edx
movq %rsi, -0x50(%rbp)
testl %ebx, %ebx
je 0x17ca
movq -0x90(%rbp), %rax
movl %r14d, (%rax)
movzwl 0x4(%r15), %r9d
movzwl %r9w, %edi
addl %edi, %edx
jmp 0x17cf
movzwl 0x4(%r15), %r9d
movq %rsp, %r12
movzbl 0x6(%r15), %eax
leal 0x8(%rax), %ecx
movl %eax, %r13d
addl $0xc, %r13d
andl $-0x4, %r13d
testb $0x3, %al
cmovel %ecx, %r13d
testq %rsi, %rsi
je 0x1881
testl %r14d, %r14d
jne 0x1881
cmpl $0x0, (%r15)
jne 0x1881
movq %rsp, -0x40(%rbp)
movq 0x28a2(%rip), %rax      # 0x40b0
movq -0x60(%rbp), %rcx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movzbl -0x2(%r15), %eax
addl $0x10, %eax
andl $-0x10, %eax
movl %r14d, -0x30(%rbp)
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movl %edi, -0x34(%rbp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, -0x48(%rbp)
movl %edx, -0x2c(%rbp)
movl %r9d, %r12d
callq 0x1050
movzbl -0x2(%r15), %eax
movb $0x0, (%r14,%rax)
movq %r14, %rdi
movl -0x30(%rbp), %r14d
movq -0x58(%rbp), %rsi
callq 0x1080
movl %r12d, %r9d
movl -0x34(%rbp), %edi
movq -0x50(%rbp), %rsi
movl -0x2c(%rbp), %edx
movq -0x48(%rbp), %r12
testl %eax, %eax
movq -0x40(%rbp), %rsp
je 0x19ea
movl %edi, %r15d
subl %r13d, %r15d
jle 0x193d
movq %r12, -0x48(%rbp)
movq %rsp, -0x88(%rbp)
setle %al
addl $0xfffffbff, %r9d       # imm = 0xFFFFFBFF
movzwl %r9w, %ecx
cmpl $0xfc00, %ecx           # imm = 0xFC00
setb %r12b
orb %al, %r12b
movl %edx, -0x2c(%rbp)
cmovnel %edx, %r14d
movq 0x27ef(%rip), %rax      # 0x40b0
addq -0x60(%rbp), %rax
movl %r14d, -0x30(%rbp)
movslq %r14d, %rcx
addq %rax, %rcx
movl %r13d, %eax
leaq (%rcx,%rax), %rdx
movq %rdx, -0x40(%rbp)
leaq (%rax,%rcx), %rbx
addq $0x8, %rbx
movzbl -0x2(%rbx), %eax
addl $0x10, %eax
andl $-0x10, %eax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1050
movzbl -0x2(%rbx), %eax
movb $0x0, (%r14,%rax)
movq %r14, %rdi
movq -0x58(%rbp), %rsi
callq 0x1080
testb %r12b, %r12b
jne 0x1922
testl %eax, %eax
je 0x19ef
movzbl %r12b, %ebx
movl -0x30(%rbp), %r14d
addl %r13d, %r14d
movl %r15d, %edi
movq -0x50(%rbp), %rsi
movl -0x2c(%rbp), %edx
movq -0x48(%rbp), %r12
jmp 0x19b0
addl %r13d, %r14d
movq 0x2769(%rip), %rax      # 0x40b0
addq -0x60(%rbp), %rax
movslq %r14d, %r15
addq %rax, %r15
testl %ebx, %ebx
movl $0x1, %ebx
jne 0x19bb
movl %edi, -0x34(%rbp)
movl %edx, -0x2c(%rbp)
movq %rsp, -0x88(%rbp)
movzbl 0x6(%r15), %eax
movq %rsp, %rbx
addl $0x10, %eax
andl $-0x10, %eax
subq %rax, %rbx
movq %rbx, %rsp
leaq 0x8(%r15), %rsi
movq %rbx, %rdi
callq 0x1050
movq %r15, -0x40(%rbp)
movzbl 0x6(%r15), %eax
movb $0x0, (%rbx,%rax)
movq %rbx, %rdi
movq -0x58(%rbp), %rsi
callq 0x1080
testl %eax, %eax
je 0x1a09
xorl %ebx, %ebx
movl -0x2c(%rbp), %edx
movq -0x50(%rbp), %rsi
movl -0x34(%rbp), %edi
movq -0x88(%rbp), %rsp
movq -0x40(%rbp), %r15
cmpl %edx, %r14d
movl $0x1, %eax
cmovel %eax, %ebx
movq %r12, %rsp
cmpl $0x400, %r14d           # imm = 0x400
jl 0x17af
movl -0xe4(%rbp,%rsi,4), %eax
incq %rsi
testl %eax, %eax
jne 0x178b
jmp 0x1a05
movq %r12, %rsp
jmp 0x19d6
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rsi
movl -0x30(%rbp), %r14d
movq -0x48(%rbp), %r12
jmp 0x1a15
xorl %eax, %eax
jmp 0x1a3b
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rsi
addl %r13d, %r14d
movl %r14d, (%rsi)
movl -0x64(%rbp), %edi
movl %edi, (%rdx)
movq -0x40(%rbp), %r8
movl (%r8), %eax
movl %eax, (%rcx)
movq %r12, %rsp
movl %r14d, (%rsi)
movl %edi, (%rdx)
movl (%r8), %eax
movl %eax, (%rcx)
movl $0x1, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1264
nop