// File: CobaltXII[P]cxcc/cxcc.cpp
// Function: main
int main(int argc, char** argv) {
	if (argc != 2 && argc != 3 && argc != 4) {
		usage(argv[0]);
	} else if (argc == 4) {
		if (std::string(argv[3]) != "-o") {
			usage(argv[0]);
		}
	}

	// Open the file.
	std::ifstream file_stream(argv[1]);
	if (!file_stream.is_open()) {
		std::cerr << "Could not open file \"" << argv[1] << "\"." << std::endl;
		exit(1);
	}

	// Load the file.
	std::string file_content(
		(std::istreambuf_iterator<char>(file_stream)),
		(std::istreambuf_iterator<char>())
	);
	file_stream.close();

	// Parse the second argument.
	std::string outfile;
	if (argc == 3) {
		outfile = argv[2];
	} else if (argc == 4) {
		outfile = "tmp" + std::to_string(time(NULL)) + ".s";
	} else {
		outfile = std::string(argv[1]) + ".s";
	}

	// Open the output file.
	std::FILE* output_file = fopen(outfile.c_str(), "w");

	// Parse the file (implicity lexes the file).
	program_t program = parser_t(argv[1], file_content).parse();

	// Validate the program.
	semantic_analyzer_t semantic_analyzer(argv[1], file_content);
	semantic_analyzer.validate(program);

	// Compile the program.
	compiler_t compiler(program, output_file);
	compiler.compile();
	fclose(output_file);

	// Optionally assembly the output using gcc.
	if (argc == 4) {
		// Assemble the output.
		std::stringstream gcc_cmd;
		gcc_cmd << "gcc " << outfile << " -o " << argv[2];
		system(gcc_cmd.str().c_str());
		// Remove the temporary file.
		std::stringstream rm_cmd;
		rm_cmd << "rm -f " << outfile;
		system(rm_cmd.str().c_str());
	}

	// Exit successfully.
	exit(EXIT_SUCCESS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x950, %rsp            # imm = 0x950
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x647c
cmpl $0x3, -0x8(%rbp)
je 0x647c
cmpl $0x4, -0x8(%rbp)
je 0x647c
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6340
jmp 0x6537
cmpl $0x4, -0x8(%rbp)
jne 0x6535
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x8c8(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x8c0(%rbp)
callq 0x34d0
movq -0x8c8(%rbp), %rsi
movq -0x8c0(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x6d70
jmp 0x64be
leaq 0x22091(%rip), %rsi     # 0x28556
leaq -0x30(%rbp), %rdi
callq 0x7010
movb %al, -0x8c9(%rbp)
jmp 0x64d6
leaq -0x30(%rbp), %rdi
callq 0x31c0
leaq -0x31(%rbp), %rdi
callq 0x3320
movb -0x8c9(%rbp), %al
testb $0x1, %al
jne 0x64f4
jmp 0x6533
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x6340
jmp 0x6533
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6525
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x31c0
leaq -0x31(%rbp), %rdi
callq 0x3320
jmp 0x6d65
jmp 0x6535
jmp 0x6537
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x250(%rbp), %rdi
movq %rdi, -0x8d8(%rbp)
movl $0x8, %edx
callq 0x3500
movq -0x8d8(%rbp), %rdi
callq 0x3470
movb %al, -0x8ca(%rbp)
jmp 0x656b
movb -0x8ca(%rbp), %al
testb $0x1, %al
jne 0x6600
jmp 0x657b
movq 0x2ea66(%rip), %rdi     # 0x34fe8
leaq 0x21fd0(%rip), %rsi     # 0x28559
callq 0x32c0
movq %rax, -0x8e0(%rbp)
jmp 0x6597
movq -0x8e0(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x32c0
movq %rax, -0x8e8(%rbp)
jmp 0x65b4
movq -0x8e8(%rbp), %rdi
leaq 0x21fad(%rip), %rsi     # 0x2856f
callq 0x32c0
movq %rax, -0x8f0(%rbp)
jmp 0x65d0
movq -0x8f0(%rbp), %rdi
movq 0x2e9e2(%rip), %rsi     # 0x34fc0
callq 0x3300
jmp 0x65e5
movl $0x1, %edi
callq 0x3380
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6d59
leaq -0x280(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x7040
leaq -0x290(%rbp), %rdi
callq 0x70a0
leaq -0x291(%rbp), %rdi
movq %rdi, -0x8f8(%rbp)
callq 0x34d0
movq -0x8f8(%rbp), %r9
movq -0x280(%rbp), %rsi
movl -0x278(%rbp), %edx
movq -0x290(%rbp), %rcx
movl -0x288(%rbp), %r8d
leaq -0x270(%rbp), %rdi
callq 0x70d0
jmp 0x6662
leaq -0x291(%rbp), %rdi
callq 0x3320
leaq -0x250(%rbp), %rdi
callq 0x3090
jmp 0x667c
leaq -0x2b8(%rbp), %rdi
callq 0x33e0
cmpl $0x3, -0x8(%rbp)
jne 0x66e8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0x2b8(%rbp), %rdi
callq 0x33d0
jmp 0x66a4
jmp 0x6883
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x291(%rbp), %rdi
callq 0x3320
jmp 0x6d59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6d4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6d41
cmpl $0x4, -0x8(%rbp)
jne 0x67b7
xorl %eax, %eax
movl %eax, %edi
callq 0x32a0
movq %rax, %rsi
leaq -0x318(%rbp), %rdi
callq 0x71b0
jmp 0x670c
leaq 0x21e5f(%rip), %rsi     # 0x28572
leaq -0x2f8(%rbp), %rdi
leaq -0x318(%rbp), %rdx
callq 0x6fc0
jmp 0x6728
leaq 0x21e47(%rip), %rdx     # 0x28576
leaq -0x2d8(%rbp), %rdi
leaq -0x2f8(%rbp), %rsi
callq 0x7160
jmp 0x6744
leaq -0x2b8(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x3390
leaq -0x2d8(%rbp), %rdi
callq 0x31c0
leaq -0x2f8(%rbp), %rdi
callq 0x31c0
leaq -0x318(%rbp), %rdi
callq 0x31c0
jmp 0x6881
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x67a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2f8(%rbp), %rdi
callq 0x31c0
leaq -0x318(%rbp), %rdi
callq 0x31c0
jmp 0x6d41
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x908(%rbp)
leaq -0x359(%rbp), %rdi
movq %rdi, -0x900(%rbp)
callq 0x34d0
movq -0x908(%rbp), %rsi
movq -0x900(%rbp), %rdx
leaq -0x358(%rbp), %rdi
callq 0x6d70
jmp 0x67f5
leaq 0x21d7a(%rip), %rdx     # 0x28576
leaq -0x338(%rbp), %rdi
leaq -0x358(%rbp), %rsi
callq 0x7160
jmp 0x6811
leaq -0x2b8(%rbp), %rdi
leaq -0x338(%rbp), %rsi
callq 0x3390
leaq -0x338(%rbp), %rdi
callq 0x31c0
leaq -0x358(%rbp), %rdi
callq 0x31c0
leaq -0x359(%rbp), %rdi
callq 0x3320
jmp 0x6881
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6870
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x358(%rbp), %rdi
callq 0x31c0
leaq -0x359(%rbp), %rdi
callq 0x3320
jmp 0x6d41
jmp 0x6883
leaq -0x2b8(%rbp), %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x21ce0(%rip), %rsi     # 0x28579
callq 0x3370
movq %rax, -0x910(%rbp)
jmp 0x68a7
movq -0x910(%rbp), %rax
movq %rax, -0x368(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x920(%rbp)
leaq -0x479(%rbp), %rdi
movq %rdi, -0x918(%rbp)
callq 0x34d0
movq -0x920(%rbp), %rsi
movq -0x918(%rbp), %rdx
leaq -0x478(%rbp), %rdi
callq 0x6d70
jmp 0x68f3
leaq -0x4a0(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x3130
jmp 0x6908
leaq -0x458(%rbp), %rdi
leaq -0x478(%rbp), %rsi
leaq -0x4a0(%rbp), %rdx
callq 0x72e0
jmp 0x6924
leaq -0x380(%rbp), %rdi
leaq -0x458(%rbp), %rsi
callq 0x7650
jmp 0x6939
leaq -0x458(%rbp), %rdi
callq 0x7860
leaq -0x4a0(%rbp), %rdi
callq 0x31c0
leaq -0x478(%rbp), %rdi
callq 0x31c0
leaq -0x479(%rbp), %rdi
callq 0x3320
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x930(%rbp)
leaq -0x501(%rbp), %rdi
movq %rdi, -0x928(%rbp)
callq 0x34d0
movq -0x930(%rbp), %rsi
movq -0x928(%rbp), %rdx
leaq -0x500(%rbp), %rdi
callq 0x6d70
jmp 0x69a7
leaq -0x528(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x3130
jmp 0x69bc
leaq -0x4e0(%rbp), %rdi
leaq -0x500(%rbp), %rsi
leaq -0x528(%rbp), %rdx
callq 0x7890
jmp 0x69d8
leaq -0x528(%rbp), %rdi
callq 0x31c0
leaq -0x500(%rbp), %rdi
callq 0x31c0
leaq -0x501(%rbp), %rdi
callq 0x3320
leaq -0x4e0(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0x7920
jmp 0x6a11
leaq -0x568(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0x8010
jmp 0x6a26
movq -0x368(%rbp), %rdx
leaq -0x550(%rbp), %rdi
leaq -0x568(%rbp), %rsi
callq 0x8100
jmp 0x6a42
leaq -0x568(%rbp), %rdi
callq 0x8180
leaq -0x550(%rbp), %rdi
callq 0x81e0
jmp 0x6a5c
movq -0x368(%rbp), %rdi
callq 0x32d0
jmp 0x6a6a
cmpl $0x4, -0x8(%rbp)
jne 0x6d16
leaq -0x6f0(%rbp), %rdi
callq 0x3200
jmp 0x6a82
leaq -0x6e0(%rbp), %rdi
leaq 0x21aeb(%rip), %rsi     # 0x2857b
callq 0x32c0
movq %rax, -0x938(%rbp)
jmp 0x6a9e
movq -0x938(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x3290
movq %rax, -0x940(%rbp)
jmp 0x6aba
movq -0x940(%rbp), %rdi
leaq 0x21ab8(%rip), %rsi     # 0x28580
callq 0x32c0
movq %rax, -0x948(%rbp)
jmp 0x6ad6
movq -0x948(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x32c0
jmp 0x6aec
leaq -0x710(%rbp), %rdi
leaq -0x6f0(%rbp), %rsi
callq 0x32b0
jmp 0x6b01
leaq -0x710(%rbp), %rdi
callq 0x3120
movq %rax, %rdi
callq 0x3270
jmp 0x6b17
leaq -0x710(%rbp), %rdi
callq 0x31c0
leaq -0x898(%rbp), %rdi
callq 0x3200
jmp 0x6b31
leaq -0x888(%rbp), %rdi
leaq 0x21a46(%rip), %rsi     # 0x28585
callq 0x32c0
movq %rax, -0x950(%rbp)
jmp 0x6b4d
movq -0x950(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x3290
jmp 0x6b62
leaq -0x8b8(%rbp), %rdi
leaq -0x898(%rbp), %rsi
callq 0x32b0
jmp 0x6b77
leaq -0x8b8(%rbp), %rdi
callq 0x3120
movq %rax, %rdi
callq 0x3270
jmp 0x6b8d
leaq -0x8b8(%rbp), %rdi
callq 0x31c0
leaq -0x898(%rbp), %rdi
callq 0x3220
leaq -0x6f0(%rbp), %rdi
callq 0x3220
jmp 0x6d16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6c10
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6c04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6bf8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x458(%rbp), %rdi
callq 0x7860
leaq -0x4a0(%rbp), %rdi
callq 0x31c0
leaq -0x478(%rbp), %rdi
callq 0x31c0
leaq -0x479(%rbp), %rdi
callq 0x3320
jmp 0x6d41
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6c61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6c55
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x528(%rbp), %rdi
callq 0x31c0
leaq -0x500(%rbp), %rdi
callq 0x31c0
leaq -0x501(%rbp), %rdi
callq 0x3320
jmp 0x6d35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6d29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x568(%rbp), %rdi
callq 0x8180
jmp 0x6d29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6d1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6d08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x710(%rbp), %rdi
callq 0x31c0
jmp 0x6d08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6cfc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x8b8(%rbp), %rdi
callq 0x31c0
leaq -0x898(%rbp), %rdi
callq 0x3220
leaq -0x6f0(%rbp), %rdi
callq 0x3220
jmp 0x6d1d
xorl %edi, %edi
callq 0x3380
leaq -0x550(%rbp), %rdi
callq 0x8430
leaq -0x4e0(%rbp), %rdi
callq 0x8450
leaq -0x380(%rbp), %rdi
callq 0x8180
leaq -0x2b8(%rbp), %rdi
callq 0x31c0
leaq -0x270(%rbp), %rdi
callq 0x31c0
leaq -0x250(%rbp), %rdi
callq 0x30c0
movq -0x40(%rbp), %rdi
callq 0x34c0
nop