// File: /workspace/github_c_makefile_star2_O3/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
xorl %ebx, %ebx
leaq 0x10(%rsp), %rsi
movl $0x40, %edx
xorl %edi, %edi
callq 0x20a0
testl %eax, %eax
jle 0x369e
movl $0x1, %r15d
xorl %ebx, %ebx
leaq 0x10(%rsp), %r14
movq %rax, %r12
leal (%rax,%r15), %r13d
testq %rbx, %rbx
je 0x3657
addl %r13d, %eax
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2110
jmp 0x365f
movl %r13d, %edi
callq 0x2100
movq %rax, %rbx
decl %r15d
addq %rax, %r15
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x20f0
leal -0x1(%r13), %eax
movb $0x0, (%rbx,%rax)
movl $0x40, %edx
xorl %edi, %edi
movq %r14, %rsi
callq 0x20a0
movq %rax, %r12
movl %r13d, %r15d
testl %eax, %eax
jg 0x363e
leaq 0xc(%rsp), %r14
movl $0x0, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6cac
movq %rax, %r15
movq %rbx, %rdi
callq 0x2030
movl (%r14), %esi
movq %r15, %rdi
callq 0x7af0
movq %rax, %rbx
movq %rax, %rdi
callq 0x7a9d
testq %rax, %rax
je 0x3807
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x66b7
leaq 0x5ba8(%rip), %rdi      # 0x929b
callq 0x2060
leaq 0x5b04(%rip), %r12      # 0x9203
movq %r12, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x26cb
movl $0xa, %edi
callq 0x2040
movq %r12, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x4124
movq %rax, %r15
movq %rax, %rdi
callq 0x3d57
leaq 0x5b6e(%rip), %rdi      # 0x92a9
callq 0x2060
movq %r12, %rdi
callq 0x2060
movq %r15, %rdi
callq 0x25ea
leaq 0x8(%rsp), %rbp
movl $0x0, (%rbp)
movq %r12, %rdi
callq 0x2060
leaq 0x5b5e(%rip), %rdi      # 0x92c9
callq 0x2060
movq %r12, %rdi
callq 0x2060
movq %r15, %rdi
movq %rbp, %rsi
callq 0x327d
movq %rax, %r13
movq (%rax), %rdi
callq 0x315c
movq %r15, %rdi
callq 0x3d57
movq %r12, %rdi
callq 0x2060
leaq 0x5b37(%rip), %rdi      # 0x92dc
callq 0x2060
movq %r15, %rdi
callq 0x25ea
cmpl $0x0, (%rbp)
jle 0x37d2
xorl %r12d, %r12d
movq (%r13,%r12,8), %rdi
callq 0x2f1e
incq %r12
movslq 0x8(%rsp), %rax
cmpq %rax, %r12
jl 0x37bb
movq %r13, %rdi
callq 0x2030
leaq 0x5a22(%rip), %rdi      # 0x9203
callq 0x2060
testq %r15, %r15
je 0x37ff
movq 0x28(%r15), %r12
movq %r15, %rdi
callq 0x54c4
movq %r12, %r15
testq %r12, %r12
jne 0x37eb
movq %r14, %rdi
callq 0x57a0
movq %rbx, %rdi
callq 0x7ba6
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq