// File: /workspace/github_cpp_makefile_star2_O2/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 $0x258, %rsp            # imm = 0x258
movq %rsi, %r14
movq %rdi, %rbx
callq 0x3a56
leaq 0x50(%rsp), %r15
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2270
movq %r15, %rdi
callq 0x22d0
movq %rdx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xc(%rbx), %r14
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0xc(%rbx)
leaq 0x50(%rsp), %r15
leaq 0x3(%rsp), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3636
movzbl 0x3(%rsp), %eax
cmpl $0x65, %eax
je 0x35e9
cmpl $0x74, %eax
je 0x3587
cmpl $0x76, %eax
jne 0x34f7
movq %r15, %rdi
leaq 0x4(%rsp), %rsi
callq 0x2110
movq %rax, %rdi
leaq 0x14(%rsp), %rsi
callq 0x2110
movq 0x20(%rbx), %rax
movslq 0x4(%rsp), %rcx
andl $0x0, (%rax,%rcx,4)
movl 0x14(%rsp), %eax
movq 0x28(%rbx), %rcx
movslq 0x4(%rsp), %rdx
movl %eax, (%rcx,%rdx,4)
movq 0x30(%rbx), %rax
movslq 0x4(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq %r15, %rdi
callq 0x22d0
movq %rax, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
jmp 0x34f7
movq %r15, %rdi
leaq 0x2c(%rsp), %rsi
callq 0x2110
movq %rax, %rdi
movq %r14, %rsi
callq 0x2110
movslq (%r14), %r12
leaq (,%r12,4), %r13
testq %r12, %r12
pushq $-0x1
popq %rax
cmovsq %rax, %r13
movq %r13, %rdi
callq 0x2030
movq %rax, 0x18(%rbx)
movq %r13, %rdi
callq 0x2030
movq %rax, 0x20(%rbx)
movq %r13, %rdi
callq 0x2030
movq %rax, 0x28(%rbx)
movq %r12, %rdi
callq 0x2030
movq %rax, 0x30(%rbx)
jmp 0x34f7
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2110
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x2110
movq %rax, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
movslq 0xc(%rsp), %rax
cmpl 0x8(%rsp), %eax
je 0x398a
movq 0x20(%rbx), %rcx
incl (%rcx,%rax,4)
movslq 0x8(%rsp), %rax
incl (%rcx,%rax,4)
incl 0x10(%rbx)
jmp 0x34f7
movslq (%r14), %r12
leaq (,%r12,8), %rdi
testq %r12, %r12
pushq $-0x1
popq %r13
cmovsq %r13, %rdi
callq 0x2030
movq %rax, 0x38(%rbx)
xorl %eax, %eax
movl %eax, 0x4(%rsp)
cmpl %r12d, %eax
jge 0x369c
movq 0x20(%rbx), %rcx
movslq %eax, %r12
movslq (%rcx,%r12,4), %rax
leaq (,%rax,4), %rdi
testq %rax, %rax
cmovsq %r13, %rdi
callq 0x2030
movq 0x38(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x20(%rbx), %rax
andl $0x0, (%rax,%r12,4)
movl 0x4(%rsp), %eax
incl %eax
movl 0xc(%rbx), %r12d
jmp 0x3657
movq 0x50(%rsp), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x22b0
leaq 0x50(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x21c0
leaq 0x50(%rsp), %r15
leaq 0x3(%rsp), %r12
leaq 0x8(%rsp), %rbp
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x21f0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3754
cmpb $0x65, 0x3(%rsp)
jne 0x36d7
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2110
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2110
movq %rax, %rdi
movq %r13, %rsi
callq 0x2110
movslq 0x8(%rsp), %rax
movq 0x20(%rbx), %rcx
movq 0x38(%rbx), %rdx
movslq 0xc(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movslq (%rcx,%rsi,4), %rsi
movl %eax, (%rdi,%rsi,4)
movslq 0xc(%rsp), %rsi
movq (%rdx,%rax,8), %rdx
movslq (%rcx,%rax,4), %rax
movl %esi, (%rdx,%rax,4)
incl (%rcx,%rsi,4)
movslq 0x8(%rsp), %rax
incl (%rcx,%rax,4)
jmp 0x36d7
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
xorl %edx, %edx
pushq $-0x1
popq %rbp
movl %edx, 0x4(%rsp)
movl (%r14), %esi
cmpl %esi, %edx
jge 0x377d
movslq %edx, %rdx
movl (%rcx,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
movl 0x4(%rsp), %edx
incl %edx
jmp 0x3761
movq (%rbx), %rdi
addq $0x18, %rdi
callq 0x3f1a
movq %rax, %r15
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x3f1a
movq %rax, %r12
movslq 0xc(%rbx), %rdx
shlq $0x2, %rdx
xorl %r13d, %r13d
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
movq 0x20(%rbx), %rax
movb 0x9(%rbx), %sil
pushq $-0x1
popq %rcx
movslq (%r14), %rdx
cmpq %rdx, %r13
jge 0x3829
testb $0x1, %sil
jne 0x3829
movq 0x38(%rbx), %rdx
xorl %edi, %edi
movslq (%rax,%r13,4), %r8
cmpq %r8, %rdi
jge 0x3820
testb $0x1, %sil
jne 0x3820
movq (%rdx,%r13,8), %r8
movl (%r8,%rdi,4), %r8d
movslq %r8d, %r8
cmpl $0x0, (%r12,%r8,4)
je 0x37fc
movb $0x1, 0x9(%rbx)
movb $0x1, %sil
jmp 0x380e
movl $0x1, (%r12,%r8,4)
movslq %ecx, %r9
incl %ecx
movl %r8d, 0x4(%r15,%r9,4)
incq %rdi
jmp 0x37d2
movl %ecx, %edx
movslq (%r15,%rdx,4), %rdx
decl %ecx
andl $0x0, (%r12,%rdx,4)
testl %ecx, %ecx
jns 0x3813
incq %r13
jmp 0x37be
movq (%rbx), %rdi
addq $0x18, %rdi
movq %r12, %rsi
callq 0x3fee
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %edx
movq %r15, %rsi
callq 0x3fee
cmpb $0x1, 0x9(%rbx)
jne 0x387a
movq 0xc799(%rip), %rdi      # 0xfff0
leaq 0x7a9b(%rip), %rsi      # 0xb2f9
callq 0x2170
movq %rax, %rdi
callq 0x20e0
leaq 0x50(%rsp), %rdi
callq 0x2060
jmp 0x39b3
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x3f1a
movq %rax, %r15
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %esi
callq 0x3f1a
movq %rax, %r12
movslq 0xc(%rbx), %rdx
shlq $0x2, %rdx
xorl %r13d, %r13d
movq %rax, %rdi
xorl %esi, %esi
callq 0x2100
andl $0x0, (%r15)
movl $0x1, (%r12)
movq 0x20(%rbx), %rax
xorl %ecx, %ecx
movslq %ecx, %rdx
cmpq %rdx, %r13
jg 0x390e
movslq (%r15,%r13,4), %rdx
movq 0x38(%rbx), %rsi
xorl %edi, %edi
movslq (%rax,%rdx,4), %r8
cmpq %r8, %rdi
jge 0x3909
movq (%rsi,%rdx,8), %r8
movl (%r8,%rdi,4), %r8d
movslq %r8d, %r8
cmpl $0x0, (%r12,%r8,4)
jne 0x3904
movslq %ecx, %r9
incl %ecx
movl %r8d, 0x4(%r15,%r9,4)
movl $0x1, (%r12,%r8,4)
incq %rdi
jmp 0x38d7
incq %r13
jmp 0x38c5
movl 0xc(%rbx), %edx
movb 0x9(%rbx), %cl
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %esi
cmovgl %edx, %esi
cmpq %rax, %rsi
je 0x393c
testb $0x1, %cl
jne 0x393c
cmpl $0x0, (%r12,%rax,4)
jne 0x3937
movb $0x1, 0x9(%rbx)
movb $0x1, %cl
incq %rax
jmp 0x3920
movq (%rbx), %rdi
addq $0x18, %rdi
movq %r15, %rsi
callq 0x3fee
movq (%rbx), %rdi
addq $0x18, %rdi
movl 0xc(%rbx), %edx
movq %r12, %rsi
callq 0x3fee
cmpb $0x1, 0x9(%rbx)
jne 0x39d9
movq 0xc686(%rip), %rdi      # 0xfff0
leaq 0x79ae(%rip), %rsi      # 0xb31f
callq 0x2170
movq %rax, %rdi
callq 0x20e0
leaq 0x50(%rsp), %rdi
callq 0x2060
jmp 0x39b3
movq 0xc65f(%rip), %rdi      # 0xfff0
leaq 0x7943(%rip), %rsi      # 0xb2db
callq 0x2170
movq %rax, %rdi
callq 0x20e0
movb $0x1, 0x9(%rbx)
leaq 0x50(%rsp), %rdi
callq 0x2060
leaq 0x30(%rsp), %rdi
callq 0x20c0
leaq 0x50(%rsp), %rdi
callq 0x2070
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq (%r14), %r14
leaq (,%r14,4), %rdi
testq %r14, %r14
cmovsq %rbp, %rdi
callq 0x2030
movq %rax, 0x40(%rbx)
andl $0x0, (%rax)
movq 0x20(%rbx), %rcx
pushq $0x1
popq %rdx
xorl %esi, %esi
movl %edx, 0x4(%rsp)
cmpq %r14, %rdx
jge 0x397e
addl -0x4(%rcx,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
jmp 0x3a00
jmp 0x3a30
jmp 0x3a30
jmp 0x3a30
jmp 0x3a30
movq %rax, %rbx
jmp 0x3a3d
jmp 0x3a30
jmp 0x3a30
jmp 0x3a30
jmp 0x3a30
jmp 0x3a30
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x20c0
leaq 0x50(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x22e0
nop