// File: /workspace/github_cpp_makefile_star2_O2/EnderGed[P]fast-random-walk/WeightedGraph.cpp
// Function: WeightedGraph::readGraph(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long)
WeightedGraph WeightedGraph::readGraph(std::string const input, uint64_t const flags)
{
	bool isSorted = true;
	read_t numRows;
	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
		numRows = count(fileContent, fileContentEnd, '\n') + 1;

		// Malloc data array (3 dimensions)
		data = (uint *) malloc(numRows * 3 * 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 < numRows; ++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
			pos += readLen;
			pos += nonDigits(pos, fileContentEnd);

			// Read weight
			uint const weight = readInt(pos, fileContentEnd, readLen);

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

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

			if(from < prev) isSorted = false;

			prev = from;
		}
	}

	// Sort if necessary
	if(!isSorted) countSort2D(data, 3, numRows);

	////		Render node starts	////
	uint * weights = (uint*)malloc(numRows * sizeof(*weights));
	uint const numIndices = data[3 * numRows - 3] + 1;
	read_t * const indices = (read_t *) malloc((numIndices + 1) * sizeof(*indices));
	
	// Create adjacency list
	uint curr = 0;
	indices[0] = 0;

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

		// Copy target node id to new (shrinked) position
		data[i] = data[3 * i + 1];
		// Copy the weight of the transition to weights
		weights[i] = data[3 * i + 2];
	}

	// Set target[curr] to end of data-set for all remaining indices
	while(curr < numIndices) indices[++curr] = numRows;

	// Shrink the size of data to its real content
	data = (uint *) realloc(data, numRows * sizeof(*data));

	////		Convert the graph to an undirected graph	////
	if(flags & (flag_undirected | flag_reflect))
	{
		// Sort the transitions for each source node
		// (Target nodes in increasing order)
		for(uint i = 0; i < numIndices; ++i)
		{
			read_t const start = indices[i];
			read_t const count = indices[i + 1] - start;

			if(count > 1) countSort2D({data + start, weights + start}, count);
		}

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

		// For every source node ...
		for(uint node = 0; node < numIndices; ++node)
		{
			// For every transition of the node ...
			for(read_t trans = indices[node]; trans < indices[node + 1]; ++trans)
			{
				uint const targetNode = data[trans];
				uint const * const from = data + indices[targetNode];
				uint const * const to = data + indices[targetNode + 1];
				uint const * const found = binaryFind(from, to, node);

				// If has no counterpart ...
				if(found == to || *found != node)
				{
					// Increment counter for target node
					++counters[targetNode];
					// Increment number of transitions
					++acc;
					// Set bit in bitSet
					bitSet[trans] = true;
				}
				else if(flags & flag_reflect)
				{
					read_t const other_trans = found - data;
					if(other_trans > trans) weights[trans] = weights[other_trans] = weights[other_trans] + weights[trans];
				}
			}
		}

		if(acc)
		{
			////	Adapt list (make it undirected)		////
			// Resize transitions lists (data and weights)
			numRows += acc;
			data = (uint *) realloc(data, numRows * sizeof(*data));
			weights = (uint *) realloc(weights, numRows * sizeof(*weights));

			// For every node (from last to first)
			for(uint node = numIndices - 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));
				memmove(weights + indices[node] + acc, weights + indices[node], len * sizeof(*weights));
				// 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);
			}

			// Node 0
			indices[1] += acc;

			////	Add new transitions		////
			// For every node ...
			for(uint node = 0; node < numIndices; ++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];
						uint const weight = weights[trans];
						data[indices[targetNode + 1] - (counters[targetNode])] = node;
						weights[indices[targetNode + 1] - (counters[targetNode])] = weight;

						--counters[targetNode];
					}
				}
			}
		}
		else if(!(flags & flag_reflect)) printf("Graph is already undirected\n");

		free(counters);
	}

	////		Optimize transition order	////
	if((flags & flag_optiTransOrder) && numIndices > 0)
	{
		// Sort each node
		for(uint nodeId = 0; nodeId < numIndices; ++nodeId)
		{
			// Determine sort range
			read_t const start = indices[nodeId];
			read_t const count = indices[nodeId + 1] - start;

			// Sort node (lowest weight first)
			countSort2D({weights + start, data + start}, count);

			// Reverse order (highest weight first)
			std::reverse(data + start, data + start + count);
			std::reverse(weights + start, weights + start + count);
		}
	}

	////		Post-Processing		////
	bool errorFound = false;
	// Accumulate weights
	for(read_t const * posIndices = indices, * indicesEnd = indices + numIndices; posIndices < indicesEnd; ++posIndices)
	{ // For each node ...
		uint newWeight = weights[*posIndices];
	
		// Accumulate weights of this nodes transitions
		for(read_t posWeights = *posIndices + 1; posWeights < *(posIndices + 1); ++posWeights)
		{
			//if(__builtin_add_overflow(weights[posWeights], weights[posWeights - 1], &weights[posWeights])) std::cout << "!!!!!Problem!!!!!" << std::endl;
			newWeight = weights[posWeights] + weights[posWeights - 1];
			if(newWeight < weights[posWeights] || newWeight < weights[posWeights - 1])
			{
				printf("Integer Overflow!!!");
				exit(1);
			}
			weights[posWeights] = newWeight;
		}

		// Check whether sum of weights is 0
		if(newWeight == 0)
		{
			printf("Node %lu has an accumulative weight of 0!\n", posIndices - indices);
			errorFound = true;
		}
	}
	
	if(errorFound) exit(1);

	return WeightedGraph(numIndices, indices, data, weights);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, 0x40(%rsp)
movq %rdi, 0x78(%rsp)
movq (%rsi), %rbp
movq 0x8(%rsi), %rax
addq %rbp, %rax
cmpq %rax, %rbp
jae 0x416a
cmpb $0x21, (%rbp)
jge 0x416a
incq %rbp
jmp 0x414f
leaq -0x1(%r15), %rax
cmpb $0x21, -0x1(%r15)
jge 0x4172
movq %rax, %r15
cmpq %rax, %rbp
jb 0x415f
pushq $0xa
popq %rdx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x36d4
movq %rax, %rbx
incq %rbx
imulq $0xc, %rbx, %rdi
movq %rdi, 0x18(%rsp)
callq 0x2210
leaq 0x50(%rsp), %rcx
andl $0x0, (%rcx)
movq %rax, 0x80(%rsp)
movq %rax, %rdx
addq $0x8, %rdx
movb $0x1, %cl
xorl %eax, %eax
movq %rbx, 0x48(%rsp)
subq $0x1, %rbx
jb 0x427b
movq %rbp, %rdi
movq %r15, %rsi
leaq 0x50(%rsp), %r13
movq %rdx, 0x20(%rsp)
movq %r13, %rdx
movb %cl, 0x10(%rsp)
movq %rax, 0x30(%rsp)
callq 0x265e
movl %eax, 0x8(%rsp)
movl 0x50(%rsp), %r14d
addq %rbp, %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2642
movl %eax, %ebp
addq %r14, %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x265e
movl %eax, %r14d
movl 0x50(%rsp), %r12d
addq %rbp, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x2642
movl %eax, %ebp
addq %r12, %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x265e
movl %eax, %r12d
movl 0x50(%rsp), %r13d
addq %rbp, %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x2642
movq 0x20(%rsp), %rdx
movl %eax, %ebp
addq %r13, %rbp
movl 0x8(%rsp), %eax
movl %eax, -0x8(%rdx)
movl %r14d, -0x4(%rdx)
movl %eax, %eax
cmpq %rax, 0x30(%rsp)
setbe %cl
movl %r12d, (%rdx)
andb %cl, 0x10(%rsp)
movb 0x10(%rsp), %cl
addq $0xc, %rdx
jmp 0x41b4
testb $0x1, %cl
movq 0x48(%rsp), %r14
movq 0x80(%rsp), %r15
jne 0x429d
pushq $0x3
popq %rsi
movq %r15, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x38ee
leaq (,%r14,4), %rbx
movq %rbx, %rdi
callq 0x2210
movq %rax, %rbp
movq 0x18(%rsp), %rax
movl -0xc(%r15,%rax), %eax
movq %rax, 0x38(%rsp)
leal 0x2(%rax), %edi
shlq $0x3, %rdi
callq 0x2210
movq %rax, %r12
andq $0x0, (%rax)
xorl %ecx, %ecx
xorl %eax, %eax
movl %eax, %eax
cmpq %r14, %rcx
je 0x430b
imulq $0xc, %rcx, %rdx
movl (%r15,%rdx), %esi
cmpq %rsi, %rax
jae 0x42f4
movq %rcx, 0x8(%r12,%rax,8)
incq %rax
jmp 0x42e5
movl 0x4(%r15,%rdx), %esi
movl %esi, (%r15,%rcx,4)
movl 0x8(%r15,%rdx), %edx
movl %edx, (%rbp,%rcx,4)
incq %rcx
jmp 0x42d6
movq 0x38(%rsp), %rcx
incl %ecx
movq %rcx, 0x18(%rsp)
cmpq 0x18(%rsp), %rax
jae 0x4328
movq %r14, 0x8(%r12,%rax,8)
incq %rax
jmp 0x4317
movq %r15, %rdi
movq %rbx, %rsi
callq 0x22b0
movq %rax, 0x10(%rsp)
testb $0x9, 0x40(%rsp)
movq %rbp, 0x8(%rsp)
je 0x4757
xorl %r15d, %r15d
leaq 0xa0(%rsp), %rbx
leaq 0x7(%rsp), %r14
cmpq %r15, 0x18(%rsp)
je 0x43ba
movq (%r12,%r15,8), %rax
movq 0x8(%r12,%r15,8), %r13
subq %rax, %r13
cmpq $0x2, %r13
jb 0x43b5
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,4), %rcx
movq %rcx, 0x50(%rsp)
leaq (,%rax,4), %rax
addq %rbp, %rax
movq %rax, 0x58(%rsp)
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
pushq $0x2
popq %rdx
movq %r14, %rcx
callq 0x4914
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3c4a
movq %rbx, %rdi
callq 0x4a1e
incq %r15
jmp 0x4358
movq %r12, 0x30(%rsp)
pushq $0x4
popq %rsi
movq 0x18(%rsp), %rdi
callq 0x2100
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %r14
leaq (%r14,%r14), %rsi
leaq 0x7(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0x2f(%rsp), %rcx
callq 0x2c82
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpq 0x18(%rsp), %rbx
je 0x44b5
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %r14
leaq 0x1(%rbx), %r15
movq 0x30(%rsp), %rdx
cmpq (%rdx,%r15,8), %r14
jae 0x44a8
movq 0x10(%rsp), %rcx
movl (%rcx,%r14,4), %ebp
movq (%rdx,%rbp,8), %rax
leaq (%rcx,%rax,4), %rdi
leal 0x1(%rbp), %eax
movq (%rdx,%rax,8), %rax
leaq (%rcx,%rax,4), %r13
movq %r13, %rsi
movl %ebx, %edx
callq 0x494a
cmpq %r13, %rax
je 0x4480
movl (%rax), %ecx
cmpq %rcx, %rbx
jne 0x4480
testb $0x8, 0x40(%rsp)
movq 0x8(%rsp), %rbp
je 0x44a0
subq 0x10(%rsp), %rax
movq %rax, %rcx
sarq $0x2, %rcx
cmpq %r14, %rcx
jbe 0x44a0
movl (%rbp,%r14,4), %ecx
addl (%rbp,%rax), %ecx
movl %ecx, (%rbp,%rax)
movl %ecx, (%rbp,%r14,4)
jmp 0x44a0
movq 0x20(%rsp), %rax
incl (%rax,%rbp,4)
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x2d0c
incq %r12
orq %rdx, (%rax)
movq 0x8(%rsp), %rbp
incq %r14
jmp 0x440e
movq %r15, %rbx
movq 0x48(%rsp), %r14
jmp 0x43f6
testq %r12, %r12
je 0x4726
movq %r12, %rbx
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
addq %rbx, %r14
shlq $0x2, %r14
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x22b0
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x22b0
movq %rax, %rbp
movq 0x38(%rsp), %r15
incq %r15
leaq 0x3f(%r12), %rdx
movq 0x30(%rsp), %r12
movq %rdx, 0x8(%rsp)
cmpq $0x1, %r15
je 0x46af
movl %r15d, %eax
movq (%r12,%rax,8), %r14
movq %r15, %rdx
movq -0x8(%r12,%r15,8), %r15
movq %r14, %r13
subq %r15, %r13
leaq (%r14,%rbx), %rcx
movq %rcx, (%r12,%rax,8)
movq %rdx, 0x38(%rsp)
movq 0x20(%rsp), %rax
movl -0x4(%rax,%rdx,4), %eax
subq %rax, %rbx
movq 0x10(%rsp), %rax
leaq (%rax,%r15,4), %rsi
leaq (%rsi,%rbx,4), %rdi
shlq $0x2, %r13
movq %r13, %rdx
callq 0x22a0
leaq (,%r15,4), %rsi
addq %rbp, %rsi
leaq (%rsi,%rbx,4), %rdi
movq %r13, %rdx
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
callq 0x22a0
movq 0x50(%rsp), %rsi
movq %r15, %rax
cqto
pushq $0x40
popq %r10
idivq %r10
leaq (%rsi,%rax,8), %rax
movq %r15, %rcx
movq 0x8(%rsp), %r8
andq %r8, %rcx
xorl %edx, %edx
cmpq %r13, %rcx
setbe %dl
leaq (%rax,%rdx,8), %rdi
addq $-0x8, %rdi
andl $0x3f, %r15d
movq %r14, %rax
cqto
idivq %r10
leaq (%rsi,%rax,8), %rax
movq %r14, %rcx
andq %r8, %rcx
xorl %edx, %edx
cmpq %r13, %rcx
movq %r13, %r11
setbe %dl
leaq (%rax,%rdx,8), %r9
addq $-0x8, %r9
movl %r14d, %ecx
andl $0x3f, %ecx
addq %rbx, %r14
movq %r14, %rax
cqto
movq %r10, %r13
idivq %r10
leaq (%rsi,%rax,8), %rax
movq %r14, %rdx
andq %r8, %rdx
xorl %esi, %esi
cmpq %r11, %rdx
setbe %sil
leaq (%rax,%rsi,8), %r8
addq $-0x8, %r8
andl $0x3f, %r14d
movl %r15d, %esi
movq %r9, %rdx
movl %r14d, %r9d
callq 0x355e
movq 0x50(%rsp), %r8
movq 0x38(%rsp), %r15
movq -0x8(%r12,%r15,8), %rcx
addq %rbx, %rcx
leal -0x2(%r15), %eax
movq 0x20(%rsp), %rdx
movl (%rdx,%rax,4), %eax
movq %rcx, %rsi
subq %rax, %rsi
movq %rsi, %rax
cqto
movq %r13, %r11
idivq %r13
leaq (%r8,%rax,8), %rax
movq %rsi, %rdx
movq 0x8(%rsp), %r13
andq %r13, %rdx
xorl %edi, %edi
movabsq $-0x8000000000000000, %r10 # imm = 0x8000000000000000
cmpq %r10, %rdx
setbe %dil
leaq (%rax,%rdi,8), %rdi
addq $-0x8, %rdi
andl $0x3f, %esi
movq %rcx, %rax
cqto
idivq %r11
movq %rcx, %rdx
andq %r13, %rdx
xorl %r9d, %r9d
cmpq %r10, %rdx
setbe %r9b
leaq (%r8,%rax,8), %rax
leaq (%rax,%r9,8), %rdx
addq $-0x8, %rdx
andl $0x3f, %ecx
movb $0x0, 0x7(%rsp)
leaq 0x7(%rsp), %r8
callq 0x361b
decq %r15
jmp 0x4509
addq %rbx, 0x8(%r12)
xorl %r15d, %r15d
leaq 0x50(%rsp), %rbx
cmpq 0x18(%rsp), %r15
je 0x473e
movq (%r12,%r15,8), %r14
movq 0x8(%r12,%r15,8), %r13
leaq 0x1(%r15), %rax
movq %rax, 0x8(%rsp)
cmpq %r13, %r14
jae 0x471f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2d0c
testq %rdx, (%rax)
je 0x471a
movq 0x10(%rsp), %rdi
movl (%rdi,%r14,4), %eax
movl (%rbp,%r14,4), %ecx
leal 0x1(%rax), %edx
movq (%r12,%rdx,8), %rdx
movq 0x20(%rsp), %r8
movl (%r8,%rax,4), %esi
subq %rsi, %rdx
movl %r15d, (%rdi,%rdx,4)
movl %ecx, (%rbp,%rdx,4)
leal -0x1(%rsi), %ecx
movl %ecx, (%r8,%rax,4)
incq %r14
jmp 0x46d5
movq 0x8(%rsp), %r15
jmp 0x46bc
testb $0x8, 0x40(%rsp)
movq 0x30(%rsp), %r12
jne 0x473e
leaq 0x1940(%rip), %rdi      # 0x6079
callq 0x2280
movq %rbp, 0x8(%rsp)
movq 0x20(%rsp), %rdi
callq 0x21f0
leaq 0x50(%rsp), %rdi
callq 0x2d86
testb $0x2, 0x40(%rsp)
je 0x4806
cmpl $0x0, 0x18(%rsp)
je 0x4806
xorl %eax, %eax
leaq 0x88(%rsp), %r15
cmpq %rax, 0x18(%rsp)
je 0x4806
movq (%r12,%rax,8), %rbp
movq %rax, 0x20(%rsp)
movq 0x8(%r12,%rax,8), %rbx
movq 0x8(%rsp), %rax
leaq (%rax,%rbp,4), %r13
movq %r13, 0x50(%rsp)
movq 0x10(%rsp), %rax
leaq (%rax,%rbp,4), %r14
movq %r14, 0x58(%rsp)
movq %r15, %rdi
leaq 0x50(%rsp), %rsi
pushq $0x2
popq %rdx
leaq 0x7(%rsp), %rcx
callq 0x4914
movq %rbx, %rsi
subq %rbp, %rsi
movq %r15, %rdi
callq 0x3c4a
movq %r15, %rdi
callq 0x4a1e
movq 0x10(%rsp), %rax
leaq (%rax,%rbx,4), %rsi
movq %r14, %rdi
callq 0x4aba
movq 0x8(%rsp), %rax
leaq (%rax,%rbx,4), %rsi
movq %r13, %rdi
callq 0x4aba
movq 0x20(%rsp), %rax
incq %rax
jmp 0x4777
movq 0x18(%rsp), %rax
leaq (%r12,%rax,8), %r14
xorl %eax, %eax
leaq 0x19e4(%rip), %rbx      # 0x61fc
movq %r12, %r15
movq 0x8(%rsp), %r13
cmpq %r14, %r15
jae 0x4867
movq %r15, %rsi
movq (%r15), %rcx
movq 0x8(%r15), %rdi
movl (%r13,%rcx,4), %edx
addq $0x8, %r15
incq %rcx
cmpq %rdi, %rcx
jae 0x484e
addl (%r13,%rcx,4), %edx
jb 0x489d
movl %edx, (%r13,%rcx,4)
jmp 0x4838
testl %edx, %edx
jne 0x4820
subq %r12, %rsi
sarq $0x3, %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2040
movb $0x1, %al
jmp 0x4820
testb $0x1, %al
jne 0x48ab
movq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0x3e38
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1944(%rip), %rdi      # 0x61e8
xorl %eax, %eax
callq 0x2040
pushq $0x1
popq %rdi
callq 0x2200
jmp 0x48c8
jmp 0x48c8
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x48d8
jmp 0x48e1
jmp 0x48e1
movq %rax, %rbx
jmp 0x48ee
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x4a1e
jmp 0x48ee
jmp 0x48e1
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2d86
movq %rbx, %rdi
callq 0x22c0