// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
movq %rdx, %r15
movl %esi, %ebp
movl %edi, %ebx
movq %rdx, %rdi
callq 0x63ba
testl %eax, %eax
je 0x17320
movl (%r15), %eax
cmpl %ebx, %eax
setg %cl
cmpl %ebp, %eax
setl %al
orb %cl, %al
je 0x17335
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
callq 0x11619
movq %rax, %r14
movq %rax, %rdi
movl %ebx, 0x8(%rsp)
movl %ebx, %esi
callq 0x5c80
xorl %r12d, %r12d
pushq $0x1
popq %r13
movslq (%r15), %rbx
cmpq %rbx, %r12
jge 0x173b3
movq 0x10(%r15), %rax
movq (%rax,%r12,8), %rax
movq 0x10(%r14), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x10(%r15), %rax
andq $0x0, (%rax,%r12,8)
movq 0x10(%r14), %rax
movq (%rax,%r12,8), %rdi
callq 0x5a8a
movq 0x10(%r14), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, 0x28(%rcx)
movl %r13d, %edi
callq 0xb405
movq 0x10(%r14), %rcx
movq (%rcx,%r12,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%r12,8), %rax
movl $0x1, 0x24(%rax)
incq %r12
jmp 0x17357
movl 0x4(%r15), %eax
movl %eax, 0x4(%r14)
movslq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
pushq $0x1
popq %r12
cmpq 0x8(%rsp), %rbx
jge 0x17414
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x114a5
movq 0x10(%r14), %rcx
movq %rax, (%rcx,%rbx,8)
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rax
movl %ebx, 0x18(%rax)
movl %r12d, %edi
callq 0xb405
movq 0x10(%r14), %rcx
movq (%rcx,%rbx,8), %rdx
movq %rax, (%rdx)
movq (%rcx,%rbx,8), %rax
movl $0x1, 0x24(%rax)
incq %rbx
jmp 0x173c9
movslq (%r15), %r15
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
testl %r15d, %r15d
movl $0x0, %r12d
cmovgl %r15d, %r12d
cmpq 0x8(%rsp), %r15
jge 0x17323
xorl %ebx, %ebx
cmpl %ebp, %ebx
je 0x174c0
callq 0x1240
movl 0x4(%r14), %ecx
addl %ecx, %ecx
cltd
idivl %ecx
xorl %r13d, %r13d
xorl %eax, %eax
cmpq %r13, %r12
je 0x174b9
movq 0x10(%r14), %rcx
movq (%rcx,%r13,8), %rcx
addl 0x28(%rcx), %eax
cmpl %eax, %edx
jl 0x1746c
incq %r13
jmp 0x17453
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x613f
testb %al, %al
jne 0x174b9
pushq $0x1
popq %rdi
callq 0xb405
movl %r15d, %edi
movl %r13d, %esi
movq %rax, %rdx
movq %r14, %rcx
movq 0x10(%rsp), %r8
callq 0x6200
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rcx
incl 0x28(%rcx)
movq (%rax,%r13,8), %rax
incl 0x28(%rax)
movq 0x8(%rcx), %rax
movl $0x1, 0x28(%rax)
incl %ebx
jmp 0x17438
incq %r15
jmp 0x1741e