// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3fb0
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x3210
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x3f11
movq -0x30(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x3f11
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
cmpl $0x3, (%rax)
je 0x3f0f
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rsi
callq 0x39d0
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
movq -0x48(%rbp), %rsi
callq 0x3a60
jmp 0x3f11
movq -0x30(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x3f24
movq -0x30(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x1, -0x24(%rbp)
jne 0x3f51
movl $0x8, %edi
callq 0x20e0
movq %rax, -0x20(%rbp)
movl $0x4, %edi
callq 0x20e0
movq %rax, -0x18(%rbp)
jmp 0x3f7b
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
shlq $0x3, %rsi
callq 0x20f0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x24(%rbp), %rsi
shlq $0x2, %rsi
callq 0x20f0
movq %rax, -0x18(%rbp)
movq -0x48(%rbp), %rdx
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x3c(%rbp), %edx
movq -0x18(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x3e9a
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x40b5
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x3fe3
jmp 0x40a7
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movq -0x58(%rbp), %rax
cmpl $0x3, (%rax)
je 0x3ffc
movq -0x58(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x4006
movq -0x58(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x5c(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x406e
movq -0x18(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x5c(%rbp), %eax
jne 0x4042
movq -0x20(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0x404d
xorl %eax, %eax
movq %rax, -0x98(%rbp)
jmp 0x404d
movq -0x98(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x4061
jmp 0x406e
jmp 0x4063
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x4015
cmpl $-0x1, -0x5c(%rbp)
je 0x40a5
cmpq $0x0, -0x68(%rbp)
je 0x40a5
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x68(%rbp), %rsi
callq 0x3a60
movq -0x68(%rbp), %rdi
movq -0x20(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x39d0
jmp 0x40a7
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x3fb7
movl $0x1, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x41d8
movq -0x20(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x30(%rax)
jne 0x41c8
movq -0x20(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq %rax, -0x80(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x41a7
movl $0x0, -0x8c(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x78(%rbp)
movb %al, -0x99(%rbp)
je 0x4146
movl -0x8c(%rbp), %eax
movq -0x20(%rbp), %rcx
movslq -0x70(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
cmpl 0x10(%rcx), %eax
setl %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x4152
jmp 0x4180
movq -0x78(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
callq 0x7180
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x4119
movq -0x78(%rbp), %rcx
movq -0x88(%rbp), %rax
movq %rcx, 0x28(%rax)
cmpq $0x0, -0x78(%rbp)
je 0x41a5
movq -0x88(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x41a7
movq -0x20(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3840
movq -0x20(%rbp), %rax
movslq -0x70(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
jmp 0x41ca
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x40bc
movq -0x18(%rbp), %rdi
callq 0x2030
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)