// File: /workspace/github_cpp_makefile_star2_O3/SNUCSE-CTA[P]GI/src/graph.cpp
// Function: Graph::readGraph(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Graph::readGraph(string aFileName) //recommend to use after checkFormat() passed
{
	#ifdef DEBUG
	cout << __PRETTY_FUNCTION__ << endl;
	#endif

	clear(); // avoid double allocation

	ifstream infile(aFileName);

	streampos edgePos = infile.tellg();
	char tag;
	int32_t vid, vlabel, left, right, elabel, gid;
	string line;
	numNode = -1;
	numEdge = 0;

	//first read: read vertices and count degree for each vertex
	while( infile >> tag ) {
		if( tag == 't' ) {
			infile >> gid >> numNode;

			//allocate memory for vertices
			od = new int32_t[numNode];
			d = new int32_t[numNode];
			l = new int32_t[numNode];
			one = new char[numNode];
		}
		else if( tag == 'v' ) {
			infile >> vid >> vlabel;

			d[vid] = 0;
			l[vid] = vlabel;
			one[vid] = 0;

			edgePos = infile.tellg(); //record the last line that tag 'v' appears.
		}
		else if( tag == 'e' ) {
			infile >> left >> right >> elabel;

			if (left == right) {
				cerr << "Error: loops are not allowed." << endl;
				errfile = true;
				infile.close();
				return;
			}

			++(d[left]);
			++(d[right]);
			++numEdge;
		}
	}

	//allocate memory for edges
	e = new int32_t*[numNode];
	for(vid = 0; vid < numNode; ++vid) {
		e[vid] = new int32_t[ d[vid] ];
		d[vid] = 0; //we will use it as index in the second read.
	}

	//second read: read edges
	infile.clear();
	infile.seekg(edgePos);

	while( infile >> tag ) {
		if( tag == 'e' ) { //always true after checkFormat()
			infile >> left >> right >> elabel;
			e[left][ d[left] ] = right;
			e[right][ d[right] ] = left;
			++(d[left]);
			++(d[right]);
		}
	}

	//store original degree (since prepCoreOne changes d)
	for(vid = 0; vid < numNode; ++vid)
		od[vid] = d[vid];

	// Make sure there are no parallel edges.
	int32_t* s = cont.global_memory.getLLArray(numNode);
	int32_t s_top = -1;

	int32_t* adj = cont.global_memory.getLLArray(numNode);
	memset(adj, 0, sizeof(int32_t) * numNode);

	// For each node u in the graph,
	for (int32_t u = 0; u < numNode && !errfile; ++u) {
		// and for each neighbor v of u,
		for (int32_t i = 0; i < d[u] && !errfile; ++i) {
			int32_t v = e[u][i];

			// mark v as adjacent.
			// If v was already marked as adjacent, then there exists a parallel edge.
			if (!adj[v]) {
				adj[v] = 1;

				++s_top;
				s[s_top] = v;
			} else {
				errfile = true;
			}
		}

		// Clean marks for the next iteration.
		while (!(s_top < 0)) {
			int32_t v = s[s_top];
			--s_top;

			adj[v] = 0;
		}
	}

	cont.global_memory.returnLLArray(adj, numNode);
	cont.global_memory.returnLLArray(s, numNode);

	if (errfile) {
		cerr << "Error: no parallel edges are allowed." << endl;
		infile.close();
		return;
	}

	// Make sure the graph is connected
	int32_t* q = cont.global_memory.getLLArray(numNode);
	int32_t q_front = 0;
	int32_t q_rear = -1;

	int32_t* pushed = cont.global_memory.getLLArray(numNode);
	memset(pushed, 0, sizeof(int32_t) * numNode);

	// Perform a BFS on the graph, starting from vertex 0.
	// Enqueue vertex 0.
	++q_rear;
	q[q_rear] = 0;
	pushed[0] = true;
	while (!(q_front > q_rear)) {
		// Pop a vertex from the queue, and call it u.
		int32_t u = q[q_front];
		++q_front;

		// For each neighbor v of u (the current vertex),
		for (int i = 0; i < d[u]; ++i) {
			int32_t v = e[u][i];

			// if v is not visited, mark v as visited and enqueue v.
			if (!pushed[v]) {
				++q_rear;
				q[q_rear] = v;
				pushed[v] = true;
			}
		}
	}

	// If any vertex is not visited, then the graph is not connected.
	for (int32_t u = 0; u < numNode && !errfile; ++u) {
		if (!pushed[u]) {
			errfile = true;
		}
	}

	cont.global_memory.returnLLArray(q, numNode);
	cont.global_memory.returnLLArray(pushed, numNode);

	if (errfile) {
		cerr << "Error: the graph is not connected." << endl;
		infile.close();
		return;
	}

	//calculate the start position of edge list for each vertex
	adjPos = new int32_t[numNode];
	adjPos[0] = 0;
	for(vid = 1; vid < numNode; ++vid)
		adjPos[vid] = adjPos[vid - 1] + d[vid - 1];

	infile.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rsi, %r14
movq %rdi, %rbx
callq 0x454a
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x2280
movq %r15, %rdi
callq 0x22e0
movq %rdx, 0x48(%rsp)
movq %rax, 0x50(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xc(%rbx), %rax
movq %rax, 0x18(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0xc(%rbx)
leaq 0x60(%rsp), %r15
leaq 0xb(%rsp), %rbp
leaq 0x5c(%rsp), %r13
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2200
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4009
movzbl 0xb(%rsp), %eax
cmpl $0x65, %eax
je 0x3fbc
cmpl $0x74, %eax
je 0x3f51
cmpl $0x76, %eax
jne 0x3ebe
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x20f0
movq %rax, %rdi
leaq 0x44(%rsp), %rsi
callq 0x20f0
movq 0x20(%rbx), %rax
movslq 0xc(%rsp), %rcx
movl $0x0, (%rax,%rcx,4)
movl 0x44(%rsp), %eax
movq 0x28(%rbx), %rcx
movslq 0xc(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
movq 0x30(%rbx), %rax
movslq 0xc(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq %r15, %rdi
callq 0x22e0
movq %rax, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
jmp 0x3ebe
movq %r15, %rdi
movq %r13, %rsi
callq 0x20f0
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x20f0
movq 0x18(%rsp), %rax
movslq (%rax), %r14
leaq (,%r14,4), %r12
testq %r14, %r14
movq $-0x1, %rax
cmovsq %rax, %r12
movq %r12, %rdi
callq 0x2030
movq %rax, 0x18(%rbx)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x20(%rbx)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x28(%rbx)
movq %r14, %rdi
callq 0x2030
movq %rax, 0x30(%rbx)
jmp 0x3ebe
movq %r15, %rdi
leaq 0x14(%rsp), %rsi
callq 0x20f0
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
callq 0x20f0
movq %rax, %rdi
leaq 0x40(%rsp), %rsi
callq 0x20f0
movslq 0x14(%rsp), %rax
cmpl 0x10(%rsp), %eax
je 0x440c
movq 0x20(%rbx), %rcx
incl (%rcx,%rax,4)
movslq 0x10(%rsp), %rax
incl (%rcx,%rax,4)
incl 0x10(%rbx)
jmp 0x3ebe
movq 0x18(%rsp), %rax
movslq (%rax), %r14
leaq (,%r14,8), %rax
testq %r14, %r14
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x38(%rbx)
movl $0x0, 0xc(%rsp)
testl %r14d, %r14d
jle 0x4088
movq 0x20(%rbx), %rax
xorl %ecx, %ecx
movq $-0x1, %r14
movslq %ecx, %r12
movslq (%rax,%r12,4), %rax
leaq (,%rax,4), %rdi
testq %rax, %rax
cmovsq %r14, %rdi
callq 0x2030
movq 0x38(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x20(%rbx), %rax
movl $0x0, (%rax,%r12,4)
movl 0xc(%rsp), %ecx
incl %ecx
movl %ecx, 0xc(%rsp)
cmpl 0xc(%rbx), %ecx
jl 0x404a
movq 0x60(%rsp), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x22c0
leaq 0x60(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x21d0
leaq 0x60(%rsp), %r15
leaq 0xb(%rsp), %r12
leaq 0x14(%rsp), %r13
leaq 0x10(%rsp), %rbp
leaq 0x40(%rsp), %r14
movq %r15, %rdi
movq %r12, %rsi
callq 0x2200
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4143
cmpb $0x65, 0xb(%rsp)
jne 0x40c8
movq %r15, %rdi
movq %r13, %rsi
callq 0x20f0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x20f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x20f0
movslq 0x10(%rsp), %rax
movq 0x20(%rbx), %rcx
movq 0x38(%rbx), %rdx
movslq 0x14(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movslq (%rcx,%rsi,4), %rsi
movl %eax, (%rdi,%rsi,4)
movslq 0x14(%rsp), %rsi
movq (%rdx,%rax,8), %rdx
movslq (%rcx,%rax,4), %rax
movl %esi, (%rdx,%rax,4)
incl (%rcx,%rsi,4)
movslq 0x10(%rsp), %rax
incl (%rcx,%rax,4)
jmp 0x40c8
movl $0x0, 0xc(%rsp)
movq 0x18(%rsp), %r13
movl (%r13), %esi
testl %esi, %esi
jle 0x417d
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
xorl %edx, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
movl 0xc(%rsp), %edx
incl %edx
movl %edx, 0xc(%rsp)
movl (%r13), %esi
cmpl %esi, %edx
jl 0x4162
movq (%rbx), %rdi
addq $0x18, %rdi
callq 0x4aba
movq %rax, %r15
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x4aba
movq %rax, %r12
movslq (%r13), %rdx
shlq $0x2, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movl (%r13), %edx
testl %edx, %edx
jle 0x424e
movb 0x9(%rbx), %al
testb $0x1, %al
jne 0x424e
movq 0x20(%rbx), %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl (%rcx,%rsi,4), %r9d
testl %r9d, %r9d
jle 0x423e
movq 0x38(%rbx), %rdx
xorl %r8d, %r8d
testb $0x1, %al
jne 0x4221
movq (%rdx,%rsi,8), %r10
movl (%r10,%r8,4), %r10d
movslq %r10d, %r10
cmpl $0x0, (%r12,%r10,4)
je 0x4200
movb $0x1, 0x9(%rbx)
movb $0x1, %al
jmp 0x4216
movl $0x1, (%r12,%r10,4)
movslq %edi, %r9
incl %edi
movl %r10d, 0x4(%r15,%r9,4)
movl (%rcx,%rsi,4), %r9d
incq %r8
movslq %r9d, %r10
cmpq %r10, %r8
jl 0x41e2
testl %edi, %edi
js 0x423e
movl %edi, %edx
movslq (%r15,%rdx,4), %rdi
movl $0x0, (%r12,%rdi,4)
addq $-0x1, %rdx
jb 0x4227
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
incq %rsi
movslq (%r13), %rdx
cmpq %rdx, %rsi
jge 0x424e
testb $0x1, %al
je 0x41d2
movq (%rbx), %rdi
addq $0x18, %rdi
movq %r12, %rsi
callq 0x4c64
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %edx
movq %r15, %rsi
callq 0x4c64
cmpb $0x1, 0x9(%rbx)
jne 0x42ca
movq 0xbd74(%rip), %rbx      # 0xfff0
leaq 0x8076(%rip), %rsi      # 0xc2f9
movl $0x25, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0xbd42(%rip), %rdi      # 0xfff0
callq 0x2040
movq %rax, %rdi
callq 0x2100
leaq 0x60(%rsp), %rdi
callq 0x2060
jmp 0x44c4
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x4aba
movq %rax, %r15
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x4aba
movq %rax, %r12
movslq 0xc(%rbx), %rdx
shlq $0x2, %rdx
xorl %r14d, %r14d
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movl $0x0, (%r15)
movl $0x1, (%r12)
movq 0x20(%rbx), %rax
movq 0x38(%rbx), %rcx
xorl %edx, %edx
movslq (%r15,%r14,4), %rsi
movl (%rax,%rsi,4), %r9d
testl %r9d, %r9d
jle 0x435f
movq (%rcx,%rsi,8), %rdi
xorl %r8d, %r8d
movl (%rdi,%r8,4), %r10d
movslq %r10d, %r10
cmpl $0x0, (%r12,%r10,4)
jne 0x4354
movslq %edx, %r9
incl %edx
movl %r10d, 0x4(%r15,%r9,4)
movl $0x1, (%r12,%r10,4)
movl (%rax,%rsi,4), %r9d
incq %r8
movslq %r9d, %r10
cmpq %r10, %r8
jl 0x4330
leaq 0x1(%r14), %rsi
movslq %edx, %rdi
cmpq %rdi, %r14
movq %rsi, %r14
jl 0x431c
movslq (%r13), %rdx
testq %rdx, %rdx
jle 0x4396
movb 0x9(%rbx), %cl
xorl %eax, %eax
testb $0x1, %cl
jne 0x4396
cmpl $0x0, (%r12,%rax,4)
jne 0x438e
movb $0x1, 0x9(%rbx)
movb $0x1, %cl
incq %rax
cmpq %rax, %rdx
jne 0x437c
movq (%rbx), %rdi
addq $0x18, %rdi
movq %r15, %rsi
callq 0x4c64
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %edx
movq %r12, %rsi
callq 0x4c64
cmpb $0x1, 0x9(%rbx)
jne 0x4462
movq 0xbc28(%rip), %rbx      # 0xfff0
leaq 0x7f50(%rip), %rsi      # 0xc31f
movl $0x22, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0xbbf6(%rip), %rdi      # 0xfff0
callq 0x2040
movq %rax, %rdi
callq 0x2100
jmp 0x44ba
movq 0xbbdd(%rip), %r14      # 0xfff0
leaq 0x7ec1(%rip), %rsi      # 0xc2db
movl $0x1d, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0xbbab(%rip), %rdi      # 0xfff0
callq 0x2040
movq %rax, %rdi
callq 0x2100
movb $0x1, 0x9(%rbx)
leaq 0x60(%rsp), %rdi
callq 0x2060
jmp 0x44c4
movslq (%r13), %r14
leaq (,%r14,4), %rax
testq %r14, %r14
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, 0x40(%rbx)
movl $0x0, (%rax)
movl $0x1, 0xc(%rsp)
cmpl $0x2, %r14d
jl 0x44ba
movq 0x20(%rbx), %rcx
decq %r14
xorl %edx, %edx
xorl %esi, %esi
addl (%rcx,%rdx,4), %esi
movl %esi, 0x4(%rax,%rdx,4)
leal 0x2(%rdx), %edi
movl %edi, 0xc(%rsp)
incq %rdx
cmpq %rdx, %r14
jne 0x44a4
leaq 0x60(%rsp), %rdi
callq 0x2060
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x44e0
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2190
leaq 0x60(%rsp), %rdi
callq 0x2070
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4513
jmp 0x4513
jmp 0x4513
jmp 0x4513
movq %rax, %rbx
jmp 0x4532
jmp 0x4513
jmp 0x4513
jmp 0x4513
jmp 0x4513
jmp 0x4513
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq 0x20(%rsp), %rdi
cmpq %rax, %rdi
je 0x4532
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2190
leaq 0x60(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x22f0