// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
cmpl $0x9, 0x8(%rdx)
jl 0x3ac4
movq 0x9549(%rip), %rbx      # 0xcfd0
leaq 0x59d3(%rip), %rsi      # 0x9461
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2120
jmp 0x3ae1
movl %ecx, %r12d
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl (%rsi), %esi
xorl %ecx, %ecx
callq 0x35de
cmpl $-0x1, %eax
je 0x3af2
movl %eax, (%r14)
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x384a
movq %rax, -0x30(%rbp)
cmpl $-0x1, %eax
je 0x3cd3
movq %rbx, %rdi
callq 0x38a6
cmpl $-0x1, %eax
je 0x3ceb
movl %r12d, -0x34(%rbp)
movq %rsp, -0x80(%rbp)
movq %rax, -0x58(%rbp)
movq %rax, %r12
movl (%rbx), %eax
movq %rsp, %r13
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movl (%r14), %edx
leaq -0xf0(%rbp), %rsi
movq %rbx, %rdi
callq 0x37c4
xorl %ecx, %ecx
leaq -0xc6(%rbp), %rax
xorl %edx, %edx
movsbl (%rax), %esi
shll %cl, %esi
addl %esi, %edx
incq %rax
addq $0x8, %rcx
cmpq $0x10, %rcx
jne 0x3b57
movq %rbx, -0x48(%rbp)
leaq 0x8(%rbx), %rdi
addl $0x6, %edx
movq %rdi, -0x40(%rbp)
movq %r13, %rsi
callq 0x26d0
xorl %ebx, %ebx
leal 0xc(%rbx), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x3c41
leal 0x8(%rbx), %esi
leal 0xa(%rbx), %edx
movq %r13, %rdi
callq 0x2a38
addq $0xc, %rbx
testl %eax, %eax
jne 0x3b84
leaq (%rbx,%r13), %rcx
addq $-0x4, %rcx
leaq (%rbx,%r13), %rax
addq $-0x2, %rax
movb $0x1, %sil
movl $0x2, %edx
movb %sil, (%rcx)
incq %rcx
xorl %esi, %esi
decq %rdx
jne 0x3bc1
movq 0x8(%r15), %rcx
testl %ecx, %ecx
jle 0x3c11
leal (%rcx,%rbx), %edx
addl $-0xc, %edx
leaq -0xc(%rbx), %rsi
movq (%r15), %rdi
movl %ecx, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl $0x1, %r9d
movb -0x1(%rdi,%r9), %r10b
movb %r10b, (%r13,%rsi)
incq %rsi
cmpq %rdx, %rsi
jae 0x3c11
leaq 0x1(%r9), %r10
cmpq %r8, %r9
movq %r10, %r9
jb 0x3bf3
cmpl $0x7, %ecx
jg 0x3c1e
addq %r13, %rcx
movb $0x0, -0xc(%rbx,%rcx)
movl $0x2, %ecx
movq -0x30(%rbp), %rdx
movb %dl, (%rax)
leal 0xff(%rdx), %esi
testl %edx, %edx
cmovnsl %edx, %esi
sarl $0x8, %esi
incq %rax
movl %esi, %edx
decq %rcx
jne 0x3c27
movq %r14, -0x88(%rbp)
xorl %ecx, %ecx
leaq -0xc6(%rbp), %rax
xorl %edx, %edx
movsbl (%rax), %esi
shll %cl, %esi
addl %esi, %edx
incq %rax
addq $0x8, %rcx
cmpq $0x10, %rcx
jne 0x3c53
addl $0x6, %edx
movq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0x27d6
movq -0x30(%rbp), %r14
leal 0xf(%r14), %ebx
testl %r14d, %r14d
cmovnsl %r14d, %ebx
movl %ebx, %edx
sarl $0x4, %edx
incl %edx
movq -0x40(%rbp), %rdi
movq %r13, %rsi
movl %edx, -0x4c(%rbp)
callq 0x26d0
andl $-0x10, %ebx
subl %ebx, %r14d
shll $0x6, %r14d
leaq -0xa8(%rbp), %rdi
callq 0x2d60
movslq %r14d, %rax
cmpb $0x0, -0x34(%rbp)
je 0x3d03
leaq (%rax,%r13), %rcx
xorl %edx, %edx
movq -0x48(%rbp), %rbx
movb $0x0, (%rcx,%rdx)
incq %rdx
cmpq $0x2, %rdx
jne 0x3cc4
jmp 0x3d1f
movq 0x92f6(%rip), %rbx      # 0xcfd0
leaq 0x579d(%rip), %rsi      # 0x947e
movl $0x22, %edx
jmp 0x3a93
movq 0x92de(%rip), %rbx      # 0xcfd0
leaq 0x57a8(%rip), %rsi      # 0x94a1
movl $0x27, %edx
jmp 0x3a93
leaq (%rax,%r13), %rcx
movb $0x1, %sil
xorl %edx, %edx
movq -0x48(%rbp), %rbx
movb %sil, (%rcx,%rdx)
incq %rdx
xorl %esi, %esi
cmpq $0x2, %rdx
jne 0x3d10
movq 0x8(%r15), %rcx
testl %ecx, %ecx
movq -0x88(%rbp), %r14
jle 0x3d68
leal (%rcx,%rax), %edx
addl $0x2, %edx
movslq %edx, %rdx
movq (%r15), %rsi
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x3(%rax), %rdi
movl $0x1, %r8d
movb -0x1(%rsi,%r8), %r9b
movb %r9b, -0x1(%r13,%rdi)
cmpq %rdx, %rdi
jge 0x3d68
incq %rdi
leaq 0x1(%r8), %r9
cmpq %rcx, %r8
movq %r9, %r8
jb 0x3d4a
movq -0xa0(%rbp), %rcx
testl %ecx, %ecx
movq -0x40(%rbp), %r15
jle 0x3e00
leal (%rax,%rcx), %edx
leal 0xa(%rdx), %esi
movslq %esi, %rsi
movq -0xa8(%rbp), %rdi
movl %ecx, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
leaq 0xb(%rax), %r9
leaq (%rax,%r13), %r10
addq $0xa, %r10
xorl %r11d, %r11d
movb (%rdi,%r11), %bl
movb %bl, (%r10,%r11)
leaq (%r9,%r11), %rbx
cmpq %rsi, %rbx
jge 0x3dbd
incq %r11
cmpq %r8, %r11
jb 0x3da4
testl %ecx, %ecx
movq -0x48(%rbp), %rbx
jle 0x3e00
addl $0x1a, %edx
movslq %edx, %rdx
movq -0xa8(%rbp), %rsi
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq 0x1b(%rax), %rdi
leaq (%rax,%r13), %r8
addq $0x1a, %r8
xorl %r9d, %r9d
movb (%rsi,%r9), %r10b
movb %r10b, (%r8,%r9)
leaq (%rdi,%r9), %r10
cmpq %rdx, %r10
jge 0x3e00
incq %r9
cmpq %rcx, %r9
jb 0x3de7
addq %r13, %rax
addq $0x2a, %rax
xorl %ecx, %ecx
movb %r12b, (%rax,%rcx)
leal 0xff(%r12), %edx
testl %r12d, %r12d
cmovnsl %r12d, %edx
sarl $0x8, %edx
incq %rcx
movl %edx, %r12d
cmpq $0x2, %rcx
jne 0x3e09
movq %r15, %rdi
movq %r13, %rsi
movl -0x4c(%rbp), %edx
callq 0x27d6
movq -0x58(%rbp), %r12
movq %r15, %rdi
leal 0x6(%r12), %r15d
movq %r13, %rsi
movl %r15d, %edx
callq 0x26d0
cmpb $0x0, -0x34(%rbp)
je 0x3e60
movb $-0x1, (%r13)
jmp 0x3f93
leaq -0x68(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x565a(%rip), %rsi      # 0x94ca
leaq 0x5654(%rip), %rdx      # 0x94cb
leaq -0x78(%rbp), %rdi
callq 0x2910
movq -0x70(%rbp), %rax
testl %eax, %eax
jle 0x3eaa
movq -0x78(%rbp), %rcx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
movb (%rcx,%rdx), %sil
movb %sil, (%r13,%rdx)
cmpq $0x6, %rdx
ja 0x3eaa
incq %rdx
cmpq %rax, %rdx
jb 0x3e93
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x3ebf
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2200
movb $0x1, %cl
xorl %eax, %eax
movb %cl, 0x8(%r13,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x2, %rax
jne 0x3ec3
xorl %eax, %eax
movq -0x30(%rbp), %rcx
movb %cl, 0xa(%r13,%rax)
leal 0xff(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x8, %edx
incq %rax
movl %edx, %ecx
cmpq $0x2, %rax
jne 0x3ed9
leaq -0x78(%rbp), %rdi
movq %r12, (%rdi)
leaq 0x55c4(%rip), %rsi      # 0x94c9
leaq 0x55bf(%rip), %rdx      # 0x94cb
callq 0x2910
movq -0x70(%rbp), %rax
testl %eax, %eax
jle 0x3f43
movq -0x78(%rbp), %rcx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0xc, %edx
movb -0xc(%rcx,%rdx), %sil
movb %sil, (%r13,%rdx)
cmpq $0x12, %rdx
ja 0x3f43
leaq -0xb(%rdx), %rsi
incq %rdx
cmpq %rax, %rsi
jb 0x3f27
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x3f58
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x2200
movb $0x1, %cl
xorl %eax, %eax
movq -0x58(%rbp), %r12
movb %cl, 0x14(%r13,%rax)
incq %rax
xorl %ecx, %ecx
cmpq $0x2, %rax
jne 0x3f60
movl (%r14), %ecx
xorl %eax, %eax
movb %cl, 0x16(%r13,%rax)
leal 0xff(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x8, %edx
incq %rax
movl %edx, %ecx
cmpq $0x2, %rax
jne 0x3f75
movq -0x40(%rbp), %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x27d6
movq -0x30(%rbp), %rsi
movslq %esi, %rax
imulq $0x66666667, %rax, %rax # imm = 0x66666667
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x22, %rax
addl %ecx, %eax
leal (%rax,%rax), %ecx
leal (%rcx,%rcx,4), %edx
movl %esi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shll %cl, %edx
cltq
orb %dl, 0x50(%rbx,%rax)
movl $0x1, %eax
movslq %r12d, %rcx
imulq $0x66666667, %rcx, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x25, %rcx
addl %edx, %ecx
movslq %ecx, %rdx
shll $0x4, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %r12d
movl %r12d, %ecx
shll %cl, %eax
orb %al, 0x5a(%rbx,%rdx)
movq %rbx, %rdi
callq 0x3996
movq -0x30(%rbp), %rax
movl %eax, (%r14)
leaq -0x98(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4033
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x80(%rbp), %rsp
movb $0x1, %al
jmp 0x3ae3
jmp 0x4042
jmp 0x4042
movq %rax, %rbx
leaq -0x98(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4064
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2350