// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, %rbx
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
callq 0x4f12
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4365
testq %rax, %rax
je 0x46c5
callq 0x4f82
movq %rax, %rbx
leaq -0x48(%rbp), %r14
andq $0x0, (%r14)
movq $0x4, 0x8(%r14)
movq 0xaab4(%rip), %rax      # 0xefd0
movq %rax, 0x10(%r14)
movq 0xaaa1(%rip), %rax      # 0xefc8
movq %rax, 0x18(%r14)
movl $0x260, %edi            # imm = 0x260
callq 0x1200
movq %rax, -0x8(%r14)
leaq -0x138(%rbp), %r14
leaq -0xa0(%rbp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x4fb4
leaq -0x1d0(%rbp), %r12
movl $0x98, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x10e0
leaq -0x50(%rbp), %r13
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x8690
movq 0x8(%r13), %rax
leaq 0x1(%rax), %r15
movq %r15, 0x8(%r13)
imulq $0x98, %rax, %rdi
addq (%r13), %rdi
movl $0x98, %edx
movq %r12, %rsi
callq 0x10e0
testq %r15, %r15
je 0x4677
decq %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x5076
movq %rax, %r15
movq %rax, %rdi
callq 0x50a8
testl %eax, %eax
je 0x4643
cmpl $0x2, %eax
je 0x45e7
cmpl $0x1, %eax
jne 0x46a6
movq %r15, %rdi
callq 0x5a62
movq %r12, %rdi
movq %r13, %rsi
callq 0x5ac6
jmp 0x4643
movq 0x90(%r15), %rcx
movq %r14, %rdi
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdx
callq 0x4fb4
movl $0x98, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x10e0
movq -0x48(%rbp), %rsi
incq %rsi
movq %r13, %rdi
callq 0x8690
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
callq 0x3c5f
xorl %r15d, %r15d
cmpq %r15, -0x48(%rbp)
jbe 0x4669
movq %r13, %rdi
movq %r15, %rsi
callq 0x5076
movq %rax, %rdi
callq 0x5b2e
incq %r15
jmp 0x464b
callq 0x3c93
movq -0x48(%rbp), %r15
jmp 0x459e
movq -0x50(%rbp), %rdi
callq *-0x30(%rbp)
xorps %xmm0, %xmm0
leaq -0x48(%rbp), %rax
movups %xmm0, (%rax)
leaq -0xa0(%rbp), %rdi
callq 0x5ba2
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4dc1(%rip), %rdi      # 0x946e
leaq 0x506a(%rip), %rsi      # 0x971e
leaq 0x506f(%rip), %rcx      # 0x972a
movl $0x8d, %edx
callq 0x10d0
leaq 0x5038(%rip), %rdi      # 0x9704
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1928