// File: sidvishnoi[P]filesystem-assignment/day5/changeDir.cpp
// Function: FileSystem::changeDir(char const*)
int FileSystem::changeDir(const char *title) {
    /*
    objective: to change the current working directory
    input: (title) directory to change to
    return:
        0: success
        1: fail (child directory does not exist in current directory)
    effect: the currentDir and current path (PATH_ID and PATH_NAME) are updated
    */

    if (!strcmp(title, "..")) {
        if (!PATH_ID.empty()) {
            currentDir = PATH_ID.back();
            PATH_NAME.pop_back();
            PATH_ID.pop_back();
            return 0;
        }
        return 1;
    }

    if (!strcmp(title, "/")) {
        currentDir = reservedSectors_k;
        PATH_ID.clear();
        PATH_NAME.clear();
        return 0;
    }

    vector<int> offset; // %% value returned by alreadyExists()

    // %% use alreadyExists() to check if given dir. exists
    bool dirExists;
    if (!dirExists) {
        // %% print error message
        return 1;
    }

    TypeCastEntry entry;
    char buffer[sectorSize_k];
    readSector(offset[0], buffer);

    // %% read 32-bytes corresponding to this entry into entry.str

    PATH_ID.push_back(currentDir);
    PATH_NAME.push_back(entry.entry.name);
    currentDir = entry.entry.startsAt;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x153c(%rip), %rsi      # 0xc3d5
callq 0x3380
cmpl $0x0, %eax
jne 0xaf04
movq -0xb0(%rbp), %rdi
addq $0x38, %rdi
callq 0x87e0
testb $0x1, %al
jne 0xaef8
movq -0xb0(%rbp), %rdi
addq $0x38, %rdi
callq 0x8a80
movq -0xb0(%rbp), %rdi
movl (%rax), %eax
movl %eax, 0x34(%rdi)
addq $0x50, %rdi
callq 0xb0e0
movq -0xb0(%rbp), %rdi
addq $0x38, %rdi
callq 0xb110
movl $0x0, -0x4(%rbp)
jmp 0xb0cf
movl $0x1, -0x4(%rbp)
jmp 0xb0cf
movq -0x18(%rbp), %rdi
leaq 0x14c9(%rip), %rsi      # 0xc3d8
callq 0x3380
cmpl $0x0, %eax
jne 0xaf4b
movq -0xb0(%rbp), %rdi
movl 0x28(%rdi), %eax
movl %eax, 0x34(%rdi)
addq $0x38, %rdi
callq 0xb140
movq -0xb0(%rbp), %rdi
addq $0x50, %rdi
callq 0xb160
movl $0x0, -0x4(%rbp)
jmp 0xb0cf
leaq -0x30(%rbp), %rdi
callq 0x7aa0
testb $0x1, -0x31(%rbp)
jne 0xaf6d
movl $0x1, -0x4(%rbp)
movl $0x1, -0x38(%rbp)
jmp 0xb0b9
movq -0xb0(%rbp), %rax
movl 0x20(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x68(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rax, -0x70(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x9c20
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rdx
movl (%rax), %esi
callq 0x8290
jmp 0xafc1
movq -0xb0(%rbp), %rsi
movq %rsi, %rdi
addq $0x38, %rdi
addq $0x34, %rsi
callq 0xb180
jmp 0xafda
movq -0xb0(%rbp), %rax
addq $0x50, %rax
movq %rax, -0xd0(%rbp)
leaq -0x4c(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x3460
movq -0xc8(%rbp), %rsi
movq -0xc0(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
callq 0x63a0
jmp 0xb026
movq -0xd0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xb1f0
jmp 0xb03b
leaq -0xa0(%rbp), %rdi
callq 0x3150
leaq -0xa1(%rbp), %rdi
callq 0x3280
movq -0xb0(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, 0x34(%rax)
movl $0x0, -0x4(%rbp)
movl $0x1, -0x38(%rbp)
movq -0x68(%rbp), %rax
movq %rax, %rsp
jmp 0xb0b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xb0c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xb0ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3150
leaq -0xa1(%rbp), %rdi
callq 0x3280
jmp 0xb0c4
leaq -0x30(%rbp), %rdi
callq 0x3db0
jmp 0xb0cf
leaq -0x30(%rbp), %rdi
callq 0x3db0
jmp 0xb0d7
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x3450