// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq (%rsi), %rax
movq 0x8(%rsi), %rdx
leaq (%rax,%rdx), %rcx
movq %rax, %rbp
testq %rdx, %rdx
jle 0x284f
movq %rax, %rbp
cmpb $0x20, (%rbp)
jg 0x284f
incq %rbp
cmpq %rcx, %rbp
jb 0x2841
movq %rdi, 0x70(%rsp)
addq %rdx, %rax
negq %rax
movq %rcx, %r14
movq %rax, %rdx
cmpq %rcx, %rbp
jae 0x2874
leaq -0x1(%r14), %rcx
leaq 0x1(%rdx), %rax
cmpb $0x21, -0x1(%r14)
jl 0x285a
movq %rdx, 0x10(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movl $0xa, %edx
callq 0x3914
movq %rax, %r13
leaq 0x1(%rax), %rdi
leaq (%rdi,%rdi), %rax
movq %rax, 0x28(%rsp)
shlq $0x3, %rdi
callq 0x21f0
movq %rax, %r12
movq %r13, %rbx
movl $0x0, 0x40(%rsp)
incq %rbx
movq %rbx, 0x68(%rsp)
je 0x29bc
movq %r12, 0x18(%rsp)
movq %r15, 0x30(%rsp)
negq 0x10(%rsp)
movb $0x1, %r15b
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r13, 0x38(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
callq 0x2752
movl %eax, %r13d
movl 0x40(%rsp), %eax
addq %rbp, %rax
movq %rax, %rbp
cmpq %r14, %rax
jae 0x2917
movq %rax, %rbp
movb (%rbp), %cl
addb $-0x3a, %cl
cmpb $-0xb, %cl
ja 0x2917
incq %rbp
cmpq %rbp, %r14
jne 0x28ff
movq 0x10(%rsp), %rbp
subl %eax, %ebp
addq %rax, %rbp
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
callq 0x2752
movl 0x40(%rsp), %ecx
addq %rbp, %rcx
movq %rcx, %rbp
cmpq %r14, %rcx
jae 0x2956
movq %rcx, %rbp
movb (%rbp), %dl
addb $-0x3a, %dl
cmpb $-0xb, %dl
ja 0x2956
incq %rbp
cmpq %rbp, %r14
jne 0x293e
movq 0x10(%rsp), %rbp
subl %ecx, %ebp
addq %rcx, %rbp
movq 0x18(%rsp), %rcx
movl %r13d, (%rcx,%r12,8)
movl %eax, 0x4(%rcx,%r12,8)
movl %r13d, %eax
cmpq %rax, %rbx
movzbl %r15b, %r15d
movl $0x0, %ecx
cmoval %ecx, %r15d
leaq 0x1(%r12), %rcx
movq %rax, %rbx
movq 0x38(%rsp), %r13
cmpq %r13, %r12
movq %rcx, %r12
jne 0x28da
testb $0x1, %r15b
movq 0x68(%rsp), %rbx
movq 0x30(%rsp), %r15
movq 0x18(%rsp), %r12
jne 0x29bc
movl $0x2, %esi
movq %r12, %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x3b5a
movq 0x28(%rsp), %rax
movl -0x8(%r12,%rax,4), %ecx
leal 0x1(%rcx), %eax
movl %eax, 0xc(%rsp)
movq %rcx, 0x30(%rsp)
leal 0x2(%rcx), %edi
shlq $0x3, %rdi
callq 0x21f0
movq %rax, %r14
movq $0x0, (%rax)
testq %rbx, %rbx
je 0x2a37
leaq 0x8(%r14), %rcx
xorl %eax, %eax
xorl %edx, %edx
movl (%r12,%rdx,8), %esi
cmpl %eax, %esi
jbe 0x2a1c
movl %eax, %eax
movq %rax, %rdi
subq %rsi, %rdi
leaq (%rcx,%rax,8), %r8
xorl %esi, %esi
movq %rdx, (%r8,%rsi,8)
incq %rsi
movq %rdi, %r9
addq %rsi, %r9
jne 0x2a0b
addl %esi, %eax
leaq (%rdx,%rdx), %rsi
movl 0x4(%r12,%rsi,4), %esi
movl %esi, (%r12,%rdx,4)
leaq 0x1(%rdx), %rsi
cmpq %r13, %rdx
movq %rsi, %rdx
jne 0x29f5
jmp 0x2a39
xorl %eax, %eax
movl 0xc(%rsp), %ecx
movl %ecx, %edx
movq %rdx, 0x10(%rsp)
cmpl %ecx, %eax
jae 0x2aea
movl %eax, %ecx
movq 0x10(%rsp), %rdx
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 0x3593(%rip), %xmm1     # 0x6010
movdqa 0x359b(%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 0x2ace
movq %rbx, -0x8(%rcx,%rdx,8)
pxor %xmm3, %xmm4
pextrw $0x4, %xmm4, %esi
testb $0x1, %sil
je 0x2ae1
movq %rbx, (%rcx,%rdx,8)
addq $0x2, %rdx
cmpq %rdx, %rax
jne 0x2a8d
leaq (,%rbx,4), %rsi
movq %r12, %rdi
callq 0x2280
movq %rax, %r12
testb $0x9, %r15b
je 0x2ff0
cmpl $0x0, 0xc(%rsp)
je 0x2b6e
xorl %r15d, %r15d
movq (%r14,%r15,8), %rcx
movq 0x8(%r14,%r15,8), %rax
movq %rax, %rdx
subq %rcx, %rdx
cmpq $0x2, %rdx
jb 0x2b64
leaq (%r12,%rax,4), %rbx
shlq $0x2, %rax
leaq (,%rcx,4), %rdx
subq %rdx, %rax
leaq (%r12,%rcx,4), %r13
sarq $0x2, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3477
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3552
incq %r15
cmpq %r15, 0x10(%rsp)
jne 0x2b11
movabsq $-0x8000000000000000, %rbx # imm = 0x8000000000000000
movl $0x4, %esi
movq 0x10(%rsp), %rdi
callq 0x20f0
movq %rax, %rbp
leaq 0x27(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x40(%rsp), %rdi
leaq 0x26(%rsp), %rcx
movq 0x28(%rsp), %rsi
callq 0x30cc
cmpl $0x0, 0xc(%rsp)
je 0x2cbf
xorl %edx, %edx
leaq 0x3f(%rbx), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movq %rdx, %rsi
movq (%r14,%rdx,8), %rcx
incq %rdx
cmpq 0x8(%r14,%rsi,8), %rcx
jae 0x2cb2
movq 0x40(%rsp), %rdi
movq %rbp, %rax
movl (%r12,%rcx,4), %r8d
movq (%r14,%r8,8), %rbx
leaq (%r12,%rbx,4), %r9
shlq $0x2, %rbx
leal 0x1(%r8), %r10d
movq (%r14,%r10,8), %r10
leaq (,%r10,4), %r15
subq %rbx, %r15
sarq $0x2, %r15
testq %r15, %r15
jle 0x2c3c
movq %r15, %r11
shrq %r11
leaq (%r9,%r11,4), %r13
addq $0x4, %r13
movl -0x4(%r13), %ebp
movq %r11, %rbx
notq %rbx
addq %r15, %rbx
cmpq %rbp, %rsi
cmovbeq %r11, %rbx
cmovaq %r13, %r9
movq %rbx, %r15
testq %rbx, %rbx
jg 0x2c0e
leaq (%r12,%r10,4), %r10
cmpq %r10, %r9
je 0x2c4d
movl (%r9), %r9d
cmpq %r9, %rsi
jae 0x2c98
movq %rax, %rbp
incl (%rax,%r8,4)
leaq 0x3f(%rcx), %r8
testq %rcx, %rcx
cmovnsq %rcx, %r8
sarq $0x6, %r8
leaq (%rdi,%r8,8), %r8
movq %rcx, %r9
andq 0x38(%rsp), %r9
xorl %r10d, %r10d
movabsq $-0x8000000000000000, %rbx # imm = 0x8000000000000000
cmpq %rbx, %r9
setbe %r10b
movl $0x1, %r9d
shlq %cl, %r9
incq 0x18(%rsp)
orq %r9, -0x8(%r8,%r10,8)
jmp 0x2ca5
movq %rax, %rbp
movabsq $-0x8000000000000000, %rbx # imm = 0x8000000000000000
incq %rcx
cmpq (%r14,%rdx,8), %rcx
jb 0x2bdf
cmpq 0x10(%rsp), %rdx
jne 0x2bc5
jmp 0x2cc8
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x2fb4
movq 0x68(%rsp), %rsi
addq %rax, %rsi
shlq $0x2, %rsi
movq %r12, %rdi
callq 0x2280
movq %rax, %r12
movq 0x30(%rsp), %r15
testq %r15, %r15
je 0x2f1a
incq %r15
leaq 0x3f(%rbx), %r8
movq %r8, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movl %r15d, %eax
movq (%r14,%rax,8), %r13
movq -0x8(%r14,%r15,8), %rsi
movq %rsi, 0x38(%rsp)
movq %r13, %rdx
subq %rsi, %rdx
movq 0x18(%rsp), %rbx
leaq (%rbx,%r13), %rcx
movq %rcx, (%r14,%rax,8)
movl -0x4(%rbp,%r15,4), %eax
subq %rax, %rbx
leaq (%r12,%rsi,4), %rsi
leaq (%rsi,%rbx,4), %rdi
shlq $0x2, %rdx
movq %r8, %r15
callq 0x2270
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rsi
leaq 0x3f(%rsi), %rcx
testq %rsi, %rsi
cmovnsq %rsi, %rcx
sarq $0x6, %rcx
leaq (%rax,%rcx,8), %rcx
movq %rsi, %rdx
andq %r15, %rdx
xorl %r8d, %r8d
movabsq $-0x8000000000000000, %r10 # imm = 0x8000000000000000
cmpq %r10, %rdx
setbe %r8b
leaq (%rcx,%r8,8), %rdi
addq $-0x8, %rdi
andl $0x3f, %esi
leaq 0x3f(%r13), %rcx
testq %r13, %r13
cmovnsq %r13, %rcx
sarq $0x6, %rcx
leaq (%rax,%rcx,8), %rcx
movq %r13, %rdx
andq %r15, %rdx
xorl %r8d, %r8d
cmpq %r10, %rdx
setbe %r8b
leaq (%rcx,%r8,8), %rdx
addq $-0x8, %rdx
movl %r13d, %ecx
andl $0x3f, %ecx
movq %rbx, %r9
addq %r13, %r9
leaq 0x3f(%rbx,%r13), %r8
cmovnsq %r9, %r8
sarq $0x6, %r8
leaq (%rax,%r8,8), %rax
movq %r9, %r11
andq %r15, %r11
xorl %r8d, %r8d
cmpq %r10, %r11
setbe %r8b
leaq (%rax,%r8,8), %r8
addq $-0x8, %r8
andl $0x3f, %r9d
callq 0x387c
movq 0x30(%rsp), %r15
movq -0x8(%r14,%r15,8), %rdx
leaq (%rdx,%rbx), %r13
movl -0x8(%rbp,%r15,4), %ecx
movq %r13, %rax
subq %rcx, %rax
leaq 0x3f(%rax), %rsi
testq %rax, %rax
cmovnsq %rax, %rsi
movq 0x40(%rsp), %rcx
sarq $0x6, %rsi
movq %rax, %rdi
movq 0x28(%rsp), %r11
andq %r11, %rdi
xorl %r8d, %r8d
movabsq $-0x8000000000000000, %r9 # imm = 0x8000000000000000
cmpq %r9, %rdi
setbe %r8b
leaq (%rcx,%rsi,8), %rsi
leaq (%rsi,%r8,8), %r9
movl %eax, %edi
andl $0x3f, %edi
movq %rbx, 0x18(%rsp)
leaq (%rdx,%rbx), %r10
addq $0x3f, %r10
testq %r13, %r13
cmovnsq %r13, %r10
leaq (%rsi,%r8,8), %rbx
addq $-0x8, %rbx
sarq $0x6, %r10
movq %r13, %rsi
andq %r11, %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, %r13d
cmpq %rcx, %r9
je 0x2ee7
testl %edi, %edi
je 0x2eb3
movq $-0x1, %rdi
movl %eax, %ecx
shlq %cl, %rdi
notq %rdi
andq %rdi, (%rbx)
addq $0x8, %rbx
movq %rbx, %rdi
leaq (%rsi,%rdx,8), %rbx
addq $-0x8, %rbx
movq %rbx, %rdx
subq %rdi, %rdx
xorl %esi, %esi
callq 0x20e0
testl %r13d, %r13d
movq 0x28(%rsp), %r8
je 0x2f0d
negb %r13b
movq $-0x1, %rdx
movl %r13d, %ecx
shrq %cl, %rdx
jmp 0x2f07
movq %r11, %r8
cmpl %r13d, %edi
je 0x2f0d
negb %r13b
movq $-0x1, %rdx
movl %r13d, %ecx
shrq %cl, %rdx
movl %eax, %ecx
shrq %cl, %rdx
shlq %cl, %rdx
notq %rdx
andq %rdx, (%rbx)
decq %r15
cmpq $0x1, %r15
jne 0x2d07
movq 0x18(%rsp), %rax
addq %rax, 0x8(%r14)
cmpl $0x0, 0xc(%rsp)
je 0x2fc0
movq 0x40(%rsp), %rax
xorl %ecx, %ecx
movabsq $-0x8000000000000000, %rbx # imm = 0x8000000000000000
leaq 0x3f(%rbx), %rdx
movq (%r14,%rcx,8), %rsi
movq 0x8(%r14,%rcx,8), %rdi
cmpq %rdi, %rsi
jae 0x2fa8
leaq 0x3f(%rsi), %r8
testq %rsi, %rsi
cmovnsq %rsi, %r8
sarq $0x6, %r8
leaq (%rax,%r8,8), %r8
movq %rsi, %r9
andq %rdx, %r9
xorl %r10d, %r10d
cmpq %rbx, %r9
setbe %r10b
movq -0x8(%r8,%r10,8), %r8
btq %rsi, %r8
jae 0x2fa0
movl (%r12,%rsi,4), %r8d
leal 0x1(%r8), %r9d
movq (%r14,%r9,8), %r9
movl (%rbp,%r8,4), %r10d
leal -0x1(%r10), %r11d
movl %r11d, (%rbp,%r8,4)
subq %r10, %r9
movl %ecx, (%r12,%r9,4)
incq %rsi
cmpq %rsi, %rdi
jne 0x2f51
incq %rcx
cmpq 0x10(%rsp), %rcx
jne 0x2f43
jmp 0x2fc0
leaq 0x311a(%rip), %rdi      # 0x60d5
callq 0x2250
movq %rbp, %rdi
callq 0x21d0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2ff0
callq 0x2160
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movl %eax, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movq 0x70(%rsp), %rax
leaq 0x5d3c(%rip), %rcx      # 0x8d38
movq %rcx, (%rax)
movl 0xc(%rsp), %ecx
movl %ecx, 0x8(%rax)
movq %r14, 0x10(%rax)
movq %r12, 0x18(%rax)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x40(%rsp), %rsi
testq %rsi, %rsi
je 0x3034
leaq 0x40(%rsp), %rdi
callq 0x22e0
movq %rbx, %rdi
callq 0x2290