// File: /workspace/github_cpp_makefile_star2_O1/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 0x5bd3(%rip), %r14      # 0x8fd0
leaq 0x2cb2(%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 0x2c98(%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 0x2f0b(%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 0x34e1
movq 0x5b30(%rip), %rdi      # 0x8fd0
leaq 0x2c22(%rip), %rsi      # 0x60c9
movl $0x6, %edx
callq 0x2240
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq 0x5b0d(%rip), %rdi      # 0x8fd0
callq 0x2240
leaq 0x2c01(%rip), %rsi      # 0x60d0
movl $0x12, %edx
movq %rax, %rdi
callq 0x2240
jmp 0x3701
leaq -0x260(%rbp), %rdi
callq 0x2390
movq %rax, -0x38(%rbp)
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x21f0
movq %rax, %r15
movl $0x1, (%rax)
movl $0x0, 0xb08(%rax)
movq -0x38(%rbp), %rax
movl %eax, 0x504(%r15)
leaq -0x48(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0x5524
movq -0x58(%rbp), %rbx
movq -0x50(%rbp), %rdx
movq %r15, %rdi
addq $0x404, %rdi            # imm = 0x404
movq %rbx, %rsi
callq 0x2170
cmpq %r14, %rbx
je 0x355f
movq %rbx, %rdi
callq 0x21b0
cmpl $0x0, -0x38(%rbp)
jle 0x3661
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, %r14
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 0x35eb
movb %cl, (%r14,%rax)
incq %rax
cmpq $0x404, %rax            # imm = 0x404
jne 0x35d1
movslq 0x17612e(%rip), %rax    # 0x179720
leal 0x1(%rax), %ecx
movl %ecx, 0x176125(%rip)    # 0x179720
leaq 0x17411e(%rip), %rdx    # 0x177720
movq %r14, (%rdx,%rax,8)
cmpl $0x400, %ecx            # imm = 0x400
jne 0x361a
leaq 0x17410b(%rip), %rdi    # 0x177720
callq 0x45b4
movl 0x105c24(%rip), %eax    # 0x109244
shll $0xa, %eax
addl 0x1760f7(%rip), %eax    # 0x179720
movslq 0xb08(%r15), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0xb08(%r15)
movl %eax, 0x508(%r15,%rcx,4)
movq %r12, %rcx
shrq $0x8, %rcx
movl %eax, -0x460(%rbp,%rcx)
addq $0x400, %r12            # imm = 0x400
cmpl -0x38(%rbp), %r12d
jl 0x357a
movslq 0x1760b8(%rip), %rax    # 0x179720
leaq 0x1740b1(%rip), %rcx    # 0x177720
leal 0x1(%rax), %edx
movl %edx, 0x1760a8(%rip)    # 0x179720
movq %r15, (%rcx,%rax,8)
cmpl $0x400, %edx            # imm = 0x400
jne 0x3690
leaq 0x174095(%rip), %rdi    # 0x177720
callq 0x45b4
movl 0x17608a(%rip), %edx    # 0x179720
leaq 0x174083(%rip), %rdi    # 0x177720
leaq -0x460(%rbp), %rsi
callq 0x376a
movl 0x174069(%rip), %eax    # 0x177718
movslq 0x169f9a(%rip), %rcx    # 0x16d650
leaq (%rcx,%rcx,4), %rcx
leaq 0x105f8f(%rip), %rdx    # 0x109650
leaq 0x8(%rdx,%rcx,8), %rdi
movl %eax, -0x8(%rdi)
movq -0x30(%rbp), %rsi
callq 0x20f0
incl 0x169f78(%rip)          # 0x16d650
movl 0x105b66(%rip), %eax    # 0x109244
shll $0xa, %eax
addl 0x176039(%rip), %eax    # 0x179720
movslq 0x17402a(%rip), %rcx    # 0x177718
leaq 0x16a023(%rip), %rdx    # 0x16d718
leal 0x1(%rcx), %esi
movl %esi, 0x17401a(%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 0x372b
jmp 0x372b
jmp 0x372b
jmp 0x372b
jmp 0x372b
movq %rax, %rbx
leaq -0x260(%rbp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x23a0