// File: /workspace/github_cpp_makefile_star2_O2/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, 0x68(%rsp)
movq %rdi, 0x60(%rsp)
movq (%rsi), %r14
movq 0x8(%rsi), %rax
addq %r14, %rax
cmpq %rax, %r14
jae 0x272a
cmpb $0x21, (%r14)
jge 0x272a
incq %r14
jmp 0x2710
leaq -0x1(%rbx), %rax
cmpb $0x21, -0x1(%rbx)
jge 0x2732
movq %rax, %rbx
cmpq %rax, %r14
jb 0x2720
pushq $0xa
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x36d4
movq %rax, %r15
leaq 0x8(,%rax,8), %rdi
incq %r15
callq 0x2210
leaq 0x38(%rsp), %rcx
andl $0x0, (%rcx)
movq %rax, %r13
movb $0x1, %cl
xorl %r12d, %r12d
xorl %eax, %eax
movq %r15, 0x30(%rsp)
movq %r13, 0x70(%rsp)
cmpq %r12, %r15
je 0x280d
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x38(%rsp), %r15
movq %r15, %rdx
movl %ecx, %ebp
movq %rax, 0x20(%rsp)
callq 0x265e
movl %eax, 0x18(%rsp)
movl 0x38(%rsp), %r13d
addq %r14, %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2642
movl %eax, %r14d
addq %r13, %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x265e
movl %eax, 0x10(%rsp)
movl 0x38(%rsp), %r15d
addq %r14, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2642
movl %eax, %r14d
addq %r15, %r14
movq 0x30(%rsp), %r15
movq 0x70(%rsp), %r13
movl 0x18(%rsp), %eax
movl %eax, (%r13,%r12,8)
movl %eax, %eax
cmpq %rax, 0x20(%rsp)
setbe %cl
movl 0x10(%rsp), %edx
movl %edx, 0x4(%r13,%r12,8)
andb %cl, %bpl
movl %ebp, %ecx
incq %r12
jmp 0x276f
leaq (%r15,%r15), %rbp
testb $0x1, %cl
jne 0x2826
pushq $0x2
popq %rsi
movq %r13, %rdi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x38ee
movl -0x8(%r13,%rbp,4), %ebx
leal 0x2(%rbx), %edi
shlq $0x3, %rdi
callq 0x2210
movq %rax, %r14
andq $0x0, (%rax)
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %eax
cmpq %r15, %rcx
je 0x2870
leaq (%rcx,%rcx), %rdx
movl (%r13,%rcx,8), %esi
cmpq %rsi, %rax
jae 0x2861
movq %rcx, 0x8(%r14,%rax,8)
incq %rax
jmp 0x2852
movl 0x4(%r13,%rdx,4), %edx
movl %edx, (%r13,%rcx,4)
incq %rcx
jmp 0x2842
leal 0x1(%rbx), %ecx
movq %rcx, 0x18(%rsp)
cmpq 0x18(%rsp), %rax
jae 0x2889
movq %r15, 0x8(%r14,%rax,8)
incq %rax
jmp 0x2878
leaq (,%r15,4), %rsi
movq %r13, %rdi
callq 0x22b0
movq %rax, %r12
testb $0x9, 0x68(%rsp)
je 0x2bb9
movq %rbx, 0x10(%rsp)
xorl %ebx, %ebx
cmpq %rbx, 0x18(%rsp)
je 0x28dc
movq (%r14,%rbx,8), %rcx
movq 0x8(%r14,%rbx,8), %rax
movq %rax, %rdx
subq %rcx, %rdx
cmpq $0x1, %rdx
jbe 0x28d7
leaq (%r12,%rcx,4), %rdi
leaq (%r12,%rax,4), %rsi
callq 0x3039
incq %rbx
jmp 0x28ae
pushq $0x4
popq %rsi
movq 0x18(%rsp), %rdi
callq 0x2100
movq %rax, 0x20(%rsp)
leaq 0xc(%rsp), %rbx
movb $0x0, (%rbx)
leaq 0x38(%rsp), %rdi
leaq 0x2f(%rsp), %rcx
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x2c82
xorl %eax, %eax
xorl %r15d, %r15d
movl %eax, 0xc(%rsp)
cmpl 0x18(%rsp), %eax
jae 0x2971
movl %eax, %ecx
movq (%r14,%rcx,8), %rbp
incl %eax
cmpq (%r14,%rax,8), %rbp
jae 0x2910
movl (%r12,%rbp,4), %r13d
movq (%r14,%r13,8), %rax
leaq (%r12,%rax,4), %rdi
leal 0x1(%r13), %eax
movq (%r14,%rax,8), %rax
leaq (%r12,%rax,4), %rsi
movq %rbx, %rdx
callq 0x2ce5
testb %al, %al
jne 0x2968
movq 0x20(%rsp), %rax
incl (%rax,%r13,4)
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x2d0c
incq %r15
orq %rdx, (%rax)
incq %rbp
movl 0xc(%rsp), %eax
jmp 0x2920
testq %r15, %r15
je 0x2b99
movabsq $-0x8000000000000000, %rbx # imm = 0x8000000000000000
movq 0x30(%rsp), %rsi
addq %r15, %rsi
shlq $0x2, %rsi
movq %r12, %rdi
callq 0x22b0
movq %rax, %r12
movq 0x10(%rsp), %r13
incq %r13
pushq $0x40
popq %rcx
leaq 0x3f(%rbx), %rdx
movq %rdx, 0x30(%rsp)
cmpq $0x1, %r13
je 0x2b34
movl %r13d, %eax
movq %r13, %rsi
movq (%r14,%rax,8), %rbx
movq %rcx, %r13
movq -0x8(%r14,%rsi,8), %rbp
movq %rbx, %rdx
subq %rbp, %rdx
leaq (%rbx,%r15), %rcx
movq %rcx, (%r14,%rax,8)
movq %rsi, 0x10(%rsp)
movq 0x20(%rsp), %rax
movl -0x4(%rax,%rsi,4), %eax
subq %rax, %r15
leaq (%r12,%rbp,4), %rsi
leaq (%rsi,%r15,4), %rdi
shlq $0x2, %rdx
callq 0x22a0
movq 0x38(%rsp), %rsi
movq %rbp, %rax
cqto
idivq %r13
leaq (%rsi,%rax,8), %rax
movq %rbp, %rcx
movq 0x30(%rsp), %r9
andq %r9, %rcx
xorl %edx, %edx
movabsq $-0x8000000000000000, %r8 # imm = 0x8000000000000000
cmpq %r8, %rcx
setbe %dl
leaq (%rax,%rdx,8), %rdi
addq $-0x8, %rdi
andl $0x3f, %ebp
movq %rbx, %rax
cqto
idivq %r13
leaq (%rsi,%rax,8), %rax
movq %rbx, %rcx
andq %r9, %rcx
movq %r9, %r10
xorl %edx, %edx
cmpq %r8, %rcx
setbe %dl
leaq (%rax,%rdx,8), %r9
addq $-0x8, %r9
movl %ebx, %ecx
andl $0x3f, %ecx
addq %r15, %rbx
movq %rbx, %rax
cqto
idivq %r13
leaq (%rsi,%rax,8), %rax
movq %rbx, %rdx
andq %r10, %rdx
xorl %esi, %esi
movq %r8, %r13
cmpq %r8, %rdx
setbe %sil
leaq (%rax,%rsi,8), %r8
addq $-0x8, %r8
andl $0x3f, %ebx
movl %ebp, %esi
movq %r9, %rdx
movl %ebx, %r9d
callq 0x355e
movq 0x38(%rsp), %r8
movq 0x10(%rsp), %rax
movq -0x8(%r14,%rax,8), %rcx
addq %r15, %rcx
addl $-0x2, %eax
movq 0x20(%rsp), %rdx
movl (%rdx,%rax,4), %eax
movq %rcx, %rsi
subq %rax, %rsi
movq %rsi, %rax
cqto
pushq $0x40
popq %rdi
idivq %rdi
movq %rdi, %r9
leaq (%r8,%rax,8), %rax
movq %rsi, %rdx
movq 0x30(%rsp), %rbp
andq %rbp, %rdx
xorl %edi, %edi
cmpq %r13, %rdx
setbe %dil
leaq (%rax,%rdi,8), %rdi
addq $-0x8, %rdi
andl $0x3f, %esi
movq %rcx, %rax
cqto
idivq %r9
movq %r9, %rbx
movq %rcx, %rdx
andq %rbp, %rdx
xorl %r9d, %r9d
cmpq %r13, %rdx
setbe %r9b
leaq (%r8,%rax,8), %rax
leaq (%rax,%r9,8), %rdx
addq $-0x8, %rdx
andl $0x3f, %ecx
movb $0x0, 0xc(%rsp)
leaq 0xc(%rsp), %r8
callq 0x361b
movq 0x10(%rsp), %r13
movq %rbx, %rcx
decq %r13
jmp 0x29af
addq %r15, 0x8(%r14)
xorl %ebp, %ebp
leaq 0x38(%rsp), %rbx
cmpq 0x18(%rsp), %rbp
je 0x2ba5
movq (%r14,%rbp,8), %r15
movq 0x8(%r14,%rbp,8), %r13
leaq 0x1(%rbp), %rax
movq %rax, 0x10(%rsp)
cmpq %r13, %r15
jae 0x2b92
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2d0c
testq %rdx, (%rax)
je 0x2b8d
movl (%r12,%r15,4), %eax
leal 0x1(%rax), %ecx
movq (%r14,%rcx,8), %rcx
movq 0x20(%rsp), %rdi
movl (%rdi,%rax,4), %edx
leal -0x1(%rdx), %esi
movl %esi, (%rdi,%rax,4)
subq %rdx, %rcx
movl %ebp, (%r12,%rcx,4)
incq %r15
jmp 0x2b58
movq 0x10(%rsp), %rbp
jmp 0x2b3f
leaq 0x34d9(%rip), %rdi      # 0x6079
callq 0x2280
movq 0x20(%rsp), %rdi
callq 0x21f0
leaq 0x38(%rsp), %rdi
callq 0x2d86
leaq 0x6178(%rip), %rcx      # 0x8d38
movq 0x60(%rsp), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rcx
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
jmp 0x2bef
jmp 0x2bef
jmp 0x2bef
jmp 0x2bef
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x2d86
movq %rbx, %rdi
callq 0x22c0