// File: /workspace/github_c_makefile_star2_O0/kevinp89[P]Operating-System-EXT2-file-system-with-commands/./ext2_utils.c
// Function: check_parent
int check_parent(unsigned char *disk, char *path) {
	struct ext2_super_block *sb = (struct ext2_super_block *)(disk + 1024);
	struct ext2_group_desc *desc = (struct ext2_group_desc *)(disk + 2*1024);
	// Copying the path string into an array
	// Getting amount of dir inputs
	char *dupe = strdup(path);
	strcpy(dupe, path);
	int dircount = 0;
	int i;
	for (i = 0; dupe[i] != 0; i++) {
		if (dupe[i] == '/' && dupe[i+1] != '\0') {
			dircount++;
		}
	}
	if (dircount == 0) {
		fprintf(stderr, "Invalid directory.\n");
		exit(ENOENT);
	}
	// Creating array and copying
	char *dirs[dircount];
	char *tail = dupe;
	for (i = 0; i < dircount; i++) {
		dirs[i] = strsep(&tail, "/");
	}

	// Inode related
	struct ext2_inode *curinode;
	int node = 1; // Root directory begins at index 1
	
	i = 1;
	while (i < dircount) {
		if (strcmp(dirs[i], ".") != 0) {
			curinode = (struct ext2_inode *)((disk + 1024 * desc->bg_inode_table) + (sb->s_inode_size * node));
			node = search_directories(disk, curinode, dirs[i], 1);
			if (node == -1) {
				break;
			}
		}
		i++;
	}

	free(dupe);
	return node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
addq $0x400, %rax            # imm = 0x400
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
addq $0x800, %rax            # imm = 0x800
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1150
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1050
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1602
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x15f5
movq -0x28(%rbp), %rax
movl -0x30(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x15f5
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x15f7
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x15b4
cmpl $0x0, -0x2c(%rbp)
jne 0x162a
movq 0x39e9(%rip), %rax      # 0x4ff8
movq (%rax), %rdi
leaq 0x19eb(%rip), %rsi      # 0x3004
movb $0x0, %al
callq 0x10c0
movl $0x2, %edi
callq 0x1140
movl -0x2c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x38(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1692
leaq -0x48(%rbp), %rdi
leaq 0x19a5(%rip), %rsi      # 0x3018
callq 0x1100
movq %rax, %rdx
movq -0x60(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1660
movl $0x1, -0x54(%rbp)
movl $0x1, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1727
movq -0x60(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1a7e(%rip), %rsi      # 0x3139
callq 0x10b0
cmpl $0x0, %eax
je 0x1719
movq -0x60(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl 0x8(%rdx), %edx
shll $0xa, %edx
movl %edx, %edx
addq %rdx, %rcx
movq -0x18(%rbp), %rdx
movzwl 0x58(%rdx), %edx
imull -0x54(%rbp), %edx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movl $0x1, %ecx
callq 0x1410
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x1717
jmp 0x1727
jmp 0x1719
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x16a0
movq -0x28(%rbp), %rdi
callq 0x1030
movl -0x54(%rbp), %eax
movq -0x38(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop