// File: /workspace/github_cpp_makefile_star2_O2/hyp1231[P]FMSS/filesystem.cpp
// Function: FileSystem::OpenDir(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void FileSystem::OpenDir(const string &dirpath) {
    /*
        step 1: get to parent dir
     */

    vector<string> path;
    string dirname;
    int dir_inodeNum;
    if(!Decomposition_path(dirpath, path)) {
        cout << "[Error] Illegal path!" << endl;
        return ;
    }

    if(path.empty()) {
        cout << "[Error] Empty path!" << endl;
        return ;
    }

    if((int)path.size() >= 2) {
        dirname = path[path.size() - 1];
    } else {
        // if path is "/" (root directory), return
        if (path[0] == "/" && path.size() == 1) {
            cur_dir_inodeNum = root_dir_inodeNum;
            cur_path = path;
            return ;
        }
        dirname = path[0]; // omit path
    }
    path.pop_back();

    if((int)path.size() >= 1) {
        dir_inodeNum = Get_dir_inodeNum_from_path(path);
    } else {
        dir_inodeNum = cur_dir_inodeNum;    // omit path -> cur dir
    }
  
    if (dir_inodeNum == -1) {
        cout << "[Error] Directory \"" << dirpath << "\" doesn't exist!" << endl;
        return;
    }

    /*
        step 2: open dir
     */

    char inode_buf[64], dir_buf[BLKsize];
    Get_inode_from_inodeNum(inode_buf, dir_inodeNum);
    // get dir data block
    D.Getblk(dir_buf, Get_actual_dataBLKnumber(byte2int(inode_buf, 42, 44)));

    for (int i = 0; i + 12 < BLKsize; i += 12) {
        if (byte2int(dir_buf, i + 8, i + 10) != 0) {
            // find file that has "dirname" in dir_buf
            if (byte2string(dir_buf, i, i + 8) == dirname) {
                char tmp_buf[64];
                int tmp_inodeNum = byte2int(dir_buf, i + 10, i + 12);
                Get_inode_from_inodeNum(tmp_buf, tmp_inodeNum);

                // make sure the found file is a directory
                if (byte2int(tmp_buf, 0, 2) == 0) {
                    cout << "[Error] \"" << dirpath << "\" is not a directory" << endl;
                    return;
                }

                // the found file is a directory
                //      modify cur_path
                path.push_back(dirname);
                // if path starts with '/', it's a complete path
                if (path[0] == "/") {
                    cur_dir_inodeNum = root_dir_inodeNum;
                    int cur_path_size = cur_path.size();
                    for (int i = 0; i < cur_path_size - 1; i++)
                        cur_path.pop_back();
                    path.erase(path.begin());
                }
                for (int i = 0; i < (int)path.size(); i++) {
                    if (path[i] == ".") continue;
                    else if (path[i] == "..") {
                        if (cur_dir_inodeNum != root_dir_inodeNum)
                            cur_path.pop_back();
                    }                            
                    else
                        cur_path.push_back(path[i]);
                }

                //      modify cur_dir_inodeNum
                cur_dir_inodeNum = tmp_inodeNum;

                return ;
            }
        }
    }

    cout << "[Error] Directory \"" << dirpath << "\" doesn't exist!" << endl;
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, -0x30(%rbp)
andq $0x0, -0x40(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0xb0(%rbp), %rdi
movq %rsi, -0x60(%rbp)
callq 0x22b0
leaq -0xb0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x29e2
movl %eax, %ebx
leaq -0xb0(%rbp), %rdi
callq 0x20e0
movq 0x6b63(%rip), %rdi      # 0xafd0
testb %bl, %bl
je 0x4497
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
cmpq %rcx, %rax
je 0x44a3
subq %rax, %rcx
movq %rcx, %rdx
shrq $0x5, %rdx
cmpl $0x2, %edx
jl 0x44af
leaq (%rax,%rcx), %rsi
addq $-0x20, %rsi
jmp 0x44ef
leaq 0x302e(%rip), %rsi      # 0x74cc
jmp 0x4663
leaq 0x3038(%rip), %rsi      # 0x74e2
jmp 0x4663
leaq 0x2bf8(%rip), %rsi      # 0x70ae
movq %rax, %rdi
callq 0x54ac
movq -0x50(%rbp), %rsi
testb %al, %al
je 0x44ef
movq -0x48(%rbp), %rax
subq %rsi, %rax
cmpq $0x20, %rax
jne 0x44ef
movq -0x30(%rbp), %rdi
movl 0x30(%rdi), %eax
movl %eax, 0x34(%rdi)
addq $0x38, %rdi
leaq -0x50(%rbp), %rsi
callq 0x5532
jmp 0x4670
leaq -0x90(%rbp), %rdi
callq 0x2110
movq -0x48(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x48(%rbp)
callq 0x20e0
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0x452b
leaq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0x2f36
jmp 0x4532
movq -0x30(%rbp), %rax
movl 0x34(%rax), %eax
cmpl $-0x1, %eax
je 0x463a
movq %rsp, -0x70(%rbp)
movq -0x30(%rbp), %rdi
movl (%rdi), %ecx
movq %rsp, %rdx
addq $0xf, %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x58(%rbp)
movq %rdx, %rsp
leaq -0x130(%rbp), %rsi
movl %eax, %edx
callq 0x3176
leaq -0x130(%rbp), %rdi
pushq $0x2a
popq %rsi
pushq $0x2c
popq %rdx
callq 0x2917
movq -0x30(%rbp), %rcx
leaq 0x8(%rcx), %rdi
addl $0x6, %eax
movq -0x58(%rbp), %rsi
movl %eax, %edx
callq 0x2648
xorl %r13d, %r13d
leaq -0xf0(%rbp), %rbx
leal 0xc(%r13), %r14d
movq -0x30(%rbp), %rax
cmpl (%rax), %r14d
jge 0x45ff
leal 0x8(%r13), %r12d
leal 0xa(%r13), %edx
movq -0x58(%rbp), %rdi
movl %r12d, %esi
movl %edx, -0x64(%rbp)
callq 0x2917
testl %eax, %eax
je 0x45fa
movq %rbx, %r15
movq %rbx, %rdi
movq -0x58(%rbp), %rsi
movl %r13d, %edx
movl %r12d, %ecx
callq 0x2967
movq %r15, %rdi
leaq -0x90(%rbp), %rsi
callq 0x55fb
movl %eax, %r12d
movq %r15, %rdi
callq 0x20e0
testb %r12b, %r12b
jne 0x4694
movl %r14d, %r13d
jmp 0x459a
movq 0x69ca(%rip), %rdi      # 0xafd0
leaq 0x2fea(%rip), %rsi      # 0x75f7
callq 0x21d0
movq %rax, %rdi
movq -0x60(%rbp), %rsi
callq 0x21b0
leaq 0x2fe6(%rip), %rsi      # 0x760b
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
jmp 0x47ff
movq 0x698f(%rip), %rdi      # 0xafd0
leaq 0x2faf(%rip), %rsi      # 0x75f7
callq 0x21d0
movq %rax, %rdi
movq -0x60(%rbp), %rsi
callq 0x21b0
movq %rax, %rdi
leaq 0x2fa8(%rip), %rsi      # 0x760b
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
leaq -0x90(%rbp), %rdi
callq 0x20e0
leaq -0x50(%rbp), %rdi
callq 0x5480
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x58(%rbp), %rdi
movl -0x64(%rbp), %esi
movl %r14d, %edx
callq 0x2917
movl %eax, %r15d
leaq -0xf0(%rbp), %rsi
movq -0x30(%rbp), %rdi
movl %eax, %edx
callq 0x3176
leaq -0xf0(%rbp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x2917
testl %eax, %eax
je 0x4729
leaq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x5454
movq -0x50(%rbp), %rdi
leaq 0x29c6(%rip), %rsi      # 0x70ae
callq 0x54ac
testb %al, %al
je 0x4770
movq -0x30(%rbp), %rcx
movl 0x30(%rcx), %eax
movl %eax, 0x34(%rcx)
leaq 0x38(%rcx), %rbx
movq 0x40(%rcx), %rax
subq 0x38(%rcx), %rax
shrq $0x5, %rax
cmpl $0x2, %eax
pushq $0x1
popq %r14
cmovgel %eax, %r14d
decl %r14d
subl $0x1, %r14d
jb 0x4764
movq %rbx, %rdi
callq 0x551e
jmp 0x4719
movq 0x68a0(%rip), %rdi      # 0xafd0
leaq 0x2ea4(%rip), %rsi      # 0x75db
callq 0x21d0
movq %rax, %rdi
movq -0x60(%rbp), %rsi
callq 0x21b0
leaq 0x2ecd(%rip), %rsi      # 0x761c
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
jmp 0x47ff
leaq -0x50(%rbp), %rdi
movq (%rdi), %rsi
callq 0x54ba
movl %r15d, -0x58(%rbp)
movq -0x30(%rbp), %rax
leaq 0x38(%rax), %r14
xorl %ebx, %ebx
leaq 0x2d45(%rip), %r12      # 0x74ca
leaq 0x2d3d(%rip), %r13      # 0x74c9
xorl %r15d, %r15d
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rax
subq %rdi, %rax
shrq $0x5, %rax
cltq
cmpq %rax, %r15
jge 0x47f5
addq %rbx, %rdi
movq %r12, %rsi
callq 0x54ac
testb %al, %al
jne 0x47ec
movq -0x50(%rbp), %rdi
addq %rbx, %rdi
movq %r13, %rsi
callq 0x54ac
testb %al, %al
je 0x47dd
movq -0x30(%rbp), %rcx
movl 0x34(%rcx), %eax
cmpl 0x30(%rcx), %eax
je 0x47ec
movq %r14, %rdi
callq 0x551e
jmp 0x47ec
movq -0x50(%rbp), %rsi
addq %rbx, %rsi
movq %r14, %rdi
callq 0x5454
incq %r15
addq $0x20, %rbx
jmp 0x478f
movq -0x30(%rbp), %rax
movl -0x58(%rbp), %ecx
movl %ecx, 0x34(%rax)
movq -0x70(%rbp), %rsp
jmp 0x4670
jmp 0x4825
jmp 0x4825
jmp 0x4825
jmp 0x4825
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x20e0
jmp 0x4828
jmp 0x4825
jmp 0x4825
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x20e0
leaq -0x50(%rbp), %rdi
callq 0x5480
movq %rbx, %rdi
callq 0x2340
nop