// File: hyp1231[P]FMSS/filesystem.cpp
// Function: FileSystem::Get_file_inodeNum_from_dir(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool)
int FileSystem::Get_file_inodeNum_from_dir(int dir_inodeNum, const string& filename, bool isDelete) {
	char inode_buf[64], buf[BLKsize];
	
	if(!Get_inode_from_inodeNum(inode_buf, dir_inodeNum)) {
		cout << "[Error] Cannot get inode: " << dir_inodeNum << endl;
		return -1;
	}

	// if inode doesn't point to a directory
	if(byte2int(inode_buf, 0, 2) == 0) {
		cout << "[Error] Inode " << dir_inodeNum << " is not a dir" << endl;
		return -1;
	}

	// get the directory data block
	D.Getblk(buf, Get_actual_dataBLKnumber(byte2int(inode_buf, 42, 44)));

	for(int i = 0; i + 12 <= BLKsize; i += 12) {
		// check if filename EQU
		if(byteEQUstring(buf, i, i + (int)filename.size(), filename)) {
			// check if is used
			if(byte2int(buf, i + 8, i + 10) == 1) {
				// return inodeNum
                if (isDelete) {
                    Fill_byte_by_num(buf, i + 8, i + 10, 0);
                    D.Putblk(buf, Get_actual_dataBLKnumber(byte2int(inode_buf, 42, 44)));
                }
				return byte2int(buf, i + 10, i + 12);
			}
		}
	}

	// not found
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movb %cl, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x98(%rbp)
movl (%rdi), %eax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
leaq -0x70(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x4ff0
testb $0x1, %al
jne 0x4dd3
movq 0x10239(%rip), %rdi     # 0x14fd0
leaq 0xa6a6(%rip), %rsi      # 0xf444
callq 0x3250
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x3480
movq %rax, %rdi
movq 0x10208(%rip), %rsi     # 0x14fc0
callq 0x32a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x84(%rbp)
jmp 0x4fd7
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x40e0
cmpl $0x0, %eax
jne 0x4e3a
movq 0x101e1(%rip), %rdi     # 0x14fd0
leaq 0xa669(%rip), %rsi      # 0xf45f
callq 0x3250
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x3480
movq %rax, %rdi
leaq 0xa65e(%rip), %rsi      # 0xf46e
callq 0x3250
movq %rax, %rdi
movq 0x101a1(%rip), %rsi     # 0x14fc0
callq 0x32a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x84(%rbp)
jmp 0x4fd7
movq -0x98(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xa0(%rbp)
leaq -0x70(%rbp), %rdi
movl $0x2a, %esi
movl $0x2c, %edx
callq 0x40e0
movq -0x98(%rbp), %rdi
movl %eax, %esi
callq 0x9390
movq -0xa0(%rbp), %rdi
movq -0x90(%rbp), %rsi
movl %eax, %edx
callq 0x3900
movl $0x0, -0x88(%rbp)
movq -0x98(%rbp), %rcx
movl -0x88(%rbp), %eax
addl $0xc, %eax
cmpl (%rcx), %eax
jg 0x4fc6
movl -0x88(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0xa8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31a0
movl -0xa8(%rbp), %edx
movq -0x90(%rbp), %rdi
movl -0xa4(%rbp), %esi
addl %eax, %edx
movq -0x20(%rbp), %rcx
callq 0x40a0
testb $0x1, %al
jne 0x4eec
jmp 0x4fb0
movq -0x90(%rbp), %rdi
movl -0x88(%rbp), %esi
addl $0x8, %esi
movl -0x88(%rbp), %edx
addl $0xa, %edx
callq 0x40e0
cmpl $0x1, %eax
jne 0x4fae
testb $0x1, -0x21(%rbp)
je 0x4f81
movq -0x90(%rbp), %rdi
movl -0x88(%rbp), %esi
addl $0x8, %esi
movl -0x88(%rbp), %edx
addl $0xa, %edx
xorl %ecx, %ecx
callq 0x44b0
movq -0x98(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
leaq -0x70(%rbp), %rdi
movl $0x2a, %esi
movl $0x2c, %edx
callq 0x40e0
movq -0x98(%rbp), %rdi
movl %eax, %esi
callq 0x9390
movq -0xb0(%rbp), %rdi
movq -0x90(%rbp), %rsi
movl %eax, %edx
callq 0x3aa0
movq -0x90(%rbp), %rdi
movl -0x88(%rbp), %esi
addl $0xa, %esi
movl -0x88(%rbp), %edx
addl $0xc, %edx
callq 0x40e0
movl %eax, -0x4(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x4fd7
jmp 0x4fb0
jmp 0x4fb2
movl -0x88(%rbp), %eax
addl $0xc, %eax
movl %eax, -0x88(%rbp)
jmp 0x4e8c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x84(%rbp)
movq -0x78(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)