// File: rtackx[P]ComSim/graph.cpp
// Function: Graph::load_graph(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Graph::load_graph(string bip_filename)
{
	char delim = ' ';

	int fd = open(bip_filename.c_str(), O_RDONLY);
	if(fd == -1)
	{
		perror("Can't open file.");
		exit(EXIT_FAILURE);
	}

	Node* node, *node_tmp;

	char* w = (char*) malloc(sizeof(char) * SIZE_BUFFER);
	string word;
	char c;
	float weight;
	int i, size_w, count;
	bool new_w, new_line;
	new_line = new_w;

	unsigned int index = 0;
	int index1, index2;
	unordered_map<string, unsigned int> map_word;
	unordered_map<unsigned int, Node*> map_node;
	unordered_map<unsigned int, set<pair<unsigned int, float>, comp_pair>> map_neighbor;

	index2 = -1;
	size_w = 0;
	count = 0;
	weight = 1.0;
	node_tmp = NULL;

	do
	{
		i = read(fd, &c, 1);

		if(size_w == SIZE_BUFFER)
			w = (char*) realloc(w, sizeof(char) * SIZE_BUFFER + size_w);

		if(c == '\r')
			continue;

		if(c == delim || c == '\n')
		{
			if(c == '\n')
				new_line = true;

			w[size_w] = '\0';
			new_w = true;
			word = w;
			count++;
		}
		else
		{
			w[size_w] = c;
			size_w++;
		}

		if(new_w && size_w > 0)
		{
			new_w = false;
			if(count == 3)
			{
				weight = stof(word);
			}
			else
			{
				if(map_word.find(word) == map_word.end())
				{
					node = new Node();
					node->id = word;
					map_node[index] = node;
					map_word[word] = index;

					index1 = index;
					index++;
				}
				else
					index1 = map_word[word];
			}

			if(new_line)
			{
				map_neighbor[index1].insert(make_pair(index2, weight));
				map_neighbor[index2].insert(make_pair(index1, weight));

				new_line = false;
				count = 0;
				weight = 1.0;
				index2 = -1;
			}
			else
			{
				if(index2 == -1)
					index2 = index1;
			}
			
			free(w);
			w = (char*) malloc(sizeof(char) * SIZE_BUFFER);
			size_w = 0;
		}

	} while(i != 0);
	free(w);

	create_graph(map_node, map_neighbor);
}

// Assembly:
subq $0x228, %rsp            # imm = 0x228
movq %rsi, 0x88(%rsp)
movq %rdi, %rax
movq 0x88(%rsp), %rdi
movq %rax, 0x220(%rsp)
movq %rdi, 0x218(%rsp)
movq 0x220(%rsp), %rax
movq %rax, 0x90(%rsp)
movb $0x20, 0x217(%rsp)
callq 0x30f0
movq %rax, %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x31a0
movl %eax, 0x210(%rsp)
cmpl $-0x1, 0x210(%rsp)
jne 0x6eba
leaq 0x1b9e9(%rip), %rdi     # 0x22894
callq 0x31d0
movl $0x1, %edi
callq 0x32b0
movl $0x400, %edi            # imm = 0x400
callq 0x3300
movq %rax, 0x1f8(%rsp)
leaq 0x1d8(%rsp), %rdi
callq 0x3340
movb 0x1c3(%rsp), %al
andb $0x1, %al
movb %al, 0x1c2(%rsp)
movl $0x0, 0x1bc(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x7790
leaq 0x140(%rsp), %rdi
callq 0x77a0
leaq 0x108(%rsp), %rdi
callq 0x77b0
movl $0xffffffff, 0x1b4(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x1c8(%rsp)
movl $0x0, 0x1c4(%rsp)
movss 0x1b0c0(%rip), %xmm0    # 0x22004
movss %xmm0, 0x1d0(%rsp)
movq $0x0, 0x200(%rsp)
movl 0x210(%rsp), %edi
leaq 0x1d7(%rsp), %rsi
movl $0x1, %edx
callq 0x3350
movq %rax, 0x80(%rsp)
jmp 0x6f7c
movq 0x80(%rsp), %rax
movl %eax, 0x1cc(%rsp)
cmpl $0x400, 0x1c8(%rsp) # imm = 0x400
jne 0x6fd7
movq 0x1f8(%rsp), %rdi
movslq 0x1c8(%rsp), %rsi
addq $0x400, %rsi            # imm = 0x400
callq 0x33e0
movq %rax, 0x1f8(%rsp)
jmp 0x6fd7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
jmp 0x749d
movsbl 0x1d7(%rsp), %eax
cmpl $0xd, %eax
jne 0x6fe9
jmp 0x7427
movsbl 0x1d7(%rsp), %eax
movsbl 0x217(%rsp), %ecx
cmpl %ecx, %eax
je 0x700a
movsbl 0x1d7(%rsp), %eax
cmpl $0xa, %eax
jne 0x7065
movsbl 0x1d7(%rsp), %eax
cmpl $0xa, %eax
jne 0x701f
movb $0x1, 0x1c2(%rsp)
movq 0x1f8(%rsp), %rax
movslq 0x1c8(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movb $0x1, 0x1c3(%rsp)
movq 0x1f8(%rsp), %rsi
leaq 0x1d8(%rsp), %rdi
callq 0x3320
jmp 0x7052
movl 0x1c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c4(%rsp)
jmp 0x7090
movb 0x1d7(%rsp), %dl
movq 0x1f8(%rsp), %rax
movslq 0x1c8(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x1c8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c8(%rsp)
testb $0x1, 0x1c3(%rsp)
je 0x7425
cmpl $0x0, 0x1c8(%rsp)
jle 0x7425
movb $0x0, 0x1c3(%rsp)
cmpl $0x3, 0x1c4(%rsp)
jne 0x70eb
xorl %eax, %eax
movl %eax, %esi
leaq 0x1d8(%rsp), %rdi
callq 0x77c0
movss %xmm0, 0x7c(%rsp)
jmp 0x70d7
movss 0x7c(%rsp), %xmm0
movss %xmm0, 0x1d0(%rsp)
jmp 0x7264
leaq 0x178(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x7820
movq %rax, 0x70(%rsp)
jmp 0x7107
movq 0x70(%rsp), %rax
movq %rax, 0xf0(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x7850
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x7800
testb $0x1, %al
jne 0x7147
jmp 0x7238
movl $0x68, %edi
callq 0x3250
movq %rax, 0x68(%rsp)
jmp 0x7158
movq 0x68(%rsp), %rdi
callq 0x36a0
jmp 0x7164
movq 0x68(%rsp), %rax
movq %rax, 0x208(%rsp)
movq 0x208(%rsp), %rdi
addq $0x8, %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x3050
jmp 0x718c
movq 0x208(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x140(%rsp), %rdi
leaq 0x1bc(%rsp), %rsi
callq 0x7880
movq %rax, 0x60(%rsp)
jmp 0x71b5
movq 0x60(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, (%rax)
movl 0x1bc(%rsp), %eax
movl %eax, 0x4c(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x78b0
movq %rax, 0x50(%rsp)
jmp 0x71e9
movq 0x50(%rsp), %rax
movl 0x4c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1bc(%rsp), %eax
movl %eax, 0x1b8(%rsp)
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x7262
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
callq 0x3220
jmp 0x749d
leaq 0x178(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x78b0
movq %rax, 0x40(%rsp)
jmp 0x7254
movq 0x40(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1b8(%rsp)
jmp 0x7264
testb $0x1, 0x1c2(%rsp)
je 0x73e1
movl 0x1b8(%rsp), %eax
movl %eax, 0xe4(%rsp)
leaq 0x108(%rsp), %rdi
leaq 0xe4(%rsp), %rsi
callq 0x78e0
movq %rax, 0x38(%rsp)
jmp 0x729c
leaq 0x1b4(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x7970
movq %rax, 0x30(%rsp)
jmp 0x72b8
movq 0x30(%rsp), %rax
movq %rax, 0xd4(%rsp)
leaq 0xdc(%rsp), %rdi
leaq 0xd4(%rsp), %rsi
callq 0x79a0
jmp 0x72dc
movq 0x38(%rsp), %rdi
leaq 0xdc(%rsp), %rsi
callq 0x7910
movb %dl, 0x27(%rsp)
movq %rax, 0x28(%rsp)
jmp 0x72f9
movb 0x27(%rsp), %al
movq 0x28(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movb %al, 0xc8(%rsp)
movl 0x1b4(%rsp), %eax
movl %eax, 0xbc(%rsp)
leaq 0x108(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
callq 0x78e0
movq %rax, 0x18(%rsp)
jmp 0x733b
leaq 0x1b8(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x7970
movq %rax, 0x10(%rsp)
jmp 0x7357
movq 0x10(%rsp), %rax
movq %rax, 0xac(%rsp)
leaq 0xb4(%rsp), %rdi
leaq 0xac(%rsp), %rsi
callq 0x79a0
jmp 0x737b
movq 0x18(%rsp), %rdi
leaq 0xb4(%rsp), %rsi
callq 0x7910
movb %dl, 0x7(%rsp)
movq %rax, 0x8(%rsp)
jmp 0x7398
movb 0x7(%rsp), %al
movq 0x8(%rsp), %rcx
movq %rcx, 0x98(%rsp)
movb %al, 0xa0(%rsp)
movb $0x0, 0x1c2(%rsp)
movl $0x0, 0x1c4(%rsp)
movss 0x1ac39(%rip), %xmm0    # 0x22004
movss %xmm0, 0x1d0(%rsp)
movl $0xffffffff, 0x1b4(%rsp) # imm = 0xFFFFFFFF
jmp 0x73fb
cmpl $-0x1, 0x1b4(%rsp)
jne 0x73f9
movl 0x1b8(%rsp), %eax
movl %eax, 0x1b4(%rsp)
jmp 0x73fb
movq 0x1f8(%rsp), %rdi
callq 0x32a0
movl $0x400, %edi            # imm = 0x400
callq 0x3300
movq %rax, 0x1f8(%rsp)
movl $0x0, 0x1c8(%rsp)
jmp 0x7427
cmpl $0x0, 0x1cc(%rsp)
jne 0x6f59
movq 0x1f8(%rsp), %rdi
callq 0x32a0
movq 0x90(%rsp), %rdi
leaq 0x140(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0x74e0
jmp 0x7461
leaq 0x108(%rsp), %rdi
callq 0x79d0
leaq 0x140(%rsp), %rdi
callq 0x79e0
leaq 0x178(%rsp), %rdi
callq 0x79f0
leaq 0x1d8(%rsp), %rdi
callq 0x3150
addq $0x228, %rsp            # imm = 0x228
retq
leaq 0x108(%rsp), %rdi
callq 0x79d0
leaq 0x140(%rsp), %rdi
callq 0x79e0
leaq 0x178(%rsp), %rdi
callq 0x79f0
leaq 0x1d8(%rsp), %rdi
callq 0x3150
movq 0x100(%rsp), %rdi
callq 0x3400
nop