// File: /workspace/github_c_makefile_star2_O2/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 $0x88, %rsp
cmpl $0x2, %edi
jge 0x795c
leaq 0x5ad7(%rip), %rdi      # 0xd426
callq 0x3070
pushq $-0x1
popq %rbx
jmp 0x7b71
movq %rsi, %rbx
movl %edi, %r12d
leaq 0x5a87(%rip), %r14      # 0xd3f0
pushq $0x1
popq %r13
leaq 0x5a4c(%rip), %r15      # 0xd3c0
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ecx, %ecx
movl %ecx, %ebp
movl %r12d, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x31e0
leal -0x65(%rax), %ecx
cmpl $0xb, %ecx
ja 0x79b1
movslq (%r15,%rcx,4), %rdx
addq %r15, %rdx
movl %r13d, %ecx
jmpq *%rdx
movl %r13d, %eax
jmp 0x79dc
cmpl $0x45, %eax
je 0x79d9
cmpl $0x4c, %eax
je 0x79e3
cmpl $-0x1, %eax
je 0x79ef
xorl %edi, %edi
leaq 0x5a30(%rip), %rsi      # 0xd3f9
movl %eax, %edx
xorl %eax, %eax
callq 0x35e4
jmp 0x79eb
movl %r13d, %eax
jmp 0x79e6
pushq $0x2
popq %rax
movq %rax, 0x10(%rsp)
jmp 0x79eb
pushq $0x2
popq %rax
movq %rax, 0x8(%rsp)
movl %ebp, %ecx
jmp 0x7988
testl %ebp, %ebp
je 0x7a02
pushq $0x1
popq %rdi
callq 0x78d4
xorl %ebx, %ebx
jmp 0x7b71
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0xb39c
movq %r14, %rbp
movq %r14, %rdi
callq 0xb3ca
xorl %eax, %eax
callq 0x53f0
movq 0xb5b0(%rip), %rax      # 0x12fd8
movslq (%rax), %r14
movq 0x10(%rsp), %rax
decl %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rax
decl %eax
movl %eax, 0x18(%rsp)
movl %r12d, %eax
movq %rax, 0x20(%rsp)
cmpq 0x20(%rsp), %r14
jge 0x7b46
movq (%rbx,%r14,8), %r15
movq %r15, %rdi
callq 0x30b0
movq %rax, %r12
leaq 0x1(%rax), %rdi
callq 0x3170
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3060
movq %r15, %rdi
callq 0x30b0
movb $0x0, (%r13,%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x5704
testq %rax, %rax
je 0x7b23
movq %rax, %r13
cmpl $0x0, 0x10(%rsp)
je 0x7ab7
movq %r13, %rdi
movl 0x1c(%rsp), %esi
callq 0x7885
jmp 0x7b1b
cmpl $0x0, 0x8(%rsp)
je 0x7acc
movq %r13, %rdi
movl 0x18(%rsp), %esi
callq 0x77a2
jmp 0x7b1b
movq %r13, %rdi
xorl %eax, %eax
callq 0x7b88
movq %rax, %r12
leaq 0xb994(%rip), %rax      # 0x13474
cmpl $0x0, (%rax)
jne 0x7b13
leaq 0x40(%rsp), %r15
movq %r15, %rdi
callq 0x5365
movq %r15, %rdi
movq %r12, %rsi
callq 0x509f
leaq 0xb970(%rip), %rax      # 0x13474
cmpl $0x0, (%rax)
je 0x7b2b
leaq 0x40(%rsp), %rdi
callq 0x5382
movq %r12, %rdi
callq 0x337d
movq %r13, %rdi
callq 0x58d3
incq %r14
jmp 0x7a49
leaq 0x40(%rsp), %r15
movq (%r15), %r15
testq %r15, %r15
je 0x7b09
movq %r15, %rdi
callq 0x51f8
addq $0x70, %r15
jmp 0x7b30
leaq 0xb927(%rip), %rax      # 0x13474
cmpl $0x0, (%rax)
je 0x7b5e
leaq 0x58b5(%rip), %rdi      # 0xd40e
callq 0x3070
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x56f8
leaq 0x28(%rsp), %rdi
callq 0xb639
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)