// File: jasperandrew[P]cs350-project2/./lab7.h
// Function: initDrive()
int initDrive()
{
    string path = "DRIVE";
    ifstream f(path.c_str());
    if(f.good()){
        if(DBG) cout << "Loading data from existing DRIVE\n";
        return 1;
    }

    if(DBG) cout << "Creating DRIVE directory\n";

    // Create drive directory
    mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);

    // Create file map
    if(DBG) cout << "Creating FILE_MAP file\n";
    ofstream filemap(path + string("/FILE_MAP"), ios::out);
    filemap.close();

    // Create checkpoint region file
    if(DBG) cout << "Creating CHECKPOINT_REGION file\n";
    /*ofstream checkpoint(path + string("/CHECKPOINT_REGION"), ios::out );
      checkpoint.seekp(192-1);
      checkpoint.write("", 1);
      checkpoint.close();*/

    // Create segment files
    path += "/SEGMENT";
    for(int i = 0; i < 32; i++){
        if(DBG) cout << "Creating segment file " << i + 1 << "\n";
        ofstream segment(path + to_string(i+1), ios::out | ios::binary);
        segment.seekp(SEG_SZ-1);
        segment.write("", 1);
        segment.close();
    }

    g_imap.idx = 0;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x720, %rsp            # imm = 0x720
leaq -0x29(%rbp), %rdi
movq %rdi, -0x6f0(%rbp)
callq 0x3440
movq -0x6f0(%rbp), %rdx
leaq 0x3bed(%rip), %rsi      # 0x9146
leaq -0x28(%rbp), %rdi
callq 0x3320
jmp 0x5564
leaq -0x29(%rbp), %rdi
callq 0x3260
leaq -0x28(%rbp), %rdi
callq 0x30e0
movq %rax, %rsi
leaq -0x248(%rbp), %rdi
movl $0x8, %edx
callq 0x3450
jmp 0x558c
movq -0x248(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x248(%rbp,%rax), %rdi
callq 0x3400
movb %al, -0x6f1(%rbp)
jmp 0x55ac
movb -0x6f1(%rbp), %al
testb $0x1, %al
jne 0x55b8
jmp 0x561f
movq 0x7a09(%rip), %rdi      # 0xcfc8
leaq 0x3b86(%rip), %rsi      # 0x914c
callq 0x3220
jmp 0x55cd
movl $0x1, -0x4(%rbp)
movl $0x1, -0x24c(%rbp)
jmp 0x5953
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x3260
jmp 0x5989
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5980
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5974
movq 0x79a2(%rip), %rdi      # 0xcfc8
leaq 0x3b41(%rip), %rsi      # 0x916e
callq 0x3220
jmp 0x5634
leaq -0x28(%rbp), %rdi
callq 0x30e0
movq %rax, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x3110
movq 0x7977(%rip), %rdi      # 0xcfc8
leaq 0x3b30(%rip), %rsi      # 0x9188
callq 0x3220
jmp 0x565f
leaq -0x491(%rbp), %rdi
movq %rdi, -0x700(%rbp)
callq 0x3440
movq -0x700(%rbp), %rdx
leaq 0x39ee(%rip), %rsi      # 0x906e
leaq -0x490(%rbp), %rdi
callq 0x3320
jmp 0x568e
leaq -0x470(%rbp), %rdi
leaq -0x28(%rbp), %rsi
leaq -0x490(%rbp), %rdx
callq 0x6f00
jmp 0x56a7
leaq -0x450(%rbp), %rdi
leaq -0x470(%rbp), %rsi
movl $0x10, %edx
callq 0x3480
jmp 0x56c1
leaq -0x470(%rbp), %rdi
callq 0x3170
leaq -0x490(%rbp), %rdi
callq 0x3170
leaq -0x491(%rbp), %rdi
callq 0x3260
leaq -0x450(%rbp), %rdi
callq 0x3340
jmp 0x56f3
movq 0x78ce(%rip), %rdi      # 0xcfc8
leaq 0x3a9f(%rip), %rsi      # 0x91a0
callq 0x3220
jmp 0x5708
leaq 0x38fa(%rip), %rsi      # 0x9009
leaq -0x28(%rbp), %rdi
callq 0x3210
jmp 0x571a
movl $0x0, -0x498(%rbp)
cmpl $0x20, -0x498(%rbp)
jge 0x591c
movq 0x7890(%rip), %rdi      # 0xcfc8
leaq 0x3a82(%rip), %rsi      # 0x91c1
callq 0x3220
movq %rax, -0x708(%rbp)
jmp 0x574d
movq -0x708(%rbp), %rdi
movl -0x498(%rbp), %esi
incl %esi
callq 0x3410
movq %rax, -0x710(%rbp)
jmp 0x576a
movq -0x710(%rbp), %rdi
leaq 0x3bd4(%rip), %rsi      # 0x934c
callq 0x3220
jmp 0x577f
movl -0x498(%rbp), %esi
incl %esi
leaq -0x6d8(%rbp), %rdi
callq 0x69f0
jmp 0x5795
leaq -0x6b8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
leaq -0x6d8(%rbp), %rdx
callq 0x6f00
jmp 0x57ae
movl $0x10, %edi
movl $0x4, %esi
callq 0x6c00
movl %eax, -0x714(%rbp)
jmp 0x57c5
movl -0x714(%rbp), %edx
leaq -0x698(%rbp), %rdi
leaq -0x6b8(%rbp), %rsi
callq 0x3480
jmp 0x57e0
leaq -0x6b8(%rbp), %rdi
callq 0x3170
leaq -0x6d8(%rbp), %rdi
callq 0x3170
leaq -0x6e8(%rbp), %rdi
movl $0xfffff, %esi          # imm = 0xFFFFF
callq 0x6c60
jmp 0x580b
movq -0x6e8(%rbp), %rsi
movq -0x6e0(%rbp), %rdx
leaq -0x698(%rbp), %rdi
callq 0x32a0
jmp 0x5827
leaq 0x3877(%rip), %rsi      # 0x90a5
leaq -0x698(%rbp), %rdi
movl $0x1, %edx
callq 0x3310
jmp 0x5841
leaq -0x698(%rbp), %rdi
callq 0x3340
jmp 0x584f
leaq -0x698(%rbp), %rdi
callq 0x3370
movl -0x498(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x498(%rbp)
jmp 0x5724
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x58af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x58a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x470(%rbp), %rdi
callq 0x3170
leaq -0x490(%rbp), %rdi
callq 0x3170
leaq -0x491(%rbp), %rdi
callq 0x3260
jmp 0x5974
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5945
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x58f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x6b8(%rbp), %rdi
callq 0x3170
leaq -0x6d8(%rbp), %rdi
callq 0x3170
jmp 0x5945
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x698(%rbp), %rdi
callq 0x3370
jmp 0x5945
movl $0x0, 0x175e42(%rip)    # 0x17b768
movl $0x0, -0x4(%rbp)
movl $0x1, -0x24c(%rbp)
leaq -0x450(%rbp), %rdi
callq 0x3370
jmp 0x5953
leaq -0x450(%rbp), %rdi
callq 0x3370
jmp 0x5974
leaq -0x248(%rbp), %rdi
callq 0x3090
leaq -0x28(%rbp), %rdi
callq 0x3170
movl -0x4(%rbp), %eax
addq $0x720, %rsp            # imm = 0x720
popq %rbp
retq
leaq -0x248(%rbp), %rdi
callq 0x3090
leaq -0x28(%rbp), %rdi
callq 0x3170
movq -0x38(%rbp), %rdi
callq 0x3430
nopw %cs:(%rax,%rax)