// File: /workspace/github_cpp_makefile_star2_O3/EnderGed[P]fast-random-walk/Graph.cpp
// Function: Graph::readGraph(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long)
Graph Graph::readGraph(std::string const input, uint64_t const flags)
{
	bool isSorted = true;
	read_t numTransitions;
	uint * data;

	////	Read from file	////
	{
		char const * fileContent = input.c_str();
		char const * fileContentEnd = fileContent + input.size();

		// Trim file content
		while(fileContent < fileContentEnd && *fileContent <= ' ') ++fileContent;
		while(fileContent < fileContentEnd && *(fileContentEnd - 1) <= ' ') --fileContentEnd;

		// Count rows
		numTransitions = count(fileContent, fileContentEnd, '\n') + 1;

		// Malloc data array (2 dimensions)
		data = (uint *) malloc(numTransitions * 2 * sizeof(*data));

		// File content to array
		char const * pos = fileContent;
		read_t prev = 0;
		unsigned readLen = 0;

		// For every row/transition ...
		for(read_t i = 0; i < numTransitions; ++i)
		{
			// Read source node
			uint const from = readInt(pos, fileContentEnd, readLen);

			// Go to next number
			pos += readLen;
			pos += nonDigits(pos, fileContentEnd);

			// Read target node
			uint const to = readInt(pos, fileContentEnd, readLen);

			// Go to next number/line (or end of file)
			pos += readLen;
			pos += nonDigits(pos, fileContentEnd);

			// Put into data array
			data[2 * i] = from;
			data[2 * i + 1] = to;

			if(from < prev) isSorted = false;

			prev = from;
		}
	}

	// Sort by source node if necessary
	if(!isSorted) countSort2D(data, 2, numTransitions);

	////	Render node starts	////
	uint const numNodes = data[2 * numTransitions - 2] + 1;
	read_t * const indices = (read_t *) malloc((numNodes + 1) * sizeof(*indices));
	
	// Create adjacency list
	uint node_id = 0;
	indices[0] = 0;

	for(read_t i = 0; i < numTransitions; ++i)
	{
		// Set target[node_id] for all nodes until the start node of the i-th transition
		while(data[2 * i] > node_id) indices[++node_id] = i;

		// Copy target node id to new (shrinked) position
		data[i] = data[2 * i + 1];
	}

	// Set target[node_id] to end of data-set for all remaining node_ids
	while(node_id < numNodes) indices[++node_id] = numTransitions;

	// Resize data to the shrinked size
	data = (uint *) realloc(data, numTransitions * sizeof(*data));

	// Convert the graph to an undirected graph
	if(flags & (flag_undirected | flag_reflect))
	{
		// Sort the transitions for each source node
		for(uint i = 0; i < numNodes; ++i)
			if(indices[i + 1] - indices[i] > 1)
				std::sort(data + indices[i], data + indices[i + 1]);

		////	Count number of missing transitions for every node	////
		uint * const counters = (uint *) calloc(numNodes, sizeof(*counters));
		std::vector<bool> bitSet(numTransitions * 2, false);
		read_t acc = 0; // Sum of all counters

		// For every source node ...
		for(uint node = 0; node < numNodes; ++node)
		{
			// For every transition of the node ...
			for(read_t trans = indices[node]; trans < indices[node + 1]; ++trans)
			{
				uint const targetNode = data[trans];
				bool const found = std::binary_search(data + indices[targetNode], data + indices[targetNode + 1], node);

				// If has no counterpart ...
				if(!found)
				{
					// Increment counter for target node
					++counters[targetNode];
					// Increment number of transitions
					++acc;
					// Set bit in bitSet
					//bitMapSet(bitSet, node, true);
					bitSet[trans] = true;
				}
			}
		}

		if(acc)
		{
			////	Adapt list	////
			// Resize transitions list (data)
			numTransitions += acc;
			data = (uint *) realloc(data, numTransitions * sizeof(*data));

			// For every node (from last to first) ...
			for(uint node = numNodes - 1; node > 0; --node)
			{
				// Get number of transitions
				size_t const len = indices[node + 1] - indices[node];
				// Increase end by accumulative counter
				indices[node + 1] += acc;
				// Subtract own new transitions from accumulative counter
				acc -= counters[node];
				// Move transitions to new position
				memmove(data + indices[node] + acc, data + indices[node], len * sizeof(*data));
				// Move bits in bitSet
				std::move_backward(bitSet.begin() + indices[node], bitSet.begin() + (indices[node] + len), bitSet.begin() + (indices[node] + len + acc));
				std::fill(bitSet.begin() + (indices[node] + acc - counters[node - 1]), bitSet.begin() + (indices[node] + acc), false);
			}

			// Modify last element (first node) (that is not part of the loop anymore)
			indices[1] += acc;

			////	Add new transitions	////
			// For every node ...
			for(uint node = 0; node < numNodes; ++node)
			{
				// For every transition of that node ...
				for(read_t trans = indices[node]; trans < indices[node + 1]; ++trans)
				{
					if(bitSet[trans])
					{
						uint const targetNode = data[trans];
						data[indices[targetNode + 1] - (counters[targetNode]--)] = node;
					}
				}
			}
		}
		else printf("Graph is already undirected\n");
		
		free(counters);
	}

	return Graph(numNodes, indices, data);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r12
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
leaq (%rax,%rdx), %rcx
movq %rax, %r13
testq %rdx, %rdx
jle 0x27c2
movq %rax, %r13
cmpb $0x20, (%r13)
jg 0x27c2
incq %r13
cmpq %rcx, %r13
jb 0x27b3
movq %rdi, 0x48(%rsp)
addq %rdx, %rax
negq %rax
movq %rcx, %r14
movq %rax, %rdx
cmpq %rcx, %r13
jae 0x27e7
leaq -0x1(%r14), %rcx
leaq 0x1(%rdx), %rax
cmpb $0x21, -0x1(%r14)
jl 0x27cd
movq %rdx, 0x10(%rsp)
movq %r13, %rdi
movq %r14, %rsi
movl $0xa, %edx
callq 0x38cc
movq %rax, %r15
leaq 0x1(%rax), %rdi
leaq (%rdi,%rdi), %rbp
shlq $0x3, %rdi
callq 0x21f0
movq %r15, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movl $0x0, 0x50(%rsp)
incq %r15
movq %r15, 0x40(%rsp)
jne 0x2836
movq 0x8(%rsp), %r13
jmp 0x292d
movq %r12, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
negq 0x10(%rsp)
movb $0x1, %r15b
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r13, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
callq 0x26f0
movl %eax, 0x20(%rsp)
movl 0x50(%rsp), %ebx
addq %r13, %rbx
movq %rbx, %rax
cmpq %r14, %rbx
jae 0x288a
movq %rbx, %rax
movb (%rax), %cl
addb $-0x3a, %cl
cmpb $-0xb, %cl
ja 0x288a
incq %rax
cmpq %rax, %r14
jne 0x2873
movq 0x10(%rsp), %rax
subl %ebx, %eax
addq %rax, %rbx
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
callq 0x26f0
movl 0x50(%rsp), %r13d
addq %rbx, %r13
movq %r13, %rcx
cmpq %r14, %r13
movq 0x18(%rsp), %rsi
jae 0x28ce
movq %r13, %rcx
movb (%rcx), %dl
addb $-0x3a, %dl
cmpb $-0xb, %dl
ja 0x28ce
incq %rcx
cmpq %rcx, %r14
jne 0x28b7
movq 0x10(%rsp), %rcx
subl %r13d, %ecx
addq %rcx, %r13
movq 0x8(%rsp), %rcx
movl 0x20(%rsp), %edx
movl %edx, (%rcx,%rbp,8)
movl %eax, 0x4(%rcx,%rbp,8)
movl %edx, %eax
cmpq %rax, %r12
setbe %cl
andb %cl, %r15b
leaq 0x1(%rbp), %rcx
movq %rax, %r12
cmpq %rsi, %rbp
movq %rcx, %rbp
jne 0x284d
testb %r15b, %r15b
movq 0x40(%rsp), %r15
movq 0x38(%rsp), %rbp
movq 0x30(%rsp), %r12
movq 0x8(%rsp), %r13
jne 0x292d
movl $0x2, %esi
movq %r13, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x3b12
movl -0x8(%r13,%rbp,4), %eax
leal 0x1(%rax), %ebx
movq %rax, 0x10(%rsp)
leal 0x2(%rax), %edi
shlq $0x3, %rdi
callq 0x21f0
movq %rax, %r14
movq $0x0, (%rax)
testq %r15, %r15
je 0x2a31
leaq 0x10(%r14), %rax
xorl %esi, %esi
movdqa 0x36a9(%rip), %xmm0     # 0x6010
movdqa 0x36b1(%rip), %xmm1     # 0x6020
pcmpeqd %xmm2, %xmm2
xorl %ecx, %ecx
movq 0x18(%rsp), %r10
movl (%r13,%rcx,8), %edx
cmpl %esi, %edx
jbe 0x2a11
movl %esi, %edi
movq %rdx, %r8
subq %rdi, %r8
leaq 0x1(%r8), %rsi
andq $-0x2, %rsi
decq %r8
movq %r8, %xmm3
pshufd $0x44, %xmm3, %xmm3     # xmm3 = xmm3[0,1,0,1]
leaq (%rax,%rdi,8), %rdi
pxor %xmm1, %xmm3
xorl %r8d, %r8d
movq %r8, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm0, %xmm4
pxor %xmm1, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm3, %xmm5
pcmpeqd %xmm3, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %r9d
notl %r9d
testb $0x1, %r9b
je 0x29f2
movq %rcx, -0x8(%rdi,%r8,8)
pxor %xmm2, %xmm4
pextrw $0x4, %xmm4, %r9d
testb $0x1, %r9b
je 0x2a06
movq %rcx, (%rdi,%r8,8)
addq $0x2, %r8
cmpq %r8, %rsi
jne 0x29af
movl %edx, %esi
leaq (%rcx,%rcx), %rdx
movl 0x4(%r13,%rdx,4), %edx
movl %edx, (%r13,%rcx,4)
leaq 0x1(%rcx), %rdx
cmpq %r10, %rcx
movq %rdx, %rcx
jne 0x297a
jmp 0x2a33
xorl %esi, %esi
cmpl %ebx, %esi
jae 0x2ad6
movl %esi, %ecx
movl %ebx, %edx
subq %rcx, %rdx
leaq 0x1(%rdx), %rax
andq $-0x2, %rax
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%r14,%rcx,8), %rcx
addq $0x10, %rcx
xorl %edx, %edx
movdqa 0x35a7(%rip), %xmm1     # 0x6010
movdqa 0x35af(%rip), %xmm2     # 0x6020
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movq %rdx, %xmm4
pshufd $0x44, %xmm4, %xmm4     # xmm4 = xmm4[0,1,0,1]
por %xmm1, %xmm4
pxor %xmm2, %xmm4
movdqa %xmm4, %xmm5
pcmpgtd %xmm0, %xmm5
pcmpeqd %xmm0, %xmm4
pshufd $0xf5, %xmm4, %xmm6     # xmm6 = xmm4[1,1,3,3]
pand %xmm5, %xmm6
pshufd $0xf5, %xmm5, %xmm4     # xmm4 = xmm5[1,1,3,3]
por %xmm6, %xmm4
movd %xmm4, %esi
notl %esi
testb $0x1, %sil
je 0x2aba
movq %r15, -0x8(%rcx,%rdx,8)
pxor %xmm3, %xmm4
pextrw $0x4, %xmm4, %esi
testb $0x1, %sil
je 0x2acd
movq %r15, (%rcx,%rdx,8)
addq $0x2, %rdx
cmpq %rdx, %rax
jne 0x2a79
movl %ebx, 0x30(%rsp)
leaq (,%r15,4), %rsi
movq %r13, %rdi
callq 0x2280
movq %rax, %r15
testb $0x9, %r12b
je 0x2fc1
movl 0x30(%rsp), %eax
testl %eax, %eax
je 0x2b7c
movq %rbp, 0x38(%rsp)
movl %eax, %r12d
movq (%r14), %rax
xorl %r13d, %r13d
movq %r12, 0x18(%rsp)
movq 0x8(%r14,%r13,8), %rbp
movq %rbp, %rcx
subq %rax, %rcx
cmpq $0x2, %rcx
jb 0x2b6a
leaq (,%rbp,4), %rcx
leaq (%r15,%rbp,4), %rbx
leaq (,%rax,4), %rdx
subq %rdx, %rcx
leaq (%r15,%rax,4), %r12
sarq $0x2, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3447
movq %r12, %rdi
movq 0x18(%rsp), %r12
movq %rbx, %rsi
callq 0x3522
incq %r13
movq %rbp, %rax
cmpq %r13, %r12
jne 0x2b12
movq 0x38(%rsp), %rbp
jmp 0x2b7f
xorl %r12d, %r12d
movl $0x4, %esi
movq %r12, 0x18(%rsp)
movq %r12, %rdi
callq 0x20f0
movq %rax, 0x20(%rsp)
leaq 0x2f(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0x2e(%rsp), %rcx
movq %rbp, %rsi
callq 0x3098
cmpl $0x0, 0x30(%rsp)
je 0x2eff
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
movq (%r14), %rax
xorl %edx, %edx
addq $0x3f, %rcx
movq %rcx, 0x8(%rsp)
xorl %r12d, %r12d
movq %rax, %rcx
movq 0x8(%r14,%rdx,8), %rax
cmpq %rax, %rcx
jae 0x2cab
movq 0x50(%rsp), %rsi
movl (%r15,%rcx,4), %edi
movq (%r14,%rdi,8), %r11
leaq (%r15,%r11,4), %r8
shlq $0x2, %r11
leal 0x1(%rdi), %r9d
movq (%r14,%r9,8), %r9
leaq (,%r9,4), %r13
subq %r11, %r13
sarq $0x2, %r13
testq %r13, %r13
jle 0x2c45
movq %r13, %r10
shrq %r10
leaq (%r8,%r10,4), %rbx
addq $0x4, %rbx
movl -0x4(%rbx), %ebp
movq %r10, %r11
notq %r11
addq %r13, %r11
cmpq %rbp, %rdx
cmovbeq %r10, %r11
cmovaq %rbx, %r8
movq %r11, %r13
testq %r11, %r11
jg 0x2c18
leaq (%r15,%r9,4), %r9
cmpq %r9, %r8
je 0x2c56
movl (%r8), %r8d
cmpq %r8, %rdx
jae 0x2c9f
movq 0x20(%rsp), %r8
incl (%r8,%rdi,4)
leaq 0x3f(%rcx), %rdi
testq %rcx, %rcx
cmovnsq %rcx, %rdi
sarq $0x6, %rdi
leaq (%rsi,%rdi,8), %rdi
movq %rcx, %r8
andq 0x8(%rsp), %r8
xorl %r9d, %r9d
movabsq $-0x8000000000000000, %r10 # imm = 0x8000000000000000
cmpq %r10, %r8
setbe %r9b
movl $0x1, %r8d
shlq %cl, %r8
incq %r12
orq %r8, -0x8(%rdi,%r9,8)
incq %rcx
cmpq %rax, %rcx
jb 0x2bec
incq %rdx
cmpq 0x18(%rsp), %rdx
jne 0x2bd6
testq %r12, %r12
je 0x2eff
movq 0x40(%rsp), %rsi
addq %r12, %rsi
shlq $0x2, %rsi
movq %r15, %rdi
callq 0x2280
movq %rax, %r15
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x2f15
movq 0x20(%rsp), %rax
movl (%rax,%r13,4), %eax
movq %rax, 0x38(%rsp)
incq %r13
movq %r13, 0x10(%rsp)
movl %r13d, %eax
movq (%r14,%rax,8), %rbx
movq -0x8(%r14,%r13,8), %r13
movq %rbx, %rdx
subq %r13, %rdx
leaq (%rbx,%r12), %rcx
movq %rcx, (%r14,%rax,8)
movl 0x38(%rsp), %eax
subq %rax, %r12
leaq (%r15,%r13,4), %rsi
leaq (%rsi,%r12,4), %rdi
shlq $0x2, %rdx
callq 0x2270
movq 0x50(%rsp), %rax
movq %r13, %rsi
leaq 0x3f(%r13), %rcx
testq %r13, %r13
cmovnsq %r13, %rcx
sarq $0x6, %rcx
leaq (%rax,%rcx,8), %rcx
movq %r13, %rdx
movq 0x8(%rsp), %r13
andq %r13, %rdx
xorl %edi, %edi
movabsq $-0x8000000000000000, %rbp # imm = 0x8000000000000000
cmpq %rbp, %rdx
setbe %dil
leaq (%rcx,%rdi,8), %rdi
addq $-0x8, %rdi
andl $0x3f, %esi
leaq 0x3f(%rbx), %rcx
testq %rbx, %rbx
cmovnsq %rbx, %rcx
sarq $0x6, %rcx
leaq (%rax,%rcx,8), %rcx
movq %rbx, %rdx
andq %r13, %rdx
xorl %r8d, %r8d
cmpq %rbp, %rdx
setbe %r8b
leaq (%rcx,%r8,8), %rdx
addq $-0x8, %rdx
movl %ebx, %ecx
andl $0x3f, %ecx
movq %r12, %r9
addq %rbx, %r9
leaq 0x3f(%r12,%rbx), %r8
cmovnsq %r9, %r8
sarq $0x6, %r8
leaq (%rax,%r8,8), %rax
movq %r9, %r10
andq %r13, %r10
xorl %r8d, %r8d
cmpq %rbp, %r10
setbe %r8b
leaq (%rax,%r8,8), %r8
addq $-0x8, %r8
andl $0x3f, %r9d
callq 0x3834
movq 0x10(%rsp), %r11
movq -0x8(%r14,%r11,8), %rdx
leaq (%rdx,%r12), %rbx
movq 0x20(%rsp), %rax
movl -0x8(%rax,%r11,4), %ecx
movq %rbx, %rax
movq %rcx, 0x38(%rsp)
subq %rcx, %rax
leaq 0x3f(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
movq 0x50(%rsp), %rcx
sarq $0x6, %rsi
movq %rax, %rdi
andq %r13, %rdi
xorl %r8d, %r8d
cmpq %rbp, %rdi
setbe %r8b
leaq (%rcx,%rsi,8), %rsi
leaq (%rsi,%r8,8), %r9
movl %eax, %edi
andl $0x3f, %edi
leaq (%rdx,%r12), %r10
addq $0x3f, %r10
testq %rbx, %rbx
cmovnsq %rbx, %r10
leaq (%rsi,%r8,8), %rbp
addq $-0x8, %rbp
sarq $0x6, %r10
movq %rbx, %rsi
andq %r13, %rsi
xorl %edx, %edx
movabsq $-0x8000000000000000, %r8 # imm = 0x8000000000000000
cmpq %r8, %rsi
setbe %dl
leaq (%rcx,%r10,8), %rsi
leaq (%rsi,%rdx,8), %rcx
andl $0x3f, %ebx
cmpq %rcx, %r9
movq %r11, %r13
je 0x2ebc
testl %edi, %edi
je 0x2e90
movq $-0x1, %rdi
movl %eax, %ecx
shlq %cl, %rdi
notq %rdi
andq %rdi, (%rbp)
addq $0x8, %rbp
movq %rbp, %rdi
leaq (%rsi,%rdx,8), %rbp
addq $-0x8, %rbp
movq %rbp, %rdx
subq %rdi, %rdx
xorl %esi, %esi
callq 0x20e0
testl %ebx, %ebx
je 0x2edd
negb %bl
movq $-0x1, %rdx
movl %ebx, %ecx
shrq %cl, %rdx
jmp 0x2ed6
cmpl %ebx, %edi
je 0x2edd
negb %bl
movq $-0x1, %rdx
movl %ebx, %ecx
shrq %cl, %rdx
movl %eax, %ecx
shrq %cl, %rdx
shlq %cl, %rdx
notq %rdx
andq %rdx, (%rbp)
decq %r13
cmpq $0x1, %r13
jne 0x2cf8
addq %r12, 0x8(%r14)
movq 0x50(%rsp), %rbx
cmpl $0x0, 0x30(%rsp)
jne 0x2f1e
jmp 0x2faa
leaq 0x31cf(%rip), %rdi      # 0x60d5
callq 0x2250
movq 0x50(%rsp), %rbx
jmp 0x2faa
addq %r12, 0x8(%r14)
movq 0x50(%rsp), %rbx
movq (%r14), %rax
xorl %ecx, %ecx
movq 0x18(%rsp), %r10
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
movq 0x8(%rsp), %r9
movq 0x20(%rsp), %r11
movq %rax, %rdx
movq 0x8(%r14,%rcx,8), %rax
cmpq %rax, %rdx
jae 0x2fa2
leaq 0x3f(%rdx), %rsi
testq %rdx, %rdx
cmovnsq %rdx, %rsi
sarq $0x6, %rsi
leaq (%rbx,%rsi,8), %rsi
movq %rdx, %rdi
andq %r9, %rdi
xorl %r8d, %r8d
cmpq %r12, %rdi
setbe %r8b
movq -0x8(%rsi,%r8,8), %rsi
btq %rdx, %rsi
jae 0x2f9a
movl (%r15,%rdx,4), %esi
leal 0x1(%rsi), %edi
movq (%r14,%rdi,8), %rdi
movl (%r11,%rsi,4), %r8d
leal -0x1(%r8), %r9d
movl %r9d, (%r11,%rsi,4)
movq 0x8(%rsp), %r9
subq %r8, %rdi
movl %ecx, (%r15,%rdi,4)
incq %rdx
cmpq %rdx, %rax
jne 0x2f49
incq %rcx
cmpq %r10, %rcx
jne 0x2f3c
movq 0x20(%rsp), %rdi
callq 0x21d0
testq %rbx, %rbx
je 0x2fc1
movq %rbx, %rdi
callq 0x2160
movq 0x48(%rsp), %rax
leaq 0x5d6b(%rip), %rcx      # 0x8d38
movq %rcx, (%rax)
movl 0x30(%rsp), %ecx
movl %ecx, 0x8(%rax)
movq %r14, 0x10(%rax)
movq %r15, 0x18(%rax)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3000
callq 0x2160
movq %rbx, %rdi
callq 0x2290