// File: /workspace/github_cpp_makefile_star2_O1/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 $0xf8, %rsp
movq %rsi, %r14
movq %rdi, %r12
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
xorl %eax, %eax
movq %rax, -0x30(%rbp)
leaq -0x60(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
leaq -0x88(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rsi), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x98(%rbp), %rdi
callq 0x6c9e
leaq -0x98(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x2b4a
movl %eax, %ebx
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x54ae
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2200
testb %bl, %bl
je 0x54df
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x54ed
subq %rdi, %rax
movq %rax, %rcx
shrq $0x5, %rcx
cmpl $0x2, %ecx
jl 0x5560
leaq (%rdi,%rax), %rsi
addq $-0x20, %rsi
jmp 0x559e
movl $0x15, %edx
leaq 0x3fe1(%rip), %rsi      # 0x94cc
jmp 0x54f9
movl $0x13, %edx
leaq 0x3fe9(%rip), %rsi      # 0x94e2
movq 0x7ad0(%rip), %rbx      # 0xcfd0
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x5548
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x40(%rbp), %rdi
callq 0x6abe
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3b47(%rip), %rsi      # 0x90ae
callq 0x2090
testl %eax, %eax
jne 0x559a
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
cmpq $0x20, %rax
jne 0x559a
movl 0x30(%r12), %eax
movl %eax, 0x34(%r12)
addq $0x38, %r12
leaq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x6b24
jmp 0x552f
movq -0x40(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2110
movq -0x38(%rbp), %rax
leaq -0x20(%rax), %rcx
movq %rcx, -0x38(%rbp)
movq -0x20(%rax), %rdi
addq $-0x10, %rax
cmpq %rax, %rdi
je 0x55cb
movq (%rax), %rsi
incq %rsi
callq 0x2200
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0x55e9
leaq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x34c6
jmp 0x55ee
movl 0x34(%r12), %eax
cmpl $-0x1, %eax
je 0x5772
movq %rsp, -0x78(%rbp)
movl (%r12), %ecx
movq %rsp, %r15
addq $0xf, %rcx
andq $-0x10, %rcx
subq %rcx, %r15
movq %r15, %rsp
leaq -0x120(%rbp), %rsi
movq %r12, %rdi
movl %eax, %edx
callq 0x37c4
xorl %ecx, %ecx
leaq -0xf6(%rbp), %rax
xorl %edx, %edx
movsbl (%rax), %esi
shll %cl, %esi
addl %esi, %edx
incq %rax
addq $0x8, %rcx
cmpq $0x10, %rcx
jne 0x562c
leaq 0x8(%r12), %rdi
addl $0x6, %edx
movq %r15, %rsi
callq 0x26d0
movq %r12, -0x48(%rbp)
cmpl $0xd, (%r12)
jl 0x5700
movl $0xc, %r12d
leal -0x4(%r12), %ebx
leal -0x2(%r12), %r13d
movq %r15, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x2a38
testl %eax, %eax
je 0x56ef
leal -0xc(%r12), %edx
leaq -0xe0(%rbp), %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x2ab6
movq -0xd8(%rbp), %rdx
cmpq -0x68(%rbp), %rdx
jne 0x56bf
testq %rdx, %rdx
je 0x56c3
movq -0x70(%rbp), %rsi
movq -0xe0(%rbp), %rdi
callq 0x2210
testl %eax, %eax
sete %bl
jmp 0x56c5
xorl %ebx, %ebx
jmp 0x56c5
movb $0x1, %bl
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x56e7
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x2200
testb %bl, %bl
jne 0x57cd
addl $0xc, %r12d
movq -0x48(%rbp), %rax
cmpl (%rax), %r12d
jl 0x5665
movq 0x78c9(%rip), %rdi      # 0xcfd0
leaq 0x3ee9(%rip), %rsi      # 0x95f7
movl $0x13, %edx
callq 0x2240
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x78aa(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x3ed6(%rip), %rsi      # 0x960b
movl $0x10, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
movq -0x78(%rbp), %rsp
jmp 0x552f
movq 0x7857(%rip), %rdi      # 0xcfd0
leaq 0x3e77(%rip), %rsi      # 0x95f7
movl $0x13, %edx
callq 0x2240
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x7838(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x3e64(%rip), %rsi      # 0x960b
movl $0x10, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
jmp 0x551c
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x2a38
movl %eax, %ebx
leaq -0xe0(%rbp), %r12
movq -0x48(%rbp), %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x37c4
xorl %ecx, %ecx
xorl %eax, %eax
movsbl (%r12), %edx
shll %cl, %edx
addl %edx, %eax
addq $0x8, %rcx
incq %r12
cmpq $0x10, %rcx
jne 0x57f6
testl %eax, %eax
je 0x5954
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x6a80
movl %ebx, -0x4c(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x387f(%rip), %rsi      # 0x90ae
callq 0x2090
testl %eax, %eax
jne 0x588b
movq -0x48(%rbp), %r14
movl 0x30(%r14), %eax
movl %eax, 0x34(%r14)
movq 0x40(%r14), %rbx
subq 0x38(%r14), %rbx
shrq $0x5, %rbx
cmpl $0x2, %ebx
jl 0x587f
decl %ebx
movq 0x40(%r14), %rax
leaq -0x20(%rax), %rcx
movq %rcx, 0x40(%r14)
movq -0x20(%rax), %rdi
addq $-0x10, %rax
cmpq %rax, %rdi
je 0x587b
movq (%rax), %rsi
incq %rsi
callq 0x2200
decl %ebx
jne 0x5857
leaq -0x40(%rbp), %rdi
movq (%rdi), %rsi
callq 0x71b8
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0x5945
movq -0x48(%rbp), %rax
leaq 0x38(%rax), %r14
xorl %ebx, %ebx
leaq 0x3c17(%rip), %r12      # 0x94ca
leaq 0x3c0f(%rip), %r13      # 0x94c9
xorl %r15d, %r15d
addq %rbx, %rdi
movq %r12, %rsi
callq 0x2090
testl %eax, %eax
je 0x5924
movq -0x40(%rbp), %rdi
addq %rbx, %rdi
movq %r13, %rsi
callq 0x2090
testl %eax, %eax
je 0x58f0
movq -0x40(%rbp), %rsi
addq %rbx, %rsi
movq %r14, %rdi
callq 0x6a80
jmp 0x5924
movq -0x48(%rbp), %rcx
movl 0x34(%rcx), %eax
cmpl 0x30(%rcx), %eax
je 0x5924
movq -0x48(%rbp), %rdx
movq 0x40(%rdx), %rax
leaq -0x20(%rax), %rcx
movq %rcx, 0x40(%rdx)
movq -0x20(%rax), %rdi
addq $-0x10, %rax
cmpq %rax, %rdi
je 0x5924
movq (%rax), %rsi
incq %rsi
callq 0x2200
incq %r15
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
shrq $0x5, %rax
cltq
addq $0x20, %rbx
cmpq %rax, %r15
jl 0x58bd
movq -0x48(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %ecx, 0x34(%rax)
jmp 0x5769
movq 0x7675(%rip), %rdi      # 0xcfd0
leaq 0x3c79(%rip), %rsi      # 0x95db
movl $0x9, %edx
callq 0x2240
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x7656(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x3c93(%rip), %rsi      # 0x961c
movl $0x14, %edx
movq %rax, %rdi
callq 0x2240
movq %rbx, %rdi
callq 0x20f0
jmp 0x5769
jmp 0x59cf
jmp 0x59cf
jmp 0x59cf
movq %rax, %rbx
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x59d2
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x59d2
jmp 0x59cf
jmp 0x59cf
jmp 0x59cf
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x59eb
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x40(%rbp), %rdi
callq 0x6abe
movq %rbx, %rdi
callq 0x2350