// File: /workspace/github_c_makefile_star2_O1/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 $0x168, %rsp            # imm = 0x168
movq %rsi, %r15
cmpl $0x3, %edi
jne 0x197d
movq 0x8(%r15), %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
je 0x1985
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl $0x3, %edx
movl $0x1, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2a42(%rip)      # 0x40a8
cmpq $-0x1, %rax
je 0x1991
movq %rax, %rdi
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x2a30(%rip)      # 0x40b0
leaq 0x800(%rdi), %rax
movq %rax, 0x2a2a(%rip)      # 0x40b8
movl 0x800(%rdi), %r13d
movl 0x804(%rdi), %ebx
movq 0x10(%r15), %rsi
movq %rdi, -0x48(%rbp)
callq 0x14ca
cmpl $-0x1, %eax
je 0x1996
movl %eax, %r14d
movq 0x10(%r15), %r12
movq %r12, %rdi
callq 0x1060
cmpb $0x2f, -0x1(%r12,%rax)
je 0x199b
movslq %eax, %rcx
cmpb $0x2f, (%r12,%rcx)
movq %rbx, -0x68(%rbp)
movq %r13, -0x38(%rbp)
jne 0x16e2
xorl %eax, %eax
jmp 0x16f9
movslq %eax, %rdx
xorl %eax, %eax
leaq -0x1(%rdx), %rcx
incq %rax
cmpb $0x2f, -0x1(%r12,%rdx)
movq %rcx, %rdx
jne 0x16e7
movq %rsp, %r13
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
leaq (%r12,%rcx), %rsi
incq %rsi
movq %r13, %rdi
callq 0x1050
movq 0x2988(%rip), %r15      # 0x40a8
movq 0x2991(%rip), %rax      # 0x40b8
movl 0x8(%rax), %r12d
shll $0xa, %r12d
leaq (%r15,%r12), %rcx
movq 0x2976(%rip), %rax      # 0x40b0
movzwl 0x58(%rax), %ebx
movslq %r14d, %rsi
movq %rcx, %r14
imulq %rbx, %rsi
addq %rcx, %rsi
movq %r15, %rdi
movq %rsi, -0x30(%rbp)
movq %r13, -0x88(%rbp)
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x13e2
cmpl $-0x1, %eax
je 0x19a0
movq %r12, -0x50(%rbp)
movq %r15, -0x58(%rbp)
movq %rax, -0x60(%rbp)
cltq
imulq %rbx, %rax
movq %r14, -0x78(%rbp)
movq %rax, -0x70(%rbp)
movzwl (%r14,%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
je 0x19a5
shll $0xa, %ecx
addq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x18ac
movq -0x30(%rbp), %rax
addq $0x28, %rax
xorl %ecx, %ecx
movq %rax, -0x30(%rbp)
movq %rax, -0x40(%rbp)
movq %rcx, -0x80(%rbp)
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r15, -0x90(%rbp)
movl $0x100, %edx            # imm = 0x100
leaq -0x190(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1080
movq -0x40(%rbp), %rax
movl (%rax), %r15d
shll $0xa, %r15d
addq 0x28a2(%rip), %r15      # 0x40a8
movl %r13d, %ebx
leaq (%rbx,%r15), %r12
addq $0x8, %r12
movzbl -0x2(%r12), %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x1040
movzwl -0x4(%r12), %r12d
movq %r14, %rdi
movq -0x88(%rbp), %rsi
callq 0x1090
testl %eax, %eax
je 0x1840
addq %rbx, %r15
jmp 0x1875
movq -0x90(%rbp), %rax
testq %rax, %rax
jne 0x1887
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
movq -0x38(%rbp), %rax
xorb $-0x80, (%rax)
movq 0x284c(%rip), %rax      # 0x40b0
movq 0x284d(%rip), %rcx      # 0x40b8
incl 0xc(%rax)
incw 0xc(%rcx)
xorl %r15d, %r15d
addl %r12d, %r13d
cmpl $0x400, %r13d           # imm = 0x400
jb 0x17d7
jmp 0x188c
addw %r12w, 0x4(%rax)
movq -0x30(%rbp), %rax
movq -0x80(%rbp), %rcx
leaq (%rax,%rcx,4), %rax
addq $0x4, %rax
incq %rcx
movq %rax, -0x40(%rbp)
cmpl $0x0, (%rax)
jne 0x17cd
movq -0x78(%rbp), %r15
movq -0x70(%rbp), %r12
decw 0x1a(%r15,%r12)
movq -0x38(%rbp), %rbx
jne 0x196c
movq -0x68(%rbp), %rax
shll $0xa, %eax
movq -0x48(%rbp), %r14
addq %rax, %r14
xorl %edi, %edi
callq 0x10b0
movq -0x60(%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 0x27aa(%rip), %rax      # 0x40b0
movq 0x27ab(%rip), %rcx      # 0x40b8
incl 0x10(%rax)
incw 0xe(%rcx)
movl 0x28(%r15,%r12), %edx
testl %edx, %edx
je 0x196c
addq -0x50(%rbp), %r12
movq -0x58(%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 0x275c(%rip), %rcx      # 0x40b0
movq 0x275d(%rip), %rdx      # 0x40b8
incl 0xc(%rcx)
incw 0xc(%rdx)
movl (%rax), %edx
addq $0x4, %rax
testl %edx, %edx
jne 0x192c
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