// File: /workspace/github_c_makefile_star2_O1/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 0x7f97
leaq 0x649e(%rip), %rdi      # 0xe426
callq 0x3070
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x81bb
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x644d(%rip), %r14      # 0xe3f0
leaq 0x644f(%rip), %r15      # 0xe3f9
leaq 0x640f(%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 0x7ff6
leal -0x65(%rax), %edx
cmpl $0xb, %edx
ja 0x8023
movl $0x1, %ecx
movslq (%r12,%rdx,4), %rdx
addq %r12, %rdx
jmpq *%rdx
movl $0x1, %eax
jmp 0x8017
cmpl $0x45, %eax
je 0x8012
cmpl $0x4c, %eax
jne 0x801e
movl $0x2, %eax
jmp 0x800c
movl $0x1, %eax
movq %rax, (%rsp)
jmp 0x8031
movl $0x2, %eax
movq %rax, 0x8(%rsp)
jmp 0x8031
cmpl $-0x1, %eax
je 0x8036
xorl %edi, %edi
movq %r15, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x3250
movl %r13d, %ecx
jmp 0x7fc4
testl %r13d, %r13d
je 0x804c
movl $0x1, %edi
callq 0x7f10
xorl %ebx, %ebx
jmp 0x81bb
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0xbc4c
movq %r14, %rdi
callq 0xbc7e
xorl %eax, %eax
callq 0x5564
movq 0xbf69(%rip), %rax      # 0x13fd8
movslq (%rax), %r12
cmpl %ebp, %r12d
jge 0x8190
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 0x58ce
testq %rax, %rax
je 0x8184
movq %rax, %r13
cmpl $0x0, 0x8(%rsp)
je 0x80f8
movq %r13, %rdi
movl 0x14(%rsp), %esi
callq 0x7eac
jmp 0x817c
cmpl $0x0, (%rsp)
je 0x810c
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x7d71
jmp 0x817c
movq %r13, %rdi
xorl %eax, %eax
callq 0x81cc
movq %rax, %r15
leaq 0xc354(%rip), %rax      # 0x14474
cmpl $0x0, (%rax)
jne 0x8174
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x54c9
movq %r14, %rdi
movq %r15, %rsi
callq 0x51ec
leaq 0xc330(%rip), %rax      # 0x14474
cmpl $0x0, (%rax)
setne %al
movq 0x30(%rsp), %r14
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x816a
movq %r14, %rdi
callq 0x5351
movq 0x70(%r14), %r14
testq %r14, %r14
jne 0x8159
leaq 0x30(%rsp), %rdi
callq 0x54eb
movq %r15, %rdi
callq 0x35b1
movq %r13, %rdi
callq 0x5a81
incq %r12
cmpl %ebp, %r12d
jne 0x8090
leaq 0xc2dd(%rip), %rax      # 0x14474
cmpl $0x0, (%rax)
je 0x81a8
leaq 0x626b(%rip), %rdi      # 0xe40e
callq 0x3070
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x58c2
leaq 0x18(%rsp), %rdi
callq 0xc021
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq