// File: /workspace/github_c_makefile_star2_O2/pwelke[P]GraphMiningTools/randomGraphGenerators.c
// Function: barabasiAlpha
struct Graph* barabasiAlpha(int n, int edgesAddedPerVertex, double alpha, 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) {
		if (rand() <= alpha * RAND_MAX) {
			int w = rand() % v;
			addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
			g->vertices[v]->d += 1;
			g->vertices[w]->d += 1;
			g->vertices[v]->neighborhood->isStringMaster = 1;
		} else {
			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, 0x8(%rsp)
movq %rdx, %r15
movsd %xmm0, 0x10(%rsp)
movl %esi, %ebp
movl %edi, %ebx
movq %rdx, %rdi
callq 0x63ba
testl %eax, %eax
je 0x170cf
movl (%r15), %eax
cmpl %ebx, %eax
setg %cl
cmpl %ebp, %eax
setl %al
orb %cl, %al
je 0x170e4
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rdi
callq 0x11619
movq %rax, %r14
movq %rax, %rdi
movl %ebx, (%rsp)
movl %ebx, %esi
callq 0x5c80
xorl %r12d, %r12d
pushq $0x1
popq %r13
movslq (%r15), %rbx
cmpq %rbx, %r12
jge 0x17161
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 0x17105
movl 0x4(%r15), %eax
movl %eax, 0x4(%r14)
movslq (%rsp), %rax
movq %rax, (%rsp)
pushq $0x1
popq %r12
cmpq (%rsp), %rbx
jge 0x171bf
movq 0x8(%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 0x17175
movslq (%r15), %r15
movsd 0x10(%rsp), %xmm0
mulsd 0x6c60(%rip), %xmm0     # 0x1de30
movsd %xmm0, 0x10(%rsp)
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
testl %r15d, %r15d
movl $0x0, %r12d
cmovgl %r15d, %r12d
cmpq (%rsp), %r15
jge 0x170d2
callq 0x1240
cvtsi2sd %eax, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x17297
xorl %ebx, %ebx
cmpl %ebp, %ebx
je 0x172e1
callq 0x1240
movl 0x4(%r14), %ecx
addl %ecx, %ecx
cltd
idivl %ecx
xorl %r13d, %r13d
xorl %eax, %eax
cmpq %r13, %r12
je 0x17290
movq 0x10(%r14), %rcx
movq (%rcx,%r13,8), %rcx
addl 0x28(%rcx), %eax
cmpl %eax, %edx
jl 0x17243
incq %r13
jmp 0x1722a
movq %r14, %rdi
movl %r15d, %esi
movl %r13d, %edx
callq 0x613f
testb %al, %al
jne 0x17290
pushq $0x1
popq %rdi
callq 0xb405
movl %r15d, %edi
movl %r13d, %esi
movq %rax, %rdx
movq %r14, %rcx
movq 0x8(%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 0x1720f
callq 0x1240
cltd
idivl %r15d
movl %edx, %r13d
pushq $0x1
popq %rdi
callq 0xb405
movl %r15d, %edi
movl %r13d, %esi
movq %rax, %rdx
movq %r14, %rcx
movq 0x8(%rsp), %r8
callq 0x6200
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rcx
incl 0x28(%rcx)
movslq %r13d, %rdx
movq (%rax,%rdx,8), %rax
incl 0x28(%rax)
movq 0x8(%rcx), %rax
movl $0x1, 0x28(%rax)
incq %r15
jmp 0x171dd