// File: /workspace/github_c_makefile_star2_O2/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, -0x88(%rbp)
movq %r8, -0x80(%rbp)
movq %rcx, -0x70(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, %rbx
movq %rdi, %r14
pushq $0x8
popq %rdi
callq 0x10d0
movq 0x2ad5(%rip), %rsi      # 0x40b0
movq %r14, %rdi
movq %rax, %rdx
callq 0x1a6d
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 %dl
cmpl $0x2, %r14d
setne %cl
xorl %eax, %eax
testb %cl, %dl
jne 0x18f5
movq 0x2aa2(%rip), %rax      # 0x40c8
movl %r14d, %ecx
shlq $0x7, %rcx
leaq (%rax,%rcx), %rsi
addq $-0x80, %rsi
pushq $0x20
popq %rcx
leaq -0x108(%rbp), %rax
movq %rax, %rdi
rep  movsl (%rsi), %es:(%rdi)
testb $0x40, 0x1(%rax)
je 0x1908
pushq $0x1
popq %rbx
xorl %r15d, %r15d
movl -0xe0(%rbp,%r15,4), %eax
testl %eax, %eax
je 0x1904
movl %eax, -0x64(%rbp)
movl %eax, %ecx
shll $0xa, %ecx
movq 0x2a3d(%rip), %r9       # 0x40b0
movq %rcx, -0x60(%rbp)
addq %rcx, %r9
xorl %esi, %esi
movl %ebx, %r14d
xorl %edx, %edx
movq %r15, -0x50(%rbp)
cmpl $0x3ff, %edx            # imm = 0x3FF
jg 0x18b5
movq %rsp, %r8
testl %r14d, %r14d
je 0x16af
movq -0x88(%rbp), %rax
movl %edx, (%rax)
movzwl 0x4(%r9), %r10d
movzwl %r10w, %edi
addl %edi, %esi
jmp 0x16b4
movzwl 0x4(%r9), %r10d
movzbl 0x6(%r9), %eax
leal 0x8(%rax), %ecx
movl %eax, %r13d
addl $0xc, %r13d
andl $-0x4, %r13d
testb $0x3, %al
cmovel %ecx, %r13d
testq %r15, %r15
je 0x1761
testl %edx, %edx
jne 0x1761
cmpl $0x0, (%r9)
jne 0x1761
movq %rsp, -0x48(%rbp)
movq 0x29c1(%rip), %rax      # 0x40b0
movq -0x60(%rbp), %rcx
leaq (%rax,%rcx), %rbx
addq $0x8, %rbx
movzbl -0x2(%rbx), %eax
addl $0x10, %eax
andl $-0x10, %eax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movl %edi, -0x40(%rbp)
movq %r15, %rdi
movl %esi, -0x30(%rbp)
movq %rbx, %rsi
movl %edx, -0x2c(%rbp)
movq %r8, -0x38(%rbp)
movl %r10d, %r12d
callq 0x1050
movzbl -0x2(%rbx), %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
movq -0x50(%rbp), %r15
movq -0x58(%rbp), %rsi
callq 0x1080
movl %r12d, %r10d
movl -0x40(%rbp), %edi
movq -0x38(%rbp), %r8
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edx
testl %eax, %eax
movq -0x48(%rbp), %rsp
pushq $0x1
popq %rbx
je 0x18b2
movl %edi, %r12d
subl %r13d, %r12d
jle 0x1818
movq %r8, -0x38(%rbp)
movq %rsp, -0x48(%rbp)
setle %al
addl $0xfffffbff, %r10d      # imm = 0xFFFFFBFF
cmpw $0xfc00, %r10w          # imm = 0xFC00
setb %bl
orb %al, %bl
movl %esi, -0x30(%rbp)
cmovnel %esi, %edx
movq 0x2919(%rip), %rax      # 0x40b0
addq -0x60(%rbp), %rax
movl %edx, -0x2c(%rbp)
movslq %edx, %rcx
addq %rax, %rcx
movl %r13d, %eax
leaq (%rcx,%rax), %rdx
movq %rdx, -0x40(%rbp)
leaq (%rax,%rcx), %r14
addq $0x8, %r14
movzbl -0x2(%r14), %eax
addl $0x10, %eax
andl $-0x10, %eax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
movq %r14, %rsi
callq 0x1050
movzbl -0x2(%r14), %eax
movb $0x0, (%r15,%rax)
movq %r15, %rdi
movq -0x58(%rbp), %rsi
callq 0x1080
testb %bl, %bl
jne 0x17f8
testl %eax, %eax
je 0x18bd
movzbl %bl, %r14d
movl -0x2c(%rbp), %edx
addl %r13d, %edx
movl %r12d, %edi
pushq $0x1
popq %rbx
movq -0x50(%rbp), %r15
movl -0x30(%rbp), %esi
movq -0x38(%rbp), %r8
jmp 0x189c
addl %r13d, %edx
movq 0x288e(%rip), %rax      # 0x40b0
addq -0x60(%rbp), %rax
movslq %edx, %r9
addq %rax, %r9
testl %r14d, %r14d
je 0x1836
movl %ebx, %r14d
jmp 0x18a4
movl %edi, %r12d
movq %r8, -0x38(%rbp)
movl %esi, -0x30(%rbp)
movl %edx, -0x2c(%rbp)
movq %rsp, -0x48(%rbp)
movzbl 0x6(%r9), %eax
movq %rsp, %r14
addl $0x10, %eax
andl $-0x10, %eax
subq %rax, %r14
movq %r14, %rsp
leaq 0x8(%r9), %rsi
movq %r14, %rdi
movq %r9, %r15
callq 0x1050
movq %r15, -0x40(%rbp)
movzbl 0x6(%r15), %eax
movb $0x0, (%r14,%rax)
movq %r14, %rdi
movq -0x58(%rbp), %rsi
callq 0x1080
testl %eax, %eax
je 0x18bd
xorl %r14d, %r14d
movl -0x2c(%rbp), %edx
movl -0x30(%rbp), %esi
movq -0x38(%rbp), %r8
movl %r12d, %edi
movq -0x50(%rbp), %r15
movq -0x48(%rbp), %rsp
movq -0x40(%rbp), %r9
cmpl %esi, %edx
cmovel %ebx, %r14d
movq %r8, %rsp
jmp 0x1685
movq %r8, %rsp
incq %r15
jmp 0x1654
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq -0x70(%rbp), %rsi
movl -0x2c(%rbp), %r12d
movq -0x38(%rbp), %r8
addl %r13d, %r12d
movl %r12d, (%rsi)
movl -0x64(%rbp), %edi
movl %edi, (%rdx)
movq -0x40(%rbp), %r9
movl (%r9), %eax
movl %eax, (%rcx)
movq %r8, %rsp
movl %r12d, (%rsi)
movl %edi, (%rdx)
movl (%r9), %eax
movl %eax, (%rcx)
pushq $0x1
popq %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x18f5
movq 0x26e9(%rip), %rax      # 0x3ff8
movq (%rax), %rbx
pushq $0x14
popq %r14
movl %r14d, %edi
callq 0x1120
leaq 0x71b(%rip), %rsi       # 0x2040
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1090
movl %r14d, %edi
callq 0x1100
nop