// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rdx, %r12
movsd %xmm0, (%rsp)
movl %esi, %ebp
movl %edi, %r13d
movq %rdx, %rdi
callq 0x8684
testl %eax, %eax
je 0x1945a
movl (%r12), %eax
cmpl %r13d, %eax
setg %cl
cmpl %ebp, %eax
setl %al
orb %cl, %al
je 0x1946f
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 0x194f1
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 0x19493
movl 0x4(%r12), %ecx
movl %ecx, 0x4(%r15)
cmpl %r13d, %eax
jge 0x1954a
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 0x19502
movl (%r12), %eax
cmpl %r13d, %eax
jge 0x1945d
movq %r14, 0x10(%rsp)
movsd (%rsp), %xmm0
mulsd 0x78fb(%rip), %xmm0     # 0x20e60
movsd %xmm0, (%rsp)
movslq %eax, %r12
movl %r13d, 0xc(%rsp)
callq 0x1250
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x19626
testl %ebp, %ebp
jle 0x19672
movl %r12d, %r14d
xorl %ebx, %ebx
callq 0x1250
movl 0x4(%r15), %ecx
addl %ecx, %ecx
cltd
idivl %ecx
testq %r12, %r12
jle 0x1961a
movq 0x10(%r15), %rax
xorl %r13d, %r13d
xorl %ecx, %ecx
movq (%rax,%r13,8), %rsi
addl 0x28(%rsi), %ecx
cmpl %ecx, %edx
jl 0x195cb
incq %r13
cmpq %r13, %r14
jne 0x195b6
jmp 0x1961a
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x83c5
testb %al, %al
jne 0x1961a
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 0x1959a
jmp 0x19672
callq 0x1250
cltd
idivl %r12d
movl %edx, %r13d
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)
movslq %r13d, %rdx
movq (%rax,%rdx,8), %rax
incl 0x28(%rax)
movq 0x8(%rcx), %rax
movl $0x1, 0x28(%rax)
incq %r12
movl 0xc(%rsp), %r13d
cmpl %r13d, %r12d
jne 0x19572
jmp 0x1945d