// File: /workspace/github_c_makefile_star2_O2/Ihsoh[P]ilang/compiler/frontend/main.c
// Function: main
int main(int argc, char *argv[]) {
	ParserContext *ctx = NULL;

	char *opt_file = NULL;
	char *opt_arch = NULL;
	char *opt_incpath = NULL;
	char *opt_action = NULL;
	char *opt_target = NULL;
	char *opt_output = NULL;

	// 跑测试。
	_test();

	// 没有任何参数时直接打印帮助信息。
	if (argc == 1) {
		_help();
		exit(0);
	}

	Map defs;
	map_init(
		&defs,
		map_rstr_comparer, map_rstr_comparer,
		(MapReleaser) rstr_delete, (MapReleaser) rstr_delete
	);

	for (int i = 1; i < argc; i++) {
		#define	_NEXT_ARG(opt_name) { if (i + 1 < argc) { opt = argv[++i]; } else { _error("the option '%s' expects a parameter.", opt_name); } }

		char *opt = argv[i];

		if (*opt == '-') {
			if (strcmp(opt, _OPT_ARCH) == 0) {
				if (opt_arch != NULL) {
					_error("the architecture option '-arch' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_ARCH)
				opt_arch = opt;
				if (strcmp(opt_arch, _OPT_ARCH_32) != 0
						&& strcmp(opt_arch, _OPT_ARCH_64) != 0) {
					_error(
						"the option '-arch' is specified a invalid value '%s', maybe specify a '32' or '64'.",
						opt_arch
					);
				}
			} else if (strcmp(opt, _OPT_INCPATH) == 0) {
				if (opt_incpath != NULL) {
					_error("the include path option '-incpath' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_INCPATH)
				opt_incpath = opt;
			} else if (strcmp(opt, _OPT_ACTION) == 0) {
				if (opt_action != NULL) {
					_error("the action option '-action' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_ACTION)
				opt_action = opt;
				if (strcmp(opt_action, _OPT_ACTION_COMPILE) != 0
						&& strcmp(opt_action, _OPT_ACTION_PRINTAST) != 0
						&& strcmp(opt_action, _OPT_ACTION_PREPROCESS) != 0) {
					_error(
						"the option '-action' is specified a invalid value '%s', maybe specify a 'compile' or 'printast' or 'preprocess'.",
						opt_action
					);
				}
			} else if (strcmp(opt, _OPT_TARGET) == 0) {
				if (opt_target != NULL) {
					_error("the target option '-target' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_TARGET)
				opt_target = opt;
				if (strcmp(opt_target, _OPT_TARGET_C) != 0
						&& strcmp(opt_target, _OPT_TARGET_LLVMIR) != 0
						&& strcmp(opt_target, _OPT_TARGET_ILIR) != 0) {
					_error(
						"the option '-target' is specified a invalid value '%s', maybe specify a 'c' or 'llvmir' or 'ilir'.",
						opt_target
					);
				}
			} else if (strcmp(opt, _OPT_DEF) == 0) {
				_NEXT_ARG(_OPT_DEF)
				const char *name = opt;
				_NEXT_ARG(_OPT_DEF)
				const char *value = opt;

				ResizableString *rstr_name = rstr_new_with_cstr(name);
				ResizableString *rstr_value = rstr_new_with_cstr(value);

				map_remove(&defs, rstr_name, true, true);
				map_set(&defs, rstr_name, rstr_value, true);
			} else if (strcmp(opt, _OPT_OUTPUT) == 0) {
				if (opt_output != NULL) {
					_error("the output option '-output' cannot be specified multi-times.");
				}
				_NEXT_ARG(_OPT_OUTPUT)
				opt_output = opt;
			} else if (strcmp(opt, _OPT_HELP) == 0) {
				_help();
				exit(0);
			} else {
				_error(
					"the option '%s' is invalid.",
					opt
				);
			}
		} else {
			if (opt_file != NULL) {
				_error(
					"the source file cannot be specified multi-times, '%s' and '%s'.",
					opt_file,
					opt
				);
			}
			opt_file = opt;
		}

		#undef	_NEXT_ARG
	}

	if (opt_file == NULL) {
		_error("the source file not be specified.");
	}
	if (opt_arch == NULL) {
		opt_arch = _OPT_ARCH_32;
	}
	if (opt_incpath == NULL) {
		opt_incpath = "";
	}
	if (opt_action == NULL) {
		opt_action = _OPT_ACTION_COMPILE;
	}
	if (opt_target == NULL) {
		opt_target = _OPT_TARGET_LLVMIR;
	}

	ResizableString *rstr_arch_name = rstr_new_with_cstr("__ARCH__");
	ResizableString *rstr_arch = rstr_new();
	rstr_appendf(rstr_arch, "\"%s\"", opt_arch);
	map_remove(&defs, rstr_arch_name, true, true);
	map_set(
		&defs,
		rstr_arch_name,
		rstr_arch,
		true
	);

	ResizableString *rstr_target_name = rstr_new_with_cstr("__TARGET__");
	ResizableString *rstr_target = rstr_new();
	rstr_appendf(rstr_target, "\"%s\"", opt_target);
	map_remove(&defs, rstr_target_name, true, true);
	map_set(
		&defs,
		rstr_target_name,
		rstr_target,
		true
	);

	// 读取源文件。
	const char *source = NULL;
	int len = 0;
	source = (const char *)util_get_file_content(opt_file, &len);
	if (source == NULL) {
		_error("cannot open source file.");
	}

	// 打开输出文件。
	FILE *output = stdout;
	if (opt_output != NULL) {
		output = fopen(opt_output, "w");
		if (output == NULL) {
			_error("cannot write target code to target file '%s'.", opt_output);
		}
	}

	// 预处理。
	ResizableString preprocessed_source;
	rstr_init(&preprocessed_source);
	pp_process(
		opt_file, opt_incpath, source, len, &defs, NULL, &preprocessed_source
	);
	free((void *)source);

	map_free(&defs, true, true);

	if (strcmp(opt_action, _OPT_ACTION_PREPROCESS) == 0) {
		fputs(RSTR_CSTR(&preprocessed_source), output);
		goto _end;
	}

	// 语法解析。
	int arch = FE_ARCH_32;
	if (strcmp(opt_arch, _OPT_ARCH_32) == 0) {
		arch = FE_ARCH_32;
	} else if (strcmp(opt_arch, _OPT_ARCH_64) == 0) {
		arch = FE_ARCH_64;
	} else {
		assert(0);
	}
	ctx = fe_parser_new_context(
		opt_file, RSTR_CSTR(&preprocessed_source), RSTR_LEN(&preprocessed_source), arch
	);
	fe_parser_parse(ctx);

	// 语义解析。
	fe_sem_process(ctx);
	
	// 执行操作（编译、打印AST）。
	if (strcmp(opt_action, _OPT_ACTION_COMPILE) == 0) {
		if (strcmp(opt_target, _OPT_TARGET_LLVMIR) == 0) {
			// 输出LLVM IR。
			IRGeneratorLlvmirContext *irgen_ctx = fe_irgen_llvmir_new_context(ctx, output);
			fe_irgen_llvmir_generate(irgen_ctx);
			fe_irgen_llvmir_free_context(irgen_ctx);
		} else if (strcmp(opt_target, _OPT_TARGET_C) == 0) {
			// 输出C。
			IRGeneratorCContext *irgen_ctx = fe_irgen_c_new_context(ctx, output);
			fe_irgen_c_generate(irgen_ctx);
			fe_irgen_c_free_context(irgen_ctx);
		} else if (strcmp(opt_target, _OPT_TARGET_ILIR) == 0) {
			// 输出到ILIR。
			IRGeneratorContext *irgen_ctx = fe_irgen_new_context(ctx, output);
			fe_irgen_generate(irgen_ctx);
			fe_irgen_free_context(irgen_ctx);
		} else {
			assert(0);
		}
	} else if (strcmp(opt_action, _OPT_ACTION_PRINTAST) == 0) {
		// 打印语法树。
		fe_parser_print_ast(ctx, output);
	} else {
		assert(0);
	}
	
	// 释放资源。
_end:
	rstr_free(&preprocessed_source);
	if (opt_output != NULL) {
		fclose(output);
	}
	if (ctx != NULL) {
		fe_parser_free_context(ctx);
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r15
movl %edi, %ebx
callq 0x83e2
callq 0x3633
callq 0x4e10
callq 0x5949
movl %ebx, -0x30(%rbp)
cmpl $0x1, %ebx
je 0x227ff
leaq -0x1c799(%rip), %rdx    # 0x593f
leaq -0x1ade5(%rip), %rcx    # 0x72fa
leaq -0xc0(%rbp), %rdi
movq %rdx, %rsi
movq %rcx, %r8
callq 0x513c
pushq $0x1
popq %rcx
movq $0x0, -0x60(%rbp)
leaq 0x22b0f(%rip), %rax     # 0x44c12
movq %rax, -0x58(%rbp)
movl %ecx, %r13d
movq $0x0, -0x48(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x50(%rbp)
xorl %edx, %edx
xorl %r14d, %r14d
movl -0x30(%rbp), %ecx
cmpl %ecx, %r13d
jge 0x223d9
movslq %r13d, %rax
movq (%r15,%rax,8), %r12
cmpb $0x2d, (%r12)
jne 0x221fb
movq %rdx, -0x38(%rbp)
movq %r12, %rdi
leaq 0x22973(%rip), %rsi     # 0x44ac0
callq 0x30d0
testl %eax, %eax
je 0x2220c
movq %r12, %rdi
leaq 0x22a1c(%rip), %rsi     # 0x44b80
callq 0x30d0
testl %eax, %eax
je 0x22249
movq %r12, %rdi
leaq 0x22a52(%rip), %rsi     # 0x44bcd
callq 0x30d0
testl %eax, %eax
je 0x2228c
movq %r12, %rdi
leaq 0x22b0d(%rip), %rsi     # 0x44c9f
callq 0x30d0
testl %eax, %eax
je 0x222fb
movq %r12, %rdi
leaq 0x22baa(%rip), %rsi     # 0x44d53
callq 0x30d0
testl %eax, %eax
je 0x2235e
movq %r12, %rdi
leaq 0x22b98(%rip), %rsi     # 0x44d58
callq 0x30d0
testl %eax, %eax
jne 0x227ec
cmpq $0x0, -0x60(%rbp)
movl -0x30(%rbp), %ecx
jne 0x227e3
incl %r13d
cmpl %ecx, %r13d
movq -0x38(%rbp), %rdx
jge 0x2280b
movslq %r13d, %rax
movq (%r15,%rax,8), %rax
movq %rax, -0x60(%rbp)
jmp 0x223d1
testq %r14, %r14
jne 0x22731
movq %r12, %r14
jmp 0x223d1
cmpq $0x0, -0x38(%rbp)
jne 0x22767
incl %r13d
movl -0x30(%rbp), %ecx
cmpl %ecx, %r13d
jge 0x22754
movslq %r13d, %rax
movq (%r15,%rax,8), %rdx
movzbl (%rdx), %eax
cmpl $0x36, %eax
je 0x22273
cmpl $0x33, %eax
jne 0x22745
cmpb $0x32, 0x1(%rdx)
je 0x2227d
jmp 0x22745
cmpq $0x0, -0x50(%rbp)
jne 0x22773
incl %r13d
movl -0x30(%rbp), %ecx
cmpl %ecx, %r13d
jge 0x2277f
movslq %r13d, %rax
movq (%r15,%rax,8), %rax
movq %rax, -0x50(%rbp)
jmp 0x223cd
cmpb $0x34, 0x1(%rdx)
jne 0x22745
cmpb $0x0, 0x2(%rdx)
je 0x223d1
jmp 0x22745
testq %rbx, %rbx
jne 0x22792
incl %r13d
cmpl -0x30(%rbp), %r13d
jge 0x2279e
movslq %r13d, %rax
movq (%r15,%rax,8), %rbx
movq %rbx, %rdi
leaq 0x2295f(%rip), %rsi     # 0x44c12
callq 0x30d0
testl %eax, %eax
je 0x223ca
movq %rbx, %rdi
leaq 0x22950(%rip), %rsi     # 0x44c1a
callq 0x30d0
testl %eax, %eax
je 0x223ca
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
leaq 0x2293e(%rip), %rsi     # 0x44c23
callq 0x30d0
testl %eax, %eax
jne 0x2284b
movq -0x40(%rbp), %rbx
jmp 0x223ca
cmpq $0x0, -0x48(%rbp)
jne 0x2281b
incl %r13d
movl -0x30(%rbp), %ecx
cmpl %ecx, %r13d
jge 0x22824
movslq %r13d, %rax
movq (%r15,%rax,8), %rdi
cmpb $0x63, (%rdi)
movq %rdi, -0x48(%rbp)
jne 0x2232f
cmpb $0x0, 0x1(%rdi)
je 0x223cd
movq %rdi, %r12
leaq 0x229ab(%rip), %rsi     # 0x44ce4
callq 0x30d0
testl %eax, %eax
je 0x223ca
movq %r12, %rdi
leaq 0x2299b(%rip), %rsi     # 0x44ceb
callq 0x30d0
testl %eax, %eax
je 0x223ca
jmp 0x22864
leal 0x1(%r13), %eax
movl -0x30(%rbp), %ecx
cmpl %ecx, %eax
jge 0x227b1
addl $0x2, %r13d
cmpl %ecx, %r13d
jge 0x227b1
cltq
movq (%r15,%rax,8), %rdi
movslq %r13d, %rax
movq (%r15,%rax,8), %rax
movq %rax, -0x40(%rbp)
callq 0x71f8
movq %rax, %r12
movq -0x40(%rbp), %rdi
callq 0x71f8
movq %rax, -0x40(%rbp)
leaq -0xc0(%rbp), %rdi
movq %r12, %rsi
pushq $0x1
popq %rcx
movl %ecx, %edx
callq 0x5477
leaq -0xc0(%rbp), %rdi
movq %r12, %rsi
movq -0x40(%rbp), %rdx
pushq $0x1
popq %rcx
callq 0x529d
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rdx
incl %r13d
jmp 0x22124
testq %r14, %r14
movq %r14, -0x38(%rbp)
je 0x22834
testq %rdx, %rdx
leaq 0x24132(%rip), %r15     # 0x46522
cmovneq %rdx, %r15
movq -0x50(%rbp), %rax
testq %rax, %rax
leaq 0x1db14(%rip), %rcx     # 0x3ff16
cmoveq %rcx, %rax
movq %rax, -0x50(%rbp)
testq %rbx, %rbx
movq -0x58(%rbp), %rax
cmovneq %rbx, %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
testq %rax, %rax
leaq 0x228bd(%rip), %r13     # 0x44ce4
cmovneq %rax, %r13
leaq 0x229ef(%rip), %rdi     # 0x44e21
callq 0x71f8
movq %rax, %r14
callq 0x71d5
movq %rax, %rbx
leaq 0x28a55(%rip), %rsi     # 0x4ae9e
movq %rax, %rdi
movq %r15, -0x30(%rbp)
movq %r15, %rdx
xorl %eax, %eax
callq 0x739a
leaq -0xc0(%rbp), %r12
pushq $0x1
popq %r15
movq %r12, %rdi
movq %r14, %rsi
movl %r15d, %edx
movl %r15d, %ecx
callq 0x5477
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl %r15d, %ecx
callq 0x529d
leaq 0x2299c(%rip), %rdi     # 0x44e2a
callq 0x71f8
movq %rax, %rbx
callq 0x71d5
movq %rax, %r14
movq %rax, %rdi
leaq 0x289f6(%rip), %rsi     # 0x4ae9e
movq %r13, -0x48(%rbp)
movq %r13, %rdx
xorl %eax, %eax
callq 0x739a
movq %r12, %rdi
movq %rbx, %rsi
movl %r15d, %edx
movl %r15d, %ecx
callq 0x5477
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl %r15d, %ecx
callq 0x529d
leaq -0x64(%rbp), %rsi
andl $0x0, (%rsi)
movq -0x38(%rbp), %rdi
movq %rdi, %r14
callq 0x8e3a
testq %rax, %rax
movq -0x60(%rbp), %r13
je 0x2283d
movq %rax, %r12
testq %r13, %r13
je 0x22526
leaq 0x22947(%rip), %rsi     # 0x44e4e
movq %r13, %rdi
callq 0x3130
movq %rax, %r15
testq %rax, %rax
jne 0x22530
leaq 0x22932(%rip), %rdi     # 0x44e50
movq %r13, %rsi
jmp 0x2286f
movq 0x32aab(%rip), %rax     # 0x54fd8
movq (%rax), %r15
leaq -0x78(%rbp), %rbx
movq %rbx, %rdi
callq 0x6e1c
movl -0x64(%rbp), %ecx
movq %rbx, (%rsp)
leaq -0xc0(%rbp), %rbx
movq %r14, %rdi
movq -0x50(%rbp), %rsi
movq %r12, %rdx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0x3a3cc
movq %r12, %rdi
callq 0x3190
pushq $0x1
popq %r12
movq %rbx, %rdi
movl %r12d, %esi
movl %r12d, %edx
callq 0x5214
leaq 0x226a3(%rip), %rsi     # 0x44c23
movq -0x58(%rbp), %rdi
callq 0x30d0
testl %eax, %eax
je 0x225b7
movq -0x30(%rbp), %rcx
movzbl (%rcx), %eax
cmpl $0x36, %eax
je 0x225d8
cmpl $0x33, %eax
jne 0x227c4
cmpb $0x32, 0x1(%rcx)
jne 0x227c4
cmpb $0x0, 0x2(%rcx)
je 0x225f0
jmp 0x227c4
movq -0x70(%rbp), %rax
testq %rax, %rax
leaq 0x1d951(%rip), %rdi     # 0x3ff16
cmovneq %rax, %rdi
movq %r15, %rsi
callq 0x30b0
xorl %ebx, %ebx
jmp 0x226fa
cmpb $0x34, 0x1(%rcx)
jne 0x227c4
cmpb $0x0, 0x2(%rcx)
jne 0x227c4
pushq $0x2
popq %r12
movq -0x70(%rbp), %rax
testq %rax, %rax
leaq 0x1d918(%rip), %rsi     # 0x3ff16
cmovneq %rax, %rsi
movl -0x74(%rbp), %edx
movq %r14, %rdi
movl %r12d, %ecx
callq 0x22bff
movq %rax, %rbx
movq %rax, %rdi
callq 0x22ceb
movq %rbx, %rdi
callq 0x27a08
leaq 0x225e8(%rip), %rsi     # 0x44c12
movq -0x58(%rbp), %r12
movq %r12, %rdi
callq 0x30d0
testl %eax, %eax
je 0x22661
leaq 0x225d9(%rip), %rsi     # 0x44c1a
movq %r12, %rdi
callq 0x30d0
testl %eax, %eax
jne 0x22876
movq %rbx, %rdi
movq %r15, %rsi
callq 0x231e4
jmp 0x226fa
leaq 0x2267c(%rip), %rsi     # 0x44ce4
movq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x30d0
testl %eax, %eax
je 0x226bc
cmpb $0x63, (%r14)
jne 0x22685
cmpb $0x0, 0x1(%r14)
je 0x226dc
leaq 0x2265f(%rip), %rsi     # 0x44ceb
movq %r14, %rdi
callq 0x30d0
testl %eax, %eax
jne 0x22895
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb486
movq %rax, %r14
movq %rax, %rdi
callq 0xad20
movq %r14, %rdi
callq 0xb5c4
jmp 0x226fa
movq %rbx, %rdi
movq %r15, %rsi
callq 0x17a09
movq %rax, %r14
movq %rax, %rdi
callq 0x16e8c
movq %r14, %rdi
callq 0x17b77
jmp 0x226fa
movq %rbx, %rdi
movq %r15, %rsi
callq 0x13758
movq %rax, %r14
movq %rax, %rdi
callq 0x130ec
movq %r14, %rdi
callq 0x13860
leaq -0x78(%rbp), %rdi
callq 0x72b9
testq %r13, %r13
je 0x22710
movq %r15, %rdi
callq 0x3060
testq %rbx, %rbx
je 0x2271d
movq %rbx, %rdi
callq 0x22ca8
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22687(%rip), %rdi     # 0x44dbf
movq %r14, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x228cf
leaq 0x223e0(%rip), %rdi     # 0x44b2c
movq %rdx, %rsi
jmp 0x2286f
leaq 0x223ac(%rip), %rdi     # 0x44b07
leaq 0x2235e(%rip), %rsi     # 0x44ac0
jmp 0x2286f
leaq 0x22358(%rip), %rdi     # 0x44ac6
jmp 0x22844
leaq 0x2240f(%rip), %rdi     # 0x44b89
jmp 0x22844
leaq 0x22381(%rip), %rdi     # 0x44b07
leaq 0x223f3(%rip), %rsi     # 0x44b80
jmp 0x2286f
leaq 0x2243c(%rip), %rdi     # 0x44bd5
jmp 0x22844
leaq 0x22362(%rip), %rdi     # 0x44b07
leaq 0x22421(%rip), %rsi     # 0x44bcd
jmp 0x2286f
leaq 0x2234f(%rip), %rdi     # 0x44b07
leaq 0x22594(%rip), %rsi     # 0x44d53
jmp 0x2286f
leaq 0x19b95(%rip), %rdi     # 0x3c360
leaq 0x226ac(%rip), %rsi     # 0x44e7e
leaq 0x226be(%rip), %rcx     # 0x44e97
movl $0x125, %edx            # imm = 0x125
callq 0x30a0
leaq 0x22576(%rip), %rdi     # 0x44d60
jmp 0x22844
leaq 0x225aa(%rip), %rsi     # 0x44d9d
movq %r12, %rdi
callq 0x30d0
testl %eax, %eax
jne 0x22858
callq 0x228b4
xorl %edi, %edi
callq 0x3160
leaq 0x222f5(%rip), %rdi     # 0x44b07
leaq 0x2253f(%rip), %rsi     # 0x44d58
jmp 0x2286f
leaq 0x22485(%rip), %rdi     # 0x44ca7
jmp 0x22844
leaq 0x222dc(%rip), %rdi     # 0x44b07
leaq 0x2246d(%rip), %rsi     # 0x44c9f
jmp 0x2286f
leaq 0x225c4(%rip), %rdi     # 0x44dff
jmp 0x22844
leaq 0x225f1(%rip), %rdi     # 0x44e35
xorl %eax, %eax
callq 0x228cf
leaq 0x223dc(%rip), %rdi     # 0x44c2e
movq -0x40(%rbp), %rsi
jmp 0x2286f
leaq 0x22544(%rip), %rdi     # 0x44da3
movq %r12, %rsi
jmp 0x2286f
leaq 0x22485(%rip), %rdi     # 0x44cf0
movq -0x48(%rbp), %rsi
xorl %eax, %eax
callq 0x228cf
leaq 0x19ae3(%rip), %rdi     # 0x3c360
leaq 0x225fa(%rip), %rsi     # 0x44e7e
leaq 0x2260c(%rip), %rcx     # 0x44e97
movl $0x147, %edx            # imm = 0x147
callq 0x30a0
leaq 0x19ac4(%rip), %rdi     # 0x3c360
leaq 0x225db(%rip), %rsi     # 0x44e7e
leaq 0x225ed(%rip), %rcx     # 0x44e97
movl $0x141, %edx            # imm = 0x141
callq 0x30a0