// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
cmpl $0x9, 0x8(%rdx)
jl 0x4004
movq 0x9009(%rip), %rbx      # 0xcfd0
leaq 0x5524(%rip), %rsi      # 0x94f2
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 0x4023
movl %ecx, %r15d
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r12
movl (%rsi), %esi
xorl %r14d, %r14d
xorl %ecx, %ecx
callq 0x35dc
cmpl $-0x1, %eax
je 0x4034
movl %eax, (%rbx)
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, -0x68(%rbp)
xorl %eax, %eax
xorl %ecx, %ecx
movsbl 0x50(%r12,%rcx), %edx
cmpl $-0x1, %edx
je 0x406a
movl $0x1, %esi
movl %r14d, %r8d
movl %eax, %edi
xorl %ebx, %ebx
testl %edx, %esi
je 0x4092
addl %esi, %esi
addq $0x40, %rbx
decl %edi
incl %r8d
cmpl $0x200, %ebx            # imm = 0x200
jne 0x4053
incq %rcx
addl $-0x8, %eax
addl $0x8, %r14d
cmpq $0xa, %rcx
jne 0x403c
movq 0x8f4f(%rip), %rbx      # 0xcfd0
leaq 0x5487(%rip), %rsi      # 0x950f
movl $0x22, %edx
jmp 0x3fd3
movq %r13, -0x60(%rbp)
movq %r12, -0x50(%rbp)
movl %edi, -0x78(%rbp)
movl %edi, %ecx
negl %ecx
movl %ecx, -0x34(%rbp)
movzbl %al, %r11d
andl $0xf, %r11d
shll $0x6, %r11d
leaq (%r11,%rbx), %r12
addq $0x3, %r12
leaq (%r11,%rbx), %r14
addq $0xb, %r14
leaq (%r11,%rbx), %r13
addq $0x1b, %r13
xorl %eax, %eax
xorl %edi, %edi
xorl %ecx, %ecx
movq -0x50(%rbp), %rdx
movsbl 0x5a(%rdx,%rcx), %edx
cmpl $-0x1, %edx
je 0x40f9
xorl %r9d, %r9d
movl $0x1, %esi
movl %eax, %r10d
testl %edx, %esi
je 0x4120
incl %r9d
addl %esi, %esi
incl %r10d
cmpl $0x8, %r9d
jne 0x40e7
incq %rcx
addl $-0x8, %edi
addl $0x8, %eax
cmpq $0x50, %rcx
jne 0x40ce
movq 0x8ec1(%rip), %rbx      # 0xcfd0
leaq 0x541c(%rip), %rsi      # 0x9532
movl $0x27, %edx
jmp 0x3fd3
movq %r11, -0x80(%rbp)
movl %r15d, -0x54(%rbp)
movl %r10d, -0x6c(%rbp)
movl %r8d, -0x70(%rbp)
movq %rsp, -0xa8(%rbp)
movl %edi, -0x40(%rbp)
movl %r9d, -0x3c(%rbp)
movq -0x50(%rbp), %rcx
movl (%rcx), %eax
movq %rcx, %rdi
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq -0x68(%rbp), %rax
movl (%rax), %edx
leaq -0x110(%rbp), %rsi
callq 0x3d48
leaq -0x110(%rbp), %rcx
movsbl 0x2a(%rcx), %eax
movsbl 0x2b(%rcx), %ecx
shll $0x8, %ecx
movl -0x3c(%rbp), %edx
subl -0x40(%rbp), %edx
movl %edx, -0x74(%rbp)
movq -0x50(%rbp), %rdx
leaq 0x8(%rdx), %rdi
leal (%rcx,%rax), %edx
addl $0x6, %edx
movq %rdi, -0x48(%rbp)
movq %r15, -0x30(%rbp)
movq %r15, %rsi
movl %edx, -0x38(%rbp)
callq 0x26e0
xorl %r15d, %r15d
leal 0xc(%r15), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x427a
leal 0x8(%r15), %esi
leal 0xa(%r15), %edx
movq -0x30(%rbp), %rdi
callq 0x2a36
addq $0xc, %r15
testl %eax, %eax
jne 0x41a9
movq -0x30(%rbp), %rax
leaq (%rax,%r15), %rcx
addq $-0x4, %rcx
addq %r15, %rax
addq $-0x2, %rax
movb $0x1, %sil
movl $0x2, %edx
movb %sil, (%rcx)
incq %rcx
xorl %esi, %esi
decq %rdx
jne 0x41ed
movq -0x60(%rbp), %rcx
movq 0x8(%rcx), %rcx
testl %ecx, %ecx
jle 0x424e
leal (%rcx,%r15), %edx
addl $-0xc, %edx
leaq -0xc(%r15), %rsi
movq -0x60(%rbp), %rdi
movq (%rdi), %rdi
movl %ecx, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl $0x1, %r9d
movq -0x30(%rbp), %r11
movb -0x1(%rdi,%r9), %r10b
movb %r10b, (%r11,%rsi)
incq %rsi
cmpq %rdx, %rsi
jae 0x4249
leaq 0x1(%r9), %r10
cmpq %r8, %r9
movq %r10, %r9
jb 0x422c
cmpl $0x7, %ecx
jg 0x4258
addq -0x30(%rbp), %rcx
movb $0x0, -0xc(%r15,%rcx)
movl $0x2, %ecx
movl -0x34(%rbp), %edx
movb %dl, (%rax)
leal 0xff(%rdx), %esi
testl %edx, %edx
cmovnsl %edx, %esi
sarl $0x8, %esi
incq %rax
movl %esi, %edx
decq %rcx
jne 0x4260
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %r15
movq %r15, %rsi
movl -0x38(%rbp), %edx
callq 0x27e6
movl -0x34(%rbp), %edx
shrl $0x4, %edx
incl %edx
movq -0x48(%rbp), %rdi
movq %r15, %rsi
movl %edx, -0x38(%rbp)
callq 0x26e0
leaq -0xc8(%rbp), %rdi
callq 0x2d5e
cmpb $0x0, -0x54(%rbp)
je 0x42d3
movq -0x80(%rbp), %r10
leaq (%r15,%r10), %rax
xorl %ecx, %ecx
leaq (%rax,%rcx), %rdx
movb $0x0, (%rbx,%rdx)
incq %rcx
cmpq $0x2, %rcx
jne 0x42c0
jmp 0x42f1
movq -0x80(%rbp), %r10
leaq (%r15,%r10), %rax
movb $0x1, %dl
xorl %ecx, %ecx
leaq (%rax,%rcx), %rsi
movb %dl, (%rbx,%rsi)
incq %rcx
xorl %edx, %edx
cmpq $0x2, %rcx
jne 0x42df
movq -0x60(%rbp), %rdx
movq 0x8(%rdx), %rax
testl %eax, %eax
jle 0x4335
leal (%rax,%r10), %ecx
addl %ebx, %ecx
addl $0x2, %ecx
movq (%rdx), %rdx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0x1, %esi
movq -0x30(%rbp), %r8
movb -0x1(%rdx,%rsi), %dil
movb %dil, -0x1(%r8,%r12)
cmpq %rcx, %r12
jae 0x4335
incq %r12
leaq 0x1(%rsi), %rdi
cmpq %rax, %rsi
movq %rdi, %rsi
jb 0x4317
movq -0xc0(%rbp), %rdx
testl %edx, %edx
movq -0x50(%rbp), %r12
jle 0x43a3
leal (%rdx,%r10), %eax
leal (%rbx,%rax), %esi
addl $0xa, %esi
movq -0xc8(%rbp), %rax
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edi, %edi
movq -0x30(%rbp), %r9
movb (%rax,%rdi), %r8b
movb %r8b, -0x1(%r9,%r14)
cmpq %rsi, %r14
jae 0x437c
incq %rdi
incq %r14
cmpq %rcx, %rdi
jb 0x4363
addl %r10d, %edx
addl %ebx, %edx
addl $0x1a, %edx
xorl %esi, %esi
movq -0x30(%rbp), %r8
movb (%rax,%rsi), %dil
movb %dil, -0x1(%r8,%r13)
cmpq %rdx, %r13
jae 0x43a3
incq %rsi
incq %r13
cmpq %rcx, %rsi
jb 0x438a
movq -0x30(%rbp), %rsi
addq %rsi, %r10
xorl %eax, %eax
movl -0x74(%rbp), %r13d
movl %r13d, %ecx
leaq (%r10,%rax), %rdx
movb %cl, 0x2a(%rbx,%rdx)
leal 0xff(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x8, %edx
incq %rax
movl %edx, %ecx
cmpq $0x2, %rax
jne 0x43b3
movq -0x48(%rbp), %r15
movq %r15, %rdi
movq %rsi, %rbx
movl -0x38(%rbp), %edx
callq 0x27e6
movl -0x78(%rbp), %r14d
movl -0x3c(%rbp), %eax
subl -0x40(%rbp), %eax
addl $0x6, %eax
movq %r15, %rdi
movl %eax, %r15d
movq %rbx, %rsi
movl %eax, %edx
callq 0x26e0
cmpb $0x0, -0x54(%rbp)
je 0x4415
movq -0x30(%rbp), %rsi
movb $-0x1, (%rsi)
jmp 0x4522
leaq -0x90(%rbp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x5134(%rip), %rsi      # 0x955b
leaq 0x512e(%rip), %rdx      # 0x955c
leaq -0xa0(%rbp), %rdi
callq 0x2920
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rax
testl %eax, %eax
movq -0x30(%rbp), %rsi
jle 0x446b
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movb (%rdi,%rcx), %dl
movb %dl, (%rsi,%rcx)
cmpq $0x6, %rcx
ja 0x446b
incq %rcx
cmpq %rax, %rcx
jb 0x4457
cmpq %rbx, %rdi
je 0x4483
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x30(%rbp), %rsi
movw $0x1, 0x8(%rsi)
movl %r14d, %eax
negb %al
movb %al, 0xa(%rsi)
movl -0x34(%rbp), %eax
movb %ah, 0xb(%rsi)
leaq -0xa0(%rbp), %rdi
movq %rbx, (%rdi)
leaq 0x50b2(%rip), %rsi      # 0x955a
leaq 0x50ad(%rip), %rdx      # 0x955c
callq 0x2920
movq -0xa0(%rbp), %rdi
movq -0x98(%rbp), %rax
testl %eax, %eax
movq -0x30(%rbp), %rsi
jle 0x44ed
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl $0xc, %ecx
movb -0xc(%rdi,%rcx), %dl
movb %dl, (%rsi,%rcx)
cmpq $0x12, %rcx
ja 0x44ed
leaq -0xb(%rcx), %rdx
incq %rcx
cmpq %rax, %rdx
jb 0x44d4
cmpq %rbx, %rdi
je 0x4505
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x30(%rbp), %rsi
movw $0x1, 0x14(%rsi)
movq -0x68(%rbp), %rax
movl (%rax), %eax
movb %al, 0x16(%rsi)
leal 0xff(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movb %ch, 0x17(%rsi)
movq -0x48(%rbp), %rdi
movl %r15d, %edx
callq 0x27e6
movl -0x70(%rbp), %ecx
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
imulq %rax, %rcx
shrq $0x23, %rcx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
addl %r14d, %ecx
negl %ecx
movl $0x1, %edx
shll %cl, %edx
movl -0x34(%rbp), %ecx
imulq %rax, %rcx
shrq $0x23, %rcx
orb %dl, 0x50(%r12,%rcx)
movl $0x1, %edx
movl -0x6c(%rbp), %ecx
imulq %rax, %rcx
shrq $0x26, %rcx
shll $0x4, %ecx
leal (%rcx,%rcx,4), %esi
addl -0x40(%rbp), %esi
movl -0x3c(%rbp), %ecx
subl %esi, %ecx
shll %cl, %edx
movl %r13d, %ecx
imulq %rax, %rcx
shrq $0x26, %rcx
orb %dl, 0x5a(%r12,%rcx)
movq %r12, %rdi
callq 0x3ed6
movl -0x34(%rbp), %eax
movq -0x68(%rbp), %rcx
movl %eax, (%rcx)
leaq -0xb8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x45bf
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0xa8(%rbp), %rsp
movb $0x1, %al
jmp 0x4025
jmp 0x45d1
jmp 0x45d1
movq %rax, %rbx
leaq -0xb8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x45f3
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2350
nop