// File: /workspace/github_c_makefile_star2_O3/kevinp89[P]Operating-System-EXT2-file-system-with-commands/ext2_rm.c
// Function: main
int main(int argc, char **argv) {
	// We need two arguments, so argc must be 3.
	if (argc != 3) {
		fprintf(stderr, "Usage: %s [disk] [path]\n", argv[0]);
		exit(1);
	}
	// Opening disk
	int fd = open(argv[1], O_RDWR);
	if (!fd) {
		fprintf(stderr, "Disk image '%s' not found.", argv[1]);
		exit(ENOENT);
	}
	// mmap the disk
	disk = mmap(NULL, 128*1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (disk == MAP_FAILED) {
		perror("mmap");
		exit(1);
	}
	// Grabbing super block and block descriptor
	sb = (struct ext2_super_block *)(disk + 1024);
	desc = (struct ext2_group_desc *)(disk + 2*1024);
	// Grabbing inode map and data map
	unsigned char *imap = (unsigned char *)(disk + 1024 * desc->bg_inode_bitmap);
	unsigned char *map = (unsigned char *)(disk + 1024 * desc->bg_block_bitmap);
	// Get parent inode index
	int parent_inode_index = check_parent(disk, argv[2]);
	if (parent_inode_index == -1) { // Directory not found.
		fprintf(stderr, "Directory does not exist.");
		exit(ENOENT);
	}
	// Get file name
	int i;
	int filename_len = 0;
	if (argv[2][strlen(argv[2])-1] == '/') {
		fprintf(stderr, "Invalid filename.");
		exit(ENOENT);
	}
	for (i = strlen(argv[2]); argv[2][i] != '/'; i--) {
		filename_len++;
	}
	char filename[filename_len];
	strcpy(filename, argv[2] + i + 1);
	// Check that the file exists
	struct ext2_inode *parent = (struct ext2_inode *)((disk + 1024 * desc->bg_inode_table) +
													(sb->s_inode_size * parent_inode_index));
	int targ_inode = search_directories(disk, parent, filename, 0);
	if (targ_inode == -1) {
		fprintf(stderr, "File does not exist.");
		exit(ENOENT);
	}
	// Find whether the directory is a link or file
	struct ext2_inode *target = (struct ext2_inode *)((disk + 1024 * desc->bg_inode_table) +
													(sb->s_inode_size * targ_inode));
	if (get_inode_type(target) == 'd') {
		fprintf(stderr, "Cannot remove directories.");
		exit(ENOENT);
	} else {
		// Directory stuff straight from readimage.c
		int cur_rec_len;
		char curfilename[256];
		struct ext2_dir_entry *cur_dir;
		struct ext2_dir_entry *prev_dir;
		for (i = 0; parent->i_block[i] != 0; i++) {
			prev_dir = NULL;
			for (cur_rec_len = 0; cur_rec_len < 1024; ) {
				memset(curfilename, 0, sizeof(curfilename));
				cur_dir = (struct ext2_dir_entry *)(disk + (1024 * parent->i_block[i]) + cur_rec_len);
				strncpy(curfilename, cur_dir->name, cur_dir->name_len);
				cur_rec_len += cur_dir->rec_len;
				// Checking if the current file is the file we're looking for
				if (strcmp(curfilename, filename) == 0) {
					if (prev_dir != NULL) {
						prev_dir->rec_len += cur_dir->rec_len;
						break;
					} else {
						// rec-len is 1024, dir_entry has its own data block
						parent->i_block[i] = 0;
						set_node(parent->i_block[i] - 1, map);
						adjust_free_blocks(1, sb, desc);
					}
				} else {
					// Setting the prev_dir to modify later
					prev_dir = cur_dir;
				}
			}
		}
		target->i_links_count--;
		if (target->i_links_count <= 0) {
			// De-allocate everything, set inode's i_dtime, find directory and set prev dir's rec_len over
			target->i_dtime = (unsigned int)time(0);
			set_node(targ_inode, imap);
			adjust_free_inodes(1, sb, desc);
			// De-allocate the data blocks as well
			for (i = 0; target->i_block[i] != 0; i++) {
				set_node(target->i_block[i] - 1, map);
				adjust_free_blocks(1, sb, desc);
			}
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, %r15
cmpl $0x3, %edi
jne 0x1996
movq 0x8(%r15), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
je 0x199e
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2a35(%rip)      # 0x40a8
cmpq $-0x1, %rax
je 0x19aa
movq %rax, %rdi
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x2a23(%rip)      # 0x40b0
leaq 0x800(%rdi), %rax
movq %rax, 0x2a1d(%rip)      # 0x40b8
movl 0x800(%rdi), %eax
movq %rax, -0x38(%rbp)
movl 0x804(%rdi), %ebx
movq 0x10(%r15), %rsi
movq %rdi, -0x50(%rbp)
callq 0x14ca
cmpl $-0x1, %eax
je 0x19af
movl %eax, %r13d
movq 0x10(%r15), %r12
movq %r12, %rdi
callq 0x1060
cmpb $0x2f, -0x1(%r12,%rax)
je 0x19b4
movslq %eax, %rcx
cmpb $0x2f, (%r12,%rcx)
movq %rbx, -0x70(%rbp)
jne 0x16ee
xorl %eax, %eax
jmp 0x1705
movslq %eax, %rdx
xorl %eax, %eax
leaq -0x1(%rdx), %rcx
incq %rax
cmpb $0x2f, -0x1(%r12,%rdx)
movq %rcx, %rdx
jne 0x16f3
movq %rsp, %r14
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
leaq (%r12,%rcx), %rsi
incq %rsi
movq %r14, %rdi
callq 0x1050
movq 0x297c(%rip), %rdi      # 0x40a8
movq 0x2985(%rip), %rax      # 0x40b8
movl 0x8(%rax), %r15d
shll $0xa, %r15d
leaq (%rdi,%r15), %r12
movq 0x296a(%rip), %rax      # 0x40b0
movzwl 0x58(%rax), %ebx
movslq %r13d, %rsi
imulq %rbx, %rsi
addq %r12, %rsi
movq %rdi, -0x48(%rbp)
movq %rsi, -0x30(%rbp)
movq %r14, -0x90(%rbp)
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x13e2
cmpl $-0x1, %eax
je 0x19b9
movq %r15, -0x60(%rbp)
movq %rax, -0x68(%rbp)
cltq
imulq %rbx, %rax
movq %r12, -0x80(%rbp)
movq %rax, -0x78(%rbp)
movzwl (%r12,%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
movq -0x38(%rbp), %r13
movq -0x50(%rbp), %rax
je 0x19be
shll $0xa, %r13d
addq %rax, %r13
movq %r13, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x18c5
movq -0x30(%rbp), %rax
addq $0x28, %rax
xorl %ecx, %ecx
movq -0x48(%rbp), %rdx
movq %rdx, -0x58(%rbp)
movq %rax, -0x30(%rbp)
movq %rax, -0x40(%rbp)
movq %rcx, -0x88(%rbp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %rbx, -0x98(%rbp)
movl $0x100, %edx            # imm = 0x100
leaq -0x1a0(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1080
movq -0x40(%rbp), %rax
movl (%rax), %ebx
shll $0xa, %ebx
addq -0x58(%rbp), %rbx
movl %r15d, %r13d
leaq (%rbx,%r13), %r12
addq $0x8, %r12
movzbl -0x2(%r12), %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x1040
movzwl -0x4(%r12), %r12d
movq %r14, %rdi
movq -0x90(%rbp), %rsi
callq 0x1090
testl %eax, %eax
je 0x184c
addq %r13, %rbx
jmp 0x188b
movq -0x98(%rbp), %rax
testq %rax, %rax
jne 0x189d
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
movq -0x38(%rbp), %rax
xorb $-0x80, (%rax)
movq 0x2840(%rip), %rax      # 0x40b0
movq 0x2841(%rip), %rcx      # 0x40b8
incl 0xc(%rax)
incw 0xc(%rcx)
movq 0x2823(%rip), %rax      # 0x40a8
movq %rax, -0x58(%rbp)
xorl %ebx, %ebx
addl %r12d, %r15d
cmpl $0x400, %r15d           # imm = 0x400
jb 0x17e8
jmp 0x18a2
addw %r12w, 0x4(%rax)
movq -0x30(%rbp), %rax
movq -0x88(%rbp), %rcx
leaq (%rax,%rcx,4), %rax
addq $0x4, %rax
incq %rcx
movq %rax, -0x40(%rbp)
cmpl $0x0, (%rax)
jne 0x17dc
movq -0x80(%rbp), %r15
movq -0x78(%rbp), %r12
decw 0x1a(%r15,%r12)
movq -0x38(%rbp), %rbx
jne 0x1985
movq -0x70(%rbp), %rax
shll $0xa, %eax
movq -0x50(%rbp), %r14
addq %rax, %r14
xorl %edi, %edi
callq 0x10b0
movq -0x68(%rbp), %rsi
movl %esi, %ecx
andb $0x7, %cl
movl $0x1, %edx
shll %cl, %edx
movl %eax, 0x14(%r15,%r12)
leal 0x7(%rsi), %eax
testl %esi, %esi
cmovnsl %esi, %eax
sarl $0x3, %eax
cltq
xorb %dl, (%r14,%rax)
movq 0x2791(%rip), %rax      # 0x40b0
movq 0x2792(%rip), %rcx      # 0x40b8
incl 0x10(%rax)
incw 0xe(%rcx)
movl 0x28(%r15,%r12), %edx
testl %edx, %edx
je 0x1985
addq -0x60(%rbp), %r12
movq -0x48(%rbp), %rax
addq %r12, %rax
addq $0x2c, %rax
leal -0x1(%rdx), %esi
movl %esi, %ecx
andb $0x7, %cl
movl $0x1, %edi
shll %cl, %edi
addl $0x6, %edx
testl %esi, %esi
cmovnsl %esi, %edx
sarl $0x3, %edx
movslq %edx, %rcx
xorb %dil, (%rbx,%rcx)
movq 0x2743(%rip), %rcx      # 0x40b0
movq 0x2744(%rip), %rdx      # 0x40b8
incl 0xc(%rcx)
incw 0xc(%rdx)
movl (%rax), %edx
addq $0x4, %rax
testl %edx, %edx
jne 0x1945
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x1155
addq $0x8, %r15
movq %r15, %rdi
callq 0x1225
callq 0x1210
callq 0x11eb
callq 0x11c6
callq 0x11a1
callq 0x117c