// File: /workspace/github_c_makefile_star2_O3/Yamsman[P]scc/src/main.c
// Function: main
int main(int argc, char **argv) {
	if (argc < 2) {
		usage(0);
		return -1;
	}

	/*
	 * Handle option parsing
	 */
	int opt, hflag = 0, eflag = 0, lflag = 0, pflag = 0;
	char *ofname = "out.s";
	while ((opt = getopt(argc, argv, "ho:eElLp")) != -1) {
		switch (opt) {
			case 'h': hflag = 1;		break;
			case 'o': ofname = optarg;	break;
			case 'e': eflag = 1;		break;
			case 'E': eflag = 2;		break;
			case 'l': lflag = 1;		break;
			case 'L': lflag = 2;		break;
			case 'p': pflag = 1;		break;
			default:
				c_error(NULL, "Unknown option '%c'\n", opt);
				break;
		}
	}
	if (hflag) {
		usage(1);
		return 0;
	}

	//Open symbol table
	symtable stb;
	symtable_init(&stb);	
	symtable_predef(&stb);

	//Compile each file
	init_kwtable();
	for (int i=optind; i<argc; i++) {
		//Get filename
		char *fname = malloc(strlen(argv[i])+1);
		strncpy(fname, argv[i], strlen(argv[i]));
		fname[strlen(argv[i])] = '\0';

		/*
		 * Initialize lexer
		 */
		lexer *lx = lexer_init(&stb, fname);
		if (!lx) continue;

		//Stop early and print information for eflag/lflag
		if (eflag) {
			lexer_debug_pprc(lx, eflag-1);
			goto lclean;
		} else if (lflag) {
			lexer_debug(lx, lflag-1);
			goto lclean;
		}

		/*
		 * Perform parsing
		 */
		ast_n *tree = parse(lx);
		if (c_errflag)
			goto pclean;

		//Generate assembly
		asm_f f;
		asmf_init(&f);
		gen_text(&f, tree);
		if (c_errflag)
			goto aclean;

		//Print instructions
		inst_n *n = f.text;
		while (n != NULL) {
			inst_str(n);
			n = n->next;
		}

		//Clean up
aclean:		asmf_close(&f);
pclean:		astn_del(tree);
lclean:		lexer_close(lx);
	}
	if (c_errflag)
		puts("Compilation terminated.");
	close_kwtable();
	symtable_close(&stb);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
cmpl $0x2, %edi
jge 0x7f03
leaq 0x6532(%rip), %rdi      # 0xe426
callq 0x3070
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x8127
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x64e1(%rip), %r14      # 0xe3f0
leaq 0x64e3(%rip), %r15      # 0xe3f9
leaq 0x64a3(%rip), %r12      # 0xe3c0
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
xorl %ecx, %ecx
movl %ecx, %r13d
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x31e0
cmpl $0x64, %eax
jle 0x7f62
leal -0x65(%rax), %edx
cmpl $0xb, %edx
ja 0x7f8f
movl $0x1, %ecx
movslq (%r12,%rdx,4), %rdx
addq %r12, %rdx
jmpq *%rdx
movl $0x1, %eax
jmp 0x7f83
cmpl $0x45, %eax
je 0x7f7e
cmpl $0x4c, %eax
jne 0x7f8a
movl $0x2, %eax
jmp 0x7f78
movl $0x1, %eax
movq %rax, (%rsp)
jmp 0x7f9d
movl $0x2, %eax
movq %rax, 0x8(%rsp)
jmp 0x7f9d
cmpl $-0x1, %eax
je 0x7fa2
xorl %edi, %edi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x3250
movl %r13d, %ecx
jmp 0x7f30
testl %r13d, %r13d
je 0x7fb8
movl $0x1, %edi
callq 0x7e7c
xorl %ebx, %ebx
jmp 0x8127
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0xbbb4
movq %r14, %rdi
callq 0xbbe6
xorl %eax, %eax
callq 0x55e8
movq 0xbffd(%rip), %rax      # 0x13fd8
movslq (%rax), %r12
cmpl %ebp, %r12d
jge 0x80fc
movq 0x8(%rsp), %rax
decl %eax
movl %eax, 0x14(%rsp)
movq (%rsp), %rax
decl %eax
movl %eax, 0x10(%rsp)
movq (%rbx,%r12,8), %r14
movq %r14, %rdi
callq 0x30b0
movq %rax, %r15
leaq 0x1(%rax), %rdi
callq 0x3170
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3060
movq %r14, %rdi
callq 0x30b0
movb $0x0, (%r13,%rax)
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x5952
testq %rax, %rax
je 0x80f0
movq %rax, %r13
cmpl $0x0, 0x8(%rsp)
je 0x8064
movq %r13, %rdi
movl 0x14(%rsp), %esi
callq 0x7e19
jmp 0x80e8
cmpl $0x0, (%rsp)
je 0x8078
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x7cde
jmp 0x80e8
movq %r13, %rdi
xorl %eax, %eax
callq 0x8138
movq %rax, %r15
leaq 0xc3e8(%rip), %rax      # 0x14474
cmpl $0x0, (%rax)
jne 0x80e0
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x5549
movq %r14, %rdi
movq %r15, %rsi
callq 0x526d
leaq 0xc3c4(%rip), %rax      # 0x14474
cmpl $0x0, (%rax)
setne %al
movq 0x30(%rsp), %r14
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x80d6
movq %r14, %rdi
callq 0x53d1
movq 0x70(%r14), %r14
testq %r14, %r14
jne 0x80c5
leaq 0x30(%rsp), %rdi
callq 0x556b
movq %r15, %rdi
callq 0x35b1
movq %r13, %rdi
callq 0x5b05
incq %r12
cmpl %ebp, %r12d
jne 0x7ffc
leaq 0xc371(%rip), %rax      # 0x14474
cmpl $0x0, (%rax)
je 0x8114
leaq 0x62ff(%rip), %rdi      # 0xe40e
callq 0x3070
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x5946
leaq 0x18(%rsp), %rdi
callq 0xbf89
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq