// File: 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
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x9260
leaq -0x48(%rbp), %rdi
callq 0x33e0
movq -0x10(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x30f0
jmp 0x739f
leaq -0x70(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x4280
movb %al, -0x179(%rbp)
jmp 0x73b4
movb -0x179(%rbp), %al
xorb $-0x1, %al
movb %al, -0x17a(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3160
movb -0x17a(%rbp), %al
testb $0x1, %al
jne 0x73d7
jmp 0x743f
movq 0xdbf2(%rip), %rdi      # 0x14fd0
leaq 0x8102(%rip), %rsi      # 0xf4e7
callq 0x3250
movq %rax, -0x188(%rbp)
jmp 0x73f3
movq -0x188(%rbp), %rdi
movq 0xdbbf(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x7408
movl $0x1, -0x80(%rbp)
jmp 0x7ae5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x7afc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3160
jmp 0x7afc
leaq -0x28(%rbp), %rdi
callq 0x92e0
testb $0x1, %al
jne 0x744e
jmp 0x748b
movq 0xdb7b(%rip), %rdi      # 0x14fd0
leaq 0x80a1(%rip), %rsi      # 0xf4fd
callq 0x3250
movq %rax, -0x190(%rbp)
jmp 0x746a
movq -0x190(%rbp), %rdi
movq 0xdb48(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x747f
movl $0x1, -0x80(%rbp)
jmp 0x7ae5
leaq -0x28(%rbp), %rdi
callq 0x9370
cmpl $0x2, %eax
jl 0x74ce
leaq -0x28(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0x9370
movq -0x198(%rbp), %rdi
movq %rax, %rsi
decq %rsi
callq 0x9350
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
callq 0x3040
jmp 0x74c9
jmp 0x7552
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0x9350
movq %rax, %rdi
leaq 0x7bc9(%rip), %rsi      # 0xf0ae
callq 0x9320
movb %al, -0x199(%rbp)
jmp 0x74f2
movb -0x199(%rbp), %al
testb $0x1, %al
jne 0x74fe
jmp 0x7535
leaq -0x28(%rbp), %rdi
callq 0x9370
cmpq $0x1, %rax
jne 0x7535
movq -0x178(%rbp), %rdi
movl 0x30(%rdi), %eax
movl %eax, 0x34(%rdi)
addq $0x38, %rdi
leaq -0x28(%rbp), %rsi
callq 0x9640
jmp 0x7529
movl $0x1, -0x80(%rbp)
jmp 0x7ae5
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0x9350
movq %rax, %rsi
leaq -0x48(%rbp), %rdi
callq 0x3040
jmp 0x7550
jmp 0x7552
leaq -0x28(%rbp), %rdi
callq 0x9610
leaq -0x28(%rbp), %rdi
callq 0x9370
cmpl $0x1, %eax
jl 0x758c
movq -0x178(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x4be0
movl %eax, -0x1a0(%rbp)
jmp 0x7581
movl -0x1a0(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x7599
movq -0x178(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0x7611
movq 0xda2a(%rip), %rdi      # 0x14fd0
leaq 0x8065(%rip), %rsi      # 0xf612
callq 0x3250
movq %rax, -0x1a8(%rbp)
jmp 0x75bb
movq -0x1a8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3230
movq %rax, -0x1b0(%rbp)
jmp 0x75d4
movq -0x1b0(%rbp), %rdi
leaq 0x8044(%rip), %rsi      # 0xf626
callq 0x3250
movq %rax, -0x1b8(%rbp)
jmp 0x75f0
movq -0x1b8(%rbp), %rdi
movq 0xd9c2(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x7605
movl $0x1, -0x80(%rbp)
jmp 0x7ae5
movq -0x178(%rbp), %rdi
movl (%rdi), %eax
movq %rsp, %rcx
movq %rcx, -0xc8(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1c0(%rbp)
movq %rcx, %rsp
movq %rax, -0xd0(%rbp)
movl -0x4c(%rbp), %edx
leaq -0xc0(%rbp), %rsi
callq 0x4ff0
jmp 0x7657
movq -0x178(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1d0(%rbp)
leaq -0xc0(%rbp), %rdi
movl $0x2a, %esi
movl $0x2c, %edx
callq 0x40e0
movl %eax, -0x1c4(%rbp)
jmp 0x7687
movl -0x1c4(%rbp), %esi
movq -0x178(%rbp), %rdi
callq 0x9390
movl %eax, -0x1d4(%rbp)
jmp 0x76a1
movl -0x1d4(%rbp), %edx
movq -0x1c0(%rbp), %rsi
movq -0x1d0(%rbp), %rdi
callq 0x3900
jmp 0x76bc
movl $0x0, -0xd4(%rbp)
movq -0x178(%rbp), %rcx
movl -0xd4(%rbp), %eax
addl $0xc, %eax
cmpl (%rcx), %eax
jge 0x7a6e
movq -0x1c0(%rbp), %rdi
movl -0xd4(%rbp), %edx
movl %edx, %esi
addl $0x8, %esi
addl $0xa, %edx
callq 0x40e0
movl %eax, -0x1d8(%rbp)
jmp 0x7700
movl -0x1d8(%rbp), %eax
cmpl $0x0, %eax
je 0x7a58
movq -0x1c0(%rbp), %rsi
movl -0xd4(%rbp), %edx
movl %edx, %ecx
addl $0x8, %ecx
leaq -0xf8(%rbp), %rdi
callq 0x4180
jmp 0x772f
leaq -0xf8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x9890
movb %al, -0x1d9(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3160
movb -0x1d9(%rbp), %al
testb $0x1, %al
jne 0x7760
jmp 0x7a56
movq -0x1c0(%rbp), %rdi
movl -0xd4(%rbp), %edx
movl %edx, %esi
addl $0xa, %esi
addl $0xc, %edx
callq 0x40e0
movl %eax, -0x1e0(%rbp)
jmp 0x7782
movq -0x178(%rbp), %rdi
movl -0x1e0(%rbp), %eax
movl %eax, -0x144(%rbp)
movl -0x144(%rbp), %edx
leaq -0x140(%rbp), %rsi
callq 0x4ff0
jmp 0x77a9
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x40e0
movl %eax, -0x1e4(%rbp)
jmp 0x77c4
movl -0x1e4(%rbp), %eax
cmpl $0x0, %eax
jne 0x7841
movq 0xd7fa(%rip), %rdi      # 0x14fd0
leaq 0x7e19(%rip), %rsi      # 0xf5f6
callq 0x3250
movq %rax, -0x1f0(%rbp)
jmp 0x77eb
movq -0x1f0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3230
movq %rax, -0x1f8(%rbp)
jmp 0x7804
movq -0x1f8(%rbp), %rdi
leaq 0x7e25(%rip), %rsi      # 0xf637
callq 0x3250
movq %rax, -0x200(%rbp)
jmp 0x7820
movq -0x200(%rbp), %rdi
movq 0xd792(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x7835
movl $0x1, -0x80(%rbp)
jmp 0x7adb
leaq -0x28(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x91d0
jmp 0x7850
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0x9350
movq %rax, %rdi
leaq 0x7847(%rip), %rsi      # 0xf0ae
callq 0x9320
movb %al, -0x201(%rbp)
jmp 0x7874
movb -0x201(%rbp), %al
testb $0x1, %al
jne 0x7883
jmp 0x7931
movq -0x178(%rbp), %rdi
movl 0x30(%rdi), %eax
movl %eax, 0x34(%rdi)
addq $0x38, %rdi
callq 0x9370
movl %eax, -0x148(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movl -0x148(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x78dd
movq -0x178(%rbp), %rdi
addq $0x38, %rdi
callq 0x9610
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0x78a9
leaq -0x28(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x9440
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x9470
movq -0x218(%rbp), %rdi
movq -0x158(%rbp), %rsi
callq 0x93d0
movq %rax, -0x210(%rbp)
jmp 0x7923
movq -0x210(%rbp), %rax
movq %rax, -0x168(%rbp)
movl $0x0, -0x16c(%rbp)
movl -0x16c(%rbp), %eax
movl %eax, -0x21c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x9370
movq %rax, %rcx
movl -0x21c(%rbp), %eax
cmpl %ecx, %eax
jge 0x7a3a
movslq -0x16c(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x9350
movq %rax, %rdi
leaq 0x7b6a(%rip), %rsi      # 0xf4e5
callq 0x9320
movb %al, -0x21d(%rbp)
jmp 0x7988
movb -0x21d(%rbp), %al
testb $0x1, %al
jne 0x7994
jmp 0x7999
jmp 0x7a26
movslq -0x16c(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x9350
movq %rax, %rdi
leaq 0x7b31(%rip), %rsi      # 0xf4e4
callq 0x9320
movb %al, -0x21e(%rbp)
jmp 0x79c0
movb -0x21e(%rbp), %al
testb $0x1, %al
jne 0x79cc
jmp 0x79ed
movq -0x178(%rbp), %rcx
movl 0x34(%rcx), %eax
cmpl 0x30(%rcx), %eax
je 0x79eb
movq -0x178(%rbp), %rdi
addq $0x38, %rdi
callq 0x9610
jmp 0x7a22
movq -0x178(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x228(%rbp)
movslq -0x16c(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x9350
movq -0x228(%rbp), %rdi
movq %rax, %rsi
callq 0x91d0
jmp 0x7a20
jmp 0x7a22
jmp 0x7a24
jmp 0x7a26
movl -0x16c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x16c(%rbp)
jmp 0x793b
movq -0x178(%rbp), %rax
movl -0x144(%rbp), %ecx
movl %ecx, 0x34(%rax)
movl $0x1, -0x80(%rbp)
jmp 0x7adb
jmp 0x7a58
jmp 0x7a5a
movl -0xd4(%rbp), %eax
addl $0xc, %eax
movl %eax, -0xd4(%rbp)
jmp 0x76c6
movq 0xd55b(%rip), %rdi      # 0x14fd0
leaq 0x7b96(%rip), %rsi      # 0xf612
callq 0x3250
movq %rax, -0x230(%rbp)
jmp 0x7a8a
movq -0x230(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3230
movq %rax, -0x238(%rbp)
jmp 0x7aa3
movq -0x238(%rbp), %rdi
leaq 0x7b75(%rip), %rsi      # 0xf626
callq 0x3250
movq %rax, -0x240(%rbp)
jmp 0x7abf
movq -0x240(%rbp), %rdi
movq 0xd4f3(%rip), %rsi      # 0x14fc0
callq 0x32a0
jmp 0x7ad4
movl $0x1, -0x80(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, %rsp
leaq -0x48(%rbp), %rdi
callq 0x3160
leaq -0x28(%rbp), %rdi
callq 0x9280
movq %rbp, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x3160
leaq -0x28(%rbp), %rdi
callq 0x9280
movq -0x78(%rbp), %rdi
callq 0x3490
nopw (%rax,%rax)