// File: /workspace/github_cpp_makefile_star2_O1/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 0x45c8
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 0x407f
movzbl 0xb(%rsp), %eax
cmpl $0x65, %eax
je 0x4032
cmpl $0x74, %eax
je 0x3fc7
cmpl $0x76, %eax
jne 0x3f34
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 0x3f34
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 0x3f34
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 0x4485
movq 0x20(%rbx), %rcx
incl (%rcx,%rax,4)
movslq 0x10(%rsp), %rax
incl (%rcx,%rax,4)
incl 0x10(%rbx)
jmp 0x3f34
movq 0x18(%rsp), %rax
movslq (%rax), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, 0x38(%rbx)
movl $0x0, 0xc(%rsp)
cmpl $0x0, 0xc(%rbx)
jle 0x40ff
xorl %eax, %eax
movq $-0x1, %r14
movq 0x20(%rbx), %rcx
movslq %eax, %r12
movslq (%rcx,%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), %eax
incl %eax
movl %eax, 0xc(%rsp)
cmpl 0xc(%rbx), %eax
jl 0x40bd
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 0x41c5
cmpb $0x65, 0xb(%rsp)
jne 0x413f
movq %r15, %rdi
movq %r13, %rsi
callq 0x20f0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x20f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x20f0
movl 0x10(%rsp), %eax
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)
movl 0x14(%rsp), %eax
movslq 0x10(%rsp), %rsi
movq (%rdx,%rsi,8), %rdx
movslq (%rcx,%rsi,4), %rsi
movl %eax, (%rdx,%rsi,4)
movslq 0x14(%rsp), %rax
incl (%rcx,%rax,4)
movslq 0x10(%rsp), %rax
incl (%rcx,%rax,4)
jmp 0x413f
movl $0x0, 0xc(%rsp)
movq 0x18(%rsp), %r13
movl (%r13), %esi
testl %esi, %esi
jle 0x41ff
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 0x41e4
movq (%rbx), %rdi
addq $0x18, %rdi
callq 0x4b34
movq %rax, %r15
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x4b34
movq %rax, %r12
movslq (%r13), %rdx
shlq $0x2, %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x20e0
movl (%r13), %edx
testl %edx, %edx
jle 0x42cd
movb 0x9(%rbx), %al
testb $0x1, %al
jne 0x42cd
movq 0x20(%rbx), %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %esi, %esi
cmpl $0x0, (%rcx,%rsi,4)
jle 0x429d
movq 0x38(%rbx), %rdx
xorl %r8d, %r8d
testb $0x1, %al
jne 0x429d
movq (%rdx,%rsi,8), %r9
movl (%r9,%r8,4), %r9d
movslq %r9d, %r9
cmpl $0x0, (%r12,%r9,4)
je 0x427f
movb $0x1, 0x9(%rbx)
movb $0x1, %al
jmp 0x4291
movl $0x1, (%r12,%r9,4)
movslq %edi, %r10
incl %edi
movl %r9d, 0x4(%r15,%r10,4)
incq %r8
movslq (%rcx,%rsi,4), %r9
cmpq %r9, %r8
jl 0x4261
testl %edi, %edi
js 0x42bd
movl %edi, %edx
incq %rdx
movslq -0x4(%r15,%rdx,4), %rdi
movl $0x0, (%r12,%rdi,4)
decq %rdx
jg 0x42a6
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
incq %rsi
movslq (%r13), %rdx
cmpq %rdx, %rsi
jge 0x42cd
testb $0x1, %al
je 0x4254
movq (%rbx), %rdi
addq $0x18, %rdi
movq %r12, %rsi
callq 0x4cf0
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %edx
movq %r15, %rsi
callq 0x4cf0
cmpb $0x1, 0x9(%rbx)
jne 0x4349
movq 0xbcf5(%rip), %rbx      # 0xfff0
leaq 0x7ff7(%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 0xbcc3(%rip), %rdi      # 0xfff0
callq 0x2040
movq %rax, %rdi
callq 0x2100
leaq 0x60(%rsp), %rdi
callq 0x2060
jmp 0x4542
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x4b34
movq %rax, %r15
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x4b34
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
cmpl $0x0, (%rax,%rsi,4)
jle 0x43d8
movq (%rcx,%rsi,8), %rdi
xorl %r8d, %r8d
movl (%rdi,%r8,4), %r9d
movslq %r9d, %r9
cmpl $0x0, (%r12,%r9,4)
jne 0x43cc
movslq %edx, %r10
incl %edx
movl %r9d, 0x4(%r15,%r10,4)
movl $0x1, (%r12,%r9,4)
incq %r8
movslq (%rax,%rsi,4), %r9
cmpq %r9, %r8
jl 0x43ac
leaq 0x1(%r14), %rsi
movslq %edx, %rdi
cmpq %rdi, %r14
movq %rsi, %r14
jl 0x439b
movslq (%r13), %rdx
testq %rdx, %rdx
jle 0x440f
movb 0x9(%rbx), %cl
xorl %eax, %eax
testb $0x1, %cl
jne 0x440f
cmpl $0x0, (%r12,%rax,4)
jne 0x4407
movb $0x1, 0x9(%rbx)
movb $0x1, %cl
incq %rax
cmpq %rax, %rdx
jne 0x43f5
movq (%rbx), %rdi
addq $0x18, %rdi
movq %r15, %rsi
callq 0x4cf0
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %edx
movq %r12, %rsi
callq 0x4cf0
cmpb $0x1, 0x9(%rbx)
jne 0x44db
movq 0xbbaf(%rip), %rbx      # 0xfff0
leaq 0x7ed7(%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 0xbb7d(%rip), %rdi      # 0xfff0
callq 0x2040
movq %rax, %rdi
callq 0x2100
jmp 0x4538
movq 0xbb64(%rip), %r14      # 0xfff0
leaq 0x7e48(%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 0xbb32(%rip), %rdi      # 0xfff0
callq 0x2040
movq %rax, %rdi
callq 0x2100
movb $0x1, 0x9(%rbx)
leaq 0x60(%rsp), %rdi
callq 0x2060
jmp 0x4542
movslq (%r13), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, 0x40(%rbx)
movl $0x0, (%rax)
movslq 0xc(%rbx), %rcx
movl $0x1, 0xc(%rsp)
cmpq $0x2, %rcx
jl 0x4538
movq 0x20(%rbx), %rdx
decq %rcx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
addl (%rax,%rsi,4), %edi
movl %edi, 0x4(%rax,%rsi,4)
leal 0x2(%rsi), %edi
movl %edi, 0xc(%rsp)
incq %rsi
cmpq %rsi, %rcx
jne 0x451f
leaq 0x60(%rsp), %rdi
callq 0x2060
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x455e
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 0x4591
jmp 0x4591
jmp 0x4591
jmp 0x4591
movq %rax, %rbx
jmp 0x45b0
jmp 0x4591
jmp 0x4591
jmp 0x4591
jmp 0x4591
jmp 0x4591
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq 0x20(%rsp), %rdi
cmpq %rax, %rdi
je 0x45b0
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2190
leaq 0x60(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x22f0