// File: sidvishnoi[P]filesystem-assignment/day5/createDir.cpp
// Function: FileSystem::createDir(char const*)
int FileSystem::createDir(const char *title) {
    /*
     Objective: To create a new directory in current working directory
     Input parameters:
     title:  directory name
     Return value:
     0: success
     1: failure
     Effect: a new directory is created in filesystem and it's entry is passed in current working directory
     */
    
    TypeCastEntry entry;
    if (strlen(title) == 0) {
        // %% get file name from user and store it in entry.entry.name
    } else {
        strcpy(entry.entry.name, title);
    }
    
    /* %% use alreadyExists() to check for duplicate entry */
    bool dirAlreadyExists; // %%
    
    if (dirAlreadyExists) {
        cout << "ERROR: Directory <" <<
        entry.entry.name <<
        "> already exists!" << endl;
        return 1;
    }
    
    vector<int>offset = getEntryPosition();
    /* %% use offset to check if space for dir. entry is available */
    
    bool enoughSpaceForEntry;
    if(!enoughSpaceForEntry) {
        cout << "ERROR: Not enough space for directory entry." << endl;
        return 1;
    }
    
    vector<int> sectorsFree = findEmptySectors(sectorsForDir_k);
    // %% check if vector returned is empty
    
    bool enoughSectors;
    if (!enoughSectors) {
        cout << "ERROR: Not enough memory!" << endl;
        return 1;
    }
    
    /* %% fill details of dir. entry (parent, size, startsAt, type)
     Hint: entry.entry.type = 'd'
     entry.entry.startsAt = first sector we found free
     */
    
    // Directory entry
    writeEntry(offset, entry.str);
    
    for (int i = 0; i < sectorsForDir_k; ++i) {
        
        // we use index of next sector as status of sector to point next continuous location
        //   last sector's gets status = 1
        setStatus(sectorsFree[i], (i != sectorsForDir_k - 1) ? sectorsFree[i+1] : 1);
    }
    
    // %% display information about directory created
    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, -0xb8(%rbp)
movq -0x18(%rbp), %rdi
callq 0x30d0
cmpq $0x0, %rax
jne 0xabcf
jmp 0xabe0
leaq -0x3c(%rbp), %rdi
addq $0x10, %rdi
movq -0x18(%rbp), %rsi
callq 0x3220
testb $0x1, -0x3d(%rbp)
je 0xac33
movq 0x63e3(%rip), %rdi      # 0x10fd0
leaq 0x17a1(%rip), %rsi      # 0xc395
callq 0x3230
movq %rax, %rdi
leaq -0x3c(%rbp), %rsi
addq $0x10, %rsi
callq 0x3230
movq %rax, %rdi
leaq 0x172e(%rip), %rsi      # 0xc341
callq 0x3230
movq %rax, %rdi
movq 0x639e(%rip), %rsi      # 0x10fc0
callq 0x3260
movl $0x1, -0x4(%rbp)
jmp 0xae56
movq -0xb8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x98e0
testb $0x1, -0x59(%rbp)
jne 0xac9e
movq 0x6380(%rip), %rdi      # 0x10fd0
leaq 0x1751(%rip), %rsi      # 0xc3a8
callq 0x3230
movq %rax, -0xc0(%rbp)
jmp 0xac65
movq -0xc0(%rbp), %rdi
movq 0x634d(%rip), %rsi      # 0x10fc0
callq 0x3260
jmp 0xac7a
movl $0x1, -0x4(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0xae40
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xae4b
movq -0xb8(%rbp), %rsi
movl 0x30(%rsi), %edx
leaq -0x88(%rbp), %rdi
callq 0x85f0
jmp 0xacb6
testb $0x1, -0x89(%rbp)
jne 0xad14
movq 0x630a(%rip), %rdi      # 0x10fd0
leaq 0x16ae(%rip), %rsi      # 0xc37b
callq 0x3230
movq %rax, -0xc8(%rbp)
jmp 0xacdb
movq -0xc8(%rbp), %rdi
movq 0x62d7(%rip), %rsi      # 0x10fc0
callq 0x3260
jmp 0xacf0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x70(%rbp)
jmp 0xae24
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xae32
leaq -0xa8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xa0b0
jmp 0xad26
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
callq 0x9b80
jmp 0xad3f
leaq -0xa8(%rbp), %rdi
callq 0x3db0
movl $0x0, -0xac(%rbp)
movq -0xb8(%rbp), %rcx
movl -0xac(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0xae16
movslq -0xac(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x9c20
movq -0xb8(%rbp), %rcx
movl (%rax), %eax
movl %eax, -0xcc(%rbp)
movl -0xac(%rbp), %eax
movl 0x30(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
je 0xadbf
movl -0xac(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq -0x88(%rbp), %rdi
callq 0x9c20
movl (%rax), %eax
movl %eax, -0xd0(%rbp)
jmp 0xadcc
movl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0xadcc
movl -0xcc(%rbp), %esi
movq -0xb8(%rbp), %rdi
movl -0xd0(%rbp), %edx
callq 0x84b0
jmp 0xade6
jmp 0xade8
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xad55
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x3db0
jmp 0xae32
movl $0x0, -0x4(%rbp)
movl $0x1, -0x70(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x3db0
jmp 0xae40
leaq -0x88(%rbp), %rdi
callq 0x3db0
jmp 0xae4b
leaq -0x58(%rbp), %rdi
callq 0x3db0
jmp 0xae56
leaq -0x58(%rbp), %rdi
callq 0x3db0
jmp 0xae62
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x3450
nopl (%rax,%rax)