// File: hyp1231[P]FMSS/filesystem.cpp
// Function: FileSystem::CreateInCurDir(int&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool)
bool FileSystem::CreateInCurDir(int &dir_inodeNum, const string &filename, bool isFile) {
    /*
        step 1: make sure filename isn't too large; otherwise, return FALSE
    */

    if((int)filename.size() > 8) {
        cout << "[Error] filename is too long" << endl;
        return false;
    }

    /*
        step 2: make sure no files have the same name; otherwise, return FALSE
    */

    int new_dir_inodeNum = Get_file_inodeNum_from_dir(dir_inodeNum, filename);
    if(new_dir_inodeNum != -1) {
        dir_inodeNum = new_dir_inodeNum;
        return false;
    }

    /*  
        step 3: search for empty index_node
    */

    int next_inodeNum = Find_empty_inodeNum();
    if(next_inodeNum == -1) {
        cout << "[Error] Cannot find an empty inode" << endl;
        return false;
    }    

    /*
        step 4: search the block_bitmap for empty blocks
    */

    int next_dataBLKNum = Find_empty_dataBLKNum();
    if(next_dataBLKNum == -1) {
        cout << "[Error] Cannot find an empty data block" << endl;
        return false;
    }

    /*
        step 5: create new pair of mapping in its directory's data block
    */

    char inode_buf[64], buf[BLKsize];
    Get_inode_from_inodeNum(inode_buf, dir_inodeNum);

    // modify directory data block
    D.Getblk(buf, Get_actual_dataBLKnumber(byte2int(inode_buf, 42, 44)));
    for(int i = 0; i + 12 <= BLKsize; i += 12) {
    	// find used = 0
    	if(byte2int(buf, i + 8, i + 10) == 0) {
    		// set used = 1;
    		Fill_byte_by_num(buf, i + 8, i + 10, 1);
    		// set filename
    		Fill_byte_by_str(buf, i, i + (int)filename.size(), filename);
    		// zero end of str
    		if((int)filename.size() < 8) {
    			buf[i + filename.size()] = 0;
    		}

    		// set next inodeNum
    		Fill_byte_by_num(buf, i + 10, i + 12, next_inodeNum);

    		break;
    	} 
    }
    D.Putblk(buf, Get_actual_dataBLKnumber(byte2int(inode_buf, 42, 44)));

    /*
        step 6: initialize the inode and the first data block ( set the first block's first byte = 0xFF(EOF) )
    */

    // modify next inode
    D.Getblk(buf, Get_actual_inodeBLKnumber(next_inodeNum));
    int st = 64 * (next_inodeNum % 16);

    string curTime = getTime();
    // set flag
    if (isFile)
        Fill_byte_by_num(buf, st, st + 2, 0);
    else
        Fill_byte_by_num(buf, st, st + 2, 1);
    Fill_byte_by_str(buf, st + 2, st + 2 + (int)filename.size(), filename);
    Fill_byte_by_str(buf, st + 10, st + (int)curTime.size() + 10, curTime);
    Fill_byte_by_str(buf, st + 26, st + (int)curTime.size() + 26, curTime);
    Fill_byte_by_num(buf, st + 42, st + 44, next_dataBLKNum);

    D.Putblk(buf, Get_actual_inodeBLKnumber(next_inodeNum));

    // modify next data block
    D.Getblk(buf, Get_actual_dataBLKnumber(next_dataBLKNum));
    if (isFile) {
        // EOF
        Fill_byte_by_num(buf, 0, 1, -1);  
    }
    else {
        // set index of "."
        Fill_byte_by_str(buf, 0, 8, ".");
        Fill_byte_by_num(buf, 8, 10, 1);
        Fill_byte_by_num(buf, 10, 12, next_inodeNum);
        // set index of ".."
        Fill_byte_by_str(buf, 12, 20, "..");
        Fill_byte_by_num(buf, 20, 22, 1);
        Fill_byte_by_num(buf, 22, 24, dir_inodeNum);
    }
    D.Putblk(buf, Get_actual_dataBLKnumber(next_dataBLKNum));

    /*
        step 7: change inode bitmap and data block bitmap, save the super block
    */

    Fill_inode_bitmap(next_inodeNum, true);
    Fill_data_block_bitmap(next_dataBLKNum, true);
    Save_superBLK();

    dir_inodeNum = next_inodeNum;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movb %cl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31a0
cmpl $0x8, %eax
jle 0x54a2
movq 0xfb52(%rip), %rdi      # 0x14fd0
leaq 0x9ff7(%rip), %rsi      # 0xf47c
callq 0x3250
movq %rax, %rdi
movq 0xfb2c(%rip), %rsi      # 0x14fc0
callq 0x32a0
movb $0x0, -0x1(%rbp)
jmp 0x5bcf
movq -0x110(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
movq -0x20(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4d30
movl %eax, -0x28(%rbp)
cmpl $-0x1, -0x28(%rbp)
je 0x54d5
movl -0x28(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movb $0x0, -0x1(%rbp)
jmp 0x5bcf
movq -0x110(%rbp), %rdi
callq 0x50a0
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x5515
movq 0xfadf(%rip), %rdi      # 0x14fd0
leaq 0x9fa1(%rip), %rsi      # 0xf499
callq 0x3250
movq %rax, %rdi
movq 0xfab9(%rip), %rsi      # 0x14fc0
callq 0x32a0
movb $0x0, -0x1(%rbp)
jmp 0x5bcf
movq -0x110(%rbp), %rdi
callq 0x5140
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x30(%rbp)
jne 0x5555
movq 0xfa9f(%rip), %rdi      # 0x14fd0
leaq 0x9f84(%rip), %rsi      # 0xf4bc
callq 0x3250
movq %rax, %rdi
movq 0xfa79(%rip), %rsi      # 0x14fc0
callq 0x32a0
movb $0x0, -0x1(%rbp)
jmp 0x5bcf
movq -0x110(%rbp), %rdi
movl (%rdi), %eax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x118(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
leaq -0x70(%rbp), %rsi
movq -0x18(%rbp), %rax
movl (%rax), %edx
callq 0x4ff0
movq -0x110(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x120(%rbp)
leaq -0x70(%rbp), %rdi
movl $0x2a, %esi
movl $0x2c, %edx
callq 0x40e0
movq -0x110(%rbp), %rdi
movl %eax, %esi
callq 0x9390
movq -0x120(%rbp), %rdi
movq -0x118(%rbp), %rsi
movl %eax, %edx
callq 0x3900
movl $0x0, -0x84(%rbp)
movq -0x110(%rbp), %rcx
movl -0x84(%rbp), %eax
addl $0xc, %eax
cmpl (%rcx), %eax
jg 0x56fc
movq -0x118(%rbp), %rdi
movl -0x84(%rbp), %esi
addl $0x8, %esi
movl -0x84(%rbp), %edx
addl $0xa, %edx
callq 0x40e0
cmpl $0x0, %eax
jne 0x56e6
movq -0x118(%rbp), %rdi
movl -0x84(%rbp), %esi
addl $0x8, %esi
movl -0x84(%rbp), %edx
addl $0xa, %edx
movl $0x1, %ecx
callq 0x44b0
movl -0x84(%rbp), %eax
movl %eax, -0x124(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x128(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31a0
movl -0x128(%rbp), %edx
movq -0x118(%rbp), %rdi
movl -0x124(%rbp), %esi
addl %eax, %edx
movq -0x20(%rbp), %rcx
callq 0x4500
movq -0x20(%rbp), %rdi
callq 0x31a0
cmpl $0x8, %eax
jge 0x56c3
movslq -0x84(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31a0
movq -0x130(%rbp), %rcx
movq %rax, %rdx
movq -0x118(%rbp), %rax
addq %rdx, %rcx
movb $0x0, (%rax,%rcx)
movq -0x118(%rbp), %rdi
movl -0x84(%rbp), %esi
addl $0xa, %esi
movl -0x84(%rbp), %edx
addl $0xc, %edx
movl -0x2c(%rbp), %ecx
callq 0x44b0
jmp 0x56fc
jmp 0x56e8
movl -0x84(%rbp), %eax
addl $0xc, %eax
movl %eax, -0x84(%rbp)
jmp 0x55e5
movq -0x110(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x140(%rbp)
leaq -0x70(%rbp), %rdi
movl $0x2a, %esi
movl $0x2c, %edx
callq 0x40e0
movq -0x110(%rbp), %rdi
movl %eax, %esi
callq 0x9390
movq -0x140(%rbp), %rdi
movq -0x118(%rbp), %rsi
movl %eax, %edx
callq 0x3aa0
movq -0x110(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x138(%rbp)
movl -0x2c(%rbp), %esi
callq 0x93b0
movq -0x138(%rbp), %rdi
movq -0x118(%rbp), %rsi
movl %eax, %edx
callq 0x3900
movl -0x2c(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
shll $0x6, %edx
movl %edx, -0x88(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x4590
testb $0x1, -0x21(%rbp)
je 0x57bb
movq -0x118(%rbp), %rdi
movl -0x88(%rbp), %esi
movl -0x88(%rbp), %edx
addl $0x2, %edx
xorl %ecx, %ecx
callq 0x44b0
jmp 0x57db
movq -0x118(%rbp), %rdi
movl -0x88(%rbp), %esi
movl -0x88(%rbp), %edx
addl $0x2, %edx
movl $0x1, %ecx
callq 0x44b0
movl -0x88(%rbp), %eax
movl %eax, -0x170(%rbp)
addl $0x2, %eax
movl %eax, -0x16c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x31a0
movl -0x170(%rbp), %ecx
movl -0x16c(%rbp), %esi
movq -0x118(%rbp), %rdi
movl %eax, %edx
movl %ecx, %eax
movl %edx, %ecx
leal 0x2(%rax,%rcx), %edx
movq -0x20(%rbp), %rcx
callq 0x4500
movl -0x88(%rbp), %eax
movl %eax, -0x168(%rbp)
addl $0xa, %eax
movl %eax, -0x164(%rbp)
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x31a0
movl -0x168(%rbp), %edx
movl -0x164(%rbp), %esi
movq -0x158(%rbp), %rcx
movq -0x118(%rbp), %rdi
movl %eax, %r8d
movl %edx, %eax
movl %r8d, %edx
leal 0xa(%rax,%rdx), %edx
callq 0x4500
movq -0x158(%rbp), %rdi
movl -0x88(%rbp), %eax
movl %eax, -0x160(%rbp)
addl $0x1a, %eax
movl %eax, -0x15c(%rbp)
callq 0x31a0
movl -0x160(%rbp), %edx
movl -0x15c(%rbp), %esi
movq -0x158(%rbp), %rcx
movq -0x118(%rbp), %rdi
movl %eax, %r8d
movl %edx, %eax
movl %r8d, %edx
leal 0x1a(%rax,%rdx), %edx
callq 0x4500
movq -0x118(%rbp), %rdi
movl -0x88(%rbp), %edx
movl %edx, %esi
addl $0x2a, %esi
addl $0x2c, %edx
movl -0x30(%rbp), %ecx
callq 0x44b0
movq -0x110(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x150(%rbp)
movl -0x2c(%rbp), %esi
callq 0x93b0
movl %eax, -0x144(%rbp)
jmp 0x5900
movl -0x144(%rbp), %edx
movq -0x118(%rbp), %rsi
movq -0x150(%rbp), %rdi
callq 0x3aa0
jmp 0x591b
movq -0x110(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x180(%rbp)
movl -0x30(%rbp), %esi
callq 0x9390
movl %eax, -0x174(%rbp)
jmp 0x5940
movl -0x174(%rbp), %edx
movq -0x118(%rbp), %rsi
movq -0x180(%rbp), %rdi
callq 0x3900
jmp 0x595b
testb $0x1, -0x21(%rbp)
je 0x5995
movq -0x118(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x44b0
jmp 0x5b22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
jmp 0x5bc1
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x34a0
movq -0x188(%rbp), %rdx
leaq 0x9b2f(%rip), %rsi      # 0xf4e5
leaq -0xd8(%rbp), %rdi
callq 0x3c50
jmp 0x59c4
movq -0x118(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
movl %edx, -0x198(%rbp)
leaq -0xd8(%rbp), %rcx
movq %rcx, -0x1a0(%rbp)
callq 0x4500
movq -0x1a0(%rbp), %rdi
callq 0x3160
leaq -0xd9(%rbp), %rdi
callq 0x32d0
movl -0x198(%rbp), %esi
movq -0x118(%rbp), %rdi
movl $0xa, %edx
movl %edx, -0x194(%rbp)
movl $0x1, %ecx
callq 0x44b0
movq -0x118(%rbp), %rdi
movl -0x194(%rbp), %esi
movl -0x2c(%rbp), %ecx
movl $0xc, %edx
callq 0x44b0
leaq -0x101(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x34a0
movq -0x190(%rbp), %rdx
leaq 0x9a84(%rip), %rsi      # 0xf4e4
leaq -0x100(%rbp), %rdi
callq 0x3c50
jmp 0x5a6e
movq -0x118(%rbp), %rdi
movl $0xc, %esi
movl $0x14, %edx
leaq -0x100(%rbp), %rcx
callq 0x4500
leaq -0x100(%rbp), %rdi
callq 0x3160
leaq -0x101(%rbp), %rdi
callq 0x32d0
movq -0x118(%rbp), %rdi
movl $0x14, %esi
movl $0x16, %edx
movl $0x1, %ecx
callq 0x44b0
movq -0x118(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movl $0x16, %esi
movl $0x18, %edx
callq 0x44b0
jmp 0x5b22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0xd9(%rbp), %rdi
callq 0x32d0
jmp 0x5bc1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x101(%rbp), %rdi
callq 0x32d0
jmp 0x5bc1
movq -0x110(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x1b0(%rbp)
movl -0x30(%rbp), %esi
callq 0x9390
movl %eax, -0x1a4(%rbp)
jmp 0x5b47
movl -0x1a4(%rbp), %edx
movq -0x118(%rbp), %rsi
movq -0x1b0(%rbp), %rdi
callq 0x3aa0
jmp 0x5b62
movq -0x110(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl $0x1, %edx
movl %edx, -0x1b4(%rbp)
callq 0x51e0
movl -0x1b4(%rbp), %edx
movq -0x110(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x5270
movq -0x110(%rbp), %rdi
callq 0x5300
jmp 0x5b9f
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x3160
movq -0x78(%rbp), %rax
movq %rax, %rsp
jmp 0x5bcf
leaq -0xa8(%rbp), %rdi
callq 0x3160
jmp 0x5bd9
movb -0x1(%rbp), %al
andb $0x1, %al
movq %rbp, %rsp
popq %rbp
retq
movq -0xb0(%rbp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)