// File: /workspace/github_c_makefile_star2_O2/Mithreindeir[P]ewcc/src/main.c
// Function: main
int main()
{
	char *str = NULL;
	int slen = 1;
	char buf[64];
	int len = 0;
	while ((len = read(0, buf, 64)) > 0) {
		slen += len;
		if (!str)
			str = malloc(slen);
		else
			str = realloc(str, slen + len);
		memcpy(str + (slen - len - 1), buf, len);
		str[slen - 1] = 0;
	}
	int toks = 0;
	struct token **tok = tokenify(str, &toks);
	free(str);

	struct parser *p = parser_init(tok, toks);
	//struct stmt *s = parse_function(p);
	struct stmt *s = parse_unit(p);
	if (s) {
		node_check(s, NULL);
		printf("AST Tree View\n");
		printf("------------------------------------\n");
		node_debug(s);
		printf("\n");
		printf("------------------------------------\n");

		struct ir_stmt *stmt = generate(s);

		optimize(stmt);
		printf("TAC Intermediate Representation\n");
		printf("------------------------------------\n");
		ir_debug(stmt);
		int nbbs=0;
		printf("------------------------------------\n");
		printf("Interference Graph\n");
		printf("------------------------------------\n");
		struct bb **bbs = cfg(stmt, &nbbs);
		bb_creg(bbs[0]);
		optimize(stmt);
		printf("------------------------------------\n");
		printf("After Register Rewriting:\n");
		//instr(stmt);
		ir_debug(stmt);
		for (int i = 0; i < nbbs; i++) {
			bb_free(bbs[i]);
		}
		free(bbs);
		printf("------------------------------------\n");

	       	struct ir_stmt *next=NULL;
		while (stmt) {
			next = stmt->next;
			ir_stmt_free(stmt);
			stmt = next;
		}
		node_free(s);
	}
	parser_free(p);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
pushq $0x1
popq %r12
xorl %ebx, %ebx
leaq 0x10(%rsp), %r14
pushq $0x40
popq %r15
xorl %edi, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2090
movq %rax, %r13
testl %r13d, %r13d
jle 0x357e
leal (%r12,%r13), %ebp
testq %rbx, %rbx
je 0x354d
leal (%rbp,%r13), %esi
movq %rbx, %rdi
callq 0x2100
jmp 0x3554
movl %ebp, %edi
callq 0x20f0
movq %rax, %rbx
decl %r12d
addq %rax, %r12
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x20e0
leal -0x1(%rbp), %eax
movb $0x0, (%rbx,%rax)
movl %ebp, %r12d
jmp 0x3520
leaq 0xc(%rsp), %r14
andl $0x0, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x624c
movq %rax, %r15
movq %rbx, %rdi
callq 0x2030
movl (%r14), %esi
movq %r15, %rdi
callq 0x6fcc
movq %rax, %rbx
movq %rax, %rdi
callq 0x6f89
testq %rax, %rax
je 0x36d8
movq %rax, %r14
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
callq 0x5c89
leaq 0x4cc9(%rip), %rdi      # 0x829b
callq 0x2060
leaq 0x4c25(%rip), %r12      # 0x8203
movq %r12, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x265d
pushq $0xa
popq %rdi
callq 0x2040
movq %r12, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x3f74
movq %rax, %r15
movq %rax, %rdi
callq 0x3bf8
leaq 0x4c91(%rip), %rdi      # 0x82a9
callq 0x2060
movq %r12, %rdi
callq 0x2060
movq %r15, %rdi
callq 0x2592
leaq 0x8(%rsp), %r13
andl $0x0, (%r13)
movq %r12, %rdi
callq 0x2060
leaq 0x4c83(%rip), %rdi      # 0x82c9
callq 0x2060
movq %r12, %rdi
callq 0x2060
movq %r15, %rdi
movq %r13, %rsi
callq 0x318b
movq %rax, %r13
movq (%rax), %rdi
callq 0x3076
movq %r15, %rdi
callq 0x3bf8
movq %r12, %rdi
callq 0x2060
leaq 0x4c5c(%rip), %rdi      # 0x82dc
callq 0x2060
movq %r15, %rdi
callq 0x2592
movslq 0x8(%rsp), %rax
cmpq %rax, %rbp
jge 0x36a6
movq (%r13,%rbp,8), %rdi
callq 0x2e46
incq %rbp
jmp 0x368d
movq %r13, %rdi
callq 0x2030
leaq 0x4b4e(%rip), %rdi      # 0x8203
callq 0x2060
testq %r15, %r15
je 0x36d0
movq 0x28(%r15), %r12
movq %r15, %rdi
callq 0x4daf
movq %r12, %r15
jmp 0x36ba
movq %r14, %rdi
callq 0x5058
movq %rbx, %rdi
callq 0x7072
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)