// File: /workspace/github_cpp_makefile_star2_O3/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, %rdx
movq %rdi, %r14
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 %rdx, -0x48(%rbp)
movq 0x8(%rdx), %rdx
addq %rsi, %rdx
leaq -0x98(%rbp), %rdi
callq 0x7268
leaq -0x98(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x2b48
movl %eax, %ebx
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x5a1a
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2200
testb %bl, %bl
je 0x5a4b
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x5a59
subq %rdi, %rax
movq %rax, %rcx
shrq $0x5, %rcx
cmpl $0x2, %ecx
jl 0x5acc
leaq (%rdi,%rax), %rsi
addq $-0x20, %rsi
jmp 0x5b07
movl $0x15, %edx
leaq 0x3b06(%rip), %rsi      # 0x955d
jmp 0x5a65
movl $0x13, %edx
leaq 0x3b0e(%rip), %rsi      # 0x9573
movq 0x7564(%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 0x5ab4
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x40(%rbp), %rdi
callq 0x7068
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x366c(%rip), %rsi      # 0x913f
callq 0x2090
movq -0x40(%rbp), %rsi
testl %eax, %eax
jne 0x5b07
movq -0x38(%rbp), %rax
subq %rsi, %rax
cmpq $0x20, %rax
jne 0x5b07
movl 0x30(%r14), %eax
movl %eax, 0x34(%r14)
addq $0x38, %r14
leaq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x70d2
jmp 0x5a9b
leaq -0x70(%rbp), %rdi
callq 0x2110
movq -0x38(%rbp), %rcx
leaq -0x20(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rcx), %rdi
addq $-0x10, %rcx
cmpq %rcx, %rdi
je 0x5b38
movq (%rcx), %rsi
incq %rsi
callq 0x2200
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0x5b52
leaq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x34c4
jmp 0x5b56
movl 0x34(%r14), %eax
cmpl $-0x1, %eax
je 0x5cda
movq %rsp, -0x78(%rbp)
movl (%r14), %ecx
movq %rsp, %r15
addq $0xf, %rcx
andq $-0x10, %rcx
subq %rcx, %r15
movq %r15, %rsp
leaq -0x120(%rbp), %rsi
movq %r14, %rdi
movl %eax, %edx
callq 0x3d48
movsbl -0xf6(%rbp), %eax
movsbl -0xf5(%rbp), %ecx
shll $0x8, %ecx
leaq 0x8(%r14), %rdi
leal (%rcx,%rax), %edx
addl $0x6, %edx
movq %r15, %rsi
callq 0x26e0
cmpl $0xd, (%r14)
jl 0x5c64
movl $0xc, %r12d
movq %r14, -0x50(%rbp)
leal -0x4(%r12), %ebx
leal -0x2(%r12), %r13d
movq %r15, %rdi
movl %ebx, %esi
movl %r13d, %edx
callq 0x2a36
testl %eax, %eax
je 0x5c57
leal -0xc(%r12), %edx
leaq -0xe0(%rbp), %rdi
movq %r15, %rsi
movl %ebx, %ecx
callq 0x2ab4
movq -0xd8(%rbp), %rdx
cmpq -0x68(%rbp), %rdx
jne 0x5c1d
movq -0xe0(%rbp), %rbx
testq %rdx, %rdx
je 0x5c29
movq -0x70(%rbp), %rsi
movq %rbx, %rdi
callq 0x2210
testl %eax, %eax
sete %r14b
jmp 0x5c2c
movq -0xe0(%rbp), %rbx
xorl %r14d, %r14d
jmp 0x5c2c
movb $0x1, %r14b
leaq -0xd0(%rbp), %rax
cmpq %rax, %rbx
je 0x5c4a
movq -0xd0(%rbp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x2200
testb %r14b, %r14b
movq -0x50(%rbp), %r14
jne 0x5d39
addl $0xc, %r12d
cmpl (%r14), %r12d
jl 0x5bbf
movq 0x7365(%rip), %rdi      # 0xcfd0
leaq 0x3a16(%rip), %rsi      # 0x9688
movl $0x13, %edx
callq 0x2240
movq -0x48(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x7342(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x39ff(%rip), %rsi      # 0x969c
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 0x5a9b
movq 0x72ef(%rip), %rdi      # 0xcfd0
leaq 0x39a0(%rip), %rsi      # 0x9688
movl $0x13, %edx
callq 0x2240
movq -0x48(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x72cc(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x3989(%rip), %rsi      # 0x969c
movl $0x10, %edx
movq %rax, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
jmp 0x5a88
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x2a36
movl %eax, %ebx
leaq -0xe0(%rbp), %rsi
movq %r14, %rdi
movl %eax, %edx
callq 0x3d48
movsbl -0xe0(%rbp), %eax
movsbl -0xdf(%rbp), %ecx
shll $0x8, %ecx
addl %eax, %ecx
jne 0x5dc2
movq 0x725a(%rip), %rdi      # 0xcfd0
leaq 0x38ef(%rip), %rsi      # 0x966c
movl $0x9, %edx
callq 0x2240
movq -0x48(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x7237(%rip), %rdi      # 0xcfd0
callq 0x2240
movq %rax, %rbx
leaq 0x3905(%rip), %rsi      # 0x96ad
movl $0x14, %edx
movq %rax, %rdi
callq 0x2240
movq %rbx, %rdi
callq 0x20f0
jmp 0x5cd1
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x702a
movl %ebx, -0x48(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x3362(%rip), %rsi      # 0x913f
callq 0x2090
testl %eax, %eax
jne 0x5e35
movl 0x30(%r14), %eax
movl %eax, 0x34(%r14)
movq 0x40(%r14), %rbx
subq 0x38(%r14), %rbx
shrq $0x5, %rbx
cmpl $0x2, %ebx
jl 0x5e29
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 0x5e25
movq (%rax), %rsi
incq %rsi
callq 0x2200
decl %ebx
jne 0x5e01
leaq -0x40(%rbp), %rdi
movq (%rdi), %rsi
callq 0x775a
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
subq %rdi, %rax
shrq $0x5, %rax
testl %eax, %eax
jle 0x5eef
movq -0x50(%rbp), %rax
leaq 0x38(%rax), %r14
xorl %ebx, %ebx
leaq 0x36fe(%rip), %r12      # 0x955b
leaq 0x36f6(%rip), %r13      # 0x955a
xorl %r15d, %r15d
addq %rbx, %rdi
movq %r12, %rsi
callq 0x2090
testl %eax, %eax
je 0x5ece
movq -0x40(%rbp), %rdi
addq %rbx, %rdi
movq %r13, %rsi
callq 0x2090
testl %eax, %eax
je 0x5e9a
movq -0x40(%rbp), %rsi
addq %rbx, %rsi
movq %r14, %rdi
callq 0x702a
jmp 0x5ece
movq -0x50(%rbp), %rcx
movl 0x34(%rcx), %eax
cmpl 0x30(%rcx), %eax
je 0x5ece
movq -0x50(%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 0x5ece
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 0x5e67
movq -0x50(%rbp), %rax
movl -0x48(%rbp), %ecx
movl %ecx, 0x34(%rax)
jmp 0x5cd1
jmp 0x5f2a
jmp 0x5f2a
jmp 0x5f2a
movq %rax, %rbx
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x5f2d
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x5f2d
jmp 0x5f2a
jmp 0x5f2a
jmp 0x5f2a
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x5f46
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x40(%rbp), %rdi
callq 0x7068
movq %rbx, %rdi
callq 0x2350
nop