// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq $0x0, 0x20(%rsp)
testq %rdi, %rdi
je 0x3464
movq %rdi, %rbx
movq %r12, 0x10(%rsp)
movl $0x8, %r12d
movl $0x4, %r13d
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
callq 0x2bbc
movq %rax, 0x8(%rsp)
testq %rbp, %rbp
je 0x32ff
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x32ff
cmpl $0x3, (%rax)
je 0x32ff
movq -0x8(%r14,%rbp,8), %rsi
movq 0x8(%rsp), %rdi
callq 0x2ffd
movq -0x8(%r14,%rbp,8), %rdi
movq 0x8(%rsp), %rsi
callq 0x3049
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, (%rbx)
jne 0x330c
movl 0x30(%rbx), %eax
movl %eax, %ebx
testq %rbp, %rbp
je 0x332e
movq %r14, %rdi
movq %r12, %rsi
callq 0x2110
movq %rax, %r14
movq %r15, %rdi
movq %r13, %rsi
callq 0x2110
jmp 0x3345
movl $0x8, %edi
callq 0x2100
movq %rax, %r14
movl $0x4, %edi
callq 0x2100
movq %rax, %r15
movq 0x8(%rsp), %rax
movq %rax, (%r14,%rbp,8)
movl %ebx, (%r15,%rbp,4)
movq 0x20(%rsp), %rbx
addq $0x8, %r12
addq $0x4, %r13
incq %rbp
testq %rbx, %rbx
jne 0x32bc
movl %ebp, %r12d
xorl %r13d, %r13d
movq (%r14,%r13,8), %rdi
movq 0x8(%rdi), %rcx
testq %rcx, %rcx
je 0x33c7
movl (%rcx), %edx
addl $-0x3, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, %edx
ja 0x3393
movl 0x30(%rcx), %eax
xorl %ecx, %ecx
cmpl %eax, (%r15,%rcx,4)
jne 0x33a4
movq (%r14,%rcx,8), %rbx
testq %rbx, %rbx
jne 0x33ae
incq %rcx
cmpq %rcx, %r12
jne 0x3395
jmp 0x33c7
cmpl $-0x1, %eax
je 0x33c7
movq %rbx, %rsi
callq 0x3049
movq (%r14,%r13,8), %rsi
movq %rbx, %rdi
callq 0x2ffd
incq %r13
cmpq %r12, %r13
jne 0x3374
cmpq $0x1, %rbp
je 0x345d
movl %ebp, %ebx
movl $0x1, %r12d
movq %rbx, 0x18(%rsp)
movq (%r14,%r12,8), %rax
cmpq $0x0, 0x30(%rax)
jne 0x3455
movq (%rax), %rdi
movq 0x20(%rdi), %rax
testq %rax, %rax
je 0x3444
movq %rax, 0x8(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq (%r14,%r12,8), %rax
incl %ebx
cmpl 0x10(%rax), %ebx
jge 0x3432
movq 0x28(%rdi), %r13
callq 0x54c4
movq %r13, %rdi
testq %r13, %r13
jne 0x3407
movq 0x8(%rsp), %rax
movq $0x0, 0x28(%rax)
jmp 0x343f
movq 0x8(%rsp), %rax
movq %rdi, 0x28(%rax)
movq %rax, 0x20(%rdi)
movq 0x18(%rsp), %rbx
movq (%r14,%r12,8), %rdi
callq 0x2f1e
movq $0x0, (%r14,%r12,8)
incq %r12
cmpq %rbx, %r12
jne 0x33e6
movq 0x10(%rsp), %r12
jmp 0x346c
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %r15, %rdi
callq 0x2030
movl %ebp, (%r12)
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop