// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x848, %rsp            # imm = 0x848
movq %rsi, %r14
movq %rdi, %rbx
movq 0x50b2(%rip), %rdi      # 0x7fc8
leaq 0x2199(%rip), %rsi      # 0x50b6
callq 0x21e0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21c0
leaq 0x218e(%rip), %rsi      # 0x50c2
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
movq %r14, -0x30(%rbp)
movq %r14, %rsi
callq 0x21c0
leaq 0x240a(%rip), %rsi      # 0x535c
movq %rax, %rdi
callq 0x21e0
leaq -0x268(%rbp), %r15
pushq $0xe
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2350
leaq -0x470(%rbp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x2140
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x248(%rbp,%rax)
je 0x2fc9
movq 0x502b(%rip), %rdi      # 0x7fc8
leaq 0x2125(%rip), %rsi      # 0x50c9
callq 0x21e0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x21c0
leaq 0x2114(%rip), %rsi      # 0x50d0
movq %rax, %rdi
callq 0x21e0
jmp 0x317c
leaq -0x268(%rbp), %rdi
callq 0x2380
movq %rax, %rbx
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x2200
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x451a
movl %ebx, 0x504(%r15)
leaq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x22c0
movq %r15, -0x38(%rbp)
addq $0x404, %r15            # imm = 0x404
leaq -0x60(%rbp), %r12
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r15, %rdi
callq 0x2190
movq %r12, %rdi
callq 0x20f0
movslq %ebx, %rax
movq %rax, -0x40(%rbp)
xorl %r12d, %r12d
leaq -0x268(%rbp), %r13
cmpq -0x40(%rbp), %r12
jge 0x30f8
movq %r14, %r15
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2280
movl $0x400, %edx            # imm = 0x400
leaq -0x870(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x2140
movl $0x400, %edx            # imm = 0x400
movq %r13, %rbx
movq %r13, %rdi
movq %r14, %rsi
callq 0x2270
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x2200
movq %rax, %r13
movq %r15, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x451a
movq %r13, %rdi
leaq -0x870(%rbp), %rsi
callq 0x45a4
leaq 0x173670(%rip), %rdi    # 0x176720
movq %r13, %rsi
callq 0x31be
movl 0x105186(%rip), %eax    # 0x108244
shll $0xa, %eax
addl 0x175659(%rip), %eax    # 0x178720
movq -0x38(%rbp), %rsi
movslq 0xb08(%rsi), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0xb08(%rsi)
movl %eax, 0x508(%rsi,%rcx,4)
movl %eax, (%r14)
addq $0x400, %r12            # imm = 0x400
addq $0x4, %r14
movq %rbx, %r13
jmp 0x303d
leaq 0x173621(%rip), %rdi    # 0x176720
movq -0x38(%rbp), %rsi
callq 0x31be
movl 0x175612(%rip), %edx    # 0x178720
leaq 0x17360b(%rip), %rdi    # 0x176720
leaq -0x470(%rbp), %rsi
callq 0x31e6
movl 0x1735f1(%rip), %eax    # 0x176718
movslq 0x169522(%rip), %rcx    # 0x16c650
imulq $0x28, %rcx, %rcx
leaq 0x105517(%rip), %rdx    # 0x108650
leaq (%rcx,%rdx), %rdi
addq $0x8, %rdi
movl %eax, -0x8(%rdi)
movq -0x30(%rbp), %rsi
callq 0x2130
incl 0x1694fd(%rip)          # 0x16c650
movl 0x1050eb(%rip), %eax    # 0x108244
shll $0xa, %eax
addl 0x1755be(%rip), %eax    # 0x178720
movslq 0x1735af(%rip), %rcx    # 0x176718
leaq 0x1695a8(%rip), %rdx    # 0x16c718
leal 0x1(%rcx), %esi
movl %esi, 0x17359f(%rip)    # 0x176718
movl %eax, (%rdx,%rcx,4)
leaq -0x268(%rbp), %rdi
callq 0x2080
xorl %eax, %eax
addq $0x848, %rsp            # imm = 0x848
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31a6
jmp 0x31a6
jmp 0x31a6
jmp 0x31a6
jmp 0x31a6
movq %rax, %rbx
leaq -0x268(%rbp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2390
nop