// File: sidvishnoi[P]filesystem-assignment/day3/getEntryPosition.cpp
// Function: FileSystem::getEntryPosition()
vector<int> FileSystem::getEntryPosition() {
    /*
    Objective: return the offsets for free position for file/directory entry
    Input Parameters:
        None
    return:
        vector [sectorOffset, byteOffset] if space available
            else: [-1, -1]
    */
    char buffer[sectorSize_k];
    TypeCastEntry cast;

    // start looking from first non-reserved sector
    // (or current working directory)
    int current = reservedSectors_k;
    for (int s = 0; s < sectorsForDir_k; ++s) {
        readSector(current, buffer);
        for (int b = 0; b < sectorSize_k; b += 32) {
            for (int k = 0; k < 32; ++k) {
                cast.str[k] = buffer[b+k];
            }
            if (strlen(cast.entry.name) == 0) {
                // found free space
                vector<int> offsets = {current, b};
                return offsets;
            }
        }
        // next sector to look in
        current = getStatus(current);
    }
    // no position found
    vector<int> offsets = {-1, -1};
    return offsets;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0xd0(%rbp)
movq %rdi, %rax
movq %rax, -0xc8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl 0x20(%rax), %ecx
movq %rsp, %rdx
movq %rdx, -0x18(%rbp)
movq %rcx, %rsi
addq $0xf, %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0xb8(%rbp)
movq %rdx, %rsp
movq %rcx, -0x20(%rbp)
movl 0x28(%rax), %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0xc0(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x9a9c
movq -0xb8(%rbp), %rdx
movq -0xc0(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x8290
movl $0x0, -0x50(%rbp)
movq -0xc0(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl 0x20(%rcx), %eax
jge 0x9a7c
movl $0x0, -0x54(%rbp)
cmpl $0x20, -0x54(%rbp)
jge 0x99bb
movq -0xb8(%rbp), %rax
movl -0x50(%rbp), %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movslq -0x54(%rbp), %rax
movb %cl, -0x44(%rbp,%rax)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x998f
leaq -0x44(%rbp), %rdi
addq $0x10, %rdi
callq 0x30d0
cmpq $0x0, %rax
jne 0x9a6c
movb $0x0, -0x55(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x6c(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x2, -0x60(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x7b90
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rcx
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x9390
jmp 0x9a1f
leaq -0x71(%rbp), %rdi
callq 0x41a0
movb $0x1, -0x55(%rbp)
movl $0x1, -0x88(%rbp)
testb $0x1, -0x55(%rbp)
jne 0x9a67
jmp 0x9a5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x71(%rbp), %rdi
callq 0x41a0
jmp 0x9b6c
movq -0xd0(%rbp), %rdi
callq 0x3db0
jmp 0x9b59
jmp 0x9a6e
movl -0x50(%rbp), %eax
addl $0x20, %eax
movl %eax, -0x50(%rbp)
jmp 0x9975
movq -0xc0(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x83a0
movl %eax, -0x48(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x9945
movb $0x0, -0x89(%rbp)
movl $0xffffffff, -0xa8(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xa4(%rbp) # imm = 0xFFFFFFFF
leaq -0xa8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x2, -0x98(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x7b90
movq -0xd0(%rbp), %rdi
movq -0xe0(%rbp), %rcx
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
callq 0x9390
jmp 0x9b06
leaq -0xa9(%rbp), %rdi
callq 0x41a0
movb $0x1, -0x89(%rbp)
movl $0x1, -0x88(%rbp)
testb $0x1, -0x89(%rbp)
jne 0x9b57
jmp 0x9b4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xa9(%rbp), %rdi
callq 0x41a0
jmp 0x9b6c
movq -0xd0(%rbp), %rdi
callq 0x3db0
jmp 0x9b59
movq -0xc8(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x3450
nopw %cs:(%rax,%rax)
nop