// File: /workspace/github_c_makefile_star2_O1/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 $0x0, 0x18(%rsp)
testq %rdi, %rdi
je 0x334a
movq %rdi, %rbp
movq $0x0, 0x8(%rsp)
movl $0x8, %r12d
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2b84
movq %rax, %rbx
testq %r13, %r13
je 0x32cb
movq 0x20(%rbp), %rax
testq %rax, %rax
je 0x32cb
cmpl $0x3, (%rax)
je 0x32cb
movq -0x8(%r14,%r13,2), %rsi
movq %rbx, %rdi
callq 0x2fc9
movq -0x8(%r14,%r13,2), %rdi
movq %rbx, %rsi
callq 0x3010
movq %rbx, 0x10(%rsp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x1, (%rbp)
jne 0x32de
movl 0x30(%rbp), %ebx
testq %r13, %r13
je 0x32ff
movq %r14, %rdi
movq %r12, %rsi
callq 0x2110
movq %rax, %r14
leaq 0x4(%r13), %rsi
movq %r15, %rdi
callq 0x2110
jmp 0x3316
movl $0x8, %edi
callq 0x2100
movq %rax, %r14
movl $0x4, %edi
callq 0x2100
movq %rax, %r15
movq 0x10(%rsp), %rax
movq %rax, (%r14,%r13,2)
movl %ebx, (%r15,%r13)
movq 0x18(%rsp), %rbp
addq $0x4, %r13
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
addq $0x8, %r12
testq %rbp, %rbp
jne 0x328e
jmp 0x3359
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movl %eax, %r12d
testl %eax, %eax
je 0x33cc
xorl %r13d, %r13d
movq (%r14,%r13,8), %rdi
movq 0x8(%rdi), %rcx
testq %rcx, %rcx
je 0x33c4
movl (%rcx), %edx
addl $-0x3, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x1, %edx
ja 0x3387
movl 0x30(%rcx), %eax
xorl %ecx, %ecx
cmpl %eax, (%r15,%rcx,4)
jne 0x3395
movq (%r14,%rcx,8), %rbx
jmp 0x3397
xorl %ebx, %ebx
testq %rbx, %rbx
jne 0x33a6
incq %rcx
cmpq %rcx, %r12
jne 0x3389
xorl %ebx, %ebx
cmpl $-0x1, %eax
je 0x33c4
testq %rbx, %rbx
je 0x33c4
movq %rbx, %rsi
callq 0x3010
movq (%r14,%r13,8), %rsi
movq %rbx, %rdi
callq 0x2fc9
incq %r13
cmpq %r12, %r13
jne 0x3368
cmpl $0x2, 0x8(%rsp)
jb 0x3450
movl $0x1, %ebx
movq (%r14,%rbx,8), %rax
cmpq $0x0, 0x30(%rax)
jne 0x3448
movq (%rax), %rdi
movq 0x20(%rdi), %rax
testq %rax, %rax
je 0x3437
movq %rax, 0x10(%rsp)
testq %rdi, %rdi
je 0x341c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq (%r14,%rbx,8), %rax
incl %ebp
cmpl 0x10(%rax), %ebp
jge 0x342a
movq 0x28(%rdi), %r13
callq 0x54f7
movq %r13, %rdi
testq %r13, %r13
jne 0x33fe
jmp 0x341f
movq %rdi, %r13
movq 0x10(%rsp), %rax
movq %r13, 0x28(%rax)
jmp 0x3437
movq 0x10(%rsp), %rax
movq %rdi, 0x28(%rax)
movq %rax, 0x20(%rdi)
movq (%r14,%rbx,8), %rdi
callq 0x2ed4
movq $0x0, (%r14,%rbx,8)
incq %rbx
cmpq %r12, %rbx
jne 0x33d8
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