// File: /workspace/github_c_makefile_star2_O0/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
subq $0x190, %rsp            # imm = 0x190
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1798
movq 0x2882(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x893(%rip), %rsi       # 0x201a
movb $0x0, %al
callq 0x10a0
movl $0x1, %edi
callq 0x10f0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x10d0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x17df
movq 0x283c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x865(%rip), %rsi       # 0x2033
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x10f0
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, 0x289a(%rip)      # 0x40a0
movq $-0x1, %rax
cmpq %rax, 0x288c(%rip)      # 0x40a0
jne 0x182c
leaq 0x831(%rip), %rdi       # 0x204e
callq 0x10e0
movl $0x1, %edi
callq 0x10f0
movq 0x286d(%rip), %rax      # 0x40a0
addq $0x400, %rax            # imm = 0x400
movq %rax, 0x2868(%rip)      # 0x40a8
movq 0x2859(%rip), %rax      # 0x40a0
addq $0x800, %rax            # imm = 0x800
movq %rax, 0x285c(%rip)      # 0x40b0
movq 0x2845(%rip), %rax      # 0x40a0
movq 0x284e(%rip), %rcx      # 0x40b0
movl 0x4(%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq 0x2828(%rip), %rax      # 0x40a0
movq 0x2831(%rip), %rcx      # 0x40b0
movl (%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq 0x280c(%rip), %rdi      # 0x40a0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1510
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x18cc
movq 0x2747(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x798(%rip), %rsi       # 0x2053
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x10f0
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x188(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1060
movq %rax, %rcx
movq -0x188(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x1928
movq 0x26eb(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x756(%rip), %rsi       # 0x206d
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x10f0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1060
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
je 0x1961
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1938
movl -0x34(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x190(%rbp)
movq %rdi, %rsp
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x30(%rbp), %rax
addq %rax, %rsi
addq $0x1, %rsi
callq 0x1050
movq -0x190(%rbp), %rdx
movq 0x26f0(%rip), %rax      # 0x40a0
movq 0x26f9(%rip), %rcx      # 0x40b0
movl 0x8(%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq 0x26df(%rip), %rcx      # 0x40a8
movzwl 0x58(%rcx), %ecx
imull -0x2c(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq 0x26be(%rip), %rdi      # 0x40a0
movq -0x50(%rbp), %rsi
xorl %ecx, %ecx
callq 0x13c0
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x1a18
movq 0x25fb(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x678(%rip), %rsi       # 0x207f
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x10f0
movq 0x2681(%rip), %rax      # 0x40a0
movq 0x268a(%rip), %rcx      # 0x40b0
movl 0x8(%rcx), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq 0x2670(%rip), %rcx      # 0x40a8
movzwl 0x58(%rcx), %ecx
imull -0x54(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0x12f0
movzbl %al, %eax
cmpl $0x64, %eax
jne 0x1a7d
movq 0x2596(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x628(%rip), %rsi       # 0x2094
movb $0x0, %al
callq 0x10a0
movl $0x2, %edi
callq 0x10f0
movl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %rax
movslq -0x30(%rbp), %rcx
cmpl $0x0, 0x28(%rax,%rcx,4)
je 0x1bcc
movq $0x0, -0x180(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x400, -0x64(%rbp)     # imm = 0x400
jge 0x1bbc
leaq -0x170(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x1080
movq 0x25d0(%rip), %rax      # 0x40a0
movq -0x50(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movl 0x28(%rcx,%rdx,4), %ecx
shll $0xa, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movslq -0x64(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x178(%rbp)
leaq -0x170(%rbp), %rdi
movq -0x178(%rbp), %rsi
addq $0x8, %rsi
movq -0x178(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %edx
callq 0x1040
movq -0x190(%rbp), %rsi
movq -0x178(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x1090
cmpl $0x0, %eax
jne 0x1ba9
cmpq $0x0, -0x180(%rbp)
je 0x1b67
movq -0x178(%rbp), %rax
movzwl 0x4(%rax), %edx
movq -0x180(%rbp), %rax
movzwl 0x4(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x4(%rax)
jmp 0x1bbc
movq -0x50(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl $0x0, 0x28(%rax,%rcx,4)
movq -0x50(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %edi
subl $0x1, %edi
movq -0x28(%rbp), %rsi
callq 0x1260
movq 0x2512(%rip), %rsi      # 0x40a8
movq 0x2513(%rip), %rdx      # 0x40b0
movl $0x1, %edi
callq 0x16f0
jmp 0x1bb7
movq -0x178(%rbp), %rax
movq %rax, -0x180(%rbp)
jmp 0x1aa9
jmp 0x1bbe
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1a84
movq -0x60(%rbp), %rax
movw 0x1a(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x1a(%rax)
movq -0x60(%rbp), %rax
movzwl 0x1a(%rax), %eax
cmpl $0x0, %eax
jg 0x1c76
xorl %eax, %eax
movl %eax, %edi
callq 0x10b0
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x54(%rbp), %edi
movq -0x20(%rbp), %rsi
callq 0x1260
movq 0x2496(%rip), %rsi      # 0x40a8
movq 0x2497(%rip), %rdx      # 0x40b0
movl $0x1, %edi
callq 0x1720
movl $0x0, -0x30(%rbp)
movq -0x60(%rbp), %rax
movslq -0x30(%rbp), %rcx
cmpl $0x0, 0x28(%rax,%rcx,4)
je 0x1c74
movq -0x60(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %edi
subl $0x1, %edi
movq -0x28(%rbp), %rsi
callq 0x1260
movq 0x2450(%rip), %rsi      # 0x40a8
movq 0x2451(%rip), %rdx      # 0x40b0
movl $0x1, %edi
callq 0x16f0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1c2a
jmp 0x1c76
jmp 0x1c78
movl $0x0, -0x4(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq