// File: sidvishnoi[P]filesystem-assignment/day3/alreadyExists.cpp
// Function: FileSystem::alreadyExists(char*, char)
vector<int> FileSystem::alreadyExists(char *name, char type) {
    /*
     Objective: To check whether a file/dir already exists in current working directory
     Input Parameters:
        name: dir/file name
        type: type (d = dir, f = file)
     Return value: vector<int>
        {-1,-1,0} if not found
        {sector_offset, byte_offset, 1} otherwise
     */
    TypeCastEntry cast;
    char buffer[sectorSize_k];
    int next = reservedSectors_k;
    for(int count = 0; count < sectorsForDir_k; ++count) {
        readSector(next, buffer);
        for (int i = 0; i < sectorSize_k/32; ++i) {
            for (int j = 0; j < 32; ++j) {
                cast.str[j] = buffer[32*i + j];
            }
            // %% check if file with same characteristics exists
            if (true /* %%  CONDITION */) {
                vector<int> offset = {next, 32*i, 1};
                return offset;
            }
        }
        next = getStatus(next);
    }
    vector<int> offset = {-1,-1,0};
    return offset;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0xe0(%rbp)
movb %cl, %al
movq %rdi, %rcx
movq %rcx, -0xd8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xd0(%rbp)
movl 0x20(%rax), %ecx
movq %rsp, %rdx
movq %rdx, -0x48(%rbp)
movq %rcx, %rsi
addq $0xf, %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0xc8(%rbp)
movq %rdx, %rsp
movq %rcx, -0x50(%rbp)
movl 0x28(%rax), %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movq -0xd0(%rbp), %rcx
movl -0x58(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x92a5
movq -0xc8(%rbp), %rdx
movq -0xd0(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x8290
movl $0x0, -0x5c(%rbp)
movq -0xd0(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, -0xe4(%rbp)
movl 0x20(%rax), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xe4(%rbp), %eax
cmpl %ecx, %eax
jge 0x9285
movl $0x0, -0x60(%rbp)
cmpl $0x20, -0x60(%rbp)
jge 0x91cf
movq -0xc8(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll $0x5, %ecx
addl -0x60(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movslq -0x60(%rbp), %rax
movb %cl, -0x40(%rbp,%rax)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x91a0
movb $0x0, -0x61(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x5c(%rbp), %eax
shll $0x5, %eax
movl %eax, -0x80(%rbp)
movl $0x1, -0x7c(%rbp)
leaq -0x84(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x3, -0x70(%rbp)
leaq -0x85(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x7b90
movq -0xe0(%rbp), %rdi
movq -0xf0(%rbp), %rcx
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x9390
jmp 0x922f
leaq -0x85(%rbp), %rdi
callq 0x41a0
movb $0x1, -0x61(%rbp)
movl $0x1, -0x98(%rbp)
testb $0x1, -0x61(%rbp)
jne 0x9280
jmp 0x9274
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x85(%rbp), %rdi
callq 0x41a0
jmp 0x9382
movq -0xe0(%rbp), %rdi
callq 0x3db0
jmp 0x936f
movq -0xd0(%rbp), %rdi
movl -0x54(%rbp), %esi
callq 0x83a0
movl %eax, -0x54(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x913e
movb $0x0, -0x99(%rbp)
movl $0xffffffff, -0xbc(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0xb8(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xb4(%rbp)
leaq -0xbc(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq $0x3, -0xa8(%rbp)
leaq -0xbd(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x7b90
movq -0xe0(%rbp), %rdi
movq -0xf8(%rbp), %rcx
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
callq 0x9390
jmp 0x9319
leaq -0xbd(%rbp), %rdi
callq 0x41a0
movb $0x1, -0x99(%rbp)
movl $0x1, -0x98(%rbp)
testb $0x1, -0x99(%rbp)
jne 0x936d
jmp 0x9361
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0xbd(%rbp), %rdi
callq 0x41a0
jmp 0x9382
movq -0xe0(%rbp), %rdi
callq 0x3db0
jmp 0x936f
movq -0xd8(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x90(%rbp), %rdi
callq 0x3450
nop