// File: sidvishnoi[P]filesystem-assignment/day4/deleteFile.cpp
// Function: FileSystem::deleteFile(char const*)
int FileSystem::deleteFile(const char *title) {
    /*
     Objective: To a delete a file or remove it if already deleted
     Input parameters:
     title: File name
     Return:
     0: success
     1: error
     */
    TypeCastEntry entry;
    if (strlen(title) == 0) {
        // %% get file name from user in entry.entry.name
    } else {
        strcpy(entry.entry.name, title);
    }
    
    vector<int> position = alreadyExists(entry.entry.name, 'f');
    // %% use position to check if given file exists
    bool exists;
    if(!exists) {
        position = alreadyExists(entry.entry.name,'F');
        // %% check if still not found
        // %% display error message
        return 1;
    }
    
    char buffer[sectorSize_k];
    // %% read sector offset returned by alreadyExists() in "buffer"
    
    //reading file entry in entry.str
    for (int i = 0; i < 32; ++i) {
        entry.str[i] = buffer[position[1] + i];
    }
    
    int pos = entry.entry.startsAt;
    
    if(entry.entry.type == 'f') {
        do {
            int temp = getStatus(pos);
            // %% change the status of "pos" to its (-)ve
            pos = temp;
        } while(pos != 1);
        
        // file marked deleted
        entry.entry.type = 'F';
        
        // %% write the new updated entry at sector offset
        // %% print successful marked deleted message
        return 0;
    }
    
    // %% check if file is already deleted
    bool alreadyDeleted;
    
    // removing file permanently
    if (alreadyDeleted) {
        // %% repeat the procedure to delete a file (and set status with 0 for every sector occupied by file to mark it as free)
        
        //deleting file entry
        for (int i = 0; i < 32; ++i) {
            buffer[position[1] + i] = '\0';
        }
        
        // %% write the buffer at sector offset
        // %% print permanent delete message
        return 0;
    }
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x30d0
cmpq $0x0, %rax
jne 0xa60f
jmp 0xa620
leaq -0x3c(%rbp), %rdi
addq $0x10, %rdi
movq -0x18(%rbp), %rsi
callq 0x3220
movq -0xb8(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
addq $0x10, %rdx
leaq -0x58(%rbp), %rdi
movl $0x66, %ecx
callq 0x90d0
testb $0x1, -0x59(%rbp)
jne 0xa6a7
movq -0xb8(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x78(%rbp), %rdi
movl $0x46, %ecx
callq 0x90d0
jmp 0xa65e
leaq -0x58(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa840
leaq -0x78(%rbp), %rdi
callq 0x3db0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0xa81a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x3db0
jmp 0xa82b
movq -0xb8(%rbp), %rax
movl 0x20(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x90(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
cmpl $0x20, -0x9c(%rbp)
jge 0xa732
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x9c20
movq %rax, %rcx
movq -0xc0(%rbp), %rax
movl (%rcx), %ecx
addl -0x9c(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movslq -0x9c(%rbp), %rax
movb %cl, -0x3c(%rbp,%rax)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0xa6e7
movl -0x3c(%rbp), %eax
movl %eax, -0xa0(%rbp)
movsbl -0x34(%rbp), %eax
cmpl $0x66, %eax
jne 0xa798
jmp 0xa746
movq -0xb8(%rbp), %rdi
movl -0xa0(%rbp), %esi
callq 0x83a0
movl %eax, -0xc4(%rbp)
jmp 0xa760
movl -0xc4(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0xa0(%rbp)
cmpl $0x1, -0xa0(%rbp)
jne 0xa746
movb $0x46, -0x34(%rbp)
movl $0x0, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0xa810
testb $0x1, -0xa5(%rbp)
je 0xa7ff
movl $0x0, -0xac(%rbp)
cmpl $0x20, -0xac(%rbp)
jge 0xa7ec
leaq -0x58(%rbp), %rdi
movl $0x1, %esi
callq 0x9c20
movq %rax, %rcx
movq -0xc0(%rbp), %rax
movl (%rcx), %ecx
addl -0xac(%rbp), %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xa7ab
movl $0x0, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
jmp 0xa810
movl $0x1, -0x4(%rbp)
movl $0x1, -0x88(%rbp)
movq -0x90(%rbp), %rax
movq %rax, %rsp
leaq -0x58(%rbp), %rdi
callq 0x3db0
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x3450
nopw %cs:(%rax,%rax)
nop