// File: /workspace/github_cpp_makefile_star2_O2/kram[P]kram/src/main.cpp
// Function: main
int main(int argc, char** argv) {

	bool debug = false;
	std::string file = "";

	// Parse arguments
	// This probable needs to become way more advanced in the future, but it is enough for now...
	if (argc > 1) {
		for (int i = 1; i < argc; i++) {
			std::string arg(argv[i]);

			if (arg.substr(0, 2) == "--") {

				if (arg == "--debug") {
					debug = true;
				} else if (arg == "--help") {
					kr_print_help();
					exit(0);
				} else if (arg == "--version") {
					kr_print_version();
					exit(0);
				}

			} else {
				file = arg;
			}
		}
	} else {
		kr_print_help();
		exit(0);
	}

	// Define output stream
	kram_output_stream = new Output(std::cout);

	lexer::Lexer lexer;
	std::vector<lexer::Token*> tokens = lexer.parse_file(file);

	if (debug) {
		lexer::Lexer::print(tokens);
	}

	Parser parser (tokens);
	std::vector<Instruction*> instructions = parser.run();

	if (debug) {
		std::cout << "Printing ins:\n";

		for (Instruction* ins : instructions) {
			ins->print();
			std::cout << "--------\n";
		}
	}

	VM vm;
	vm.boot(instructions);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa88, %rsp            # imm = 0xA88
movq %rsi, (%rsp)
movl %edi, %ebp
leaq 0xe094(%rip), %rsi      # 0x1184a
leaq 0x80(%rsp), %rdi
leaq 0x230(%rsp), %rdx
callq 0x30f0
cmpl $0x2, %ebp
jl 0x3aa1
movl %ebp, %r13d
pushq $0x1
popq %rbx
xorl %r12d, %r12d
leaq 0x230(%rsp), %r14
leaq 0x128(%rsp), %r15
cmpq %rbx, %r13
je 0x38a5
movq (%rsp), %rax
movq (%rax,%rbx,8), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x30f0
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
pushq $0x2
popq %rcx
callq 0x3240
movq %r15, %rdi
leaq 0xd88d(%rip), %rsi      # 0x110b0
callq 0x3b79
movl %eax, %ebp
movq %r15, %rdi
callq 0x3130
testb %bpl, %bpl
je 0x387f
movq %r14, %rdi
leaq 0xd872(%rip), %rsi      # 0x110b3
callq 0x3b79
movb $0x1, %bpl
testb %al, %al
jne 0x3892
movq %r14, %rdi
leaq 0xd864(%rip), %rsi      # 0x110bb
callq 0x3b79
testb %al, %al
jne 0x3a95
movq %r14, %rdi
leaq 0xd854(%rip), %rsi      # 0x110c2
callq 0x3b79
movl %r12d, %ebp
testb %al, %al
je 0x3892
jmp 0x3a8e
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x3160
movl %r12d, %ebp
movq %r14, %rdi
callq 0x3130
incq %rbx
movl %ebp, %r12d
jmp 0x37ed
movl $0x110, %edi            # imm = 0x110
callq 0x3230
movq %rax, %r14
movq 0x17717(%rip), %rsi     # 0x1afd0
movq %rax, %rdi
callq 0x3b86
movq %r14, 0x17950(%rip)     # 0x1b218
leaq 0xc0(%rsp), %rdi
callq 0x43d4
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x32b0
leaq 0x20(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0x45ac
leaq 0xa0(%rsp), %rdi
callq 0x3130
testb $0x1, %r12b
je 0x393a
leaq 0x68(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3b9a
leaq 0x68(%rsp), %rdi
callq 0x4e7a
leaq 0x68(%rsp), %rdi
callq 0x4300
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3b9a
leaq 0x128(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xc598
leaq 0x50(%rsp), %rdi
callq 0x4300
leaq 0x8(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xc972
testb $0x1, %r12b
je 0x39c8
movq 0x1764c(%rip), %rdi     # 0x1afd0
leaq 0xd741(%rip), %rsi      # 0x110cc
callq 0x3220
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %r12
movq 0x1762f(%rip), %rbx     # 0x1afd0
leaq 0xd733(%rip), %r14      # 0x110db
cmpq %r12, %r15
je 0x39c8
movq (%r15), %rdi
xorl %esi, %esi
callq 0x3be8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3220
addq $0x8, %r15
jmp 0x39a8
xorps %xmm0, %xmm0
andq $0x0, 0x240(%rsp)
movaps %xmm0, 0x230(%rsp)
andq $0x0, 0xa60(%rsp)
andl $0x0, 0xa68(%rsp)
andq $0x0, 0xa70(%rsp)
andl $0x0, 0xa78(%rsp)
andq $0x0, 0xa80(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x40b0
leaq 0x230(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x85c8
leaq 0x38(%rsp), %rdi
callq 0x42f2
leaq 0x230(%rsp), %rdi
callq 0x40fe
leaq 0x8(%rsp), %rdi
callq 0x42f2
leaq 0x128(%rsp), %rdi
callq 0x4118
leaq 0x20(%rsp), %rdi
callq 0x4300
leaq 0xc0(%rsp), %rdi
callq 0x4150
leaq 0x80(%rsp), %rdi
callq 0x3130
xorl %eax, %eax
addq $0xa88, %rsp            # imm = 0xA88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x371b
jmp 0x3a9a
callq 0x36ac
xorl %edi, %edi
callq 0x3280
callq 0x36ac
jmp 0x3a9a
jmp 0x3b1c
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x3ae4
jmp 0x3aeb
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x42f2
jmp 0x3ac8
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
callq 0x40fe
jmp 0x3b1f
movq %rax, %rbx
jmp 0x3b29
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4300
jmp 0x3b36
movq %rax, %rbx
jmp 0x3b36
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x3130
jmp 0x3b40
movq %rax, %rbx
jmp 0x3b40
jmp 0x3b4f
movq %rax, %rbx
movq %r14, %rdi
callq 0x3210
jmp 0x3b64
jmp 0x3b4f
jmp 0x3b54
jmp 0x3b54
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x42f2
leaq 0x128(%rsp), %rdi
callq 0x4118
leaq 0x20(%rsp), %rdi
callq 0x4300
leaq 0xc0(%rsp), %rdi
callq 0x4150
jmp 0x3b64
movq %rax, %rbx
jmp 0x3b64
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
callq 0x3130
leaq 0x80(%rsp), %rdi
callq 0x3130
movq %rbx, %rdi
callq 0x3390