// File: jasperandrew[P]cs350-project2/./lab7.h
// Function: import(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int import(string filepath, string filename)
{
	cout << "Importing '" << filepath << "' --> " << filename << "\n";
	ifstream input_file(filepath, ios::in | ios::ate | ios::binary);
    block_num blocknums[128] = {0};
	if(input_file){
		int file_len = input_file.tellg();
		Block *inode_block = new Block(1);
		inode_block->setSize(file_len);
		inode_block->setFilename(filename);

		for(int i = 0; i < file_len; i += BLOCK_SZ){
			input_file.seekg(i);
			char tmp_data[BLOCK_SZ] = {0};
			input_file.read(tmp_data, BLOCK_SZ);

			Block *data_block = new Block(0);
			data_block->setData(tmp_data);

			wbuffer.addBlock(data_block);
			int block_number = BLOCK_SZ * current_segment + wbuffer.getNumBlocks();
			inode_block->addBlockNum(block_number);
            blocknums[i/BLOCK_SZ] = block_number;
		}

		wbuffer.addBlock(inode_block);
        wbuffer.addSegInfo(blocknums,wbuffer.getNumBlocks());

		g_filemap[g_filemap_ctr].num = g_imap.idx;
		g_filemap[g_filemap_ctr].name = filename;
		g_filemap_ctr++;

		g_imap.list[g_imap.idx++] = BLOCK_SZ * current_segment + wbuffer.getNumBlocks(); 

		//Block * imap_block = new Block(2);
		//wbuffer.addBlock(imap_block);

		//if(imap_block->isFull()) Checkpoint_Region.imaps[Checkpoint_Region_counter++] = BLOCK_SZ * current_segment + wbuffer.getNumBlocks()%BLOCK_SZ; 
		//else writeCheckpoint();
	}
	else
	{
		cout << "File '" << filename << "' does not exist!\n";
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x910, %rsp            # imm = 0x910
movq %rdi, -0x898(%rbp)
movq %rsi, -0x8a0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq 0x8a60(%rip), %rdi      # 0xcfc8
leaq 0x4b37(%rip), %rsi      # 0x90a6
callq 0x3220
movq -0x898(%rbp), %rsi
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
leaq 0x4b25(%rip), %rsi      # 0x90b2
callq 0x3220
movq -0x8a0(%rbp), %rsi
movq %rax, %rdi
callq 0x31f0
movq %rax, %rdi
leaq 0x4da1(%rip), %rsi      # 0x934c
callq 0x3220
movl $0x8, %edi
movl $0x2, %esi
callq 0x6c00
movl %eax, %edi
movl $0x4, %esi
callq 0x6c00
movq -0x898(%rbp), %rsi
movl %eax, %edx
leaq -0x218(%rbp), %rdi
callq 0x33d0
leaq -0x420(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3130
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x32c0
movb %al, -0x88d(%rbp)
jmp 0x4613
movb -0x88d(%rbp), %al
testb $0x1, %al
jne 0x4622
jmp 0x49d5
leaq -0x218(%rbp), %rdi
callq 0x3420
movq %rdx, -0x8b0(%rbp)
movq %rax, -0x8a8(%rbp)
jmp 0x463e
movq -0x8b0(%rbp), %rax
movq -0x8a8(%rbp), %rcx
movq %rcx, -0x440(%rbp)
movq %rax, -0x438(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x6d50
movq %rax, -0x8b8(%rbp)
jmp 0x466f
movq -0x8b8(%rbp), %rax
movl %eax, -0x430(%rbp)
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x3240
movq %rax, -0x8c0(%rbp)
jmp 0x468f
movq -0x8c0(%rbp), %rdi
movl $0x1, %esi
callq 0x6b10
jmp 0x46a2
movq -0x8c0(%rbp), %rax
movq %rax, -0x448(%rbp)
movq -0x448(%rbp), %rdi
movl -0x430(%rbp), %esi
callq 0x6d70
jmp 0x46c4
movq -0x8a0(%rbp), %rsi
movq -0x448(%rbp), %rax
movq %rax, -0x8c8(%rbp)
leaq -0x468(%rbp), %rdi
callq 0x30f0
jmp 0x46e7
movq -0x8c8(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x6d90
jmp 0x46fc
leaq -0x468(%rbp), %rdi
callq 0x3170
movl $0x0, -0x46c(%rbp)
movl -0x46c(%rbp), %eax
cmpl -0x430(%rbp), %eax
jge 0x48eb
movslq -0x46c(%rbp), %rsi
leaq -0x480(%rbp), %rdi
callq 0x6c60
jmp 0x4739
movq -0x480(%rbp), %rsi
movq -0x478(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x32e0
jmp 0x4755
leaq -0x880(%rbp), %rdi
movq %rdi, -0x8d8(%rbp)
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
movq %rdx, -0x8d0(%rbp)
callq 0x3130
movq -0x8d8(%rbp), %rsi
movq -0x8d0(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x32d0
jmp 0x4792
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x3240
movq %rax, -0x8e0(%rbp)
jmp 0x47a5
movq -0x8e0(%rbp), %rdi
xorl %esi, %esi
callq 0x6b10
jmp 0x47b5
movq -0x8e0(%rbp), %rax
movq %rax, -0x888(%rbp)
movq -0x888(%rbp), %rdi
leaq -0x880(%rbp), %rsi
callq 0x6e00
jmp 0x47d8
movq -0x888(%rbp), %rsi
leaq 0x176f8a(%rip), %rdi    # 0x17b770
callq 0x4a60
jmp 0x47ed
movl 0x108aa1(%rip), %eax    # 0x10d294
shll $0xa, %eax
movl %eax, -0x8e8(%rbp)
leaq 0x176f6d(%rip), %rdi    # 0x17b770
callq 0x3d90
movl %eax, -0x8e4(%rbp)
jmp 0x4810
movl -0x8e4(%rbp), %ecx
movl -0x8e8(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x88c(%rbp)
movq -0x448(%rbp), %rdi
movl -0x88c(%rbp), %esi
callq 0x6e60
jmp 0x4838
movl -0x88c(%rbp), %ecx
movl -0x46c(%rbp), %eax
movl $0x400, %esi            # imm = 0x400
cltd
idivl %esi
cltq
movl %ecx, -0x420(%rbp,%rax,4)
movl -0x46c(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
movl %eax, -0x46c(%rbp)
jmp 0x4712
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x428(%rbp)
movl %eax, -0x42c(%rbp)
jmp 0x4a3b
movq -0x8c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x428(%rbp)
movl %eax, -0x42c(%rbp)
callq 0x3200
jmp 0x4a3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x428(%rbp)
movl %eax, -0x42c(%rbp)
leaq -0x468(%rbp), %rdi
callq 0x3170
jmp 0x4a3b
movq -0x8e0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x428(%rbp)
movl %eax, -0x42c(%rbp)
callq 0x3200
jmp 0x4a3b
movq -0x448(%rbp), %rsi
leaq 0x176e77(%rip), %rdi    # 0x17b770
callq 0x4a60
jmp 0x4900
leaq -0x420(%rbp), %rax
movq %rax, -0x8f8(%rbp)
leaq 0x176e5b(%rip), %rdi    # 0x17b770
callq 0x3d90
movl %eax, -0x8ec(%rbp)
jmp 0x4922
movl -0x8ec(%rbp), %edx
movq -0x8f8(%rbp), %rsi
leaq 0x176e3a(%rip), %rdi    # 0x17b770
callq 0x4ab0
jmp 0x493d
movq -0x8a0(%rbp), %rsi
movl 0x176e1e(%rip), %edx    # 0x17b768
movslq 0x16cd4f(%rip), %rax    # 0x1716a0
leaq (%rax,%rax,4), %rcx
leaq 0x108d44(%rip), %rax    # 0x10d6a0
movl %edx, (%rax,%rcx,8)
movslq 0x16cd3a(%rip), %rcx    # 0x1716a0
leaq (%rcx,%rcx,4), %rcx
leaq 0x8(%rax,%rcx,8), %rdi
callq 0x3030
jmp 0x4976
movl 0x16cd24(%rip), %eax    # 0x1716a0
incl %eax
movl %eax, 0x16cd1c(%rip)    # 0x1716a0
movl 0x10890a(%rip), %eax    # 0x10d294
shll $0xa, %eax
movl %eax, -0x900(%rbp)
leaq 0x176dd6(%rip), %rdi    # 0x17b770
callq 0x3d90
movl %eax, -0x8fc(%rbp)
jmp 0x49a7
movl -0x8fc(%rbp), %eax
movl -0x900(%rbp), %edx
addl %eax, %edx
movl 0x176dad(%rip), %eax    # 0x17b768
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x176da2(%rip)    # 0x17b768
movslq %eax, %rcx
leaq 0x16cd98(%rip), %rax    # 0x171768
movl %edx, (%rax,%rcx,4)
jmp 0x4a24
movq 0x85ec(%rip), %rdi      # 0xcfc8
leaq 0x46d6(%rip), %rsi      # 0x90b9
callq 0x3220
movq %rax, -0x908(%rbp)
jmp 0x49f1
movq -0x8a0(%rbp), %rsi
movq -0x908(%rbp), %rdi
callq 0x31f0
movq %rax, -0x910(%rbp)
jmp 0x4a0d
movq -0x910(%rbp), %rdi
leaq 0x46a5(%rip), %rsi      # 0x90c0
callq 0x3220
jmp 0x4a22
jmp 0x4a24
leaq -0x218(%rbp), %rdi
callq 0x3090
xorl %eax, %eax
addq $0x910, %rsp            # imm = 0x910
popq %rbp
retq
leaq -0x218(%rbp), %rdi
callq 0x3090
movq -0x428(%rbp), %rdi
callq 0x3430
nopw %cs:(%rax,%rax)