// File: /workspace/github_c_makefile_star2_O0/sheheryarbutt[P]Ext2-File-System/ext2_rm.c
// Function: main
int main(int argc, char **argv) {
	if(argc != 3) { 
        fprintf(stderr, "Usage: ext2_rm <image file name>  <Absolute path>\n");
         exit(1);
    }
    int fd = open(argv[1], O_RDWR);

	char *path = malloc(sizeof(char)*((unsigned int)strlen(argv[2]) + 1));
	strcpy(path, argv[2]);
	path[(unsigned int)strlen(argv[2])] = '\0';

    disk = mmap(NULL, 128 * EXT2_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
		perror("mmap");
		exit(1);
 	}

 	sb = (struct ext2_super_block *)(disk + EXT2_BLOCK_SIZE);
    gd = (struct ext2_group_desc *)(disk + (2*EXT2_BLOCK_SIZE));
		 
 	inode_table = (struct ext2_inode *)(disk + (5*EXT2_BLOCK_SIZE));

 	remove_trailing_slash(path);
	//the block the files in that we want to remove
	unsigned int * parent_block= malloc(sizeof(unsigned int *));
	int cur_inode=pathTraversal(path,disk,parent_block);

	if(valid(cur_inode)){ 
		struct ext2_inode inode = inode_table[cur_inode-1];// represents the current inode

		if((inode.i_mode & EXT2_S_IFDIR) == EXT2_S_IFDIR){//is it a directory? raise error!
			fprintf(stderr, "ERROR: %s\n", strerror(EISDIR)); 
			exit(EISDIR);
		}
		else if((inode.i_mode & EXT2_S_IFREG) == EXT2_S_IFREG) { 
			//1) Update the directory entries
			int size=0;
			struct ext2_dir_entry *direntry = (struct ext2_dir_entry *)(disk + *parent_block*EXT2_BLOCK_SIZE +size);
			unsigned short prev_entry=0; unsigned short prev_rec_len=0;
			while(size<EXT2_BLOCK_SIZE){// going through whole block
				if(size==0 && (direntry->inode == cur_inode)){ // so if its the first block -> dummy node
					direntry->inode=0;
					break;
				}
				else if(direntry->inode==cur_inode){
					unsigned short cur_rec_len=0; cur_rec_len=direntry->rec_len; // get currents rec_len 
					direntry = (struct ext2_dir_entry *)(disk + *parent_block*EXT2_BLOCK_SIZE + (prev_entry - prev_rec_len));
					direntry->rec_len+=cur_rec_len; // add current rec_len to previous ones rec_len 
					break;
				}			
				prev_entry+=direntry->rec_len;// contstanly adds all the rec_lens
				prev_rec_len=direntry->rec_len;
				//add to size so we can increment the pointer to point to next entry!
				size+=direntry->rec_len; 
				direntry = (struct ext2_dir_entry *)(disk + *parent_block*EXT2_BLOCK_SIZE +size);				
			}
		
			//2 and 3 if hard links count==1 then change inode and data bitmap!
			if(inode.i_links_count==1){
				unsigned int * ib = (unsigned int *)(disk+EXT2_BLOCK_SIZE*4);
				//-0 represents what we want the n=10 bit too (its really 11 tho!)
				//inode bitmap
				ib[0] ^= (-0 ^ ib[0]) & (1U << (cur_inode-1));
				//block/data bitmap
				int cur_index=0;
				int inode_i_block= inode.i_block[cur_index];
				unsigned int * bb=(unsigned int *)(disk + EXT2_BLOCK_SIZE*3);

				while(inode_i_block){
					int index_of_bb=(inode_i_block-1)/32;       
					bb[index_of_bb] ^= 1U << (inode_i_block -1) % 32;
					cur_index++;
					inode_i_block= inode.i_block[cur_index];
				}

				time_t rawtime; // setting deletion time
				inode_table[cur_inode-1].i_dtime = time(&rawtime); 
				//increasing free blocks and inodes count!
				sb->s_free_inodes_count+=1; sb->s_free_blocks_count+=inode.i_blocks/2;
				gd->bg_free_inodes_count+=1; gd->bg_free_blocks_count+=inode.i_blocks/2;
			}// end of if 

			//#4 modify links count (hardlinks)
			inode_table[cur_inode-1].i_links_count=inode_table[cur_inode-1].i_links_count -1;

		}// end of else if
		else{ // If we encounter anything other than a file
		fprintf(stderr, "ERROR: %s\n", strerror(EPERM)); 
	  	exit(EPERM);

		}
	}
	else{
		fprintf(stderr, "ERROR: %s\n", strerror(ENOENT)); 
	  	exit(ENOENT);

		}
	free(path); free(parent_block);
	return 0; 

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1261
movq 0x3db2(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x1db4(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1100
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10e0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1060
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
shlq $0x0, %rdi
callq 0x10d0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1050
movq -0x20(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1060
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movl -0x14(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x20000, %esi          # imm = 0x20000
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x1070
movq %rax, 0x3daf(%rip)      # 0x50a8
movq $-0x1, %rax
cmpq %rax, 0x3da1(%rip)      # 0x50a8
jne 0x131f
leaq 0x1d27(%rip), %rdi      # 0x3037
callq 0x10f0
movl $0x1, %edi
callq 0x1100
movq 0x3d82(%rip), %rax      # 0x50a8
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x3d7d(%rip)      # 0x50b0
movq 0x3d6e(%rip), %rax      # 0x50a8
addq $0x800, %rax            # imm = 0x800
movq %rax, 0x3d71(%rip)      # 0x50b8
movq 0x3d5a(%rip), %rax      # 0x50a8
addq $0x1400, %rax           # imm = 0x1400
movq %rax, 0x3d65(%rip)      # 0x50c0
movq -0x20(%rbp), %rdi
callq 0x17e0
movl $0x8, %edi
callq 0x10d0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
movq 0x3d2b(%rip), %rsi      # 0x50a8
movq -0x28(%rbp), %rdx
callq 0x19b0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x1890
cmpl $0x0, %eax
je 0x177b
movq 0x3d1f(%rip), %rsi      # 0x50c0
movl -0x2c(%rbp), %eax
subl $0x1, %eax
cltq
shlq $0x7, %rax
addq %rax, %rsi
leaq -0xac(%rbp), %rdi
movl $0x80, %edx
callq 0x10a0
movzwl -0xac(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x1411
movq 0x3c1d(%rip), %rax      # 0x4ff8
movq (%rax), %rax
movq %rax, -0xf8(%rbp)
movl $0x15, %edi
callq 0x1110
movq -0xf8(%rbp), %rdi
movq %rax, %rdx
leaq 0x1c3c(%rip), %rsi      # 0x303c
movb $0x0, %al
callq 0x1090
movl $0x15, %edi
callq 0x1100
movzwl -0xac(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x173a
movl $0x0, -0xb0(%rbp)
movq 0x3c6f(%rip), %rax      # 0x50a8
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movslq -0xb0(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movw $0x0, -0xba(%rbp)
movw $0x0, -0xbc(%rbp)
cmpl $0x400, -0xb0(%rbp)     # imm = 0x400
jge 0x1589
cmpl $0x0, -0xb0(%rbp)
jne 0x14a3
movq -0xb8(%rbp), %rax
movl (%rax), %eax
cmpl -0x2c(%rbp), %eax
jne 0x14a3
movq -0xb8(%rbp), %rax
movl $0x0, (%rax)
jmp 0x1589
movq -0xb8(%rbp), %rax
movl (%rax), %eax
cmpl -0x2c(%rbp), %eax
jne 0x1518
movw $0x0, -0xbe(%rbp)
movq -0xb8(%rbp), %rax
movw 0x4(%rax), %ax
movw %ax, -0xbe(%rbp)
movq 0x3bd5(%rip), %rax      # 0x50a8
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movzwl -0xba(%rbp), %ecx
movzwl -0xbc(%rbp), %edx
subl %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movzwl -0xbe(%rbp), %edx
movq -0xb8(%rbp), %rax
movzwl 0x4(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x4(%rax)
jmp 0x1589
jmp 0x151a
movq -0xb8(%rbp), %rax
movzwl 0x4(%rax), %ecx
movzwl -0xba(%rbp), %eax
addl %ecx, %eax
movw %ax, -0xba(%rbp)
movq -0xb8(%rbp), %rax
movw 0x4(%rax), %ax
movw %ax, -0xbc(%rbp)
movq -0xb8(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0xb0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq 0x3b43(%rip), %rax      # 0x50a8
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movslq -0xb0(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
jmp 0x146a
movzwl -0x92(%rbp), %eax
cmpl $0x1, %eax
jne 0x16fc
movq 0x3b08(%rip), %rax      # 0x50a8
addq $0x1000, %rax           # imm = 0x1000
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl (%rax), %eax
xorl $0x0, %eax
movl %eax, -0xfc(%rbp)
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl -0xfc(%rbp), %ecx
andl %eax, %ecx
movq -0xc8(%rbp), %rax
xorl (%rax), %ecx
movl %ecx, (%rax)
movl $0x0, -0xcc(%rbp)
movslq -0xcc(%rbp), %rax
movl -0x84(%rbp,%rax,4), %eax
movl %eax, -0xd0(%rbp)
movq 0x3aa4(%rip), %rax      # 0x50a8
addq $0xc00, %rax            # imm = 0xC00
movq %rax, -0xd8(%rbp)
cmpl $0x0, -0xd0(%rbp)
je 0x1684
movl -0xd0(%rbp), %eax
subl $0x1, %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, -0xdc(%rbp)
movl -0xd0(%rbp), %eax
subl $0x1, %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %edx, %ecx
movl $0x1, %edx
shll %cl, %edx
movq -0xd8(%rbp), %rax
movslq -0xdc(%rbp), %rcx
xorl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
movslq -0xcc(%rbp), %rax
movl -0x84(%rbp,%rax,4), %eax
movl %eax, -0xd0(%rbp)
jmp 0x1611
leaq -0xe8(%rbp), %rdi
callq 0x10c0
movl %eax, %ecx
movq 0x3a27(%rip), %rax      # 0x50c0
movl -0x2c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x7, %rdx
addq %rdx, %rax
movl %ecx, 0x14(%rax)
movq 0x39fd(%rip), %rax      # 0x50b0
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movl -0x90(%rbp), %ecx
shrl %ecx
movq 0x39e5(%rip), %rax      # 0x50b0
addl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movq 0x39e0(%rip), %rax      # 0x50b8
movzwl 0xe(%rax), %ecx
addl $0x1, %ecx
movw %cx, 0xe(%rax)
movl -0x90(%rbp), %edx
shrl %edx
movq 0x39c6(%rip), %rax      # 0x50b8
movzwl 0xc(%rax), %ecx
addl %edx, %ecx
movw %cx, 0xc(%rax)
movq 0x39bd(%rip), %rax      # 0x50c0
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movzwl 0x1a(%rax), %eax
subl $0x1, %eax
movw %ax, %cx
movq 0x399c(%rip), %rax      # 0x50c0
movl -0x2c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x7, %rdx
addq %rdx, %rax
movw %cx, 0x1a(%rax)
jmp 0x1777
movq 0x38b7(%rip), %rax      # 0x4ff8
movq (%rax), %rax
movq %rax, -0x108(%rbp)
movl $0x1, %edi
callq 0x1110
movq -0x108(%rbp), %rdi
movq %rax, %rdx
leaq 0x18d6(%rip), %rsi      # 0x303c
movb $0x0, %al
callq 0x1090
movl $0x1, %edi
callq 0x1100
jmp 0x1779
jmp 0x17b8
movq 0x3876(%rip), %rax      # 0x4ff8
movq (%rax), %rax
movq %rax, -0x110(%rbp)
movl $0x2, %edi
callq 0x1110
movq -0x110(%rbp), %rdi
movq %rax, %rdx
leaq 0x1895(%rip), %rsi      # 0x303c
movb $0x0, %al
callq 0x1090
movl $0x2, %edi
callq 0x1100
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x28(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop