// File: /workspace/github_c_makefile_star2_O3/pwelke[P]GraphMiningTools/randomGraphGenerators.c
// Function: barabasiAlbert
struct Graph* barabasiAlbert(int n, int edgesAddedPerVertex, struct Graph* core, struct GraphPool* gp) {
	// min degree of core needs to be 1
	if ((getMinDegree(core) == 0) || (n < core->n) || (edgesAddedPerVertex > core->n)) {
		return NULL;
	}

	//m <= m_0
	struct Graph* g = getGraph(gp);
	setVertexNumber(g, n);

	// TODO copying of core can be sped up by directly copying in initialized vertex array of g
	for (int v=0; v<core->n; ++v) {
		// copy vertex into g
		g->vertices[v] = core->vertices[v];
		core->vertices[v] = NULL;

		// store degree of vertex in ->d for fast access
		g->vertices[v]->d = degree(g->vertices[v]);

		// set label of v
		g->vertices[v]->label = intLabel(1);
		g->vertices[v]->isStringMaster = 1;
	}
	g->m = core->m;

	for (int v=core->n; v<n; ++v) {
		g->vertices[v] = getVertex(gp->vertexPool);
		g->vertices[v]->number = v;
		g->vertices[v]->label = intLabel(1);
		g->vertices[v]->isStringMaster = 1;
	}

	for (int v=core->n; v<n; ++v) {
		for (int i=0; i<edgesAddedPerVertex; ++i) {
			int randV = rand() % (2 * g->m);
			int find = 0;
			for (int w=0; w<v; ++w) {
				find += g->vertices[w]->d;
				if (randV < find) {
					if (!isNeighbor(g, v, w)) {
						addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
						g->vertices[v]->d += 1;
						g->vertices[w]->d += 1;
						g->vertices[v]->neighborhood->isStringMaster = 1;
					}
					break;
				}
			}
		}
	}
	return g;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r14
movq %rdx, %r12
movl %esi, %ebp
movl %edi, %r13d
movq %rdx, %rdi
callq 0x8684
testl %eax, %eax
je 0x196c0
movl (%r12), %eax
cmpl %r13d, %eax
setg %cl
cmpl %ebp, %eax
setl %al
orb %cl, %al
je 0x196d5
xorl %r15d, %r15d
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x146a9
movq %rax, %r15
movq %rax, %rdi
movl %r13d, %esi
callq 0x7e6c
movl (%r12), %eax
testl %eax, %eax
jle 0x19757
movq 0x10(%r15), %rcx
xorl %ebx, %ebx
movq 0x10(%r12), %rax
movq (%rax,%rbx,8), %rax
movq %rax, (%rcx,%rbx,8)
movq 0x10(%r12), %rax
movq $0x0, (%rax,%rbx,8)
movq 0x10(%r15), %rax
movq (%rax,%rbx,8), %rdi
callq 0x7c4e
movq 0x10(%r15), %rcx
movq (%rcx,%rbx,8), %rcx
movl %eax, 0x28(%rcx)
movl $0x1, %edi
callq 0xdcf9
movq 0x10(%r15), %rcx
movq (%rcx,%rbx,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbx,8), %rax
movl $0x1, 0x24(%rax)
incq %rbx
movslq (%r12), %rax
cmpq %rax, %rbx
jl 0x196f9
movl 0x4(%r12), %ecx
movl %ecx, 0x4(%r15)
cmpl %r13d, %eax
jge 0x197b0
movslq %eax, %rbx
movq 0x10(%r14), %rdi
callq 0x1447e
movq 0x10(%r15), %rcx
movq %rax, (%rcx,%rbx,8)
movq 0x10(%r15), %rax
movq (%rax,%rbx,8), %rax
movl %ebx, 0x18(%rax)
movl $0x1, %edi
callq 0xdcf9
movq 0x10(%r15), %rcx
movq (%rcx,%rbx,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbx,8), %rax
movl $0x1, 0x24(%rax)
incq %rbx
cmpl %ebx, %r13d
jne 0x19768
movl (%r12), %eax
cmpl %r13d, %eax
jge 0x196c3
movq %r14, 0x10(%rsp)
movslq %eax, %r12
movl %r13d, 0xc(%rsp)
testl %ebp, %ebp
jle 0x1985d
movl %r12d, %r14d
xorl %ebx, %ebx
callq 0x1250
movl 0x4(%r15), %ecx
addl %ecx, %ecx
cltd
idivl %ecx
testq %r12, %r12
jle 0x19853
movq 0x10(%r15), %rax
xorl %r13d, %r13d
xorl %ecx, %ecx
movq (%rax,%r13,8), %rsi
addl 0x28(%rsi), %ecx
cmpl %ecx, %edx
jl 0x19804
incq %r13
cmpq %r13, %r14
jne 0x197ef
jmp 0x19853
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x83c5
testb %al, %al
jne 0x19853
movl $0x1, %edi
callq 0xdcf9
movl %r12d, %edi
movl %r13d, %esi
movq %rax, %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r8
callq 0x84a3
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %rcx
incl 0x28(%rcx)
movq (%rax,%r13,8), %rax
incl 0x28(%rax)
movq 0x8(%rcx), %rax
movl $0x1, 0x28(%rax)
incl %ebx
cmpl %ebp, %ebx
jne 0x197d3
incq %r12
movl 0xc(%rsp), %r13d
cmpl %r13d, %r12d
jne 0x197c6
jmp 0x196c3