// File: /workspace/github_c_makefile_star2_O2/Mithreindeir[P]ewcc/src/live.c
// Function: cfg
struct bb **cfg(struct ir_stmt *entry, int *len)
{
	int *labels = NULL;
	struct bb **bbs = NULL;
	int nbb = 0;

	struct ir_stmt *cur = entry;
	struct ir_stmt *next = NULL;
	/*Construct basic blocks from linear IR list*/
	while (cur) {
		int label = -1;
		struct bb *cbb = bb_init(cur, &next);
		if (nbb && cur->prev) {
			if (cur->prev->type != stmt_ugoto) {
				bb_addpred(cbb, bbs[nbb-1]);
				bb_addsucc(bbs[nbb-1], cbb);
			}
		}
		if (cur->type == stmt_label) label = cur->label;

		nbb++;
		if (nbb==1) {
			bbs = malloc(sizeof(struct bb*));
			labels = malloc(sizeof(int));
		} else {
			bbs = realloc(bbs, sizeof(struct bb*)*nbb);
			labels = realloc(labels, sizeof(int)*nbb);
		}
		bbs[nbb-1] = cbb;
		labels[nbb-1] = label;

		cur = next;
	}
	/*Add edges from labels/jumps to make a control flow graph*/
	for (int i = 0; i < nbb; i++) {
		struct ir_stmt *last = bbs[i]->lst;
		if (!last) continue;
		int jump = -1;
		if (last->type == stmt_ugoto || last->type == stmt_cgoto)
			jump = last->label;

		struct bb *edge = NULL;
		for (int j = 0; j < nbb; j++) {
			edge = labels[j]==jump ? bbs[j] : NULL;
			if (edge) break;
		}
		if (jump != -1 && edge) {
			bb_addsucc(bbs[i], edge);
			bb_addpred(edge, bbs[i]);
		}
	}

	/*Get rid of dangling blocks, ignoring entry point*/
	for (int i = 1; i < nbb; i++) {
		if (!bbs[i]->pred) {
			/*Unlink dead code*/
			struct ir_stmt *cur, *next, *prev = (next = (cur = bbs[i]->blk))->prev;
			if (prev) {
				int ln = 0;
				while (cur && ln < bbs[i]->len) {
					next = cur->next;
					ir_stmt_free(cur);
					ln++;
					cur = next;
				}
				prev->next = cur;
				if (cur) {
					cur->prev = prev;
				}
			}
			bb_free(bbs[i]);
			bbs[i] = NULL;
		}
	}

	free(labels);

	*len = nbb;
	return bbs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x20(%rsp)
movq %rdi, %rbx
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
pushq $0x8
popq %r12
pushq $0x4
popq %rbp
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x3286
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2b14
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x3214
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x3214
cmpl $0x3, (%rax)
je 0x3214
movq 0x8(%rsp), %r13
movq -0x8(%r14,%r13,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x2f1d
movq -0x8(%r14,%r13,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x2f67
cmpl $0x1, (%rbx)
pushq $-0x1
popq %r13
jne 0x3221
movl 0x30(%rbx), %r13d
cmpq $0x0, 0x8(%rsp)
je 0x3244
movq %r14, %rdi
movq %r12, %rsi
callq 0x2100
movq %rax, %r14
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2100
jmp 0x3257
pushq $0x8
popq %rdi
callq 0x20f0
movq %rax, %r14
pushq $0x4
popq %rdi
callq 0x20f0
movq %rax, %r15
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%r14,%rax,8)
movl %r13d, (%r15,%rax,4)
movq 0x18(%rsp), %rbx
addq $0x8, %r12
addq $0x4, %rbp
incq %rax
movq %rax, 0x8(%rsp)
jmp 0x31c0
movl 0x8(%rsp), %r12d
xorl %r13d, %r13d
pushq $-0x1
popq %rbp
cmpq %r12, %r13
je 0x32eb
movq (%r14,%r13,8), %rdi
movq 0x8(%rdi), %rcx
testq %rcx, %rcx
je 0x32e6
movl (%rcx), %edx
addl $-0x3, %edx
movl %ebp, %eax
cmpl $0x1, %edx
ja 0x32b2
movl 0x30(%rcx), %eax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x32e6
cmpl %eax, (%r15,%rcx,4)
jne 0x32c8
movq (%r14,%rcx,8), %rbx
testq %rbx, %rbx
jne 0x32cd
incq %rcx
jmp 0x32b4
cmpl $-0x1, %eax
je 0x32e6
movq %rbx, %rsi
callq 0x2f67
movq (%r14,%r13,8), %rsi
movq %rbx, %rdi
callq 0x2f1d
incq %r13
jmp 0x3291
movl 0x8(%rsp), %ebp
pushq $0x1
popq %r12
cmpq %rbp, %r12
jae 0x3360
movq (%r14,%r12,8), %rax
cmpq $0x0, 0x30(%rax)
jne 0x335b
movq (%rax), %rdi
movq 0x20(%rdi), %rax
testq %rax, %rax
je 0x334d
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
testq %rdi, %rdi
je 0x3334
movq (%r14,%r12,8), %rax
cmpl 0x10(%rax), %ebx
jge 0x3340
movq 0x28(%rdi), %r13
callq 0x4daf
incl %ebx
movq %r13, %rdi
jmp 0x3316
movq 0x10(%rsp), %rax
andq $0x0, 0x28(%rax)
jmp 0x334d
movq 0x10(%rsp), %rax
movq %rdi, 0x28(%rax)
movq %rax, 0x20(%rdi)
movq (%r14,%r12,8), %rdi
callq 0x2e46
andq $0x0, (%r14,%r12,8)
incq %r12
jmp 0x32f3
movq %r15, %rdi
callq 0x2030
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rcx
movl %ecx, (%rax)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop