// File: /workspace/github_cpp_makefile_star2_O2/hyp1231[P]FMSS/filesystem.cpp
// Function: FileSystem::str2Inside_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
bool FileSystem::str2Inside_file(string& file_data, const string& filepath) {
	/* 
        step 1: find the right directory
    */

	string filename;
    int dir_inodeNum = Analysis_path(filepath, filename);
    if(dir_inodeNum == -1) {
    	return false;
    }

    /*
        step 2: test if there is file having the same name
    */

    // exist file, delete to clear bitmap
	if(Get_file_inodeNum_from_dir(dir_inodeNum, filename) != -1) {
		if(!DeleteFile(filepath)) {
			cout << "[Error] Cannot delete file " << filename;
			return false;
		}
	}
	CreateFile(filepath);
	int file_inodeNum = Get_file_inodeNum_from_dir(dir_inodeNum, filename);

	/*
		step 3: allocate data blocks
	*/

	// EOF
	int BLKnum = (file_data.length() + (file_data.length() != 8 * BLKsize)) / BLKsize;
	char _EOF = -1;
	if(file_data.length() != 8 * BLKsize) {
		file_data += _EOF;
	}

	char buf[BLKsize], inode_buf[64];

	// first BLK
	Get_inode_from_inodeNum(inode_buf, file_inodeNum);
	Fill_byte_by_str(buf, 0, min(1024, (int)file_data.length()), file_data);
	file_data = file_data.substr(min(1024, (int)file_data.length()));
	D.Putblk(buf, Get_actual_dataBLKnumber(byte2int(inode_buf, 42, 44)));

	for(int i = 1; i < BLKnum; ++i) {
		// find an empty data block
		int next_dataBLKNum = Find_empty_dataBLKNum();
		// set data block bitmap
		Fill_data_block_bitmap(next_dataBLKNum, true);
		// change inode.data_block_number
		Fill_byte_by_num(inode_buf, 42 + 2 * i, 44 + 2 * i, next_dataBLKNum);
		// write data
		Fill_byte_by_str(buf, 0, min(1024, (int)file_data.length()), file_data);
		// cut str
		file_data = file_data.substr(min(1024, (int)file_data.length()));
		D.Putblk(buf, Get_actual_dataBLKnumber(byte2int(inode_buf, 42 + 2 * i, 44 + 2 * i)));
	}

	PutInode(inode_buf, file_inodeNum);
	Save_superBLK();

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %rbx
movq %rsi, -0x30(%rbp)
movq %rdi, %r13
leaq -0x70(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x80(%rbp), %rdx
movq %rbx, %rsi
callq 0x3c86
movl %eax, %r15d
cmpl $-0x1, %eax
je 0x4b8a
leaq -0x80(%rbp), %rdx
movq %r13, %rdi
movl %r15d, %esi
xorl %ecx, %ecx
callq 0x2ff2
cmpl $-0x1, %eax
je 0x496c
leaq 0x2d02(%rip), %rsi      # 0x7631
leaq -0xa0(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x27fc
leaq -0xa0(%rbp), %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x38e0
movl %eax, %r12d
leaq -0xa0(%rbp), %rdi
callq 0x20e0
testb %r12b, %r12b
je 0x4b6b
movq %r13, %rdi
movq %rbx, %rsi
callq 0x372c
leaq -0x80(%rbp), %rdx
movq %r13, %rdi
movl %r15d, %esi
xorl %ecx, %ecx
callq 0x2ff2
movl %eax, %ebx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
movslq (%r13), %rcx
leaq (,%rcx,8), %rdi
xorl %eax, %eax
cmpq %rdi, %rsi
setne %al
addq %rsi, %rax
xorl %edx, %edx
divq %rcx
movq %rax, -0x60(%rbp)
cmpq %rdi, %rsi
je 0x49c7
pushq $-0x1
popq %rsi
movq -0x30(%rbp), %rdi
callq 0x2190
movl (%r13), %ecx
movq %rsp, -0x48(%rbp)
movl %ecx, %eax
movq %rsp, %rcx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
leaq -0x100(%rbp), %rsi
movq %r13, %rdi
movl %ebx, -0x3c(%rbp)
movl %ebx, %edx
callq 0x3176
movq -0x30(%rbp), %r14
movl 0x8(%r14), %eax
movl $0x400, %ebx            # imm = 0x400
cmpl %ebx, %eax
cmovll %eax, %ebx
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl %ebx, %edx
movq %r14, %rcx
callq 0x2b3c
movslq %ebx, %rdx
leaq -0xc0(%rbp), %rdi
pushq $-0x1
popq %rcx
movq %r14, %rsi
callq 0x2200
leaq -0xc0(%rbp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2240
movq %rbx, %rdi
callq 0x20e0
leaq -0x100(%rbp), %rdi
pushq $0x2a
popq %rsi
pushq $0x2c
popq %r15
movl %r15d, %edx
callq 0x2917
leaq 0x8(%r13), %rdi
addl $0x6, %eax
movq %rdi, -0x58(%rbp)
movq -0x38(%rbp), %rsi
movl %eax, %edx
callq 0x271c
pushq $0x1
popq %rax
leaq -0x100(%rbp), %r14
movq %r13, -0x50(%rbp)
cmpl -0x60(%rbp), %eax
jge 0x4b49
movl %eax, -0x40(%rbp)
movq %r13, %rdi
callq 0x321e
movl %eax, %r8d
cltd
pushq $0x50
popq %rcx
idivl %ecx
movl %edx, %ecx
pushq $0x1
popq %rdx
shll %cl, %edx
cltq
orb %dl, 0x5a(%r13,%rax)
leal 0x2(%r15), %ebx
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
movl %r8d, %ecx
callq 0x2b1c
movq %r14, %r12
movq -0x30(%rbp), %r14
movl 0x8(%r14), %r13d
movl $0x400, %eax            # imm = 0x400
cmpl %eax, %r13d
cmovgel %eax, %r13d
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl %r13d, %edx
movq %r14, %rcx
callq 0x2b3c
movslq %r13d, %rdx
movq %r14, %r13
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
pushq $-0x1
popq %rcx
callq 0x2200
movq %r13, %rdi
movq %r14, %rsi
callq 0x2240
movq %r14, %rdi
callq 0x20e0
movq %r12, %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x2917
movq %r12, %r14
addl $0x6, %eax
movq -0x58(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl %eax, %edx
callq 0x271c
movl -0x40(%rbp), %eax
incl %eax
movl %ebx, %r15d
movq -0x50(%rbp), %r13
jmp 0x4a81
leaq -0x100(%rbp), %rsi
movq %r13, %rdi
movl -0x3c(%rbp), %edx
callq 0x4846
movq %r13, %rdi
callq 0x32ca
movq -0x48(%rbp), %rsp
movb $0x1, %bl
jmp 0x4b8c
movq 0x645e(%rip), %rdi      # 0xafd0
leaq 0x2abd(%rip), %rsi      # 0x7636
callq 0x21d0
leaq -0x80(%rbp), %rsi
movq %rax, %rdi
callq 0x21b0
xorl %ebx, %ebx
leaq -0x80(%rbp), %rdi
callq 0x20e0
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4bc5
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x20e0
jmp 0x4bc8
jmp 0x4bc5
jmp 0x4bc5
jmp 0x4bc5
jmp 0x4bc5
jmp 0x4bc5
jmp 0x4bc5
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x20e0
movq %rbx, %rdi
callq 0x2340
nop