// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x9e92
xorl %edi, %edi
callq 0x9de0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa16f
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
leaq 0x8992(%rip), %rax      # 0x12847
movq %rax, -0x30(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x8986(%rip), %rdx      # 0x1284d
callq 0x3200
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
je 0x9f63
movl -0x14(%rbp), %eax
addl $-0x45, %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x2b, %eax
ja 0x9f49
movq -0xc0(%rbp), %rax
leaq 0x877e(%rip), %rcx      # 0x12678
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, -0x18(%rbp)
jmp 0x9f5e
movq 0xd0bd(%rip), %rax      # 0x16fd0
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x9f5e
movl $0x1, -0x1c(%rbp)
jmp 0x9f5e
movl $0x2, -0x1c(%rbp)
jmp 0x9f5e
movl $0x1, -0x20(%rbp)
jmp 0x9f5e
movl $0x2, -0x20(%rbp)
jmp 0x9f5e
movl $0x1, -0x24(%rbp)
jmp 0x9f5e
movl -0x14(%rbp), %edx
xorl %eax, %eax
movl %eax, %edi
leaq 0x88ff(%rip), %rsi      # 0x12856
movb $0x0, %al
callq 0x3880
jmp 0x9eb9
cmpl $0x0, -0x18(%rbp)
je 0x9f7f
movl $0x1, %edi
callq 0x9de0
movl $0x0, -0x4(%rbp)
jmp 0xa16f
leaq -0x48(%rbp), %rdi
callq 0xf310
leaq -0x48(%rbp), %rdi
callq 0xf380
movb $0x0, %al
callq 0x6940
movq 0xd029(%rip), %rax      # 0x16fc8
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0xa140
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30b0
movq %rax, %rdi
addq $0x1, %rdi
callq 0x3190
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xd0(%rbp)
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30b0
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq %rax, %rdx
callq 0x3050
movq -0x58(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x10(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30b0
movq %rax, %rcx
movq -0xc8(%rbp), %rax
movb $0x0, (%rax,%rcx)
movq -0x58(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x6d70
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xa05d
jmp 0xa132
cmpl $0x0, -0x1c(%rbp)
je 0xa077
movq -0x60(%rbp), %rdi
movl -0x1c(%rbp), %esi
subl $0x1, %esi
callq 0x9d60
jmp 0xa129
cmpl $0x0, -0x20(%rbp)
je 0xa091
movq -0x60(%rbp), %rdi
movl -0x20(%rbp), %esi
subl $0x1, %esi
callq 0x9c40
jmp 0xa129
jmp 0xa093
movq -0x60(%rbp), %rdi
movb $0x0, %al
callq 0xa180
movq %rax, -0x68(%rbp)
leaq 0xd3fb(%rip), %rax      # 0x174a4
cmpl $0x0, (%rax)
je 0xa0b0
jmp 0xa120
leaq -0xb0(%rbp), %rdi
callq 0x67f0
movq -0x68(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x6360
leaq 0xd3d1(%rip), %rax      # 0x174a4
cmpl $0x0, (%rax)
je 0xa0da
jmp 0xa114
movq -0xb0(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0xa112
movq -0xb8(%rbp), %rdi
callq 0x6600
movq -0xb8(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0xb8(%rbp)
jmp 0xa0e8
jmp 0xa114
leaq -0xb0(%rbp), %rdi
callq 0x6850
movq -0x68(%rbp), %rdi
callq 0x33f0
movq -0x60(%rbp), %rdi
callq 0x7030
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x9fa4
leaq 0xd35d(%rip), %rax      # 0x174a4
cmpl $0x0, (%rax)
je 0xa158
leaq 0x8718(%rip), %rdi      # 0x1286b
callq 0x3070
movb $0x0, %al
callq 0x6d50
leaq -0x48(%rbp), %rdi
callq 0xf7c0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)