// File: /workspace/github_cpp_makefile_star2_O3/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 $0x838, %rsp            # imm = 0x838
movq %rsi, %r15
movq %rdi, %rbx
movq 0x5b85(%rip), %r14      # 0x8fd0
leaq 0x2c64(%rip), %rsi      # 0x60b6
movl $0xb, %edx
movq %r14, %rdi
callq 0x2240
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r14, %rdi
callq 0x2240
movq %rax, %r14
leaq 0x2c4a(%rip), %rsi      # 0x60c2
movl $0x6, %edx
movq %rax, %rdi
callq 0x2240
movq (%r15), %rsi
movq %r15, -0x30(%rbp)
movq 0x8(%r15), %rdx
movq %r14, %rdi
callq 0x2240
leaq 0x2ebd(%rip), %rsi      # 0x635c
movl $0x1, %edx
movq %rax, %rdi
callq 0x2240
leaq -0x260(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0xe, %edx
callq 0x2360
leaq -0x460(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x2100
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x240(%rbp,%rax)
je 0x352f
movq 0x5ae2(%rip), %rdi      # 0x8fd0
leaq 0x2bd4(%rip), %rsi      # 0x60c9
movl $0x6, %edx
callq 0x2240
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x5abf(%rip), %rdi      # 0x8fd0
callq 0x2240
leaq 0x2bb3(%rip), %rsi      # 0x60d0
movl $0x12, %edx
movq %rax, %rdi
callq 0x2240
jmp 0x3753
leaq -0x260(%rbp), %rdi
callq 0x2390
movq %rax, -0x38(%rbp)
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x21f0
movq %rax, %r14
movl $0x1, (%rax)
movl $0x0, 0xb08(%rax)
movq -0x38(%rbp), %rax
movl %eax, 0x504(%r14)
leaq -0x48(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0x55da
movq -0x58(%rbp), %rbx
movq -0x50(%rbp), %rdx
movq %r14, %rdi
addq $0x404, %rdi            # imm = 0x404
movq %rbx, %rsi
callq 0x2170
cmpq %r15, %rbx
je 0x35ad
movq %rbx, %rdi
callq 0x21b0
cmpl $0x0, -0x38(%rbp)
jle 0x36b2
xorl %r12d, %r12d
leaq -0x260(%rbp), %r13
leaq -0x860(%rbp), %rbx
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x22a0
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2100
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2290
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x21f0
movq %rax, %r15
movl $0x0, 0xb08(%rax)
movl $0x404, %edx            # imm = 0x404
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movl $0x4, %eax
movb -0x864(%rbp,%rax), %cl
testb %cl, %cl
je 0x3639
movb %cl, (%r15,%rax)
incq %rax
cmpq $0x404, %rax            # imm = 0x404
jne 0x361f
movslq 0x1760e0(%rip), %rcx    # 0x179720
leal 0x1(%rcx), %eax
movl %eax, 0x1760d7(%rip)    # 0x179720
leaq 0x1740d0(%rip), %rdx    # 0x177720
movq %r15, (%rdx,%rcx,8)
cmpl $0x400, %eax            # imm = 0x400
jne 0x366d
leaq 0x1740be(%rip), %rdi    # 0x177720
callq 0x4644
movl 0x1760b3(%rip), %eax    # 0x179720
movl 0x105bd1(%rip), %ecx    # 0x109244
shll $0xa, %ecx
addl %eax, %ecx
movslq 0xb08(%r14), %rdx
leal 0x1(%rdx), %esi
movl %esi, 0xb08(%r14)
movl %ecx, 0x508(%r14,%rdx,4)
movq %r12, %rdx
shrq $0x8, %rdx
movl %ecx, -0x460(%rbp,%rdx)
addq $0x400, %r12            # imm = 0x400
cmpl -0x38(%rbp), %r12d
jl 0x35c8
jmp 0x36b8
movl 0x176068(%rip), %eax    # 0x179720
leal 0x1(%rax), %edx
movl %edx, 0x17605f(%rip)    # 0x179720
leaq 0x174058(%rip), %rcx    # 0x177720
cltq
movq %r14, (%rcx,%rax,8)
cmpl $0x400, %edx            # imm = 0x400
jne 0x36e8
leaq 0x174043(%rip), %rdi    # 0x177720
callq 0x4644
movl 0x176038(%rip), %edx    # 0x179720
leaq 0x174031(%rip), %rdi    # 0x177720
leaq -0x460(%rbp), %rsi
callq 0x37bc
movl 0x174017(%rip), %eax    # 0x177718
movslq 0x169f48(%rip), %rcx    # 0x16d650
leaq (%rcx,%rcx,4), %rcx
leaq 0x105f3d(%rip), %rdx    # 0x109650
leaq 0x8(%rdx,%rcx,8), %rdi
movl %eax, -0x8(%rdi)
movq -0x30(%rbp), %rsi
callq 0x20f0
incl 0x169f26(%rip)          # 0x16d650
movl 0x105b14(%rip), %eax    # 0x109244
shll $0xa, %eax
addl 0x175fe7(%rip), %eax    # 0x179720
movslq 0x173fd8(%rip), %rcx    # 0x177718
leaq 0x169fd1(%rip), %rdx    # 0x16d718
leal 0x1(%rcx), %esi
movl %esi, 0x173fc8(%rip)    # 0x177718
movl %eax, (%rdx,%rcx,4)
leaq -0x260(%rbp), %rdi
callq 0x2070
xorl %eax, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x377d
jmp 0x377d
jmp 0x377d
jmp 0x377d
jmp 0x377d
movq %rax, %rbx
leaq -0x260(%rbp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x23a0