// File: /workspace/github_cpp_makefile_star2_O1/pixlark[P]Badge/src/main.cc
// Function: work_from_source(char const*)
void work_from_source(const char * path)
{
	/*
	if (strcmp(path, "-") == 0) {
		// Read from stdin
		String_Builder builder;
		const int chunk_size = 64;
		char buf[chunk_size];
		
		size_t read;
		while ((read = fread(buf, sizeof(char), chunk_size, stdin)) == chunk_size) {
			builder.append_size(buf, chunk_size);
		}
		if (ferror(stdin)) {
			fatal("Error reading from stdin");
		}
		assert(read <= chunk_size);
		builder.append_size(buf, read);
		source = builder.final_string();
		}*/

	Blocks blocks;
	blocks.init();
	const char * source = load_and_compile_file(&blocks, path);
	if (!source) {
		fatal("File '%s' does not exist!", path);
	}

	#if OUTPUT_BYTECODE
	for (int i = 0; i < blocks.blocks.size; i++) {
		auto block = blocks.blocks[i];
		auto size = blocks.sizes[i];
		printf("Block %d:\n", i);
		for (int j = 0; j < size; j++) {
			char * s = block[j].to_string();
			printf("%02d %s\n", j, s);
			free(s);
		}
	}
	#endif
	
	auto export_scope = Environment::alloc();
	
	// Finally, just run our bytecode through the VM, starting with
	// `block_reference` 0
	List<VM> vm_stack;
	vm_stack.alloc();

	{
		VM vm;
		vm.init(&blocks, export_scope, 0);
		vm_stack.push(vm);
	}

	while (vm_stack.size > 0) {
		auto vm = &vm_stack[vm_stack.size - 1];
		auto response = vm->step();
		switch (response) {
		case VM_OK:
			break;
		case VM_HALTED: {
			vm->destroy();
			vm_stack.pop();
		} break;
		case VM_SWITCH: {
			VM new_vm;
			new_vm.init(&blocks, export_scope, vm->block_reference_to_push);
			vm_stack.push(new_vm);
		} break;
		default:
			assert(false);
		}

		#if DEBUG_OUTPUT
		vm->print_debug_info();
		#endif
		
		// This should ALWAYS run before the loop breaks, that way
		// anything that needs to get cleaned up, will be.
		#if COLLECTION
		do {
			#if RELEASE
			if (!GC::should_collect()) {
				break;
			}
			GC::reset_heuristics();
			#endif
			GC::unmark_all();
			for (int i = 0; i < vm_stack.size; i++) {
				vm_stack[i].mark_reachable();
			}
			GC::free_unmarked();
		} while(0);
		#endif
	}
	
	assert(vm_stack.size == 0);
	vm_stack.dealloc();
	
	blocks.destroy();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, %rbx
xorl %eax, %eax
leaq -0xa8(%rbp), %r14
movq %rax, 0x8(%r14)
movl $0x4, %r13d
movq %r13, 0x10(%r14)
movq 0xbfb0(%rip), %r15      # 0x10fd0
movq %r15, 0x18(%r14)
movq 0xbf9d(%rip), %r12      # 0x10fc8
movq %r12, 0x20(%r14)
movl $0x20, %edi
callq 0x1200
movq %rax, (%r14)
xorl %eax, %eax
movq %rax, 0x30(%r14)
movq %r13, 0x38(%r14)
movq %r15, 0x40(%r14)
movq %r12, 0x48(%r14)
movl $0x20, %edi
callq 0x1200
movq %rax, 0x28(%r14)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4e11
testq %rax, %rax
je 0x5313
callq 0x5f6e
movq %rax, %rbx
movq $0x0, -0x48(%rbp)
movq $0x4, -0x40(%rbp)
movq %r15, -0x38(%rbp)
movq %r12, -0x30(%rbp)
movl $0x260, %edi            # imm = 0x260
callq 0x1200
movq %rax, -0x50(%rbp)
leaq -0x1d8(%rbp), %r14
leaq -0xa8(%rbp), %rsi
movq %r14, %rdi
movq %rbx, -0x58(%rbp)
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x600c
leaq -0x140(%rbp), %rdi
movl $0x98, %edx
movq %r14, %rsi
callq 0x10e0
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
incq %rcx
cmpq %rcx, %rax
jae 0x512e
testq %rax, %rax
js 0x50ed
cvtsi2ss %rax, %xmm0
jmp 0x5102
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
addss %xmm0, %xmm0
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x5eea(%rip), %xmm0     # 0xb004
cvttss2si %xmm0, %rsi
andq %rcx, %rsi
orq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0xa334
movq -0x48(%rbp), %r15
leaq 0x1(%r15), %rax
movq %rax, -0x48(%rbp)
imulq $0x98, %r15, %rdi
addq -0x50(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movl $0x98, %edx
callq 0x10e0
incq %r15
je 0x52c5
leaq -0x140(%rbp), %r12
movq -0x50(%rbp), %r13
imulq $0x98, %r15, %rbx
leaq (%rbx,%r13), %r15
addq $-0x98, %r15
movq %r15, %rdi
callq 0x60da
testl %eax, %eax
je 0x5264
cmpl $0x2, %eax
je 0x51b3
cmpl $0x1, %eax
jne 0x52f4
movq %r15, %rdi
callq 0x6b08
movq %r12, %rdi
leaq -0x50(%rbp), %rsi
callq 0x6b6c
jmp 0x5264
addq %rbx, %r13
movq -0x8(%r13), %rcx
movq %r14, %rdi
leaq -0xa8(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x600c
movl $0x98, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x10e0
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rax
incq %rcx
cmpq %rcx, %rax
jae 0x5240
testq %rax, %rax
js 0x51fc
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x5214
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
addss %xmm0, %xmm0
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x5dd8(%rip), %xmm0     # 0xb004
cvttss2si %xmm0, %rsi
andq %rcx, %rsi
orq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0xa334
movq -0x48(%rbp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, -0x48(%rbp)
imulq $0x98, %rax, %rdi
addq -0x50(%rbp), %rdi
movl $0x98, %edx
movq %r12, %rsi
callq 0x10e0
cmpq $0x0, 0xbfa4(%rip)      # 0x11210
je 0x528a
xorl %eax, %eax
movq 0xbf91(%rip), %rcx      # 0x11208
movq (%rcx,%rax,8), %rcx
movb $0x0, (%rcx)
incq %rax
cmpq %rax, 0xbf88(%rip)      # 0x11210
ja 0x5270
cmpq $0x0, -0x48(%rbp)
je 0x52b3
xorl %r15d, %r15d
xorl %r13d, %r13d
movq -0x50(%rbp), %rdi
addq %r15, %rdi
callq 0x6bd6
incq %r13
addq $0x98, %r15
cmpq %r13, -0x48(%rbp)
ja 0x5297
callq 0x4702
movq -0x48(%rbp), %r15
testq %r15, %r15
jne 0x5166
leaq -0x48(%rbp), %rbx
movq -0x8(%rbx), %rdi
callq *0x18(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
leaq -0xa8(%rbp), %rdi
callq 0x6c88
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6173(%rip), %rdi      # 0xb46e
leaq 0x641c(%rip), %rsi      # 0xb71e
leaq 0x6421(%rip), %rcx      # 0xb72a
movl $0x8d, %edx
callq 0x10d0
leaq 0x63ea(%rip), %rdi      # 0xb704
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1a2a