// File: 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
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x33e0
movq -0x160(%rbp), %rdi
movq -0x158(%rbp), %rdx
movq -0x20(%rbp), %rsi
callq 0x65b0
movl %eax, -0x14c(%rbp)
jmp 0x7c51
movl -0x14c(%rbp), %eax
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x7c81
movb $0x0, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x81dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x81ef
movq -0x160(%rbp), %rdi
movl -0x44(%rbp), %esi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4d30
movl %eax, -0x164(%rbp)
jmp 0x7c9e
movl -0x164(%rbp), %eax
cmpl $-0x1, %eax
je 0x7d9d
movq -0x20(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x34a0
movq -0x170(%rbp), %rdx
leaq 0x7976(%rip), %rsi      # 0xf64c
leaq -0x78(%rbp), %rdi
callq 0x3c50
jmp 0x7ce1
movq -0x178(%rbp), %rsi
movq -0x160(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x5f50
movb %al, -0x179(%rbp)
jmp 0x7d00
movb -0x179(%rbp), %al
xorb $-0x1, %al
movb %al, -0x17a(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x3160
leaq -0x79(%rbp), %rdi
callq 0x32d0
movb -0x17a(%rbp), %al
testb $0x1, %al
jne 0x7d2c
jmp 0x7d9b
movq 0xd29d(%rip), %rdi      # 0x14fd0
leaq 0x7917(%rip), %rsi      # 0xf651
callq 0x3250
movq %rax, -0x188(%rbp)
jmp 0x7d48
movq -0x188(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3230
jmp 0x7d5a
movb $0x0, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
jmp 0x81dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7d8d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x3160
leaq -0x79(%rbp), %rdi
callq 0x32d0
jmp 0x81ef
jmp 0x7d9d
movq -0x160(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x5bf0
jmp 0x7daf
movq -0x160(%rbp), %rdi
movl -0x44(%rbp), %esi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4d30
movl %eax, -0x18c(%rbp)
jmp 0x7dcc
movl -0x18c(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3420
movq %rax, -0x198(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3420
movq -0x160(%rbp), %rcx
movq %rax, %rdx
movq -0x198(%rbp), %rax
movl (%rcx), %esi
shll $0x3, %esi
movslq %esi, %rsi
cmpq %rsi, %rdx
setne %dl
andb $0x1, %dl
movzbl %dl, %edx
addq %rdx, %rax
movslq (%rcx), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, -0x84(%rbp)
movb $-0x1, -0x85(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3420
movq -0x160(%rbp), %rcx
movl (%rcx), %ecx
shll $0x3, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
je 0x7e5c
movsbl -0x85(%rbp), %esi
movq -0x18(%rbp), %rdi
callq 0x3170
jmp 0x7e5a
jmp 0x7e5c
movq -0x160(%rbp), %rdi
movl (%rdi), %eax
movq %rsp, %rcx
movq %rcx, -0x90(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1a0(%rbp)
movq %rcx, %rsp
movq %rax, -0x98(%rbp)
movl -0x80(%rbp), %edx
leaq -0xe0(%rbp), %rsi
callq 0x4ff0
jmp 0x7ea2
movl $0x400, -0xe4(%rbp)     # imm = 0x400
movq -0x18(%rbp), %rdi
callq 0x3420
movl %eax, -0xe8(%rbp)
leaq -0xe4(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x9920
movq %rax, -0x1a8(%rbp)
jmp 0x7ed7
movq -0x1a0(%rbp), %rdi
movq -0x1a8(%rbp), %rax
movl (%rax), %edx
movq -0x18(%rbp), %rcx
xorl %esi, %esi
callq 0x4500
movq -0x18(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movl $0x400, -0x10c(%rbp)    # imm = 0x400
movq -0x18(%rbp), %rdi
callq 0x3420
movl %eax, -0x110(%rbp)
leaq -0x10c(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x9920
movq %rax, -0x1b0(%rbp)
jmp 0x7f32
movq -0x1b8(%rbp), %rsi
movq -0x1b0(%rbp), %rax
movslq (%rax), %rdx
leaq -0x108(%rbp), %rdi
movq $-0x1, %rcx
callq 0x3280
jmp 0x7f58
movq -0x18(%rbp), %rdi
leaq -0x108(%rbp), %rsi
movq %rsi, -0x1d0(%rbp)
callq 0x3330
movq -0x1d0(%rbp), %rdi
callq 0x3160
movq -0x160(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x1c8(%rbp)
leaq -0xe0(%rbp), %rdi
movl $0x2a, %esi
movl $0x2c, %edx
callq 0x40e0
movl %eax, -0x1bc(%rbp)
jmp 0x7fab
movl -0x1bc(%rbp), %esi
movq -0x160(%rbp), %rdi
callq 0x9390
movl %eax, -0x1d4(%rbp)
jmp 0x7fc5
movl -0x1d4(%rbp), %edx
movq -0x1a0(%rbp), %rsi
movq -0x1c8(%rbp), %rdi
callq 0x3aa0
jmp 0x7fe0
movl $0x1, -0x114(%rbp)
movl -0x114(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x81a1
movq -0x160(%rbp), %rdi
callq 0x5140
movq -0x160(%rbp), %rdi
movl %eax, -0x118(%rbp)
movl -0x118(%rbp), %esi
movl $0x1, %edx
callq 0x5270
movl -0x114(%rbp), %ecx
movl %ecx, %eax
leal 0x2a(%rax,%rax), %esi
leal 0x2c(%rax,%rax), %edx
movl -0x118(%rbp), %ecx
leaq -0xe0(%rbp), %rdi
callq 0x44b0
movl $0x400, -0x11c(%rbp)    # imm = 0x400
movq -0x18(%rbp), %rdi
callq 0x3420
movl %eax, -0x120(%rbp)
leaq -0x11c(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x9920
movq %rax, -0x1e0(%rbp)
jmp 0x807c
movq -0x1a0(%rbp), %rdi
movq -0x1e0(%rbp), %rax
movl (%rax), %edx
movq -0x18(%rbp), %rcx
xorl %esi, %esi
callq 0x4500
movq -0x18(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movl $0x400, -0x144(%rbp)    # imm = 0x400
movq -0x18(%rbp), %rdi
callq 0x3420
movl %eax, -0x148(%rbp)
leaq -0x144(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x9920
movq %rax, -0x1e8(%rbp)
jmp 0x80d7
movq -0x1f0(%rbp), %rsi
movq -0x1e8(%rbp), %rax
movslq (%rax), %rdx
leaq -0x140(%rbp), %rdi
movq $-0x1, %rcx
callq 0x3280
jmp 0x80fd
movq -0x18(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq %rsi, -0x208(%rbp)
callq 0x3330
movq -0x208(%rbp), %rdi
callq 0x3160
movq -0x160(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x200(%rbp)
movl -0x114(%rbp), %ecx
movl %ecx, %eax
leal 0x2a(%rax,%rax), %esi
leal 0x2c(%rax,%rax), %edx
leaq -0xe0(%rbp), %rdi
callq 0x40e0
movl %eax, -0x1f4(%rbp)
jmp 0x8156
movl -0x1f4(%rbp), %esi
movq -0x160(%rbp), %rdi
callq 0x9390
movl %eax, -0x20c(%rbp)
jmp 0x8170
movl -0x20c(%rbp), %edx
movq -0x1a0(%rbp), %rsi
movq -0x200(%rbp), %rdi
callq 0x3aa0
jmp 0x818b
jmp 0x818d
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0x7fea
movq -0x160(%rbp), %rdi
movl -0x80(%rbp), %edx
leaq -0xe0(%rbp), %rsi
callq 0x7b20
jmp 0x81b9
movq -0x160(%rbp), %rdi
callq 0x5300
jmp 0x81c7
movb $0x1, -0x1(%rbp)
movl $0x1, -0x58(%rbp)
movq -0x90(%rbp), %rax
movq %rax, %rsp
leaq -0x40(%rbp), %rdi
callq 0x3160
movb -0x1(%rbp), %al
andb $0x1, %al
movq %rbp, %rsp
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x3160
movq -0x50(%rbp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)