// File: /workspace/github_c_makefile_star2_O1/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 0x3672
movl $0x1, %r15d
xorl %ebx, %ebx
leaq 0x10(%rsp), %r14
movq %rax, %r12
leal (%rax,%r15), %r13d
testq %rbx, %rbx
je 0x362b
addl %r13d, %eax
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2110
jmp 0x3633
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 0x3612
leaq 0xc(%rsp), %r14
movl $0x0, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6cf8
movq %rax, %r15
movq %rbx, %rdi
callq 0x2030
movl (%r14), %esi
movq %r15, %rdi
callq 0x7b60
movq %rax, %rbx
movq %rax, %rdi
callq 0x7b0c
testq %rax, %rax
je 0x37db
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x6723
leaq 0x5bd4(%rip), %rdi      # 0x929b
callq 0x2060
leaq 0x5b30(%rip), %r12      # 0x9203
movq %r12, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x26d1
movl $0xa, %edi
callq 0x2040
movq %r12, %rdi
callq 0x2060
movq %r14, %rdi
callq 0x4134
movq %rax, %r15
movq %rax, %rdi
callq 0x3d78
leaq 0x5b9a(%rip), %rdi      # 0x92a9
callq 0x2060
movq %r12, %rdi
callq 0x2060
movq %r15, %rdi
callq 0x25f0
leaq 0x8(%rsp), %rbp
movl $0x0, (%rbp)
movq %r12, %rdi
callq 0x2060
leaq 0x5b8a(%rip), %rdi      # 0x92c9
callq 0x2060
movq %r12, %rdi
callq 0x2060
movq %r15, %rdi
movq %rbp, %rsi
callq 0x324e
movq %rax, %r13
movq (%rax), %rdi
callq 0x311e
movq %r15, %rdi
callq 0x3d78
movq %r12, %rdi
callq 0x2060
leaq 0x5b63(%rip), %rdi      # 0x92dc
callq 0x2060
movq %r15, %rdi
callq 0x25f0
cmpl $0x0, (%rbp)
jle 0x37a6
xorl %r12d, %r12d
movq (%r13,%r12,8), %rdi
callq 0x2ed4
incq %r12
movslq 0x8(%rsp), %rax
cmpq %rax, %r12
jl 0x378f
movq %r13, %rdi
callq 0x2030
leaq 0x5a4e(%rip), %rdi      # 0x9203
callq 0x2060
testq %r15, %r15
je 0x37d3
movq 0x28(%r15), %r12
movq %r15, %rdi
callq 0x54f7
movq %r12, %r15
testq %r12, %r12
jne 0x37bf
movq %r14, %rdi
callq 0x57dc
movq %rbx, %rdi
callq 0x7c08
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq