// File: /workspace/github_c_makefile_star2_O0/Mithreindeir[P]ewcc/src/ralloc.c
// Function: color_graph
void color_graph(struct vertex **graph, int num, int k)
{
	struct vstk stk;
	stk.vertices = NULL;
	stk.num_vertices = 0;
	/*Fill the vertex stack with vertex's in kempe order*/
	fill_stack(graph, num, &stk, k);
	/*Array that holds which colors the siblings of a node uses*/
	int *used_colors = calloc(sizeof(int), k);

	struct vertex *v;
	while ((v=pop(&stk))) {
		/*skip precolored nodes*/
		if (v->color > 0) continue;
		int clr = k;
		for (int i = 0; i < k; i++) used_colors[i] = 0;
		for (int i = 0; i < v->num_siblings; i++) {
			if (v->siblings[i]->color >= 0 && v->siblings[i]->color < k)
				used_colors[v->siblings[i]->color] = 1;
		}
		for (int i = 0; i < k; i++) {
			if (!used_colors[i]) {
				clr = i;
				break;
			}
		}
		if (clr >= k) {/*spill*/
			//v->color = k;
		} else {
			printf("Assigned R%d -> %d\n", v->ocolor, clr);
			v->color = clr;
		}
	}
	/*If k < the degree of all nodes, some nodes may not even be in the stack*/
	for (int i = 0; i < num; i++) {
		if (graph[i]->color < 0) {
			printf("Spilled %d\n", graph[i]->ocolor);
		}
	}

	free(stk.vertices);
	free(used_colors);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %ecx
leaq -0x20(%rbp), %rdx
callq 0x5590
movslq -0x10(%rbp), %rsi
movl $0x4, %edi
callq 0x2090
movq %rax, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x54f0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x57a7
movq -0x30(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jle 0x56a9
jmp 0x5686
movl -0x10(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x56d8
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x56b6
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0x5745
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x4(%rax)
jl 0x5738
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %eax
cmpl -0x10(%rbp), %eax
jge 0x5738
movq -0x28(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq -0x3c(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq 0x4(%rcx), %rcx
movl $0x1, (%rax,%rcx,4)
jmp 0x573a
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x56df
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x5777
movq -0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x576a
movl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x5777
jmp 0x576c
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x574c
movl -0x34(%rbp), %eax
cmpl -0x10(%rbp), %eax
jl 0x5781
jmp 0x57a2
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl -0x34(%rbp), %edx
leaq 0x6b72(%rip), %rdi      # 0xc303
movb $0x0, %al
callq 0x2060
movl -0x34(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x5686
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x57f1
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x4(%rax)
jge 0x57e4
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl (%rax), %esi
leaq 0x6b3a(%rip), %rdi      # 0xc317
movb $0x0, %al
callq 0x2060
jmp 0x57e6
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x57ae
movq -0x20(%rbp), %rdi
callq 0x2030
movq -0x28(%rbp), %rdi
callq 0x2030
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)